-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.hpp
88 lines (65 loc) · 1.66 KB
/
memory.hpp
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
// TODO(harrison): Add temporary memory faculties
struct MemoryBlock {
uint8* base;
uint8* start;
MemoryIndex used;
MemoryIndex size;
MemoryBlock* next;
};
struct MemoryArena {
uint8* base;
MemoryIndex size;
MemoryBlock* first;
};
void memoryArena_init(MemoryArena* ma, MemoryIndex size, uint8* base) {
ma->size = size;
ma->base = base;
ma->first = 0;
logfln("creating memory arena of size %zu", size);
}
void memoryArena_clear(MemoryArena* ma) {
memset(ma->base, 0, ma->size);
ma->first = 0;
}
MemoryBlock* memoryArena_getLastBlock(MemoryArena* ma) {
MemoryBlock* last = 0;
MemoryBlock* i = ma->first;
while (i != 0) {
last = i;
i = i->next;
}
return last;
}
#define memoryArena_pushStruct(arena, Type) ( (Type *)memoryArena_pushSize(arena, sizeof(Type)) )
#define memoryArena_pushArray(arena, count, Type) ( (Type *) memoryArena_pushSize(arena, (count) * sizeof(Type)) )
void* memoryArena_pushSize(MemoryArena* ma, MemoryIndex size) {
uint8* end;
MemoryBlock* last = 0;
MemoryIndex totalSize = 0;
if (ma->first == 0) {
end = ma->base;
} else {
MemoryBlock* i = ma->first;
while (i != 0) {
last = i;
totalSize += i->size;
i = i->next;
}
ensure(last != 0);
end = last->base + last->size;
}
MemoryIndex blockSize = sizeof(MemoryBlock) + size;
ensure(totalSize + blockSize < ma->size);
MemoryBlock* block = (MemoryBlock*) end;
block->base = end;
block->start = end + sizeof(MemoryBlock);
block->size = blockSize;
block->next = 0;
block->used = 0;
if (ma->first == 0) {
ma->first = block;
} else {
last->next = block;
}
return (void*) block->start;
}