forked from WangYuanStudio/lockstep
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_arena.cpp
More file actions
49 lines (41 loc) · 1.09 KB
/
memory_arena.cpp
File metadata and controls
49 lines (41 loc) · 1.09 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
#include "lib/def.h"
#include "lib/assert.h"
#include "memory_arena.h"
void InitMemoryArena(memory_arena *A, void *Base, memsize Capacity) {
A->Base = Base;
A->Length = 0;
A->CheckpointCount = 0;
A->Capacity = Capacity;
}
void* MemoryArenaAllocate(memory_arena *A, memsize Size) {
Assert(Size != 0);
Assert(A->Capacity >= A->Length+Size);
void *Result = GetMemoryArenaHead(A);
A->Length += Size;
return Result;
}
memsize GetMemoryArenaFree(memory_arena *A) {
return A->Capacity - A->Length;
}
void* GetMemoryArenaHead(memory_arena *A) {
return (ui8*)A->Base + A->Length;
}
void TerminateMemoryArena(memory_arena *A) {
A->Base = NULL;
A->Length = 0;
A->Capacity = 0;
}
memory_arena_checkpoint CreateMemoryArenaCheckpoint(memory_arena *Arena) {
memory_arena_checkpoint CP;
CP.Arena = Arena;
CP.Length = Arena->Length;
Arena->CheckpointCount++;
return CP;
}
void ReleaseMemoryArenaCheckpoint(memory_arena_checkpoint CP) {
Assert(CP.Length <= CP.Arena->Length);
CP.Arena->CheckpointCount--;
if(CP.Arena->CheckpointCount == 0) {
CP.Arena->Length = CP.Length;
}
}