Commit 9927069
notlibc.c
@@ -30,8 +30,6 @@ void *memcpy( void *dest, const void *src, unsigned long n ) {
}
-
-
/*================================ Arena =====================================*/
Status Arena_create( Arena *out, usize size ) {
TRY( u64_round_up( size, 0x1000, &size ) );
notlibc.h
@@ -96,7 +96,9 @@
#endif
#if !defined(OS) || !defined(OS_NAME)
- #error Failed to identify operating system. Please specify it via -DOS=number (choose the most compatible option from above) and -DOS_NAME=name to override the automatic detection.
+ #error Failed to identify operating system. Please specify it via \
+ -DOS=number (choose the most compatible option from above) and \
+ -DOS_NAME=name to override the automatic detection.
#endif
@@ -107,7 +109,8 @@
#define COMPILER COMPILER_CLANG
#define COMPILER_NAME "clang"
- #elif defined(__GNUC__) && !defined(__clang__) && !defined(__llvm__) && !defined(__INTEL_COMPILER)
+ #elif defined(__GNUC__) && !defined(__clang__) && \
+ !defined(__llvm__) && !defined(__INTEL_COMPILER)
#define COMPILER COMPILER_GCC
#define COMPILER_NAME "gcc"
@@ -136,7 +139,8 @@ typedef struct _##name { \
/* Packed struct */
-#if COMPILER == COMPILER_GCC || COMPILER == COMPILER_CLANG || COMPILER == COMPILER_TCC
+#if COMPILER == COMPILER_GCC || COMPILER == COMPILER_CLANG \
+ || COMPILER == COMPILER_TCC
#define PACKED_STRUCT( name, ...) \
typedef struct __attribute__((packed)) _##name { \
__VA_ARGS__ \
@@ -188,7 +192,8 @@ typedef union _##name { \
#warning No warning directive is implemented for this compiler. \
No warnings will be emitted from the code.
#endif
- #error No error directive is implemented for this compiler, thus errors can not be emitted.
+ #error No error directive is implemented for this compiler, \
+ thus errors can not be emitted.
#endif
@@ -306,9 +311,12 @@ typedef u32 Status;
#define S_MASK (3 << 30)
-#define S_SUCCESS (1 << 30) /* Requested action performed. */
-#define S_COMPLETION (0 << 30) /* Action didn't need to be performed - desired state is already present/active. */
-#define S_FAILURE (2 << 30) /* Action failed. */
+/* Requested action performed. */
+#define S_SUCCESS (1 << 30)
+/* Action didn't need to be performed - desired state is already present/active. */
+#define S_COMPLETION (0 << 30)
+/* Action failed. */
+#define S_FAILURE (2 << 30)
#define S_SOURCE_MASK (0x3ff << 20)
enum _ErrorSources {
@@ -331,13 +339,16 @@ enum _ErrorSources {
#define S_REASON_MASK (0xfffff)
enum _ErrorReasons {
- ERR_GENERIC = 0x0, /* Even numbers are for the implementations, odd are for user-defined errors */
+ /* Even numbers are for the implementations, odd are for user-defined errors */
+ ERR_GENERIC = 0x0,
ERR_INVALID_ARGUMENT = 0x2,
ERR_OVERFLOW = 0x4,
- ERR_OUT_OF_BOUND = 0x6, /* a value is out of bounds or the operation would violate certain bounds with the current arguments */
+/* a value is out of bounds or the operation would violate certain bounds \
+ * with the current arguments */
+ ERR_OUT_OF_BOUND = 0x6,
ERR_PERMISSION_DENIED = 0x8,
@@ -523,6 +534,7 @@ static inline s8 s8_max( s8 n1, s8 n2 ) {
/* Minimum */
+
static inline u64 u64_min( u64 n1, u64 n2 ) {
return (n2 >= n1)? n1 : n2;
}
@@ -595,6 +607,7 @@ static inline Status usize_round_down( usize number, usize multiple, usize *out
}
#endif
+
/* Round Up */
static inline Status u64_round_up( u64 number, u64 multiple, u64 *out ) {
@@ -1096,11 +1109,13 @@ Status Arena_align( Arena *a, usize alignment );
#if BUILD_TYPE == BUILD_RELEASE
STRUCT( HeapEntry,
- usize size; /* The next pointer is stored in the data when the block is added to the freelist */
+ usize size; /* The next pointer is stored in the data when the block
+ is added to the freelist */
);
STRUCT( Heap,
- Arena arena; /* Heap is built on top of arena. It's just an arena + a freelist with metadata */
+ Arena arena; /* Heap is built on top of arena. It's just an arena +
+ a freelist with metadata */
HeapEntry freelist;
HeapEntry *freelist_data;
);
@@ -1109,25 +1124,30 @@ Status Arena_align( Arena *a, usize alignment );
STRUCT( HeapEntry,
u64 header_start_guard;
usize allowed_size;
- struct _HeapEntry *next; /* unlike in release, in debug we have a separate next pointer for linking into the freelist */
- u64 checksum; /* size xor'd with next, allowed size and the guard magic number - (likely) detects overwriting of metadata */
+ struct _HeapEntry *next; /* unlike in release, in debug we have
+ a separate next pointer for linking into the freelist */
+ u64 checksum; /* size xor'd with next, allowed size and the
+ guard magic number - (likely) detects overwriting of metadata */
usize size;
u64 header_end_guard;
);
STRUCT( Heap,
- Arena arena; /* Heap is built on top of arena. It's just an arena + a freelist with metadata */
+ Arena arena; /* Heap is built on top of arena. It's just an arena +
+ a freelist with metadata */
HeapEntry freelist;
HeapEntry blocklist; /* The existing blocks in a list */
);
#endif
ASSERT( sizeof(HeapEntry)%sizeof(usize) == 0 );
-Status Heap_create( Heap *out, usize size ); /* size - size of underlying arena. The heap will hold less, due to bookkeeping overhead for allocations. */
+Status Heap_create( Heap *out, usize size ); /* size - size of underlying arena.
+ The heap will hold less, due to bookkeeping overhead for allocations. */
Status Heap_destroy( Heap *h );
Status Heap_alloc( Heap *h, usize amount, void **out );
-Status Heap_free( Heap *h, void **ptr ); /* u8** cause it zeroes the pointer on success automatically. */
+Status Heap_free( Heap *h, void **ptr ); /* u8** cause it zeroes the pointer\
+ on success automatically. */
usize Heap_get_asize( Heap *h, const void *ptr );
@@ -1256,7 +1276,8 @@ STRUCT( StrSlc,
u8 *data;
);
-/* On typical platforms that already have/need nul-termination we keep the nul-termination alongside the explicit length notation.
+/* On typical platforms that already have/need nul-termination we keep the
+ * nul-termination alongside the explicit length notation.
* Hacky, but no one wants to copy strings/paths for syscalls.
* NOTE: on these platforms len MUST NOT include the NUL-terminator.
* It is the indexable length of the string. */
@@ -1356,13 +1377,15 @@ enum FileOpenFlags {
FILE_ACCESS_WRITE = ((usize)1 << 2),
FILE_ACCESS_APPEND = ((usize)1 << 3),
- FILE_ACCESS_MASK = (FILE_ACCESS_READ | FILE_ACCESS_WRITE | FILE_ACCESS_APPEND),
+ FILE_ACCESS_MASK =
+ (FILE_ACCESS_READ | FILE_ACCESS_WRITE | FILE_ACCESS_APPEND),
FILE_EXISTING_OPEN = ((usize)1 << 4),
FILE_EXISTING_TRUNCATE = ((usize)1 << 5),
FILE_EXISTING_FAIL = ((usize)1 << 6),
- FILE_EXISTING_MASK = (FILE_EXISTING_OPEN | FILE_EXISTING_TRUNCATE | FILE_EXISTING_FAIL),
+ FILE_EXISTING_MASK =
+ (FILE_EXISTING_OPEN | FILE_EXISTING_TRUNCATE | FILE_EXISTING_FAIL),
FILE_MISSING_CREATE = ((usize)1 << 7),
FILE_MISSING_FAIL = ((usize)1 << 8),
@@ -1370,7 +1393,8 @@ enum FileOpenFlags {
FILE_MISSING_MASK = (FILE_MISSING_CREATE | FILE_MISSING_FAIL),
FILE_LINK_FOLLOW = ((usize)1 << 9),
- FILE_LINK_FAIL = ((usize)1 << 10), /* os-dependent option - whatever the os guarantees. O_NOFOLLOW on linux. */
+ FILE_LINK_FAIL = ((usize)1 << 10), /* os-dependent option -
+ whatever the os guarantees. O_NOFOLLOW on linux. */
FILE_LINK_MASK = (FILE_LINK_FOLLOW | FILE_LINK_FAIL),
@@ -1402,19 +1426,27 @@ enum FileViewFlags {
/* FILEVIEW_PAGE_ALIGNED_EXTEND = (1 << 2), */
};
-Status FileView_from_file( File *file, usize claim_amount, usize flags, FileView *out ); /* if claim_amount == 0 => claims exact filesize */
-Status FileView_from_path( const Str *path, usize claim_amount, usize flags, FileView *out ); /* This is for quick and easy mmaping of files. If you actually want control over the opening flags use FileView_from_file - if you don't */
+Status FileView_from_file( File *file, usize claim_amount, usize flags, FileView *out );
+/* if claim_amount == 0 => claims exact filesize */
+
+/* This is for quick and easy mmaping of files. If you actually want
+ * control over the opening flags use FileView_from_file */
+Status FileView_from_path( const Str *path, usize claim_amount, usize flags, FileView *out );
-Status FileView_extend_file( FileView *fv, usize amount ); /* Extend the file's end by amount */
+/* Extend the file's end by amount */
+Status FileView_extend_file( FileView *fv, usize amount );
Status FileView_destroy( FileView *fv );
/*=========================== Core functions =================================*/
void terminate(u32 code); /* aka exit */
-void terminate_forced(void); /* force exit, fault, anything that terminates forcefully */
-void breakpoint(void); /* raise a debug breakpoint, if possible on the target */
+/* force exit, fault, anything that terminates forcefully */
+void terminate_forced(void);
+
+/* raise a debug breakpoint, if possible on the target */
+void breakpoint(void);
static inline void assert( usize condition ) {
if( !condition ) {
@@ -1452,12 +1484,15 @@ static inline void* mem_set( void *start, u64 len, u8 value ) {
return &s[len];
}
-Status mem_claim( u32 page_count, Page **ptr_out); /* Request virtual memory range */
-Status mem_relinquish( Page *start, u32 page_count ); /* Free virtual memory range */
-
-Status mem_occupy( Page *start, u32 page_count ); /* Set up physical backing */
-Status mem_vacate( Page *start, u32 page_count ); /* Allow removal of physical backing */
+/* Request virtual memory range */
+Status mem_claim( u32 page_count, Page **ptr_out);
+/* Free virtual memory range */
+Status mem_relinquish( Page *start, u32 page_count );
+/* Set up physical backing */
+Status mem_occupy( Page *start, u32 page_count );
+/* Allow removal of physical backing */
+Status mem_vacate( Page *start, u32 page_count );
/******************************************************************************/