summaryrefslogtreecommitdiff
path: root/source/base
diff options
context:
space:
mode:
Diffstat (limited to 'source/base')
-rwxr-xr-xsource/base/base.h73
-rwxr-xr-xsource/base/base_arena.c138
-rwxr-xr-xsource/base/base_arena.h26
-rw-r--r--source/base/base_hash.c12
-rwxr-xr-xsource/base/base_include.h27
-rw-r--r--source/base/base_io.h11
-rw-r--r--source/base/base_mem.c0
-rw-r--r--source/base/base_mem.h26
-rw-r--r--source/base/base_os.h38
-rwxr-xr-xsource/base/base_stack.c187
-rwxr-xr-xsource/base/base_stack.h22
-rw-r--r--source/base/base_string.h59
-rw-r--r--source/base/base_test.h74
-rw-r--r--source/base/bash_hash.h15
14 files changed, 708 insertions, 0 deletions
diff --git a/source/base/base.h b/source/base/base.h
new file mode 100755
index 0000000..ef23391
--- /dev/null
+++ b/source/base/base.h
@@ -0,0 +1,73 @@
1#ifndef BASE_H
2#define BASE_H
3
4/* assert an expression and output the file and the line */
5
6#define internal static
7#define global_variable static
8#define local_persist static
9
10#define ERR_OK 0
11#define ERR_IO 1
12#define ERR_PARSE 2
13#define ERR_PERM 3
14#define ERR_INVALID 4
15
16#define KiB(n) (((u64)(n)) << 10)
17#define MiB(n) (((u64)(n)) << 20)
18#define GiB(n) (((u64)(n)) << 30)
19
20#define unused(x) (void)(x)
21
22#define PATH_MAX_LEN 128
23#define BUFF_SMALL 128
24#define BUFF_DEFAULT 256
25#define BUFF_LARGE 512
26
27#define NIL 0
28
29#define DEPRECATED __attribute__((__deprecated__))
30
31#if defined(__arm__) || defined(__aarch64__)
32#define breakpoint __asm__ volatile("brk #0");
33#define temp_breakpoint __asm__ volatile("udf #0");
34#elif defined(__i386__) || defined(__x86_64__)
35#define breakpoint __asm__ volatile("int3");
36#endif
37
38#define MemCpy(dest, src, len) memcpy((dest), (src), (len))
39#define MemSet(dest, len) memset((dest), (0), (len))
40
41#if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS)
42#pragma section(".rdata$", read)
43#define read_only __declspec(allocate(".rdata$"))
44#elif (COMPILER_CLANG && OS_LINUX)
45#define read_only __attribute__((section(".rodata")))
46#else
47#define read_only
48#endif
49
50typedef uint64_t u64;
51typedef uint32_t u32;
52typedef uint16_t u16;
53typedef uint8_t u8;
54
55typedef int8_t i8;
56typedef int16_t i16;
57typedef int32_t i32;
58typedef int64_t i64;
59
60typedef float f32;
61typedef double f64;
62
63typedef i32 b32;
64typedef i16 b16;
65typedef u8 b8;
66
67typedef uintptr_t umm;
68typedef intptr_t smm;
69
70#define TRUE (0 == 0)
71#define FALSE (0 != 0)
72
73#endif
diff --git a/source/base/base_arena.c b/source/base/base_arena.c
new file mode 100755
index 0000000..5855e5e
--- /dev/null
+++ b/source/base/base_arena.c
@@ -0,0 +1,138 @@
1internal mem_arena *
2arena_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
26internal void
27arena_destroy(mem_arena *arena)
28{
29 if (!arena)
30 {
31 return;
32 }
33 munmap(arena, arena->capacity + sizeof(mem_arena));
34}
35internal void *
36arena_alloc(mem_arena *arena, u64 size, b32 zero)
37{
38 if (!arena)
39 {
40 return NULL;
41 }
42 u64 aligned = Align(arena->current_position, ARENA_ALIGN);
43 u64 new_pos = aligned + size;
44 if (new_pos > arena->capacity)
45 {
46 return NULL;
47 }
48
49 void *out = arena->base_position + aligned;
50
51 arena->previous_position = arena->current_position;
52 arena->current_position = aligned + size;
53
54 if (zero) MemSet(out, size);
55
56 return out;
57}
58
59internal void
60arena_pop(mem_arena *arena, u64 size)
61{
62 size = MIN(size, arena->current_position);
63 arena->current_position -= size;
64}
65
66internal void
67arena_pop_to(mem_arena *arena, u64 pos)
68{
69 u64 size = pos < arena->current_position ? arena->current_position - pos : 0;
70 arena_pop(arena, size);
71}
72
73internal void
74arena_clear(mem_arena *arena)
75{
76 arena->current_position = 0;
77}
78
79internal mem_arena *
80arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size, umm alignment)
81{
82 u8 *old_mem = (u8 *)old_memory;
83
84 if (!is_pow(alignment))
85 {
86 Align(arena->current_position, alignment);
87 }
88
89 if (old_memory == NULL || old_size == 0)
90 {
91 return (mem_arena *)arena_alloc(arena, new_size, 0);
92 }
93 else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity))
94 {
95 if ((arena->base_position + arena->previous_position) == old_memory)
96 {
97 arena->current_position = arena->previous_position + new_size;
98 if (new_size > old_size)
99 {
100 MemSet(&arena->current_position, new_size - old_size);
101 }
102 return (mem_arena *)old_memory;
103 }
104 else
105 {
106 void *new_memory = arena_alloc(arena, new_size, 0);
107 umm copy_size = old_size < new_size ? old_size : new_size;
108 memmove(new_memory, old_mem, copy_size);
109 }
110 }
111 else
112 {
113 check(0);
114 }
115 return NULL;
116}
117
118internal mem_arena *
119arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size)
120{
121 return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN);
122}
123
124internal temp_arena
125temp_arena_begin(mem_arena *arena)
126{
127 temp_arena t;
128 t.arena = arena;
129 t.start_position = arena->current_position;
130
131 return t;
132}
133
134internal void
135temp_arena_end(temp_arena temp)
136{
137 temp.arena->current_position = temp.start_position;
138}
diff --git a/source/base/base_arena.h b/source/base/base_arena.h
new file mode 100755
index 0000000..2818ae4
--- /dev/null
+++ b/source/base/base_arena.h
@@ -0,0 +1,26 @@
1#ifndef BASE_ARENA_H
2#define BASE_ARENA_H
3
4#define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment))
5#define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type), 0)
6#define PushStructZero(arena, type) (type *)arena_alloc((arena), sizeof(type), 1)
7#define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 0)
8#define PushArrayZero(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 1)
9
10typedef struct mem_arena mem_arena;
11struct mem_arena
12{
13 u64 current_position;
14 u64 previous_position;
15 u64 capacity;
16 u8 *base_position;
17};
18
19typedef struct temp_arena temp_arena;
20struct temp_arena
21{
22 mem_arena *arena;
23 u64 start_position;
24};
25
26#endif /* BASE_ARENA_H */
diff --git a/source/base/base_hash.c b/source/base/base_hash.c
new file mode 100644
index 0000000..1964441
--- /dev/null
+++ b/source/base/base_hash.c
@@ -0,0 +1,12 @@
1
2internal u64
3generate_hash()
4{
5
6
7
8}
9
10internal hash_map
11make_hash_map
12
diff --git a/source/base/base_include.h b/source/base/base_include.h
new file mode 100755
index 0000000..40ae5ea
--- /dev/null
+++ b/source/base/base_include.h
@@ -0,0 +1,27 @@
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_stack.h"
17#include "base_test.h"
18#include "base_string.h"
19#include "base_os.h"
20
21#ifdef BASE_UNITY
22
23#include "base_arena.c"
24#include "base_stack.c"
25
26#endif
27#endif
diff --git a/source/base/base_io.h b/source/base/base_io.h
new file mode 100644
index 0000000..ece4d7c
--- /dev/null
+++ b/source/base/base_io.h
@@ -0,0 +1,11 @@
1#ifndef BASE_IO_H
2#define BASE_IO_H
3
4internal void
5input_read()
6{
7
8
9}
10
11#endif /* BASE_IO_H */
diff --git a/source/base/base_mem.c b/source/base/base_mem.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/source/base/base_mem.c
diff --git a/source/base/base_mem.h b/source/base/base_mem.h
new file mode 100644
index 0000000..2778fce
--- /dev/null
+++ b/source/base/base_mem.h
@@ -0,0 +1,26 @@
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
8internal inline b8
9is_pow(umm x)
10{
11 return (x & (x - 1)) == 0;
12}
13
14internal inline u64
15align(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
26#endif
diff --git a/source/base/base_os.h b/source/base/base_os.h
new file mode 100644
index 0000000..23587c6
--- /dev/null
+++ b/source/base/base_os.h
@@ -0,0 +1,38 @@
1#ifndef BASE_OS_H
2#define BASE_OS_H
3
4internal void
5print(const char *str)
6{
7 i32 len = 0;
8 while (str[len]) len++;
9 write(STDOUT_FILENO, str, len);
10}
11
12internal string8
13load_file(const char *path)
14{
15 string8 result = {0};
16 struct stat sbuf = {0};
17
18 i32 file = open(path, O_RDONLY);
19 if(file == -1) return result;
20
21 if(fstat(file, &sbuf) == -1)
22 {
23 print("error: fstat failed");
24 close(file);
25 return result;
26 }
27
28 result.size = (u64)sbuf.st_size;
29 if(result.size != 0)
30 {
31 result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0);
32 }
33
34 close(file);
35 return result;
36}
37
38#endif /* BASE_OS_H */
diff --git a/source/base/base_stack.c b/source/base/base_stack.c
new file mode 100755
index 0000000..9c1218a
--- /dev/null
+++ b/source/base/base_stack.c
@@ -0,0 +1,187 @@
1internal mem_stack *
2stack_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
24internal u8
25calculate_padding(u64 pointer, u8 alignment, u64 header_size)
26{
27 u8 modulo, padding;
28
29 if (!is_pow(alignment))
30 {
31 return 0;
32 }
33
34 modulo = pointer & (u8)(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
60internal mem_stack *
61stack_push_align(mem_stack *stack, u64 size, u8 alignment)
62{
63 u8 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 u64 current_address = (u64)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 u64 next_address = current_address + (u64)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}
93internal void *
94stack_push(mem_stack *stack, u64 size)
95{
96 return stack_push_align(stack, size, ARENA_ALIGN);
97}
98
99internal void
100stack_pop(mem_stack *stack, void *pointer)
101{
102 if (pointer != NULL)
103 {
104 u64 start, end, current_address;
105 mem_stack_header *header;
106 u64 prev_offset;
107
108 start = (u64)stack->base_position;
109 end = start + (u64)stack->capacity;
110 current_address = (u64)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 + (u64)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 - (u64)header->padding - start);
128 stack->current_offset = prev_offset;
129 }
130}
131
132internal mem_stack *
133stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u8 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 u64 start, end, current_address;
146 u64 min_size = old_size < new_size ? old_size : new_size;
147 void *new_pointer;
148
149 start = (u64)stack->base_position;
150 end = start + (u64)stack->capacity;
151 current_address = (u64)pointer;
152 if (!(start <= current_address && current_address < end))
153 {
154 return NULL;
155 }
156
157 if (current_address >= start + (u64)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
172internal void
173stack_pop_all(mem_stack *stack)
174{
175 stack->current_offset = 0;
176}
177
178internal void
179stack_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..54d61d3
--- /dev/null
+++ b/source/base/base_stack.h
@@ -0,0 +1,22 @@
1#ifndef STACK_H
2#define STACK_H
3
4typedef struct mem_stack_header mem_stack_header;
5struct mem_stack_header
6{
7 u8 padding;
8 u8 previous_offset;
9};
10
11
12typedef struct mem_stack mem_stack;
13struct mem_stack
14{
15 mem_stack_header *header;
16
17 u64 current_offset;
18 u64 capacity;
19 u8 *base_position;
20};
21
22#endif
diff --git a/source/base/base_string.h b/source/base/base_string.h
new file mode 100644
index 0000000..189b38a
--- /dev/null
+++ b/source/base/base_string.h
@@ -0,0 +1,59 @@
1#ifndef BASE_STRING_H
2#define BASE_STRING_H
3
4#include <string.h>
5
6#define StringLit(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#define StringFmt "%.*s"
13#define ULongFmt "%lu"
14#define ULLongFmt "%llu"
15
16typedef struct string8 string8;
17struct string8
18{
19 u8 *data;
20 u64 size;
21};
22
23internal b8
24string8_cmp(string8 a, string8 b)
25{
26 if (a.size != b.size) return 0;
27 return (b8)(memcmp(a.data, b.data, a.size) == 0);
28}
29
30internal u64
31string8_to_u64(u8 *buf, umm len)
32{
33 u64 value = 0;
34 for (umm i = 0; i < len; ++i)
35 {
36 u8 c = buf[i];
37 if (c < '0' || c > '9') break;
38 value = value * 10 + (c - '0');
39 }
40 return value;
41}
42
43internal void
44string8_append_char(string8 *buf, u8 c)
45{
46 buf->data[buf->size] = c;
47 buf->size += 1;
48}
49
50read_only global_variable
51string8 nil_string =
52{
53
54 .data = NULL,
55 .size = 0,
56
57};
58
59#endif /* BASE_STRING_H */
diff --git a/source/base/base_test.h b/source/base/base_test.h
new file mode 100644
index 0000000..412797b
--- /dev/null
+++ b/source/base/base_test.h
@@ -0,0 +1,74 @@
1#ifndef BASE_TEST_H
2#define BASE_TEST_H
3
4#define RED "\x1b[31m"
5#define GREEN "\x1b[32m"
6#define RESET "\x1b[0m"
7#define BLUE "\x1b[34m"
8
9#define LEN(s) (sizeof(s) - 1)
10
11internal void
12write_int(i32 num)
13{
14
15 if (num < 0)
16 {
17 write(STDERR_FILENO, "-", 1);
18 num = -num;
19 }
20 if (num >= 10)
21 write_int(num / 10);
22 char digit = '0' + (num % 10);
23
24 write(STDERR_FILENO, &digit, 1);
25}
26
27#define show \
28 do \
29 { \
30 write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \
31 write(STDOUT_FILENO, ":", 1); \
32 write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \
33 write(STDOUT_FILENO, ":", 1); \
34 write_int(__LINE__); \
35 write(STDOUT_FILENO, "\n", 1); \
36 } while (0)
37
38#define test(expr) \
39 { \
40 if ((expr) != 0) \
41 { \
42 write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \
43 show; \
44 _exit(1); \
45 } \
46 }
47
48#define check(expr) \
49 { \
50 if ((expr) != 0) \
51 { \
52 write(STDERR_FILENO, RED "[ERROR] ", LEN(RED "[ERROR] ")); \
53 show; \
54 write(STDERR_FILENO, RESET, LEN(RESET)); \
55 _exit(1); \
56 } \
57 else \
58 { \
59 write(STDERR_FILENO, GREEN "[SUCCESS] ", LEN(GREEN "[SUCCESS] ")); \
60 show; \
61 write(STDERR_FILENO, RESET, LEN(RESET)); \
62 } \
63 }
64
65#define checkpoint_output "<<CHECKPOINT>>\n"
66#define checkpoint_end_output "^^^^^^^^^^^^^^\n\n\n"
67#define checkpoint \
68 { \
69 write(STDERR_FILENO, BLUE checkpoint_output, LEN(BLUE checkpoint_output)); \
70 show; \
71 write(STDERR_FILENO, BLUE checkpoint_end_output, LEN(BLUE checkpoint_end_output)); \
72 }
73
74#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
4typedef struct hash_map hash_map;
5typedef struct hash hash;
6
7
8struct hash_map
9{
10
11
12};
13
14
15#endif /* HEADER_H */