blob: 49ef7f199d67bc283ee3255ddfc5791345f6610a (
plain) (
blame)
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
|
#include <lib/arena.h>
#include <platform/osal.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
int region_init(struct region *r)
{
void * mem = mmap(NULL, REGION_MAX_CAPACITY, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, 0, 0); // Abstract this under platform
if (mem == NULL) {
print("errno: %s\n", strerror(errno));
return -1;
}
r->capacity = REGION_MAX_CAPACITY;
r->start = mem;
r->end = r->start;
r->size = 0;
return 0;
}
void *region_alloc(struct region *r, size_t b)
{
if (r->capacity <= r->size + b) {
print("compiler error: region memory exceeded trying to allocate %zu bytes!", b); // Make this debug error, only present with verbose flag.
return NULL;
}
void* ptr = r->end;
r->end = (char*)r->end + b;
r->size += b;
return ptr;
}
// such a dumb workaround, fix this.
int region_pop(struct region *r, size_t b)
{
assert(r != NULL);
if (r->size > b) {
return -1;
}
r->end = (char*)r->end - b;
r->size -= b;
return 0;
}
|