-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase_arena.h
More file actions
475 lines (428 loc) · 13 KB
/
Copy pathbase_arena.h
File metadata and controls
475 lines (428 loc) · 13 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#ifndef LJRE_BASE_ARENA_H
#define LJRE_BASE_ARENA_H
#include "base.h"
#include "base_intrinsics.h"
#include "base_string.h"
#ifndef CONFIG_ARENA_DEFAULT_ALIGNMENT
# define CONFIG_ARENA_DEFAULT_ALIGNMENT 16
#endif
static_assert(CONFIG_ARENA_DEFAULT_ALIGNMENT != 0 && IsPowerOf2(CONFIG_ARENA_DEFAULT_ALIGNMENT), "Default Arena alignment should always be non-zero and a power of two");
#define ArenaPushStruct(arena, Type) \
((Type*)ArenaPushAligned(arena, SignedSizeof(Type), alignof(Type)))
#define ArenaPushStructData(arena, Type, ...) \
((Type*)MemoryCopy(ArenaPushDirtyAligned(arena, SignedSizeof(Type), alignof(Type)), __VA_ARGS__, SignedSizeof(Type)))
#define ArenaPushArray(arena, Type, count) \
((Type*)ArenaPushAligned(arena, SignedSizeof(Type)*(count), alignof(Type)))
#define ArenaPushArrayData(arena, Type, data, count) \
((Type*)MemoryCopy(ArenaPushDirtyAligned(arena, SignedSizeof(Type)*(count), alignof(Type)), data, SignedSizeof(Type)*(count)))
#define ArenaPushData(arena, data) \
MemoryCopy(ArenaPushDirtyAligned(arena, SignedSizeof(*(data)), 1), data, SignedSizeof(*(data)))
#define ArenaPushDataArray(arena, data, count) \
MemoryCopy(ArenaPushDirtyAligned(arena, SignedSizeof(*(data))*(count), 1), data, SignedSizeof(*(data))*(count))
#define ArenaTempScope(arena_) \
(ArenaSavepoint _temp__ = { arena_, (arena_)->offset }; _temp__.arena; _temp__.arena->offset = _temp__.offset, _temp__.arena = NULL)
#ifndef __cplusplus
# define ArenaPushStructInit(arena, Type, ...) \
((void*)ArenaPushMemoryAligned(arena, &(Type) __VA_ARGS__, SignedSizeof(Type), alignof(Type)))
#else //__cplusplus
# define ArenaPushStructInit(arena, Type, ...) \
((Type*)ArenaPushMemoryAligned(arena, &(Type const&) Type __VA_ARGS__, SignedSizeof(Type), alignof(Type)))
#endif //__cplusplus
#define ArenaBeginArray(arena, Type) \
((Type*)ArenaEndAligned(arena, alignof(Type)))
#define ArenaEndArray(arena, Type, begin) \
((Type*)ArenaEnd(arena) - begin)
static inline Arena ArenaFromMemory (void* memory, intz size);
static inline Arena* ArenaBootstrap (Arena arena);
static inline void* ArenaPush (Arena* arena, intz size);
static inline void* ArenaPushDirty (Arena* arena, intz size);
static inline void* ArenaPushAligned (Arena* arena, intz size, intz alignment);
static inline void* ArenaPushDirtyAligned (Arena* arena, intz size, intz alignment);
static inline void* ArenaPushMemory (Arena* arena, void const* buf, intz size);
static inline void* ArenaPushMemoryAligned(Arena* arena, void const* buf, intz size, intz alignment);
static inline String ArenaPushString (Arena* arena, String str);
static inline String ArenaPushStringAligned(Arena* arena, String str, intz alignment);
static inline char* ArenaPushCString (Arena* arena, String str);
static inline String ArenaVPrintf (Arena* arena, const char* fmt, va_list args);
static inline String ArenaPrintf (Arena* arena, const char* fmt, ...);
static inline void ArenaPop (Arena* arena, void* ptr);
static inline void* ArenaEndAligned (Arena* arena, intz alignment);
static inline void ArenaClear (Arena* arena);
static inline void* ArenaEnd (Arena* arena);
static inline ArenaSavepoint ArenaSave (Arena* arena);
static inline void ArenaRestore (ArenaSavepoint savepoint);
static inline Allocator AllocatorFromArena(Arena* arena);
static inline Arena
ArenaFromMemory(void* memory, intz size)
{
Assert(((uintptr)memory & ~(uintptr)(CONFIG_ARENA_DEFAULT_ALIGNMENT-1)) == (uintptr)memory);
Arena result = {
.size = size,
.offset = 0,
.memory = (uint8*)memory,
};
return result;
}
static inline Arena*
ArenaBootstrap(Arena arena)
{
Arena* ptr = (Arena*)ArenaPushAligned(&arena, sizeof(Arena), alignof(Arena));
*ptr = arena;
return ptr;
}
static inline void*
ArenaEndAligned(Arena* arena, intz alignment)
{
Assert(alignment != 0 && IsPowerOf2(alignment));
intptr target_offset = AlignUp((intptr)arena->memory + arena->offset, alignment-1) - (intptr)arena->memory;
arena->offset = target_offset;
return arena->memory + arena->offset;
}
static inline void*
ArenaPushDirtyAligned(Arena* arena, intz size, intz alignment)
{
Trace();
Assert(alignment != 0 && IsPowerOf2(alignment));
SafeAssert((intz)size >= 0);
intptr target_offset = AlignUp((intptr)arena->memory + arena->offset, alignment-1) - (intptr)arena->memory;
intz needed = target_offset + size;
if (Unlikely(needed > arena->size))
{
if (!arena->commit_memory_proc || !arena->commit_memory_proc(arena, needed))
return NULL;
Assert(needed <= arena->size);
}
void* result = arena->memory + target_offset;
arena->offset = needed;
return result;
}
static inline void
ArenaPop(Arena* arena, void* ptr)
{
uint8* p = (uint8*)ptr;
SafeAssert(p >= arena->memory && p <= arena->memory + arena->offset);
intz new_offset = p - arena->memory;
arena->offset = new_offset;
}
static inline String
ArenaVPrintf(Arena* arena, char const* fmt, va_list args)
{
Trace();
va_list args2;
va_copy(args2, args);
intz size = StringVPrintfSize(fmt, args2);
uint8* data = (uint8*)ArenaPushDirtyAligned(arena, size, 1);
String result = { 0 };
if (data)
{
size = StringVPrintfBuffer((char*)data, size, fmt, args);
result = StrMake(size, data);
}
va_end(args2);
return result;
}
static inline String
ArenaPrintf(Arena* arena, char const* fmt, ...)
{
Trace();
va_list args;
va_start(args, fmt);
String result = ArenaVPrintf(arena, fmt, args);
va_end(args);
return result;
}
static inline ArenaSavepoint
ArenaSave(Arena* arena)
{
ArenaSavepoint ret = {
arena,
arena->offset,
};
return ret;
}
static inline char*
ArenaPushCString(Arena* arena, String str)
{
Trace();
char* buffer = (char*)ArenaPushDirtyAligned(arena, str.size + 1, 1);
if (buffer)
{
MemoryCopy(buffer, str.data, str.size);
buffer[str.size] = 0;
}
return buffer;
}
static inline void
ArenaRestore(ArenaSavepoint savepoint)
{ savepoint.arena->offset = savepoint.offset; }
static inline void*
ArenaPushDirty(Arena* arena, intz size)
{ return ArenaPushDirtyAligned(arena, size, CONFIG_ARENA_DEFAULT_ALIGNMENT); }
static inline void*
ArenaPushAligned(Arena* arena, intz size, intz alignment)
{
Trace();
void* data = ArenaPushDirtyAligned(arena, size, alignment);
if (data)
MemoryZero(data, size);
return data;
}
static inline void*
ArenaPush(Arena* arena, intz size)
{
Trace();
void* data = ArenaPushDirtyAligned(arena, size, CONFIG_ARENA_DEFAULT_ALIGNMENT);
if (data)
MemoryZero(data, size);
return data;
}
static inline void*
ArenaPushMemory(Arena* arena, const void* buf, intz size)
{
Trace();
void* data = ArenaPushDirtyAligned(arena, size, 1);
if (data)
MemoryCopy(data, buf, size);
return data;
}
static inline void*
ArenaPushMemoryAligned(Arena* arena, const void* buf, intz size, intz alignment)
{
Trace();
void* data = ArenaPushDirtyAligned(arena, size, alignment);
if (data)
MemoryCopy(data, buf, size);
return data;
}
static inline String
ArenaPushString(Arena* arena, String str)
{
Trace();
void* data = ArenaPushDirtyAligned(arena, str.size, 1);
String result = { 0 };
if (data)
result = StrMake(str.size, MemoryCopy(data, str.data, str.size));
return result;
}
static inline String
ArenaPushStringAligned(Arena* arena, String str, intz alignment)
{
Trace();
void* data = ArenaPushDirtyAligned(arena, str.size, alignment);
String result = { 0 };
if (data)
result = StrMake(str.size, MemoryCopy(data, str.data, str.size));
return result;
}
static inline void
ArenaClear(Arena* arena)
{
// NOTE(ljre): If this arena is bootstrapped by itself, then don't free itself
if ((uint8*)arena == arena->memory)
arena->offset = SignedSizeof(Arena);
else
arena->offset = 0;
}
static inline void*
ArenaEnd(Arena* arena)
{ return arena->memory + arena->offset; }
static void*
ArenaAllocatorProc(void* instance, AllocatorMode mode, intz size, intz alignment, void* old_ptr, intz old_size, AllocatorError* out_err)
{
Trace();
void* result = NULL;
AllocatorError error = AllocatorError_Ok;
Arena* arena = (Arena*)instance;
bool is_invalid_alignment = (!alignment || (alignment & alignment-1) != 0);
switch (mode)
{
case AllocatorMode_Alloc:
{
if (size == 0)
result = alignment ? ArenaEndAligned(arena, alignment) : ArenaEnd(arena);
else
{
if (is_invalid_alignment)
{
error = AllocatorError_InvalidArgument;
break;
}
result = ArenaPushAligned(arena, size, alignment);
}
if (!result)
error = AllocatorError_OutOfMemory;
} break;
case AllocatorMode_Free:
{
if (!old_ptr)
break; // free(NULL) has to be noop?
SafeAssert((uint8*)old_ptr >= arena->memory && (uint8*)old_ptr <= arena->memory + arena->offset);
intz old_offset = (uint8*)old_ptr - arena->memory;
if (old_offset + old_size == arena->offset)
ArenaPop(arena, old_ptr);
} break;
case AllocatorMode_Resize:
{
if (is_invalid_alignment)
{
error = AllocatorError_InvalidArgument;
break;
}
if (!old_ptr)
{
result = ArenaPushAligned(arena, size, alignment);
if (!result)
error = AllocatorError_OutOfMemory;
break;
}
SafeAssert((uint8*)old_ptr >= arena->memory && (uint8*)old_ptr <= arena->memory + arena->offset);
intz old_offset = (uint8*)old_ptr - arena->memory;
if (old_offset + old_size == arena->offset)
{
if (old_size < size)
ArenaPushAligned(arena, size - old_size, 1);
else if (old_size > size)
ArenaPop(arena, arena->memory + old_offset + size);
result = old_ptr;
}
else
{
// welp, this is really sad
result = ArenaPushDirtyAligned(arena, size, alignment);
if (!result)
{
error = AllocatorError_OutOfMemory;
break;
}
intz amount_to_copy = Min(size, old_size);
MemoryCopy(result, old_ptr, amount_to_copy);
if (size > old_size)
MemoryZero((uint8*)result + old_size, size - old_size);
}
} break;
case AllocatorMode_FreeAll:
{
ArenaClear(arena);
} break;
case AllocatorMode_AllocNonZeroed:
{
if (size == 0)
result = alignment ? ArenaEndAligned(arena, alignment) : ArenaEnd(arena);
else
{
if (is_invalid_alignment)
{
error = AllocatorError_InvalidArgument;
break;
}
result = ArenaPushDirtyAligned(arena, size, alignment);
}
if (!result)
error = AllocatorError_OutOfMemory;
} break;
case AllocatorMode_ResizeNonZeroed:
{
if (is_invalid_alignment)
{
error = AllocatorError_InvalidArgument;
break;
}
if (!old_ptr)
{
result = ArenaPushDirtyAligned(arena, size, alignment);
if (!result)
error = AllocatorError_OutOfMemory;
break;
}
SafeAssert((uint8*)old_ptr >= arena->memory && (uint8*)old_ptr <= arena->memory + arena->offset);
intz old_offset = (uint8*)old_ptr - arena->memory;
if (old_offset + old_size == arena->offset)
{
if (old_size < size)
ArenaPushDirtyAligned(arena, size - old_size, 1);
else if (old_size > size)
ArenaPop(arena, arena->memory + old_offset + size);
result = old_ptr;
}
else
{
// also sad
result = ArenaPushDirtyAligned(arena, size, alignment);
if (!result)
{
error = AllocatorError_OutOfMemory;
break;
}
intz amount_to_copy = Min(size, old_size);
MemoryCopy(result, old_ptr, amount_to_copy);
}
} break;
case AllocatorMode_QueryFeatures:
{
SafeAssert(old_ptr && ((uintptr)old_ptr & sizeof(uint32)-1) == 0);
uint32* features_ptr = (uint32*)old_ptr;
*features_ptr =
AllocatorMode_Alloc |
AllocatorMode_AllocNonZeroed |
AllocatorMode_Resize |
AllocatorMode_ResizeNonZeroed |
AllocatorMode_Free |
AllocatorMode_FreeAll |
AllocatorMode_Pop |
AllocatorMode_QueryFeatures;
} break;
case AllocatorMode_Pop:
{
SafeAssert((uint8*)old_ptr >= arena->memory && (uint8*)old_ptr <= arena->memory + arena->offset);
ArenaPop(arena, old_ptr);
} break;
default:
{
error = AllocatorError_ModeNotImplemented;
} break;
}
if (out_err)
*out_err = error;
else
SafeAssert(error == AllocatorError_Ok);
return result;
}
static inline Allocator
AllocatorFromArena(Arena* arena)
{
return (Allocator) {
.proc = ArenaAllocatorProc,
.instance = arena,
};
}
// #ifdef __cplusplus
// struct ScratchScope
// {
// Arena* arena;
// Allocator allocator;
// void* end;
// inline explicit ScratchScope(Arena* arena)
// : arena(arena), allocator(), end(ArenaEnd(arena))
// {}
// inline explicit ScratchScope(Allocator allocator)
// : arena(), allocator(allocator), end()
// {
// AllocatorError err;
// end = AllocatorAllocNonZeroed(&allocator, 0, 0, &err);
// if (err)
// end = NULL;
// }
// inline ScratchScope(ScratchScope const& other) = delete;
// inline ScratchScope(ScratchScope&& other) = delete;
// inline ScratchScope& operator=(ScratchScope const& other) = delete;
// inline ScratchScope& operator=(ScratchScope&& other) = delete;
// inline ~ScratchScope()
// {
// if (arena)
// ArenaPop(arena, end);
// if (allocator.proc)
// AllocatorPop(&allocator, end, NULL);
// }
// };
// #endif
#endif //LJRE_BASE_ARENA_H