diff options
Diffstat (limited to 'source')
| -rwxr-xr-x | source/base/base.h | 77 | ||||
| -rwxr-xr-x | source/base/base_arena.c | 142 | ||||
| -rwxr-xr-x | source/base/base_arena.h | 41 | ||||
| -rw-r--r-- | source/base/base_hash.c | 18 | ||||
| -rwxr-xr-x | source/base/base_include.h | 30 | ||||
| -rw-r--r-- | source/base/base_mem.c | 17 | ||||
| -rw-r--r-- | source/base/base_mem.h | 8 | ||||
| -rwxr-xr-x | source/base/base_parse.c | 98 | ||||
| -rwxr-xr-x | source/base/base_parse.h | 28 | ||||
| -rwxr-xr-x | source/base/base_stack.c | 187 | ||||
| -rwxr-xr-x | source/base/base_stack.h | 20 | ||||
| -rw-r--r-- | source/base/base_string.c | 73 | ||||
| -rw-r--r-- | source/base/base_string.h | 17 | ||||
| -rw-r--r-- | source/base/base_test.c | 14 | ||||
| -rw-r--r-- | source/base/base_test.h | 61 | ||||
| -rw-r--r-- | source/base/bash_hash.h | 15 | ||||
| -rw-r--r-- | source/core/core.c | 126 | ||||
| -rw-r--r-- | source/core/core.h | 5 | ||||
| -rw-r--r-- | source/platform/platform.c | 12 | ||||
| -rw-r--r-- | source/platform/platform.h | 56 | ||||
| -rw-r--r-- | source/platform/platform_include.h | 7 |
21 files changed, 1052 insertions, 0 deletions
diff --git a/source/base/base.h b/source/base/base.h new file mode 100755 index 0000000..5fcf9e2 --- /dev/null +++ b/source/base/base.h | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | #ifndef BASE_H | ||
| 2 | #define BASE_H | ||
| 3 | |||
| 4 | #include <stdint.h> | ||
| 5 | #include <unistd.h> | ||
| 6 | #include <stddef.h> | ||
| 7 | #include <string.h> | ||
| 8 | |||
| 9 | /* assert an expression and output the file and the line */ | ||
| 10 | |||
| 11 | #define internal static | ||
| 12 | #define global_variable static | ||
| 13 | #define local_persist static | ||
| 14 | |||
| 15 | #define ERR_OK 0 | ||
| 16 | #define ERR_IO 1 | ||
| 17 | #define ERR_PARSE 2 | ||
| 18 | #define ERR_PERM 3 | ||
| 19 | #define ERR_INVALID 4 | ||
| 20 | |||
| 21 | #define KiB(n) (((u64)(n)) << 10) | ||
| 22 | #define MiB(n) (((u64)(n)) << 20) | ||
| 23 | #define GiB(n) (((u64)(n)) << 30) | ||
| 24 | |||
| 25 | #define unused(x) (void)(x) | ||
| 26 | |||
| 27 | #define PATH_MAX_LEN 128 | ||
| 28 | #define BUFF_SMALL 128 | ||
| 29 | #define BUFF_DEFAULT 256 | ||
| 30 | #define BUFF_LARGE 512 | ||
| 31 | |||
| 32 | #define NIL 0 | ||
| 33 | |||
| 34 | #define DEPRECATED __attribute__((__deprecated__)) | ||
| 35 | |||
| 36 | #if defined(__arm__) || defined(__aarch64__) | ||
| 37 | #define breakpoint __asm__ volatile("brk #0"); | ||
| 38 | #define temp_breakpoint __asm__ volatile("udf #0"); | ||
| 39 | #elif defined(__i386__) || defined(__x86_64__) | ||
| 40 | #define breakpoint __asm__ volatile("int3"); | ||
| 41 | #endif | ||
| 42 | |||
| 43 | #define MemCpy(dest, src, len) memcpy((dest), (src), (len)) | ||
| 44 | #define MemSet(dest, len) memset((dest), (0), (len)) | ||
| 45 | |||
| 46 | typedef uint64_t u64; | ||
| 47 | typedef uint32_t u32; | ||
| 48 | typedef uint16_t u16; | ||
| 49 | typedef uint8_t u8; | ||
| 50 | |||
| 51 | typedef int8_t i8; | ||
| 52 | typedef int16_t i16; | ||
| 53 | typedef int32_t i32; | ||
| 54 | typedef int64_t i64; | ||
| 55 | |||
| 56 | typedef float f32; | ||
| 57 | typedef double f64; | ||
| 58 | |||
| 59 | typedef i32 b32; | ||
| 60 | typedef i16 b16; | ||
| 61 | typedef u8 b8; | ||
| 62 | |||
| 63 | typedef uintptr_t umm; | ||
| 64 | typedef intptr_t smm; | ||
| 65 | |||
| 66 | #define TRUE (0 == 0) | ||
| 67 | #define FALSE (0 != 0) | ||
| 68 | |||
| 69 | typedef struct s8 s8; | ||
| 70 | |||
| 71 | struct s8 | ||
| 72 | { | ||
| 73 | char *data; | ||
| 74 | umm size; | ||
| 75 | }; | ||
| 76 | |||
| 77 | #endif | ||
diff --git a/source/base/base_arena.c b/source/base/base_arena.c new file mode 100755 index 0000000..76938bd --- /dev/null +++ b/source/base/base_arena.c | |||
| @@ -0,0 +1,142 @@ | |||
| 1 | internal mem_arena * | ||
| 2 | arena_create(u64 capacity) | ||
| 3 | { | ||
| 4 | mem_arena *arena = (mem_arena *)mmap( | ||
| 5 | /* kernel decides where to throw the arena */ | ||
| 6 | NULL, | ||
| 7 | capacity + sizeof(mem_arena), | ||
| 8 | PROT_READ | PROT_WRITE, | ||
| 9 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 10 | -1, | ||
| 11 | 0); | ||
| 12 | |||
| 13 | if (arena == MAP_FAILED) | ||
| 14 | { | ||
| 15 | return NULL; | ||
| 16 | } | ||
| 17 | |||
| 18 | arena->capacity = capacity; | ||
| 19 | arena->base_position = (u8 *)arena + sizeof(mem_arena); | ||
| 20 | arena->current_position = 0; | ||
| 21 | arena->previous_position = 0; | ||
| 22 | |||
| 23 | return arena; | ||
| 24 | } | ||
| 25 | |||
| 26 | internal void | ||
| 27 | arena_destroy(mem_arena *arena) | ||
| 28 | { | ||
| 29 | if (!arena) | ||
| 30 | { | ||
| 31 | return; | ||
| 32 | } | ||
| 33 | |||
| 34 | int code = munmap(arena, arena->capacity + sizeof(mem_arena)); | ||
| 35 | } | ||
| 36 | |||
| 37 | internal void * | ||
| 38 | arena_alloc(mem_arena *arena, u64 size) | ||
| 39 | { | ||
| 40 | if (!arena) | ||
| 41 | { | ||
| 42 | return NULL; | ||
| 43 | } | ||
| 44 | |||
| 45 | u64 aligned = Align(arena->current_position, ARENA_ALIGN); | ||
| 46 | u64 new_pos = aligned + size; | ||
| 47 | |||
| 48 | if (new_pos > arena->capacity) | ||
| 49 | { | ||
| 50 | return NULL; | ||
| 51 | } | ||
| 52 | |||
| 53 | void *out = arena->base_position + aligned; | ||
| 54 | |||
| 55 | arena->previous_position = arena->current_position; | ||
| 56 | arena->current_position = aligned + size; | ||
| 57 | |||
| 58 | MemSet(out, size); | ||
| 59 | |||
| 60 | return out; | ||
| 61 | } | ||
| 62 | |||
| 63 | internal void | ||
| 64 | arena_pop(mem_arena *arena, u64 size) | ||
| 65 | { | ||
| 66 | size = MIN(size, arena->current_position); | ||
| 67 | arena->current_position -= size; | ||
| 68 | } | ||
| 69 | |||
| 70 | internal void | ||
| 71 | arena_pop_to(mem_arena *arena, u64 pos) | ||
| 72 | { | ||
| 73 | u64 size = pos < arena->current_position ? arena->current_position - pos : 0; | ||
| 74 | arena_pop(arena, size); | ||
| 75 | } | ||
| 76 | |||
| 77 | internal void | ||
| 78 | arena_clear(mem_arena *arena) | ||
| 79 | { | ||
| 80 | arena->current_position = 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | internal mem_arena * | ||
| 84 | arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size, umm alignment) | ||
| 85 | { | ||
| 86 | u8 *old_mem = (u8 *)old_memory; | ||
| 87 | |||
| 88 | if (!is_pow(alignment)) | ||
| 89 | { | ||
| 90 | Align(arena->current_position, alignment); | ||
| 91 | } | ||
| 92 | |||
| 93 | if (old_memory == NULL || old_size == 0) | ||
| 94 | { | ||
| 95 | return (mem_arena *)arena_alloc(arena, new_size); | ||
| 96 | } | ||
| 97 | else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity)) | ||
| 98 | { | ||
| 99 | if ((arena->base_position + arena->previous_position) == old_memory) | ||
| 100 | { | ||
| 101 | arena->current_position = arena->previous_position + new_size; | ||
| 102 | if (new_size > old_size) | ||
| 103 | { | ||
| 104 | MemSet(&arena->current_position, new_size - old_size); | ||
| 105 | } | ||
| 106 | return (mem_arena *)old_memory; | ||
| 107 | } | ||
| 108 | else | ||
| 109 | { | ||
| 110 | void *new_memory = arena_alloc(arena, new_size); | ||
| 111 | umm copy_size = old_size < new_size ? old_size : new_size; | ||
| 112 | memmove(new_memory, old_mem, copy_size); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | else | ||
| 116 | { | ||
| 117 | check(0); | ||
| 118 | } | ||
| 119 | return NULL; | ||
| 120 | } | ||
| 121 | |||
| 122 | internal mem_arena * | ||
| 123 | arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size) | ||
| 124 | { | ||
| 125 | return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN); | ||
| 126 | } | ||
| 127 | |||
| 128 | internal temp_arena | ||
| 129 | temp_arena_begin(mem_arena *arena) | ||
| 130 | { | ||
| 131 | temp_arena t; | ||
| 132 | t.arena = arena; | ||
| 133 | t.start_position = arena->current_position; | ||
| 134 | |||
| 135 | return t; | ||
| 136 | } | ||
| 137 | |||
| 138 | internal void | ||
| 139 | temp_arena_end(temp_arena temp) | ||
| 140 | { | ||
| 141 | temp.arena->current_position = temp.start_position; | ||
| 142 | } | ||
diff --git a/source/base/base_arena.h b/source/base/base_arena.h new file mode 100755 index 0000000..71d9e69 --- /dev/null +++ b/source/base/base_arena.h | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #ifndef ARENA_H | ||
| 2 | #define ARENA_H | ||
| 3 | |||
| 4 | #define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment)) | ||
| 5 | #define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type)) | ||
| 6 | #define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len)) | ||
| 7 | #define PushString(arena, len) (s8 *)arena_alloc((arena), sizeof(s8)*len)) | ||
| 8 | |||
| 9 | typedef struct mem_arena mem_arena; | ||
| 10 | typedef struct temp_arena temp_arena; | ||
| 11 | |||
| 12 | struct mem_arena | ||
| 13 | { | ||
| 14 | u64 current_position; | ||
| 15 | u64 previous_position; | ||
| 16 | u64 capacity; | ||
| 17 | u8 *base_position; | ||
| 18 | }; | ||
| 19 | |||
| 20 | struct temp_arena | ||
| 21 | { | ||
| 22 | mem_arena *arena; | ||
| 23 | u64 start_position; | ||
| 24 | }; | ||
| 25 | |||
| 26 | internal mem_arena * | ||
| 27 | arena_resize_align( | ||
| 28 | mem_arena *arena, | ||
| 29 | void *old_memory, | ||
| 30 | u64 new_size, | ||
| 31 | u64 old_size, | ||
| 32 | umm alignment); | ||
| 33 | |||
| 34 | internal mem_arena * | ||
| 35 | arena_resize( | ||
| 36 | mem_arena *arena, | ||
| 37 | void *old_memory, | ||
| 38 | u64 new_size, | ||
| 39 | u64 old_size); | ||
| 40 | |||
| 41 | #endif | ||
diff --git a/source/base/base_hash.c b/source/base/base_hash.c new file mode 100644 index 0000000..c309a9a --- /dev/null +++ b/source/base/base_hash.c | |||
| @@ -0,0 +1,18 @@ | |||
| 1 | |||
| 2 | internal u64 | ||
| 3 | generate_hash() | ||
| 4 | { | ||
| 5 | // TODO(nasr): | ||
| 6 | return 0; | ||
| 7 | } | ||
| 8 | |||
| 9 | internal hash_map | ||
| 10 | make_hash_map | ||
| 11 | { | ||
| 12 | |||
| 13 | // TODO(nasr): | ||
| 14 | |||
| 15 | return {0}; | ||
| 16 | |||
| 17 | } | ||
| 18 | |||
diff --git a/source/base/base_include.h b/source/base/base_include.h new file mode 100755 index 0000000..3f83fd7 --- /dev/null +++ b/source/base/base_include.h | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | #ifndef BASE_INCLUDE_H | ||
| 2 | #define BASE_INCLUDE_H | ||
| 3 | |||
| 4 | #include <dirent.h> | ||
| 5 | #include <sys/mman.h> | ||
| 6 | #include <sys/stat.h> | ||
| 7 | #include <fcntl.h> | ||
| 8 | #include <stdint.h> | ||
| 9 | #include <stddef.h> | ||
| 10 | #include <string.h> | ||
| 11 | #include <unistd.h> | ||
| 12 | |||
| 13 | #include "base.h" | ||
| 14 | #include "base_mem.h" | ||
| 15 | #include "base_arena.h" | ||
| 16 | #include "base_parse.h" | ||
| 17 | #include "base_stack.h" | ||
| 18 | #include "base_test.h" | ||
| 19 | |||
| 20 | #if defined(TB_IMPLEMENTATION) | ||
| 21 | |||
| 22 | #include "base_mem.c" | ||
| 23 | #include "base_arena.c" | ||
| 24 | #include "base_parse.c" | ||
| 25 | #include "base_stack.c" | ||
| 26 | #include "base_test.c" | ||
| 27 | |||
| 28 | #endif | ||
| 29 | |||
| 30 | #endif | ||
diff --git a/source/base/base_mem.c b/source/base/base_mem.c new file mode 100644 index 0000000..f20ba39 --- /dev/null +++ b/source/base/base_mem.c | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | internal inline b8 | ||
| 2 | is_pow(umm x) | ||
| 3 | { | ||
| 4 | return (x & (x - 1)) == 0; | ||
| 5 | } | ||
| 6 | |||
| 7 | internal inline u64 | ||
| 8 | align(u64 pointer, umm alignment) | ||
| 9 | { | ||
| 10 | if ((alignment & (alignment - 1)) == 0) | ||
| 11 | { | ||
| 12 | return pointer; | ||
| 13 | } | ||
| 14 | |||
| 15 | return (pointer + alignment - 1) & ~(alignment - 1); | ||
| 16 | } | ||
| 17 | |||
diff --git a/source/base/base_mem.h b/source/base/base_mem.h new file mode 100644 index 0000000..945f0ce --- /dev/null +++ b/source/base/base_mem.h | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #ifndef BASE_MEM_H | ||
| 2 | #define BASE_MEM_H | ||
| 3 | |||
| 4 | #define ARENA_ALIGN (2 * sizeof(void *)) | ||
| 5 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | ||
| 6 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | ||
| 7 | |||
| 8 | #endif | ||
diff --git a/source/base/base_parse.c b/source/base/base_parse.c new file mode 100755 index 0000000..4f216bf --- /dev/null +++ b/source/base/base_parse.c | |||
| @@ -0,0 +1,98 @@ | |||
| 1 | /* | ||
| 2 | * is_numeric - Check if a string contains only digits | ||
| 3 | * @s: String to check | ||
| 4 | * | ||
| 5 | * Return: 1 if string contains only numeric characters, 0 otherwise | ||
| 6 | */ | ||
| 7 | internal b8 | ||
| 8 | is_numeric(char *s) | ||
| 9 | { | ||
| 10 | for (; *s; ++s) | ||
| 11 | { | ||
| 12 | if (*s < '0' || *s > '9') | ||
| 13 | { | ||
| 14 | return 0; | ||
| 15 | } | ||
| 16 | } | ||
| 17 | return 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | /* | ||
| 21 | * TODO(nasr): checkout i think there is a buffer overflow happening somewhere | ||
| 22 | * */ | ||
| 23 | internal proc_file * | ||
| 24 | parse_proc_files(char *path, mem_arena *arena) | ||
| 25 | { | ||
| 26 | if (!path || !arena) | ||
| 27 | { | ||
| 28 | return NULL; | ||
| 29 | } | ||
| 30 | |||
| 31 | i32 fd = open(path, O_RDONLY); | ||
| 32 | if (fd < 0) | ||
| 33 | { | ||
| 34 | return NULL; | ||
| 35 | } | ||
| 36 | |||
| 37 | char *buffer = PushArray(arena, char, KiB(4)); | ||
| 38 | u64 bytes = read(fd, buffer, KiB(4)); | ||
| 39 | close(fd); | ||
| 40 | |||
| 41 | if (bytes == 0) | ||
| 42 | { | ||
| 43 | return NULL; | ||
| 44 | } | ||
| 45 | |||
| 46 | /* guessing the count to 256 because i dont want to do a double pass of the buffer */ | ||
| 47 | proc_file *pf = PushStruct(arena, proc_file); | ||
| 48 | pf->entries = PushArray(arena, proc_entry, 256); | ||
| 49 | |||
| 50 | u64 line_start = 0; | ||
| 51 | u64 delim = -1; | ||
| 52 | u64 entry_index = 0; | ||
| 53 | |||
| 54 | for (u64 index = 0; index < bytes; ++index) | ||
| 55 | { | ||
| 56 | if (buffer[index] == ':' && delim == (u64)-1) | ||
| 57 | { | ||
| 58 | delim = index; | ||
| 59 | } | ||
| 60 | else if (buffer[index] == '\n') | ||
| 61 | { | ||
| 62 | if (delim != (-1.f)) | ||
| 63 | { | ||
| 64 | u64 key_len = delim - line_start; | ||
| 65 | if (key_len >= sizeof(pf->entries[entry_index].key)) | ||
| 66 | { | ||
| 67 | key_len = sizeof(pf->entries[entry_index].key) - 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | u64 val_start = delim + 1; | ||
| 71 | while (val_start < index && (buffer[val_start] == ' ' || buffer[val_start] == '\t')) | ||
| 72 | { | ||
| 73 | val_start++; | ||
| 74 | } | ||
| 75 | |||
| 76 | u64 val_len = index - val_start; | ||
| 77 | if (val_len >= sizeof(pf->entries[entry_index].value)) | ||
| 78 | { | ||
| 79 | val_len = sizeof(pf->entries[entry_index].value) - 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | MemCpy(pf->entries[entry_index].key, buffer + line_start, key_len - 1); | ||
| 83 | MemCpy(pf->entries[entry_index].value, buffer + val_start, val_len); | ||
| 84 | |||
| 85 | pf->entries[entry_index].key[key_len] = '\0'; | ||
| 86 | pf->entries[entry_index].value[val_len] = '\0'; | ||
| 87 | |||
| 88 | ++pf->count; | ||
| 89 | ++entry_index; | ||
| 90 | } | ||
| 91 | |||
| 92 | line_start = index + 1; | ||
| 93 | delim = (u64)-1; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | return (pf); | ||
| 98 | } | ||
diff --git a/source/base/base_parse.h b/source/base/base_parse.h new file mode 100755 index 0000000..d66fd04 --- /dev/null +++ b/source/base/base_parse.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #ifndef BASE_PARSE_H | ||
| 2 | #define BASE_PARSE_H | ||
| 3 | |||
| 4 | #define COMPARE_STRING(c1, c2) compare_string((char *)c1, (char *)c2) | ||
| 5 | |||
| 6 | typedef struct proc_entry proc_entry; | ||
| 7 | typedef struct proc_file proc_file; | ||
| 8 | |||
| 9 | struct proc_file | ||
| 10 | { | ||
| 11 | i32 count; | ||
| 12 | proc_entry *entries; | ||
| 13 | }; | ||
| 14 | |||
| 15 | struct proc_entry | ||
| 16 | { | ||
| 17 | char value[16]; | ||
| 18 | char key[16]; | ||
| 19 | }; | ||
| 20 | |||
| 21 | typedef struct | ||
| 22 | { | ||
| 23 | i8 *start; | ||
| 24 | i8 *end; | ||
| 25 | umm len; | ||
| 26 | } Line; | ||
| 27 | |||
| 28 | #endif | ||
diff --git a/source/base/base_stack.c b/source/base/base_stack.c new file mode 100755 index 0000000..7cf0570 --- /dev/null +++ b/source/base/base_stack.c | |||
| @@ -0,0 +1,187 @@ | |||
| 1 | internal mem_stack * | ||
| 2 | stack_create(u64 capacity) | ||
| 3 | { | ||
| 4 | mem_stack *stack = (mem_stack *)mmap( | ||
| 5 | 0, | ||
| 6 | capacity + sizeof(mem_stack), | ||
| 7 | PROT_READ | PROT_WRITE, | ||
| 8 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 9 | -1, | ||
| 10 | 0); | ||
| 11 | |||
| 12 | if (stack == MAP_FAILED) | ||
| 13 | { | ||
| 14 | return NULL; | ||
| 15 | } | ||
| 16 | |||
| 17 | stack->capacity = capacity; | ||
| 18 | stack->base_position = (u8 *)stack + sizeof(mem_stack); | ||
| 19 | stack->current_offset = 0; | ||
| 20 | |||
| 21 | return stack; | ||
| 22 | } | ||
| 23 | |||
| 24 | internal umm | ||
| 25 | calculate_padding(umm pointer, umm alignment, umm header_size) | ||
| 26 | { | ||
| 27 | umm modulo, padding; | ||
| 28 | |||
| 29 | if (!is_pow(alignment)) | ||
| 30 | { | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | modulo = pointer & (alignment - 1); | ||
| 35 | |||
| 36 | padding = 0; | ||
| 37 | |||
| 38 | if (0 == modulo) | ||
| 39 | { | ||
| 40 | padding = alignment - modulo; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (padding < header_size) | ||
| 44 | { | ||
| 45 | header_size -= padding; | ||
| 46 | |||
| 47 | if ((header_size & (alignment - 1)) != 0) | ||
| 48 | { | ||
| 49 | padding += alignment * (1 + (header_size / alignment)); | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | padding += alignment * (header_size / alignment); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return padding; | ||
| 58 | } | ||
| 59 | |||
| 60 | internal mem_stack * | ||
| 61 | stack_push_align(mem_stack *stack, u64 size, umm alignment) | ||
| 62 | { | ||
| 63 | umm padding = 0; | ||
| 64 | |||
| 65 | if (!is_pow(alignment)) | ||
| 66 | { | ||
| 67 | return (0); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (alignment > 128) | ||
| 71 | { | ||
| 72 | alignment = 128; | ||
| 73 | } | ||
| 74 | |||
| 75 | umm current_address = (umm)stack->base_position + stack->current_offset; | ||
| 76 | padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); | ||
| 77 | |||
| 78 | if (stack->current_offset + padding + size > stack->capacity) | ||
| 79 | { | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | stack->current_offset += padding; | ||
| 84 | |||
| 85 | umm next_address = current_address + (umm)padding; | ||
| 86 | mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); | ||
| 87 | header->padding = padding; | ||
| 88 | |||
| 89 | stack->current_offset += size; | ||
| 90 | |||
| 91 | return MemSet((void *)next_address, size); | ||
| 92 | } | ||
| 93 | internal void * | ||
| 94 | stack_push(mem_stack *stack, umm size) | ||
| 95 | { | ||
| 96 | return stack_push_align(stack, size, ARENA_ALIGN); | ||
| 97 | } | ||
| 98 | |||
| 99 | internal void | ||
| 100 | stack_pop(mem_stack *stack, void *pointer) | ||
| 101 | { | ||
| 102 | if (pointer != NULL) | ||
| 103 | { | ||
| 104 | umm start, end, current_address; | ||
| 105 | mem_stack_header *header; | ||
| 106 | umm prev_offset; | ||
| 107 | |||
| 108 | start = (umm)stack->base_position; | ||
| 109 | end = start + (umm)stack->capacity; | ||
| 110 | current_address = (umm)pointer; | ||
| 111 | |||
| 112 | if (!(start <= current_address && current_address < end)) | ||
| 113 | { | ||
| 114 | if (0 && "Out of bounds memory address passed to stack allocator (free)") | ||
| 115 | { | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (current_address >= start + (umm)stack->base_position) | ||
| 122 | { | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); | ||
| 127 | prev_offset = (size_t)(current_address - (umm)header->padding - start); | ||
| 128 | stack->current_offset = prev_offset; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | internal mem_stack * | ||
| 133 | stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u64 alignment) | ||
| 134 | { | ||
| 135 | if (pointer == NULL) | ||
| 136 | { | ||
| 137 | return stack_push_align(stack, new_size, alignment); | ||
| 138 | } | ||
| 139 | else if (new_size == 0) | ||
| 140 | { | ||
| 141 | stack_pop(stack, pointer); | ||
| 142 | return NULL; | ||
| 143 | } | ||
| 144 | |||
| 145 | umm start, end, current_address; | ||
| 146 | umm min_size = old_size < new_size ? old_size : new_size; | ||
| 147 | void *new_pointer; | ||
| 148 | |||
| 149 | start = (umm)stack->base_position; | ||
| 150 | end = start + (umm)stack->capacity; | ||
| 151 | current_address = (umm)pointer; | ||
| 152 | if (!(start <= current_address && current_address < end)) | ||
| 153 | { | ||
| 154 | return NULL; | ||
| 155 | } | ||
| 156 | |||
| 157 | if (current_address >= start + (umm)stack->current_offset) | ||
| 158 | { | ||
| 159 | return NULL; | ||
| 160 | } | ||
| 161 | |||
| 162 | if (old_size == new_size) | ||
| 163 | { | ||
| 164 | return pointer; | ||
| 165 | } | ||
| 166 | |||
| 167 | new_pointer = stack_push_align(stack, new_size, alignment); | ||
| 168 | memmove(new_pointer, pointer, min_size); | ||
| 169 | return new_pointer; | ||
| 170 | } | ||
| 171 | |||
| 172 | internal void | ||
| 173 | stack_pop_all(mem_stack *stack) | ||
| 174 | { | ||
| 175 | stack->current_offset = 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | internal void | ||
| 179 | stack_destroy(mem_stack *stack) | ||
| 180 | { | ||
| 181 | if (!stack) | ||
| 182 | { | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | munmap(stack, stack->capacity + sizeof(mem_stack)); | ||
| 187 | } | ||
diff --git a/source/base/base_stack.h b/source/base/base_stack.h new file mode 100755 index 0000000..fc80b20 --- /dev/null +++ b/source/base/base_stack.h | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #ifndef STACK_H | ||
| 2 | #define STACK_H | ||
| 3 | |||
| 4 | typedef struct mem_stack mem_stack; | ||
| 5 | typedef struct mem_stack_header mem_stack_header; | ||
| 6 | |||
| 7 | struct mem_stack | ||
| 8 | { | ||
| 9 | u8 *base_position; | ||
| 10 | umm current_offset; | ||
| 11 | umm capacity; | ||
| 12 | }; | ||
| 13 | |||
| 14 | struct mem_stack_header | ||
| 15 | { | ||
| 16 | u8 padding; | ||
| 17 | u8 previous_offset; | ||
| 18 | }; | ||
| 19 | |||
| 20 | #endif | ||
diff --git a/source/base/base_string.c b/source/base/base_string.c new file mode 100644 index 0000000..cb2d71d --- /dev/null +++ b/source/base/base_string.c | |||
| @@ -0,0 +1,73 @@ | |||
| 1 | internal umm | ||
| 2 | skip_whitespaces(string *buffer) | ||
| 3 | { | ||
| 4 | s32 index = 0; | ||
| 5 | while (buffer->size > index) | ||
| 6 | { | ||
| 7 | ++index; | ||
| 8 | } | ||
| 9 | } | ||
| 10 | |||
| 11 | internal b8 | ||
| 12 | compare_string_struct(string c1, string c2) | ||
| 13 | { | ||
| 14 | if (c1.size != c2.size) | ||
| 15 | { | ||
| 16 | return -1; | ||
| 17 | } | ||
| 18 | |||
| 19 | for (s32 index = 0; | ||
| 20 | index < c1.size; | ||
| 21 | ++index) | ||
| 22 | { | ||
| 23 | if (c1.Data[index] != c2.Data[index]) | ||
| 24 | { | ||
| 25 | return -1; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | return 0; | ||
| 30 | } | ||
| 31 | |||
| 32 | internal b8 | ||
| 33 | compare_string(char *c1, char *c2) | ||
| 34 | { | ||
| 35 | if (sizeof(*c1) != sizeof(*c2)) | ||
| 36 | { | ||
| 37 | return -1; | ||
| 38 | } | ||
| 39 | |||
| 40 | for ( | ||
| 41 | u64 word_idx = 0; | ||
| 42 | word_idx <= sizeof(*c1); | ||
| 43 | ++word_idx) | ||
| 44 | { | ||
| 45 | if (*c1 != *c2) | ||
| 46 | { | ||
| 47 | return -1; | ||
| 48 | } | ||
| 49 | ++c1; | ||
| 50 | ++c2; | ||
| 51 | } | ||
| 52 | |||
| 53 | return 0; | ||
| 54 | } | ||
| 55 | |||
| 56 | /** NOTE(nasr): Helper function to parse strings to int using ascii codes **/ | ||
| 57 | internal u64 | ||
| 58 | parse_u64(char *buf, umm len) | ||
| 59 | { | ||
| 60 | u64 value = 0; | ||
| 61 | |||
| 62 | for (umm buffer_idx = 0; buffer_idx < len; ++buffer_idx) | ||
| 63 | { | ||
| 64 | char c = buf[buffer_idx]; | ||
| 65 | if (c < '0' || c > '9') | ||
| 66 | { | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | value = value * 10 + (c - '0'); | ||
| 70 | } | ||
| 71 | |||
| 72 | return value; | ||
| 73 | } | ||
diff --git a/source/base/base_string.h b/source/base/base_string.h new file mode 100644 index 0000000..d879450 --- /dev/null +++ b/source/base/base_string.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #ifndef BASE_STRING_H | ||
| 2 | #define BASE_STRING_H | ||
| 3 | |||
| 4 | // let's use this for strings | ||
| 5 | // to better differentiate between stuff | ||
| 6 | typedef u8 s8; | ||
| 7 | typedef struct string string ; | ||
| 8 | |||
| 9 | struct string | ||
| 10 | { | ||
| 11 | s8 *data; | ||
| 12 | umm size; | ||
| 13 | }; | ||
| 14 | |||
| 15 | // TODO(nasr): helper functions for string | ||
| 16 | |||
| 17 | #endif /* BASE_STRING_H */ | ||
diff --git a/source/base/base_test.c b/source/base/base_test.c new file mode 100644 index 0000000..e44aa69 --- /dev/null +++ b/source/base/base_test.c | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | void | ||
| 2 | write_int(i32 num) | ||
| 3 | { | ||
| 4 | if (num < 0) | ||
| 5 | { | ||
| 6 | write(STDERR_FILENO, "-", 1); | ||
| 7 | num = -num; | ||
| 8 | } | ||
| 9 | if (num >= 10) | ||
| 10 | write_int(num / 10); | ||
| 11 | char digit = '0' + (num % 10); | ||
| 12 | |||
| 13 | write(STDERR_FILENO, &digit, 1); | ||
| 14 | } | ||
diff --git a/source/base/base_test.h b/source/base/base_test.h new file mode 100644 index 0000000..11028f4 --- /dev/null +++ b/source/base/base_test.h | |||
| @@ -0,0 +1,61 @@ | |||
| 1 | #ifndef BASE_TEST_H | ||
| 2 | #define BASE_TEST_H | ||
| 3 | |||
| 4 | void | ||
| 5 | write_int(i32 num); | ||
| 6 | |||
| 7 | #define RED "\x1b[31m" | ||
| 8 | #define GREEN "\x1b[32m" | ||
| 9 | #define RESET "\x1b[0m" | ||
| 10 | #define BLUE "\x1b[34m" | ||
| 11 | |||
| 12 | #define LEN(s) (sizeof(s) - 1) | ||
| 13 | |||
| 14 | #define show \ | ||
| 15 | do \ | ||
| 16 | { \ | ||
| 17 | write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \ | ||
| 18 | write(STDOUT_FILENO, ":", 1); \ | ||
| 19 | write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \ | ||
| 20 | write(STDOUT_FILENO, ":", 1); \ | ||
| 21 | write_int(__LINE__); \ | ||
| 22 | write(STDOUT_FILENO, "\n", 1); \ | ||
| 23 | } while (0) | ||
| 24 | |||
| 25 | #define test(expr) \ | ||
| 26 | { \ | ||
| 27 | if ((expr) != 0) \ | ||
| 28 | { \ | ||
| 29 | write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \ | ||
| 30 | show; \ | ||
| 31 | _exit(1); \ | ||
| 32 | } \ | ||
| 33 | } | ||
| 34 | |||
| 35 | #define check(expr) \ | ||
| 36 | { \ | ||
| 37 | if ((expr) != 0) \ | ||
| 38 | { \ | ||
| 39 | write(STDERR_FILENO, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ | ||
| 40 | show; \ | ||
| 41 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 42 | _exit(1); \ | ||
| 43 | } \ | ||
| 44 | else \ | ||
| 45 | { \ | ||
| 46 | write(STDERR_FILENO, GREEN "[SUCCESS] ", LEN(GREEN "[SUCCESS] ")); \ | ||
| 47 | show; \ | ||
| 48 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 49 | } \ | ||
| 50 | } | ||
| 51 | |||
| 52 | #define checkpoint_output "<<CHECKPOINT>>\n" | ||
| 53 | #define checkpoint_end_output "^^^^^^^^^^^^^^\n\n\n" | ||
| 54 | #define checkpoint \ | ||
| 55 | { \ | ||
| 56 | write(STDERR_FILENO, BLUE checkpoint_output, LEN(BLUE checkpoint_output)); \ | ||
| 57 | show; \ | ||
| 58 | write(STDERR_FILENO, BLUE checkpoint_end_output, LEN(BLUE checkpoint_end_output)); \ | ||
| 59 | } | ||
| 60 | |||
| 61 | #endif /* BASE_TEST_H */ | ||
diff --git a/source/base/bash_hash.h b/source/base/bash_hash.h new file mode 100644 index 0000000..2c286a2 --- /dev/null +++ b/source/base/bash_hash.h | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | ##ifndef HEADER_H | ||
| 2 | #define HEADER_H | ||
| 3 | |||
| 4 | typedef struct hash_map hash_map; | ||
| 5 | typedef struct hash hash; | ||
| 6 | |||
| 7 | |||
| 8 | struct hash_map | ||
| 9 | { | ||
| 10 | |||
| 11 | |||
| 12 | }; | ||
| 13 | |||
| 14 | |||
| 15 | #endif /* HEADER_H */ | ||
diff --git a/source/core/core.c b/source/core/core.c new file mode 100644 index 0000000..43322de --- /dev/null +++ b/source/core/core.c | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | #define TB_IMPLEMENTATION | ||
| 2 | #include "core.h" | ||
| 3 | #include "../base/base_include.h" | ||
| 4 | #include "../platform/platform_include.h" | ||
| 5 | |||
| 6 | int main() | ||
| 7 | { | ||
| 8 | b32 running = 1; | ||
| 9 | |||
| 10 | Display *MainDisplay = XOpenDisplay(0); | ||
| 11 | mem_arena *arena = arena_create(MiB(8)); | ||
| 12 | |||
| 13 | Window root = XDefaultRootWindow(MainDisplay); | ||
| 14 | int screen = DefaultScreen(MainDisplay); | ||
| 15 | |||
| 16 | Visual *v = DefaultVisual(MainDisplay, screen); | ||
| 17 | |||
| 18 | XSetWindowAttributes wa = { | ||
| 19 | .background_pixmap = None, | ||
| 20 | .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), | ||
| 21 | .border_pixmap = CopyFromParent, | ||
| 22 | .border_pixel = 0, | ||
| 23 | .bit_gravity = ForgetGravity, | ||
| 24 | .win_gravity = NorthWestGravity, | ||
| 25 | .backing_store = NotUseful, | ||
| 26 | .backing_planes = 1, | ||
| 27 | .backing_pixel = 0, | ||
| 28 | .save_under = False, | ||
| 29 | .event_mask = 0, | ||
| 30 | .do_not_propagate_mask = 0, | ||
| 31 | .override_redirect = False, | ||
| 32 | .colormap = CopyFromParent, | ||
| 33 | .cursor = None | ||
| 34 | }; | ||
| 35 | |||
| 36 | i32 dp_heigth = DisplayHeight(MainDisplay, screen); | ||
| 37 | i32 dp_width = DisplayWidth(MainDisplay, screen); | ||
| 38 | |||
| 39 | WindowProperties p = { | ||
| 40 | |||
| 41 | .x = dp_width / 2, | ||
| 42 | .y = dp_heigth / 2, | ||
| 43 | .height = (u32)800, | ||
| 44 | .width = (u32)1200, | ||
| 45 | .border_width = 0, | ||
| 46 | .window_depth = CopyFromParent, | ||
| 47 | .window_class = CopyFromParent, | ||
| 48 | .value_mask = CWBackPixel, | ||
| 49 | |||
| 50 | }; | ||
| 51 | |||
| 52 | Window window = | ||
| 53 | XCreateWindow( | ||
| 54 | MainDisplay, | ||
| 55 | root, | ||
| 56 | p.x, | ||
| 57 | p.y, | ||
| 58 | p.width, | ||
| 59 | p.height, | ||
| 60 | p.border_width, | ||
| 61 | p.window_depth, | ||
| 62 | p.window_class, | ||
| 63 | v, | ||
| 64 | p.value_mask, | ||
| 65 | &wa); | ||
| 66 | |||
| 67 | Pixmap pixmap = XCreatePixmap(MainDisplay, window, dp_width, dp_heigth, 1); | ||
| 68 | |||
| 69 | XSetWindowBorder(MainDisplay, window, 60); | ||
| 70 | |||
| 71 | // NOTE(nasr): type of input we want to handle | ||
| 72 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask | KeyReleaseMask); | ||
| 73 | |||
| 74 | XMapWindow(MainDisplay, window); | ||
| 75 | |||
| 76 | double x = p.width / 2; | ||
| 77 | double y = p.height / 2; | ||
| 78 | |||
| 79 | u32 rect_width = 50; | ||
| 80 | u32 rect_height = 50; | ||
| 81 | |||
| 82 | u64 color = 0x0000ff00; | ||
| 83 | |||
| 84 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); | ||
| 85 | XSetForeground(MainDisplay, gc, color); | ||
| 86 | |||
| 87 | double *pX = &x; | ||
| 88 | double *pY = &y; | ||
| 89 | |||
| 90 | XEvent event; | ||
| 91 | XNextEvent(MainDisplay, &event); | ||
| 92 | |||
| 93 | for (;running;) | ||
| 94 | { | ||
| 95 | KeySym keysym = XLookupKeysym(event, 0); | ||
| 96 | |||
| 97 | switch (event.type) | ||
| 98 | { | ||
| 99 | case (KeyPress): | ||
| 100 | { | ||
| 101 | if (keysym == XK_p || keysym == XK_P) | ||
| 102 | { | ||
| 103 | XDrawRectangle(MainDisplay, screen, gc, 50, 50, 50, 50); | ||
| 104 | } | ||
| 105 | |||
| 106 | break; | ||
| 107 | } | ||
| 108 | case (KeyRelease): | ||
| 109 | { | ||
| 110 | if (keysym == XK_p || keysym == XK_P) | ||
| 111 | { | ||
| 112 | XDrawRectangle(MainDisplay, screen, gc, 50, 50, 50, 50); | ||
| 113 | } | ||
| 114 | |||
| 115 | break; | ||
| 116 | } | ||
| 117 | default: | ||
| 118 | { | ||
| 119 | |||
| 120 | } | ||
| 121 | |||
| 122 | } | ||
| 123 | } | ||
| 124 | arena_clear(arena); | ||
| 125 | return 0; | ||
| 126 | } | ||
diff --git a/source/core/core.h b/source/core/core.h new file mode 100644 index 0000000..c5821c5 --- /dev/null +++ b/source/core/core.h | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | #ifndef CORE_H | ||
| 2 | #define CORE_H | ||
| 3 | |||
| 4 | |||
| 5 | #endif | ||
diff --git a/source/platform/platform.c b/source/platform/platform.c new file mode 100644 index 0000000..2e7bbad --- /dev/null +++ b/source/platform/platform.c | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | internal inline void | ||
| 2 | sleep_ms(long ms) | ||
| 3 | { | ||
| 4 | struct timespec ts; | ||
| 5 | ts.tv_sec = ms / 1000; | ||
| 6 | ts.tv_nsec = (ms % 1000) * 1000000L; | ||
| 7 | |||
| 8 | while (nanosleep(&ts, &ts)) | ||
| 9 | { | ||
| 10 | NULL; | ||
| 11 | } | ||
| 12 | } | ||
diff --git a/source/platform/platform.h b/source/platform/platform.h new file mode 100644 index 0000000..da8e065 --- /dev/null +++ b/source/platform/platform.h | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #ifndef PLATFORM_H | ||
| 2 | #define PLATFORM_H | ||
| 3 | |||
| 4 | #define NIL 0 | ||
| 5 | |||
| 6 | #include <X11/X.h> | ||
| 7 | #include <X11/Xlib.h> | ||
| 8 | #include <X11/keysym.h> | ||
| 9 | #include <time.h> | ||
| 10 | #include <unistd.h> | ||
| 11 | |||
| 12 | typedef struct WindowProperties WindowProperties; | ||
| 13 | struct WindowProperties | ||
| 14 | { | ||
| 15 | i32 x; | ||
| 16 | i32 y; | ||
| 17 | u32 height; | ||
| 18 | u32 width; | ||
| 19 | u32 border_width; | ||
| 20 | i32 window_depth; | ||
| 21 | u32 window_class; | ||
| 22 | u64 value_mask; | ||
| 23 | |||
| 24 | }; | ||
| 25 | |||
| 26 | typedef struct vertex vertex; | ||
| 27 | |||
| 28 | struct vertex | ||
| 29 | { | ||
| 30 | i32 x; | ||
| 31 | i32 y; | ||
| 32 | i32 z; | ||
| 33 | }; | ||
| 34 | |||
| 35 | typedef struct display_pos display_pos; | ||
| 36 | |||
| 37 | struct display_pos | ||
| 38 | { | ||
| 39 | i32 x; | ||
| 40 | i32 y; | ||
| 41 | |||
| 42 | }; | ||
| 43 | |||
| 44 | typedef struct pos pos; | ||
| 45 | struct pos | ||
| 46 | { | ||
| 47 | i32 x; | ||
| 48 | i32 y; | ||
| 49 | i32 z; | ||
| 50 | |||
| 51 | } ; | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | #endif /* PLATFORM_H */ | ||
diff --git a/source/platform/platform_include.h b/source/platform/platform_include.h new file mode 100644 index 0000000..886c6b8 --- /dev/null +++ b/source/platform/platform_include.h | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | #ifndef PLATFORM_INCLUDE_H | ||
| 2 | #define PLATFORM_INCLUDE_H | ||
| 3 | |||
| 4 | #include "platform.h" | ||
| 5 | #include "platform.c" | ||
| 6 | |||
| 7 | #endif /* PLATFORM_INCLUDE_H */ | ||
