/
allocator.c
59 lines (43 loc) · 1.25 KB
/
allocator.c
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
#include "allocator.h"
#define ALLOCATOR_PAGE_SIZE 0x1000
static void *get_page(void)
{
void *result;
#ifdef WINDOWS
result = VirtualAlloc(0, ALLOCATOR_PAGE_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
RT_ASSERT(result);
#else
result = mmap(0, ALLOCATOR_PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
RT_ASSERT(result != MAP_FAILED);
#endif
return result;
}
static void free_page(void *page)
{
#ifdef WINDOWS
VirtualFree(page, 0, MEM_RELEASE);
#else
munmap(page, ALLOCATOR_PAGE_SIZE);
#endif
}
void allocator_init(struct allocator *allocator)
{
allocator->next = (unsigned char *)get_page();
allocator->page = allocator->next + ALLOCATOR_PAGE_SIZE;
kv_init(allocator->pages);
}
void allocator_free(struct allocator *allocator)
{
free_page(allocator->page - ALLOCATOR_PAGE_SIZE);
for(size_t i = 0; i < kv_size(allocator->pages); i++)
free_page(kv_A(allocator->pages, i) - ALLOCATOR_PAGE_SIZE);
kv_destroy(allocator->pages);
}
void *allocator_page_alloc(struct allocator *allocator, size_t length)
{
kv_push(void *, allocator->pages, allocator->page);
unsigned char* result = (unsigned char *)get_page();
allocator->page = result + ALLOCATOR_PAGE_SIZE;
allocator->next = result + length;
return result;
}