-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharena.cpp
More file actions
194 lines (163 loc) · 5.15 KB
/
Copy patharena.cpp
File metadata and controls
194 lines (163 loc) · 5.15 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
#include "arena.h"
#if ARENA_BACKEND == ARENA_BACKEND_LIBC_MALLOC
#include <stdlib.h>
// TODO: instead of accepting specific capacity new_region() should accept the size of the object we want to fit into the region
// It should be up to new_region() to decide the actual capacity to allocate
Region* new_region(size_t capacity) {
size_t size_bytes = sizeof(Region) + sizeof(uintptr_t) * capacity;
// TODO: it would be nice if we could guarantee that the regions are allocated by ARENA_BACKEND_LIBC_MALLOC are page aligned
Region* r = (Region*)malloc(size_bytes);
ARENA_ASSERT(r);
r->next = nullptr;
r->count = 0;
r->capacity = capacity;
memset(r->data, 0, capacity);
return r;
}
void free_region(Region* r) {
free(r);
}
#elif ARENA_BACKEND == ARENA_BACKEND_LINUX_MMAP
#include <unistd.h>
#include <sys/mman.h>
Region* new_region(size_t capacity) {
size_t size_bytes = sizeof(Region) + sizeof(uintptr_t) * capacity;
Region* r = (Region*)mmap(nullptr, size_bytes, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
ARENA_ASSERT(r != MAP_FAILED);
r->next = nullptr;
r->count = 0;
r->capacity = capacity;
memset(r->data, 0, capacity);
return r;
}
void free_region(Region* r) {
size_t size_bytes = sizeof(Region) + sizeof(uintptr_t) * r->capacity;
int ret = munmap(r, size_bytes);
(void)ret;
ARENA_ASSERT(ret == 0);
}
#elif ARENA_BACKEND == ARENA_BACKEND_WIN32_VIRTUALALLOC
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#define INV_HANDLE(x) (((x) == nullptr) || ((x) == INVALID_HANDLE_VALUE))
Region* new_region(size_t capacity) {
SIZE_T size_bytes = sizeof(Region) + sizeof(uintptr_t) * capacity;
Region* r = (Region*)VirtualAllocEx(GetCurrentProcess(), /* Allocate in current process address space */
nullptr, /* Unknown position */
size_bytes, /* Bytes to allocate */
MEM_COMMIT | MEM_RESERVE, /* Reserve and commit allocated page */
PAGE_READWRITE /* Permissions ( Read/Write )*/
);
if (INV_HANDLE(r)) {
ARENA_ASSERT(0 && "VirtualAllocEx() failed.");
}
r->next = nullptr;
r->count = 0;
r->capacity = capacity;
memset(r->data, 0, capacity);
return r;
}
void free_region(Region* r) {
if (INV_HANDLE(r))
return;
BOOL free_result = VirtualFreeEx(GetCurrentProcess(), /* Deallocate from current process address space */
(LPVOID)r, /* Address to deallocate */
0, /* Bytes to deallocate ( Unknown, deallocate entire page ) */
MEM_RELEASE /* Release the page ( And implicitly decommit it ) */
);
if (FALSE == free_result)
ARENA_ASSERT(0 && "VirtualFreeEx() failed.");
}
#elif ARENA_BACKEND == ARENA_BACKEND_WASM_HEAPBASE
#error "TODO: WASM __heap_base backend is not implemented yet"
#else
#error "Unknown Arena backend"
#endif
// TODO: add debug statistic collection mode for arena
// Should collect things like:
// - How many times new_region was called
// - How many times existing region was skipped
// - How many times allocation exceeded REGION_DEFAULT_CAPACITY
void* arena_alloc(Arena* a, size_t size_bytes) {
size_t size = (size_bytes + sizeof(uintptr_t) - 1) / sizeof(uintptr_t);
ARENA_ASSERT(a != nullptr);
ARENA_ASSERT(a->begin != nullptr);
ARENA_ASSERT(a->end != nullptr);
while (a->end->count + size > a->end->capacity && a->end->next != nullptr) {
a->end = a->end->next;
}
if (a->end->count + size > a->end->capacity) {
ARENA_ASSERT(a->end->next == nullptr);
size_t capacity = REGION_DEFAULT_CAPACITY;
if (capacity < size)
capacity = size;
a->end->next = new_region(capacity);
a->end = a->end->next;
}
void* result = &a->end->data[a->end->count];
a->end->count += size;
return result;
}
void* arena_realloc(Arena* a, void* oldptr, size_t oldsz, size_t newsz) {
if (newsz <= oldsz)
return oldptr;
void* newptr = arena_alloc(a, newsz);
char* newptr_char = (char*)newptr;
char* oldptr_char = (char*)oldptr;
for (size_t i = 0; i < oldsz; ++i) {
newptr_char[i] = oldptr_char[i];
}
return newptr;
}
char* arena_strdup(Arena* a, const char* cstr) {
size_t n = strlen(cstr);
char* dup = (char*)arena_alloc(a, n + 1);
memcpy(dup, cstr, n);
dup[n] = '\0';
return dup;
}
void* arena_memdup(Arena* a, void* data, size_t size) {
return memcpy(arena_alloc(a, size), data, size);
}
#ifndef ARENA_NOSTDIO
char* arena_sprintf(Arena* a, const char* format, ...) {
va_list args;
va_start(args, format);
int n = vsnprintf(nullptr, 0, format, args);
va_end(args);
ARENA_ASSERT(n >= 0);
char* result = (char*)arena_alloc(a, n + 1);
va_start(args, format);
vsnprintf(result, n + 1, format, args);
va_end(args);
return result;
}
#endif // ARENA_NOSTDIO
void arena_init(Arena* a, size_t reservedCapacity) {
ARENA_ASSERT(a->end == nullptr && a->begin == nullptr);
a->end = new_region(reservedCapacity);
a->begin = a->end;
}
Arena arena_init(size_t reservedCapacity) {
Arena a;
a.end = new_region(reservedCapacity);
a.begin = a.end;
return a;
}
void arena_reset(Arena* a) {
for (Region* r = a->begin; r != nullptr; r = r->next) {
r->count = 0;
}
a->end = a->begin;
}
void arena_free(Arena* a) {
Region* r = a->begin;
while (r) {
Region* r0 = r;
r = r->next;
free_region(r0);
}
a->begin = nullptr;
a->end = nullptr;
}