diff options
Diffstat (limited to 'source/base')
| -rwxr-xr-x | source/base/base_include.h | 3 | ||||
| -rw-r--r-- | source/base/base_mem.c | 17 | ||||
| -rw-r--r-- | source/base/base_mem.h | 18 | ||||
| -rw-r--r-- | source/base/base_os.h | 41 | ||||
| -rw-r--r-- | source/base/base_string.c | 63 | ||||
| -rw-r--r-- | source/base/base_string.h | 45 | ||||
| -rw-r--r-- | source/base/base_test.c | 0 |
7 files changed, 96 insertions, 91 deletions
diff --git a/source/base/base_include.h b/source/base/base_include.h index 0b0f256..40ae5ea 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h | |||
| @@ -20,11 +20,8 @@ | |||
| 20 | 20 | ||
| 21 | #ifdef BASE_UNITY | 21 | #ifdef BASE_UNITY |
| 22 | 22 | ||
| 23 | #include "base_mem.c" | ||
| 24 | #include "base_arena.c" | 23 | #include "base_arena.c" |
| 25 | #include "base_stack.c" | 24 | #include "base_stack.c" |
| 26 | #include "base_test.c" | ||
| 27 | #include "base_string.c" | ||
| 28 | 25 | ||
| 29 | #endif | 26 | #endif |
| 30 | #endif | 27 | #endif |
diff --git a/source/base/base_mem.c b/source/base/base_mem.c index f20ba39..e69de29 100644 --- a/source/base/base_mem.c +++ b/source/base/base_mem.c | |||
| @@ -1,17 +0,0 @@ | |||
| 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 index 945f0ce..2778fce 100644 --- a/source/base/base_mem.h +++ b/source/base/base_mem.h | |||
| @@ -5,4 +5,22 @@ | |||
| 5 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | 5 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 6 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | 6 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 7 | 7 | ||
| 8 | internal inline b8 | ||
| 9 | is_pow(umm x) | ||
| 10 | { | ||
| 11 | return (x & (x - 1)) == 0; | ||
| 12 | } | ||
| 13 | |||
| 14 | internal inline u64 | ||
| 15 | align(u64 pointer, umm alignment) | ||
| 16 | { | ||
| 17 | if ((alignment & (alignment - 1)) == 0) | ||
| 18 | { | ||
| 19 | return pointer; | ||
| 20 | } | ||
| 21 | |||
| 22 | return (pointer + alignment - 1) & ~(alignment - 1); | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 8 | #endif | 26 | #endif |
diff --git a/source/base/base_os.h b/source/base/base_os.h index ce9acae..ec0a3de 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h | |||
| @@ -9,6 +9,45 @@ print(const char *str) | |||
| 9 | write(STDOUT_FILENO, str, len); | 9 | write(STDOUT_FILENO, str, len); |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | #define print(Format) print(Format) | 12 | internal string8 |
| 13 | load_file(const char *path) | ||
| 14 | { | ||
| 15 | string8 result = {0}; | ||
| 16 | struct stat sbuf = {0}; | ||
| 17 | int err = 0; | ||
| 18 | i32 file = open(path, O_RDONLY); | ||
| 19 | |||
| 20 | if(file) | ||
| 21 | { | ||
| 22 | |||
| 23 | if(file != -1) | ||
| 24 | { | ||
| 25 | err = fstat(file, &sbuf); | ||
| 26 | check(err != -1); | ||
| 27 | |||
| 28 | result.size = (u64)sbuf.st_size; | ||
| 29 | |||
| 30 | if(result.size != 0) | ||
| 31 | { | ||
| 32 | result.data = (u8 *)mmap(0, | ||
| 33 | result.size, | ||
| 34 | PROT_READ, | ||
| 35 | MAP_PRIVATE, | ||
| 36 | file, | ||
| 37 | 0); | ||
| 38 | |||
| 39 | check(result.data != MAP_FAILED); | ||
| 40 | } | ||
| 41 | |||
| 42 | close(file); | ||
| 43 | } | ||
| 44 | else | ||
| 45 | { | ||
| 46 | // TODO(nasr): logging | ||
| 47 | } | ||
| 48 | |||
| 49 | } | ||
| 50 | return result; | ||
| 51 | } | ||
| 13 | 52 | ||
| 14 | #endif /* BASE_OS_H */ | 53 | #endif /* BASE_OS_H */ |
diff --git a/source/base/base_string.c b/source/base/base_string.c deleted file mode 100644 index 9d09914..0000000 --- a/source/base/base_string.c +++ /dev/null | |||
| @@ -1,63 +0,0 @@ | |||
| 1 | internal b8 | ||
| 2 | compare_string(string c1, string c2) | ||
| 3 | { | ||
| 4 | if (c1.size != c2.size) | ||
| 5 | { | ||
| 6 | return 0; | ||
| 7 | } | ||
| 8 | |||
| 9 | for (u64 index = 0; index < c1.size; ++index) | ||
| 10 | { | ||
| 11 | if (c1.data[index] != c2.data[index]) | ||
| 12 | { | ||
| 13 | return 0; | ||
| 14 | } | ||
| 15 | } | ||
| 16 | |||
| 17 | return 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | internal b8 | ||
| 21 | compare_u8(u8 *c1, u8 *c2, u64 len) | ||
| 22 | { | ||
| 23 | if (sizeof(c1) != len || sizeof(c2) != len) | ||
| 24 | { | ||
| 25 | return 0; | ||
| 26 | } | ||
| 27 | |||
| 28 | if (sizeof(*c1) != sizeof(*c2)) | ||
| 29 | { | ||
| 30 | return 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | for (u64 word_idx = 0; word_idx <= sizeof(*c1); ++word_idx) | ||
| 34 | { | ||
| 35 | if (*c1 != *c2) | ||
| 36 | { | ||
| 37 | return 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | ++c1; | ||
| 41 | ++c2; | ||
| 42 | } | ||
| 43 | |||
| 44 | return 1; | ||
| 45 | } | ||
| 46 | |||
| 47 | internal u64 | ||
| 48 | to_u64(u8 *buf, umm len) | ||
| 49 | { | ||
| 50 | u64 value = 0; | ||
| 51 | |||
| 52 | for (umm buffer_idx = 0; buffer_idx < len; ++buffer_idx) | ||
| 53 | { | ||
| 54 | u8 c = buf[buffer_idx]; | ||
| 55 | if (c < '0' || c > '9') | ||
| 56 | { | ||
| 57 | break; | ||
| 58 | } | ||
| 59 | value = value * 10 + (c - '0'); | ||
| 60 | } | ||
| 61 | |||
| 62 | return value; | ||
| 63 | } | ||
diff --git a/source/base/base_string.h b/source/base/base_string.h index 5f875e0..941dc53 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h | |||
| @@ -1,15 +1,46 @@ | |||
| 1 | #ifndef BASE_STRING_H | 1 | #ifndef BASE_STRING_H |
| 2 | #define BASE_STRING_H | 2 | #define BASE_STRING_H |
| 3 | 3 | ||
| 4 | #define _StringCast (string) | 4 | #include <string.h> |
| 5 | #define StringCast { .data (u8 *)(string), .size = (sizeof((string)) - 1)} | ||
| 6 | #define PushString (Arena, Size) StringCast{.data = PushArray((arena), PushStruct(u8), (Size)), .size = (u64)(Size)} | ||
| 7 | 5 | ||
| 8 | typedef struct string string; | 6 | #define StringLit(string) \ |
| 9 | struct string | 7 | (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) } |
| 8 | |||
| 9 | #define PushString(arena, size) \ | ||
| 10 | (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) } | ||
| 11 | |||
| 12 | typedef struct string8 string8; | ||
| 13 | struct string8 | ||
| 10 | { | 14 | { |
| 11 | u8 *data; | 15 | u8 *data; |
| 12 | u64 size; | 16 | u64 size; |
| 13 | }; | 17 | }; |
| 14 | 18 | ||
| 19 | internal b8 | ||
| 20 | string8_cmp(string8 a, string8 b) | ||
| 21 | { | ||
| 22 | if (a.size != b.size) return 0; | ||
| 23 | return (b8)(memcmp(a.data, b.data, a.size) == 0); | ||
| 24 | } | ||
| 25 | |||
| 26 | internal u64 | ||
| 27 | string8_to_u64(u8 *buf, umm len) | ||
| 28 | { | ||
| 29 | u64 value = 0; | ||
| 30 | for (umm i = 0; i < len; ++i) | ||
| 31 | { | ||
| 32 | u8 c = buf[i]; | ||
| 33 | if (c < '0' || c > '9') break; | ||
| 34 | value = value * 10 + (c - '0'); | ||
| 35 | } | ||
| 36 | return value; | ||
| 37 | } | ||
| 38 | |||
| 39 | internal void | ||
| 40 | string8_append_char(string8 *buf, u8 c) | ||
| 41 | { | ||
| 42 | buf->data[buf->size] = c; | ||
| 43 | buf->size += 1; | ||
| 44 | } | ||
| 45 | |||
| 15 | #endif /* BASE_STRING_H */ | 46 | #endif /* BASE_STRING_H */ |
diff --git a/source/base/base_test.c b/source/base/base_test.c deleted file mode 100644 index e69de29..0000000 --- a/source/base/base_test.c +++ /dev/null | |||
