-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsi_memory.h
executable file
·199 lines (169 loc) · 6.03 KB
/
si_memory.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright (c) 2024 Jeremy Montgomery
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef SI_MEMORY_HEADER_GAURD
#define SI_MEMORY_HEADER_GAURD
#include <assert.h>
#include <memory.h>
#include <stddef.h>
#include <stdint.h>
#include <sys/mman.h>
typedef ptrdiff_t si_size;
typedef struct si_primary_buffer {
si_size size;
void* data;
} si_primary_buffer;
typedef struct si_memory_arena {
si_size size;
si_size used;
uint8_t* base;
} si_memory_arena;
typedef struct si_temp_memory {
si_memory_arena* arena;
size_t used;
} si_temp_memory;
#define si_kilobytes(value) ((value)*1024LL)
#define si_megabytes(value) (si_kilobytes(value) * 1024LL)
#define si_gigabytes(value) (si_megabytes(value) * 1024LL)
#define si_terabytes(value) (si_gigabytes(value) * 1024LL)
#define si_push(arena, type) (type*)si__push_size(arena, sizeof(type), 0)
#define si_push_array(arena, count, type) (type*)si__push_size(arena, (count) * sizeof(type), 0)
#define si_push_size(arena, size) si__push_size(arena, (size), 0)
#define si_push_clear(arena, type) (type*)si__push_size(arena, sizeof(type), 1)
#define si_push_array_clear(arena, count, type) (type*)si__push_size(arena, (count) * sizeof(type), 1)
#define si_push_size_clear(arena, size) si__push_size(arena, (size), 1)
#define si_push_aligned(arena, type, alignment) (type*)si__push_size_aligned(arena, sizeof(type), alignment)
#define si_push_array_aligned(arena, count, type, alignment) (type*)si__push_size_aligned(arena, (count) * sizeof(type), alignment)
#define si_push_size_aligned(arena, size, alignment) si__push_size_aligned(arena, (size), alignment)
static si_temp_memory si_start_temp_memory(si_memory_arena* arena);
static void si_pop_temp_memory(si_temp_memory temp);
static void si_pop_and_clear_temp_memory(si_temp_memory temp);
#define si_array_count(a) (sizeof(a) / sizeof(a[0]))
#ifdef SI_MEMORY_IMPLEMENTATION
#ifdef _WIN32
static si_primary_buffer
si_allocate_primary_buffer(size_t sizeInBytes, void* baseAddress)
{
si_primary_buffer result = {};
result.data = VirtualAlloc(baseAddress, sizeInBytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
assert(result.data);
result.size = sizeInBytes;
return result;
}
static void
si_free_primary_buffer(si_primary_buffer* buffer)
{
assert(buffer);
assert(buffer->data);
VirtualFree(buffer->data, 0, MEM_RELEASE);
memset(buffer, 0, sizeof(*buffer));
}
#else
static si_primary_buffer
si_allocate_primary_buffer(size_t sizeInBytes, void* baseAddress)
{
si_primary_buffer result = {};
result.data = mmap(baseAddress, sizeInBytes, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
assert(result.data);
result.size = sizeInBytes;
return result;
}
static void
si_free_primary_buffer(si_primary_buffer* buffer)
{
assert(buffer);
assert(buffer->data);
munmap(buffer->data, buffer->size);
memset(buffer, 0, sizeof(*buffer));
}
#endif //_WIN32
static void
si_initialize_arena(si_memory_arena* arena, si_size size, void* base)
{
arena->size = size;
arena->base = base;
arena->used = 0;
}
// TODO: remove stdlib
#include <string.h>
static void*
si__push_size(si_memory_arena* arena, si_size size, int clear)
{
assert((arena->used + size) <= arena->size);
void* result = arena->base + arena->used;
if (clear) {
memset(result, 0, size);
}
arena->used += size;
return result;
}
inline void*
si_align(void* ptr, int32_t alignment)
{
int32_t a = alignment - 1;
void* result = (void*)(((uintptr_t)(ptr) + a) & ~(uintptr_t)a);
assert(((uintptr_t)result & a) == 0);
return result;
}
// TODO: add versions of push that clears the memory to zero
static void*
si__push_size_aligned(si_memory_arena* arena, size_t size, int32_t alignment)
{
void* unaligned = arena->base + arena->used;
void* result = si_align(unaligned, alignment);
ptrdiff_t diff = (uint8_t*)result - (uint8_t*)unaligned;
assert(diff >= 0);
size += diff;
arena->used += size;
assert(arena->used <= arena->size);
return result;
}
static void
si_clear_arena(si_memory_arena* arena, int clearToZero)
{
if (clearToZero) {
memset(arena->base, 0, arena->size);
}
arena->used = 0;
}
static si_temp_memory
si_start_temp_memory(si_memory_arena* arena)
{
si_temp_memory result = {};
assert(arena);
result.arena = arena;
result.used = arena->used;
return result;
}
static void
si_pop_temp_memory(si_temp_memory temp)
{
assert(temp.arena);
temp.arena->used = temp.used;
}
static void
si_pop_and_clear_temp_memory(si_temp_memory temp)
{
assert(temp.arena);
ptrdiff_t bytesToClear = temp.arena->used - temp.used;
assert(bytesToClear >= 0);
memset((temp.arena->base + temp.used), 0, bytesToClear);
temp.arena->used = temp.used;
}
#endif // SI_MEMORY_IMPLEMENTATION
#endif // SI_MEMORY_HEADER_GAURD