-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.c
More file actions
198 lines (144 loc) · 5.05 KB
/
alloc.c
File metadata and controls
198 lines (144 loc) · 5.05 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
195
196
197
#include "alloc.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
bool is_power_of_two(size_t x){
return (x & (x-1)) == 0;
}
uintptr_t align_forward(uintptr_t address, size_t alignment) {
uintptr_t p,a,modulo;
assert(is_power_of_two(alignment));
p = address;
a = (uintptr_t) alignment;
modulo = p & (a-1);
if(modulo != 0) {
p += (a - modulo);
}
return p;
}
size_t calc_padding_with_header(uintptr_t addr, uintptr_t alignment, size_t header_size) {
uintptr_t p,a,modulo,padding = 0;
uintptr_t required_space = (uintptr_t) header_size;
assert(is_power_of_two(alignment));
p = addr;
a = alignment;
modulo = p & (a-1);
if(modulo != 0) {
padding = a - modulo;
}
if(padding < required_space) {
required_space -= padding;
if((required_space & (a-1)) != 0) {
padding += a * (1 + (required_space/a));
}else {
padding += a * (required_space/a);
}
}
return (size_t) padding;
}
void arena_init(Arena *arena, size_t mem_size) {
arena->buf = mmap(NULL, mem_size,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
arena->arena_capacity = mem_size;
arena->cur_offset = 0;
}
void arena_destroy(Arena *arena) {
if(arena->buf && arena->arena_capacity)
munmap(arena->buf, arena->arena_capacity);
}
void *arena_alloc(Arena *arena, size_t alloc_size) {
return arena_alloc_aligned(arena, alloc_size, DEFAULT_ALIGNMENT);
}
void *arena_alloc_aligned(Arena *arena,size_t alloc_size, size_t alignment) {
uintptr_t cur_addr = (uintptr_t) &arena->buf[arena->cur_offset];
uintptr_t aligned_addr = align_forward(cur_addr, alignment);
size_t align_offset = (aligned_addr - cur_addr);
if((arena->cur_offset + align_offset + alloc_size) < arena->arena_capacity) {
arena->cur_offset += align_offset + alloc_size;
return (void *) aligned_addr;
}
return NULL;
}
void free_list_init(FreeList *list, size_t mem_size) {
list->data = mmap(NULL, mem_size,
PROT_READ | PROT_WRITE,
MAP_ANONYMOUS | MAP_PRIVATE,
-1, 0);
list->size = mem_size;
FreeListNode *first_node = (FreeListNode *) list->data;
first_node->block_size = mem_size;
first_node->next = NULL;
list->head = first_node;
}
void free_list_destroy(FreeList *list) {
munmap(list->data, list->size);
list->size = 0;
list->head = NULL;
}
void* free_list_alloc(FreeList *list, size_t alloc_size) {
return free_list_alloc_aligned(list, alloc_size, DEFAULT_ALIGNMENT);
}
void* free_list_alloc_aligned(FreeList *list, size_t alloc_size, size_t alignment) {
FreeListNode *cur_node, *prev_node;
size_t padding,required_size;
size_t header_size = sizeof(FreeListHeader), node_size = sizeof(FreeListNode);
cur_node = list->head;
prev_node = NULL;
// Traverse the free list identify node with enough capacity
while (cur_node != NULL) {
padding = calc_padding_with_header((uintptr_t) cur_node, alignment, sizeof(FreeListHeader));
required_size = padding + alloc_size;
if(cur_node->block_size >= required_size) {
break;
}
prev_node = cur_node;
cur_node = cur_node->next;
}
if(cur_node == NULL) {
assert(0 && "could not find free list node with required capacity.");
return NULL;
}
size_t cur_node_size = cur_node->block_size;
FreeListNode *next_node = cur_node->next;
uintptr_t cur_node_addr = (uintptr_t) cur_node;
uintptr_t alloc_aligned_addr = cur_node_addr + (uintptr_t) padding;
// Set the header metedata
FreeListHeader *header = (FreeListHeader *)(alloc_aligned_addr - (uintptr_t) header_size);
header->alloc_size = alloc_size;
header->padding = padding;
size_t remaining_block_size = cur_node_size - (padding + alloc_size);
if(remaining_block_size >= node_size) {
uintptr_t cur_node_resize_addr = (uintptr_t) cur_node + (uintptr_t) (padding + alloc_size);
cur_node = (FreeListNode *) cur_node_resize_addr;
cur_node->block_size = remaining_block_size;
cur_node->next = next_node;
if(prev_node) prev_node->next = cur_node;
else list->head = cur_node;
} else{
if(prev_node) prev_node->next = next_node;
else list->head = next_node;
}
return (void *) alloc_aligned_addr;
}
void free_list_dealloc(FreeList *list, void *alloc_addr) {
size_t padding, alloc_size, header_size = sizeof(FreeListHeader);
FreeListHeader * header_ptr = (FreeListHeader *) ((uintptr_t) alloc_addr - (uintptr_t) header_size);
assert(list != NULL);
padding = header_ptr->padding;
alloc_size = header_ptr->alloc_size;
uintptr_t block_addr = (uintptr_t) alloc_addr - (uintptr_t) padding;
size_t block_size = alloc_size + padding;
// Reset the memory block
memset((void *)block_addr, 0, block_size);
// Add the block meta data
FreeListNode *node = (FreeListNode *) block_addr;
node->block_size = block_size;
node->next = NULL;
// Add the block to the free list
FreeListNode * first_node = list->head;
list->head = node;
node->next = first_node;
}