Commit 0e25e49
Changed files (27)
fs
notlibc
platform
datatypes/common.h
@@ -0,0 +1,8 @@
+#ifndef NOTLIBC_HEADER_DATATYPES_COMMON_INCLUDED
+#define NOTLIBC_HEADER_DATATYPES_COMMON_INCLUDED
+
+#include "platform/platform.h"
+#include "./datatypes/error.h"
+#include "./datatypes/string.h"
+
+#endif
platform/common/error.h → datatypes/error.h
@@ -1,6 +1,8 @@
#ifndef NOTLIBC_HEADER_PLATFORM_ERROR_INCLUDED
#define NOTLIBC_HEADER_PLATFORM_ERROR_INCLUDED
+#include "platform/common/types.h"
+
typedef u32 Status;
#define S_MASK (3 << 30)
datatypes/string.h
@@ -1,19 +1,13 @@
#ifndef NOTLIBC_HEADER_DATATYPES_STRING_INCLUDED
#define NOTLIBC_HEADER_DATATYPES_STRING_INCLUDED
-#include "../platform/platform.h"
-#include "../memory/arena.h"
-#include "platform/common/compiler.h"
+#include "platform/platform.h"
STRUCT( String,
- u32 len;
+ usize len;
u8 data[];
)
-Status String_concat( String *s1, String *s2, String **out, Arena *a );
-Status String_concat_multiple( String **strings, usize count, String **out, Arena *a );
-
-Status String_duplicate( String *s1, String **out, Arena *a );
/* 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.
fs/file.h
@@ -0,0 +1,14 @@
+#ifndef NOTLIBC_HEADER_FS_FILE_INCLUDED
+#define NOTLIBC_HEADER_FS_FILE_INCLUDED
+
+#include "datatypes/common.h"
+
+typedef struct _File File;
+
+Status File_open( String *name );
+Status File_close( File *f );
+
+Status File_memorymap( File *f, u8 **out_ptr, usize *out_size );
+Status File_memoryunmap( File *f, u8 **out_ptr, usize *out_size );
+
+#endif
memory/arena.h
@@ -1,9 +1,8 @@
#ifndef NOTLIBC_HEADER_MEMORY_ARENA_INCLUDED
#define NOTLIBC_HEADER_MEMORY_ARENA_INCLUDED
-#include "../platform/platform.h"
-#include "../util/mathops.h"
-#include "./memory.h"
+#include "util/mathops.h"
+#include "memory/memory.h"
STRUCT( Arena,
Page *start; /* The address where the arena starts */
@@ -12,18 +11,6 @@ STRUCT( Arena,
usize taken; /* The top of the arena */
)
-STRUCT( HeapEntry,
- DEBUG( usize checksum; ) /* for detecting heap corruption and bad pointers */
- usize size;
- struct _HeapEntry *next;
- u8 data[]; /* The above pointer should gives a sane alignment here despite this being u8 */
-)
-
-STRUCT( Heap,
- Arena arena; /* Heap is built on top of arena. It's just an arena + a freelist with metadata */
- HeapEntry *freelist;
-)
-
Status Arena_create( Arena *out, usize size );
Status Arena_destroy( Arena *a );
@@ -32,7 +19,7 @@ Status Arena_give( Arena *a, usize amount );
Status Arena_align( Arena *a, usize alignment );
-IMPL(
+#ifdef NOTLIBC_IMPLEMENT
Status Arena_create( Arena *out, usize size ) {
TRY( u64_round_up( size, 0x1000, &size ) );
TRY( mem_claim( size/0x1000, &out->start ) );
@@ -96,6 +83,6 @@ IMPL(
return S_SUCCESS;
}
-)
+#endif
#endif
memory/heap.h
@@ -0,0 +1,23 @@
+#ifndef NOTLIBC_HEADER_MEMORY_HEAP_INCLUDED
+#define NOTLIBC_HEADER_MEMORY_HEAP_INCLUDED
+
+#include "memory/arena.h"
+
+STRUCT( HeapEntry,
+ DEBUG( usize checksum; ) /* for detecting heap corruption and bad pointers */
+ usize size;
+ struct _HeapEntry *next;
+ u8 data[]; /* The above pointer should gives a sane alignment here despite this being u8 */
+)
+
+STRUCT( Heap,
+ Arena arena; /* Heap is built on top of arena. It's just an arena + a freelist with metadata */
+ HeapEntry *freelist;
+)
+
+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_take( usize amount, u8 **out );
+Status Heap_give( u8* ptr );
+
+#endif
notlibc/core.h
@@ -0,0 +1,14 @@
+#ifndef NOTLIBC_HEADER_NOTLIBC_CORE_INCLUDED
+#define NOTLIBC_HEADER_NOTLIBC_CORE_INCLUDED
+
+#include "../memory/memory.h"
+#include "../memory/arena.h"
+#include "../memory/heap.h"
+
+#include "../datatypes/string.h"
+
+#include "../fs/file.h"
+
+#include "../platform/platform.h"
+
+#endif
platform/common/all.h
@@ -0,0 +1,8 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_COMMON_ALL_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_COMMON_ALL_INCLUDED
+
+#include "./corefunc.h"
+#include "./headers.h"
+#include "./types.h"
+
+#endif
platform/common/corefunc.h
@@ -1,6 +1,8 @@
#ifndef NOTLIBC_HEADER_PLATFORM_COREFUNC_INCLUDED
#define NOTLIBC_HEADER_PLATFORM_COREFUNC_INCLUDED
+#include "./types.h"
+
static inline void terminate(u32 code); /* aka exit */
static inline void terminate_forced(void); /* force exit, fault, anything that terminates forcefully */
platform/common/headers.h
@@ -0,0 +1,14 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_HEADERS_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_HEADERS_INCLUDED
+
+#include "../info/os.h"
+
+#if PLATFORM == PLATFORM_LINUX
+ #define PLATFORM_TYPES_HEADER "platform/linux/core/types.h"
+ #define PLATFORM_FUNC_HEADER "platform/linux/core/func.h"
+ #define PLATFORM_ENTRYPOINT_HEADER "platform/linux/core/entry.h"
+ #define PLATFORM_FILE_HEADER "platform/linux/fs.h"
+#elif PLATFORM == PLATFORM_WINDOWS
+#endif
+
+#endif
platform/common/types.h
@@ -0,0 +1,23 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_TYPES_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_TYPES_INCLUDED
+
+#include "./headers.h"
+#include "../info/compiler.h"
+
+#include PLATFORM_TYPES_HEADER
+
+/* Type sanity checks */
+ASSERT( sizeof( u8 ) == 1 );
+ASSERT( sizeof( u16 ) == 2 );
+ASSERT( sizeof( u32 ) == 4 );
+ASSERT( sizeof( u64 ) == 8 );
+
+ASSERT( sizeof( s8 ) == 1 );
+ASSERT( sizeof( s16 ) == 2 );
+ASSERT( sizeof( s32 ) == 4 );
+ASSERT( sizeof( s64 ) == 8 );
+
+ASSERT( sizeof( f32 ) == 4 );
+ASSERT( sizeof( f64 ) == 8 );
+
+#endif
platform/common/arch.h → platform/info/arch.h
File renamed without changes
platform/common/build.h → platform/info/build.h
File renamed without changes
platform/common/compiler.h → platform/info/compiler.h
@@ -154,4 +154,5 @@ typedef struct _##name { \
#endif
#endif
+
#endif
platform/common/os.h → platform/info/os.h
File renamed without changes
platform/linux/entry.h → platform/linux/core/entry.h
@@ -1,8 +1,8 @@
#ifndef NOTLIBC_HEADER_PLATFORM_LINUX_ENTRY_INCLUDED
#define NOTLIBC_HEADER_PLATFORM_LINUX_ENTRY_INCLUDED
-#include "../common/build.h"
-#include "./coretypes.h"
+#include "platform/info/build.h"
+#include "platform/common/types.h"
u8* linux_startup_sp = 0;
platform/linux/corefunc.h → platform/linux/core/func.h
@@ -1,4 +1,7 @@
-#include "../common/build.h"
+#ifndef HEADER_PLATFORM_LINUX_CORE_FUNC_INCLUDED
+#define HEADER_PLATFORM_LINUX_CORE_FUNC_INCLUDED
+
+#include "platform/common/corefunc.h"
static inline void breakpoint(void) {
__asm__ volatile (
@@ -12,10 +15,11 @@ static inline void terminate_forced(void) {
);
}
-
-#include "./syscalls.h"
+#include "../syscalls.h"
static inline void terminate(u32 code) {
linux_exit((s32)code);
UNREACHABLE;
+
+#endif
}
platform/linux/coretypes.h → platform/linux/core/types.h
@@ -1,7 +1,7 @@
#ifndef NOTLIBC_HEADER_PLATFORM_LINUX_TYPES_INCLUDED
#define NOTLIBC_HEADER_PLATFORM_LINUX_TYPES_INCLUDED
-#include "../common/arch.h"
+#include "../../info/arch.h"
typedef unsigned char u8;
typedef unsigned short u16;
platform/linux/file.h
@@ -0,0 +1,11 @@
+#ifndef NOTLIBC_HEADER_PLATFORM_LINUX_FILE_INCLUDED
+#define NOTLIBC_HEADER_PLATFORM_LINUX_FILE_INCLUDED
+
+#include "datatypes/common.h"
+#include "fs/file.h"
+
+STRUCT( File,
+ s32 fd;
+)
+
+#endif
platform/linux/memory.h
@@ -1,7 +1,9 @@
#ifndef NOTLIBC_HEADER_MEMORY_LINUX_MEMORY_INCLUDED
#define NOTLIBC_HEADER_MEMORY_LINUX_MEMORY_INCLUDED
-#include "../../platform/platform.h"
+#include "../common/types.h"
+#include "./datatypes/error.h"
+#include "./syscalls.h"
Status mem_claim( u32 page_count, Page **ptr_out) {
void* res = (void*)linux_mmap( 0, page_count*sizeof(Page), LINUX_PROT_NONE, MMAP_MAP_PRIVATE | MMAP_MAP_ANONYMOUS, -1, 0 );
platform/linux/syscall_wrappers_gcclike.h
@@ -1,7 +1,7 @@
#ifndef HEADER_PLATFORM_LINUX_SYSCALL_WRAPPERS_GCCLIKE_INCLUDED
#define HEADER_PLATFORM_LINUX_SYSCALL_WRAPPERS_GCCLIKE_INCLUDED
-#include "./coretypes.h"
+#include "../common/types.h"
#if ARCH == ARCH_X86_64
/* All this mess, just so *maybe* the compiler can optimize register usage when inlining. Check the generic wrappers for a more typical way of going about this. This here is unusual, from what I have seen. */
platform/linux/syscall_wrappers_generic.h
@@ -1,7 +1,7 @@
#ifndef HEADER_PLATFORM_LINUX_SYSCALL_WRAPPERS_GCCLIKE_INCLUDED
#define HEADER_PLATFORM_LINUX_SYSCALL_WRAPPERS_GCCLIKE_INCLUDED
-#include "./coretypes.h"
+#include "../common/types.h"
#if ARCH == ARCH_X86_64
static inline isize linux_syscall1(usize num, usize arg1) {
platform/linux/syscalls.h
@@ -1,9 +1,7 @@
#ifndef HEADER_PLATFORM_LINUX_SYSCALLS_INCLUDED
#define HEADER_PLATFORM_LINUX_SYSCALLS_INCLUDED
-#include "../common/arch.h"
-#include "../common/compiler.h"
-#include "./coretypes.h"
+#include "platform/common/types.h"
static inline isize linux_syscall1(usize num, usize arg1);
static inline isize linux_syscall2(usize num, usize arg1, usize arg2);
platform/platform.h
@@ -1,54 +1,22 @@
#ifndef NOTLIBC_HEADER_PLATFORM_PLATFORM_INCLUDED
#define NOTLIBC_HEADER_PLATFORM_PLATFORM_INCLUDED
-#include "./common/build.h"
-#include "./common/compiler.h"
-#include "./common/arch.h"
-#include "./common/os.h"
-
-/* Core types header */
-#if PLATFORM == PLATFORM_LINUX
- #include "linux/coretypes.h"
-#elif PLATFORM == PLATFORM_WINDOWS
- #include "windows/coretypes.h"
-#endif
-
-/* Type sanity checks */
-ASSERT( sizeof( u8 ) == 1 );
-ASSERT( sizeof( u16 ) == 2 );
-ASSERT( sizeof( u32 ) == 4 );
-ASSERT( sizeof( u64 ) == 8 );
-
-ASSERT( sizeof( s8 ) == 1 );
-ASSERT( sizeof( s16 ) == 2 );
-ASSERT( sizeof( s32 ) == 4 );
-ASSERT( sizeof( s64 ) == 8 );
-
-ASSERT( sizeof( f32 ) == 4 );
-ASSERT( sizeof( f64 ) == 8 );
-
-/* Core platform functions */
-#include "./common/corefunc.h"
-
-#if PLATFORM == PLATFORM_LINUX
- #include "./linux/corefunc.h"
-#elif PLATFORM == PLATFORM_WINDOWS
- #include "./windows/corefunc.h"
-#endif
-
-/* Platform-dependent entrypoint function */
-#if PLATFORM == PLATFORM_LINUX
- #include "./linux/entry.h"
-#elif PLATFORM == PLATFORM_WINDOWS
- #include "./windows/entry.h"
+#include "platform/info/build.h"
+#include "platform/info/arch.h"
+#include "platform/info/os.h"
+
+#include "platform/info/compiler.h"
+#include "platform/common/corefunc.h"
+#include "platform/common/headers.h"
+#include "platform/common/types.h"
+
+#ifdef NOTLIBC_IMPLEMENT
+ #include PLATFORM_FUNC_HEADER
+ #include PLATFORM_ENTRYPOINT_HEADER
#endif
-/* Error handling types and macros. */
-#include "./common/error.h"
-
-
-/* Compiler-specific tricks */
+/* Compiler-specific debugging/checking system implementations */
#if (COMPILER == COMPILER_GCC || COMPILER == COMPILER_CLANG) && defined(NOTLIBC_IMPLEMENT)
/* This is so that GCC can detect stack smashing via canaries and crash the program when it happens. */
void __stack_chk_fail(void) {
@@ -56,5 +24,4 @@ ASSERT( sizeof( f64 ) == 8 );
}
#endif
-
#endif
util/safemath.h
@@ -1,7 +1,7 @@
#ifndef NOTLIBC_HEADER_UTIL_SAFEMATH_INCLUDED
#define NOTLIBC_HEADER_UTIL_SAFEMATH_INCLUDED
-#include "../platform/platform.h"
+#include "datatypes/common.h"
#include "./limits.h"
util/safemath_gcclike.h
@@ -1,7 +1,7 @@
#ifndef NOTLIBC_HEADER_UTIL_SAFEMATH_GCCLIKE_INCLUDED
#define NOTLIBC_HEADER_UTIL_SAFEMATH_GCCLIKE_INCLUDED
-#include "../platform/platform.h"
+#include "datatypes/common.h"
/* Unsigned addition */
util/safemath_generic.h
@@ -1,7 +1,7 @@
#ifndef NOTLIBC_HEADER_UTIL_SAFEMATH_GENERIC_INCLUDED
#define NOTLIBC_HEADER_UTIL_SAFEMATH_GENERIC_INCLUDED
-#include "../platform/platform.h"
+#include "datatypes/common.h"
#include "./limits.h"
/* Unsigned addition */