summaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
Diffstat (limited to 'source')
-rwxr-xr-xsource/base/base.h26
-rw-r--r--[-rwxr-xr-x]source/base/base_arena.c139
-rwxr-xr-xsource/base/base_arena.h143
-rwxr-xr-xsource/base/base_include.h28
-rw-r--r--source/base/base_math.h10
-rw-r--r--source/base/base_mem.h26
-rw-r--r--source/base/base_os.h31
-rw-r--r--source/base/base_platform.h6
-rw-r--r--source/base/base_rand.h3
-rwxr-xr-xsource/base/base_stack.h6
-rw-r--r--source/base/base_string.c30
-rw-r--r--source/base/base_string.h36
-rw-r--r--source/base/bash_hash.h24
13 files changed, 238 insertions, 270 deletions
diff --git a/source/base/base.h b/source/base/base.h
index 3fe9dca..f908962 100755
--- a/source/base/base.h
+++ b/source/base/base.h
@@ -6,6 +6,11 @@
6#define global_variable static 6#define global_variable static
7#define local_persist static 7#define local_persist static
8 8
9#define ARENA_ALIGN (2 * sizeof(void *))
10
11#define MIN(a, b) (((a) < (b)) ? (a) : (b))
12#define MAX(a, b) (((a) > (b)) ? (a) : (b))
13
9#define unused(x) (void)(x) 14#define unused(x) (void)(x)
10#define NIL 0 15#define NIL 0
11 16
@@ -62,3 +67,24 @@ typedef intptr_t smm;
62#define Len(s) (sizeof(s) - 1) 67#define Len(s) (sizeof(s) - 1)
63 68
64#endif 69#endif
70
71#ifdef BASE_IMPLEMENTATION
72
73internal inline b8
74is_pow(umm x)
75{
76 return (x & (x - 1)) == 0;
77}
78
79internal inline u64
80align(u64 pointer, umm alignment)
81{
82 if ((alignment & (alignment - 1)) == 0)
83 {
84 return pointer;
85 }
86
87 return (pointer + alignment - 1) & ~(alignment - 1);
88}
89
90#endif
diff --git a/source/base/base_arena.c b/source/base/base_arena.c
index 8fc8c13..e69de29 100755..100644
--- a/source/base/base_arena.c
+++ b/source/base/base_arena.c
@@ -1,139 +0,0 @@
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 // warn(0);
114 }
115
116 return NULL;
117}
118
119internal mem_arena *
120arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size)
121{
122 return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN);
123}
124
125internal temp_arena
126temp_arena_begin(mem_arena *arena)
127{
128 temp_arena t;
129 t.arena = arena;
130 t.start_position = arena->current_position;
131
132 return t;
133}
134
135internal void
136temp_arena_end(temp_arena temp)
137{
138 temp.arena->current_position = temp.start_position;
139}
diff --git a/source/base/base_arena.h b/source/base/base_arena.h
index dc6dd73..d24eeea 100755
--- a/source/base/base_arena.h
+++ b/source/base/base_arena.h
@@ -29,3 +29,146 @@ struct temp_arena
29}; 29};
30 30
31#endif /* BASE_ARENA_H */ 31#endif /* BASE_ARENA_H */
32
33#ifdef BASE_IMPLEMENTATION
34internal mem_arena *
35arena_create(u64 capacity)
36{
37 mem_arena *arena = (mem_arena *)mmap(
38 /* kernel decides where to throw the arena */
39 NULL,
40 capacity + sizeof(mem_arena),
41 PROT_READ | PROT_WRITE,
42 MAP_SHARED | MAP_ANONYMOUS,
43 -1,
44 0);
45
46 if (arena == MAP_FAILED)
47 {
48 return NULL;
49 }
50
51 arena->capacity = capacity;
52 arena->base_position = (u8 *)arena + sizeof(mem_arena);
53 arena->current_position = 0;
54 arena->previous_position = 0;
55
56 return arena;
57}
58
59internal void
60arena_destroy(mem_arena *arena)
61{
62 if (!arena)
63 {
64 return;
65 }
66 munmap(arena, arena->capacity + sizeof(mem_arena));
67}
68internal void *
69arena_alloc(mem_arena *arena, u64 size, b32 zero)
70{
71 if (!arena)
72 {
73 return NULL;
74 }
75 u64 aligned = Align(arena->current_position, ARENA_ALIGN);
76 u64 new_pos = aligned + size;
77 if (new_pos > arena->capacity)
78 {
79 return NULL;
80 }
81
82 void *out = arena->base_position + aligned;
83
84 arena->previous_position = arena->current_position;
85 arena->current_position = aligned + size;
86
87 if (zero) MemSet(out, size);
88
89 return out;
90}
91
92internal void
93arena_pop(mem_arena *arena, u64 size)
94{
95 size = MIN(size, arena->current_position);
96 arena->current_position -= size;
97}
98
99internal void
100arena_pop_to(mem_arena *arena, u64 pos)
101{
102 u64 size = pos < arena->current_position ? arena->current_position - pos : 0;
103 arena_pop(arena, size);
104}
105
106internal void
107arena_clear(mem_arena *arena)
108{
109 arena->current_position = 0;
110}
111
112internal mem_arena *
113arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size, umm alignment)
114{
115 u8 *old_mem = (u8 *)old_memory;
116
117 if (!is_pow(alignment))
118 {
119 Align(arena->current_position, alignment);
120 }
121
122 if (old_memory == NULL || old_size == 0)
123 {
124 return (mem_arena *)arena_alloc(arena, new_size, 0);
125 }
126 else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity))
127 {
128 if ((arena->base_position + arena->previous_position) == old_memory)
129 {
130 arena->current_position = arena->previous_position + new_size;
131 if (new_size > old_size)
132 {
133 MemSet(&arena->current_position, new_size - old_size);
134 }
135 return (mem_arena *)old_memory;
136 }
137 else
138 {
139 void *new_memory = arena_alloc(arena, new_size, 0);
140 umm copy_size = old_size < new_size ? old_size : new_size;
141 memmove(new_memory, old_mem, copy_size);
142 }
143 }
144 else
145 {
146 // warn(0);
147 }
148
149 return NULL;
150}
151
152internal mem_arena *
153arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size)
154{
155 return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN);
156}
157
158internal temp_arena
159temp_arena_begin(mem_arena *arena)
160{
161 temp_arena t;
162 t.arena = arena;
163 t.start_position = arena->current_position;
164
165 return t;
166}
167
168internal void
169temp_arena_end(temp_arena temp)
170{
171 temp.arena->current_position = temp.start_position;
172}
173
174#endif
diff --git a/source/base/base_include.h b/source/base/base_include.h
index a376f29..8eabb3e 100755
--- a/source/base/base_include.h
+++ b/source/base/base_include.h
@@ -2,37 +2,31 @@
2#define BASE_INCLUDE_H 2#define BASE_INCLUDE_H
3 3
4#include <dirent.h> 4#include <dirent.h>
5#include <unistd.h>
5#include <sys/mman.h> 6#include <sys/mman.h>
6#include <sys/stat.h> 7#include <sys/stat.h>
7#include <sys/syscall.h> 8#include <sys/syscall.h>
8#include <fcntl.h> 9#include <fcntl.h>
10//- needed for random
11#include <time.h>
12#include <math.h>
13#include <string.h>
9#include <stdint.h> 14#include <stdint.h>
10#include <stddef.h> 15#include <stddef.h>
11#include <string.h>
12#include <math.h>
13#include <unistd.h>
14#include <time.h>
15#include <stdarg.h> 16#include <stdarg.h>
16 17
17#include "base.h" 18#include <arm_neon.h>
18#include "base_mem.h" 19
20#include <stdio.h>
19 21
22#include "base.h"
20#include "base_arena.h" 23#include "base_arena.h"
21#include "base_stack.h" 24#include "base_stack.h"
22
23#ifdef BASE_IMPLEMENTATION
24#include "base_arena.c"
25#endif
26
27
28#include "base_string.h" 25#include "base_string.h"
29#include "base_string.c" 26#include "base_math.h"
30
31#include "base_os.h" 27#include "base_os.h"
32#include "base_error.h" 28#include "base_error.h"
33
34#include "base_rand.h" 29#include "base_rand.h"
35 30#include "base_simd.h"
36
37 31
38#endif 32#endif
diff --git a/source/base/base_math.h b/source/base/base_math.h
new file mode 100644
index 0000000..71ac97f
--- /dev/null
+++ b/source/base/base_math.h
@@ -0,0 +1,10 @@
1#ifndef BASE_MATH_H
2#define BASE_MATH_H
3
4
5#endif
6
7
8#ifdef BASE_MATH_IMPLEMENTATION
9
10#endif
diff --git a/source/base/base_mem.h b/source/base/base_mem.h
deleted file mode 100644
index 2778fce..0000000
--- a/source/base/base_mem.h
+++ /dev/null
@@ -1,26 +0,0 @@
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
index 82aa70d..2e192c5 100644
--- a/source/base/base_os.h
+++ b/source/base/base_os.h
@@ -15,7 +15,6 @@ load_file(mem_arena *arena, const char *path)
15 string8 result = {0}; 15 string8 result = {0};
16 struct stat sbuf = {0}; 16 struct stat sbuf = {0};
17 17
18 // TODO(nasr): abstract this to a platform layer
19 s32 file = open(path, O_RDONLY); 18 s32 file = open(path, O_RDONLY);
20 if(file == -1) 19 if(file == -1)
21 { 20 {
@@ -28,9 +27,7 @@ load_file(mem_arena *arena, const char *path)
28 return (string8){0}; 27 return (string8){0};
29 } 28 }
30 29
31
32 result = PushString8(arena, sbuf.st_size); 30 result = PushString8(arena, sbuf.st_size);
33
34 result.size = (u64)sbuf.st_size; 31 result.size = (u64)sbuf.st_size;
35 if(result.size != 0) 32 if(result.size != 0)
36 { 33 {
@@ -68,8 +65,9 @@ write_file(const char *path, string8 data)
68 return result; 65 return result;
69} 66}
70 67
71#define os_write(buf, count) _os_write(STDIN_FD, buf, count) 68#if 0
72#define os_read(buf, count) _os_read(STDIN_FD, buf, count) 69#define os_write(buf, count) _os_write(STDOUT_FD, buf, count)
70#define os_read(buf, count) _os_read(STDIN_FD, buf, count)
73 71
74internal s64 72internal s64
75_os_write(s32 fd, void const *buf, u64 count) 73_os_write(s32 fd, void const *buf, u64 count)
@@ -83,6 +81,7 @@ _os_read(s32 fd, void *buf, u64 count)
83 return syscall(SYS_read, fd, buf, count); 81 return syscall(SYS_read, fd, buf, count);
84} 82}
85 83
84
86internal void 85internal void
87log_s8(string8 s) 86log_s8(string8 s)
88{ 87{
@@ -100,10 +99,10 @@ _log(const char *str)
100#else 99#else
101 unused(str); 100 unused(str);
102#endif 101#endif
103
104} 102}
105#endif 103#endif
106 104
105
107internal void 106internal void
108write_string(const char *str) 107write_string(const char *str)
109{ 108{
@@ -112,10 +111,11 @@ write_string(const char *str)
112 os_write(str, len); 111 os_write(str, len);
113} 112}
114 113
114#endif
115
115internal void 116internal void
116write_int(s32 num) 117write_int(s32 num)
117{ 118{
118
119 if (num < 0) 119 if (num < 0)
120 { 120 {
121 write(STDERR_FILENO, "-", 1); 121 write(STDERR_FILENO, "-", 1);
@@ -124,22 +124,7 @@ write_int(s32 num)
124 if (num >= 10) write_int(num / 10); 124 if (num >= 10) write_int(num / 10);
125 125
126 char digit = '0' + (num % 10); 126 char digit = '0' + (num % 10);
127
128 write(STDERR_FILENO, &digit, 1); 127 write(STDERR_FILENO, &digit, 1);
129} 128}
130 129
131internal inline void 130#endif /* BASE_IMPLEMENTATION */
132sleep_ms(long ms)
133{
134 struct timespec ts;
135 ts.tv_sec = ms / 1000;
136 ts.tv_nsec = (ms % 1000) * 1000000L;
137
138 while (nanosleep(&ts, &ts))
139 {
140 NULL;
141 }
142}
143
144
145#endif
diff --git a/source/base/base_platform.h b/source/base/base_platform.h
deleted file mode 100644
index b34a7d0..0000000
--- a/source/base/base_platform.h
+++ /dev/null
@@ -1,6 +0,0 @@
1#ifndef BASE_PLATFORM_H
2#define BASE_PLATFORM_H
3
4#ifdef BASE_PLATFORM_IMPLEMENTATION
5#endif /* BASE_PLATFORM_IMPLEMENTATION */
6#endif /* BASE_PLATFORM_H */
diff --git a/source/base/base_rand.h b/source/base/base_rand.h
index bfdab0f..5a8668c 100644
--- a/source/base/base_rand.h
+++ b/source/base/base_rand.h
@@ -8,7 +8,8 @@
8 8
9#endif /* BASE_RAND_H */ 9#endif /* BASE_RAND_H */
10 10
11#ifdef BASE_RAND_IMPLEMENTATION 11#ifdef BASE_IMPLEMENTATION
12
12internal u64 13internal u64
13generate_random_u64(u64 constant) 14generate_random_u64(u64 constant)
14{ 15{
diff --git a/source/base/base_stack.h b/source/base/base_stack.h
index 43a7230..99217f5 100755
--- a/source/base/base_stack.h
+++ b/source/base/base_stack.h
@@ -1,5 +1,5 @@
1#ifndef STACK_H 1#ifndef BASE_STACK_H
2#define STACK_H 2#define BASE_STACK_H
3 3
4typedef struct mem_stack_header mem_stack_header; 4typedef struct mem_stack_header mem_stack_header;
5struct mem_stack_header 5struct mem_stack_header
@@ -207,4 +207,4 @@ stack_destroy(mem_stack *stack)
207 munmap(stack, stack->capacity + sizeof(mem_stack)); 207 munmap(stack, stack->capacity + sizeof(mem_stack));
208} 208}
209 209
210#endif 210#endif /* BASE_STACK_H */
diff --git a/source/base/base_string.c b/source/base/base_string.c
index 986fde5..e69de29 100644
--- a/source/base/base_string.c
+++ b/source/base/base_string.c
@@ -1,30 +0,0 @@
1internal b32
2is_alpha(u8 point)
3{
4 return ((point >= 'a' && point <= 'z') || (point >= 'A' && point <= 'Z') || (point == '_'));
5}
6
7internal b32
8is_digit(u8 point)
9{
10 return (point >= '0' && point <= '9');
11}
12
13internal b32
14is_alpha_num(u8 point)
15{
16 return (is_alpha(point) || is_digit(point));
17}
18
19internal b32 is_whitespace(u8 point)
20{
21 return (point == '\n' || point == '\r' || point == ' ' || point == '\t');
22}
23
24internal b32
25is_slash(u8 point)
26{
27 return (point == '/' || point == '\\');
28}
29
30
diff --git a/source/base/base_string.h b/source/base/base_string.h
index eb51e65..1f754f3 100644
--- a/source/base/base_string.h
+++ b/source/base/base_string.h
@@ -38,7 +38,7 @@ struct string32
38 38
39//- string linked list implementation 39//- string linked list implementation
40typedef struct string8_node string8_node; 40typedef struct string8_node string8_node;
41struct string8_node 41struct string8_node
42{ 42{
43 string8 *next; 43 string8 *next;
44 string8 string; 44 string8 string;
@@ -82,3 +82,37 @@ string8_appendc(string8 *buf, u8 c)
82} 82}
83 83
84#endif /* BASE_STRING_H */ 84#endif /* BASE_STRING_H */
85
86#ifdef BASE_IMPLEMENTATION
87
88internal b32
89is_alpha(u8 point)
90{
91 return ((point >= 'a' && point <= 'z') || (point >= 'A' && point <= 'Z') || (point == '_'));
92}
93
94internal b32
95is_digit(u8 point)
96{
97 return (point >= '0' && point <= '9');
98}
99
100internal b32
101is_alpha_num(u8 point)
102{
103 return (is_alpha(point) || is_digit(point));
104}
105
106internal b32 is_whitespace(u8 point)
107{
108 return (point == '\n' || point == '\r' || point == ' ' || point == '\t');
109}
110
111internal b32
112is_slash(u8 point)
113{
114 return (point == '/' || point == '\\');
115}
116
117
118#endif
diff --git a/source/base/bash_hash.h b/source/base/bash_hash.h
deleted file mode 100644
index 758ef72..0000000
--- a/source/base/bash_hash.h
+++ /dev/null
@@ -1,24 +0,0 @@
1#ifndef BASH_HASH_H
2#define BASH_HASH_H
3
4typedef struct hash_map hash_map;
5typedef struct hash hash;
6
7struct map
8{
9 string8
10 u64 capacity
11};
12
13
14struct index
15{
16 string8 key;
17 string8 value;
18};
19
20
21
22internal void
23
24#endif /* BASH_HASH_H */