-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibnalloc.c
More file actions
387 lines (287 loc) · 11.9 KB
/
libnalloc.c
File metadata and controls
387 lines (287 loc) · 11.9 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stddef.h>
#include <stdint.h>
#include <libnalloc.h>
#ifndef LIBNALLOC_PREALLOC_PAGES
// this many pages will be requested in addition to the required amount when requesting memory from the operating system
#define LIBNALLOC_PREALLOC_PAGES 16
#endif
#ifndef LIBNALLOC_DIRECT_THRESHOLD
// allocations above this size will be directly requested from the operating system
#define LIBNALLOC_DIRECT_THRESHOLD 32768
#endif
// these four functions need to be implemented for libnalloc to work
// allocates `pages` * `LIBNALLOC_PAGE_SIZE` amount of contiguous memory
// the address returned must be aligned to LIBNALLOC_ALIGNMENT
// returns NULL on error
void *libnalloc_alloc(size_t pages);
// frees `pages` * `LIBNALLOC_PAGE_SIZE` amount of memory starting at ptr
// `ptr` is a value previously returned by `libnalloc_alloc`
void libnalloc_free(void *ptr, size_t pages);
// locks the memory allocator
void libnalloc_lock(void);
// unlocks the memory allocator
void libnalloc_unlock(void);
#define LIBNALLOC_ALIGNMENT 16
#define LIBNALLOC_MIN_ALLOC sizeof(struct free_block_data)
#ifndef LIBNALLOC_PREFIX
#define LIBNALLOC_PREFIX
#endif
#define LIBNALLOC_MALLOC LIBNALLOC_EXPORT(malloc)
#define LIBNALLOC_CALLOC LIBNALLOC_EXPORT(calloc)
#define LIBNALLOC_REALLOC LIBNALLOC_EXPORT(realloc)
#define LIBNALLOC_FREE LIBNALLOC_EXPORT(free)
#define LIBNALLOC_POSIX_MEMALIGN LIBNALLOC_EXPORT(posix_memalign)
#define LIBNALLOC_ALIGNED_ALLOC LIBNALLOC_EXPORT(aligned_alloc)
void *memset(void *s, int c, size_t n);
void *memcpy(void *restrict s1, const void *restrict s2, size_t n);
struct block_header {
uint64_t size : 62;
uint64_t direct : 1;
uint64_t free : 1;
};
struct direct_header {
uintptr_t map_start;
};
struct free_block_data {
struct free_block *next;
struct free_block *prev;
};
struct free_block {
struct block_header header;
struct free_block_data data;
};
struct chunk_header {
struct chunk_header *next;
struct free_block *free_block;
uint64_t size;
};
static void copy_block_header_to_end(struct block_header *block);
static void *chunk_alloc(struct chunk_header *chunk, size_t size);
static void *alloc_direct(size_t size, size_t alignment);
struct chunk_header *first_chunk = NULL;
static void copy_block_header_to_end(struct block_header *block)
{
struct block_header *block_end = (struct block_header*) ((uintptr_t) block + block->size * LIBNALLOC_ALIGNMENT - sizeof(struct block_header));
block_end->free = block->free;
block_end->direct = block->direct;
block_end->size = block->size;
}
static void *chunk_alloc(struct chunk_header *chunk, size_t size)
{
struct free_block *block = chunk->free_block;
while (block != NULL) {
if (block->header.size * LIBNALLOC_ALIGNMENT >= size + 2 * sizeof(struct block_header)) {
uint64_t orig_size = block->header.size;
block->header.free = 0;
block->header.size = (size + 2 * sizeof(struct block_header)) / LIBNALLOC_ALIGNMENT;
if (block->data.prev == NULL) {
chunk->free_block = block->data.next;
} else {
block->data.prev->data.next = block->data.next;
}
if (block->data.next != NULL) {
block->data.next->data.prev = block->data.prev;
}
if ((orig_size - block->header.size) * LIBNALLOC_ALIGNMENT >= 2 * sizeof(struct block_header) + LIBNALLOC_MIN_ALLOC) {
// create a new free block from the remaining space in the found block
struct free_block *new_block = (struct free_block*) ((uintptr_t) block + block->header.size * LIBNALLOC_ALIGNMENT);
new_block->header.free = 1;
new_block->header.direct = 0;
new_block->header.size = orig_size - block->header.size;
new_block->data.prev = NULL;
new_block->data.next = NULL;
if (chunk->free_block != NULL) {
chunk->free_block->data.prev = new_block;
new_block->data.next = chunk->free_block;
}
chunk->free_block = new_block;
copy_block_header_to_end(&new_block->header);
} else {
// expand the allocated block to the whole found block as there is not enough space to create a new free block
block->header.size = orig_size;
}
copy_block_header_to_end(&block->header);
return &block->data;
}
block = block->data.next;
}
return NULL;
}
static void *alloc_direct(size_t size, size_t alignment)
{
uint64_t direct_offset = ((alignment + sizeof(struct direct_header) + sizeof(struct block_header) - 1) / alignment) * alignment - sizeof(struct direct_header) - sizeof(struct block_header);
uint64_t direct_size = (direct_offset + sizeof(struct direct_header) + sizeof(struct block_header) + size + LIBNALLOC_PAGE_SIZE - 1) / LIBNALLOC_PAGE_SIZE;
void *address = libnalloc_alloc(direct_size);
if (address == NULL) {
return NULL;
}
struct direct_header *direct = (struct direct_header*) ((uintptr_t) address + direct_offset);
struct block_header *block = (struct block_header*) (direct + 1);
block->free = 0;
block->direct = 1;
block->size = direct_size * LIBNALLOC_PAGE_SIZE / LIBNALLOC_ALIGNMENT;
direct->map_start = (uintptr_t) address;
return block + 1;
}
void *LIBNALLOC_EXPORT(malloc)(size_t size)
{
if (size == 0) {
return NULL;
}
if (size >= LIBNALLOC_DIRECT_THRESHOLD) {
return alloc_direct(size, LIBNALLOC_ALIGNMENT);
}
// align size up to LIBNALLOC_ALIGNMENT
size = ((size + LIBNALLOC_ALIGNMENT - 1) / LIBNALLOC_ALIGNMENT) * LIBNALLOC_ALIGNMENT;
if (size < LIBNALLOC_MIN_ALLOC) {
size = LIBNALLOC_MIN_ALLOC;
}
libnalloc_lock();
struct chunk_header *chunk = first_chunk;
while (chunk != NULL) {
void *res = chunk_alloc(chunk, size);
if (res != NULL) {
libnalloc_unlock();
return res;
}
chunk = chunk->next;
}
// currently allocated chunks don't have enough space, allocate new chunk with enough space
size_t pages_to_alloc = (size + sizeof(struct chunk_header) + 5 * sizeof(struct block_header) + LIBNALLOC_PAGE_SIZE - 1) / LIBNALLOC_PAGE_SIZE + LIBNALLOC_PREALLOC_PAGES;
chunk = libnalloc_alloc(pages_to_alloc);
if (chunk == NULL) {
libnalloc_unlock();
return NULL;
}
struct block_header *block = (struct block_header*) (chunk + 1);
block->free = 0;
block->direct = 0;
block->size = (size + 2 * sizeof(struct block_header)) / LIBNALLOC_ALIGNMENT;
copy_block_header_to_end(block);
struct free_block *free_block = (struct free_block*) ((uintptr_t) block + block->size * LIBNALLOC_ALIGNMENT);
free_block->header.free = 1;
free_block->header.direct = 0;
free_block->header.size = (pages_to_alloc * LIBNALLOC_PAGE_SIZE - sizeof(struct chunk_header) - size - 3 * sizeof(struct block_header)) / LIBNALLOC_ALIGNMENT;
copy_block_header_to_end(&free_block->header);
free_block->data.next = NULL;
free_block->data.prev = NULL;
chunk->free_block = free_block;
block = (struct block_header*) ((uintptr_t) free_block + free_block->header.size * LIBNALLOC_ALIGNMENT);
block->size = 0;
block->direct = 0;
block->free = 0;
chunk->size = pages_to_alloc * LIBNALLOC_PAGE_SIZE;
chunk->next = first_chunk;
first_chunk = chunk;
libnalloc_unlock();
return (void*) ((uintptr_t) chunk + sizeof(struct chunk_header) + sizeof(struct block_header));
}
void *LIBNALLOC_EXPORT(calloc)(size_t nelem, size_t elsize)
{
void *tgt = LIBNALLOC_MALLOC(nelem * elsize);
if (tgt != NULL) {
memset(tgt, 0, nelem * elsize);
}
return tgt;
}
void *LIBNALLOC_EXPORT(realloc)(void *ptr, size_t size)
{
// some programs (namely grep) take issue with realloc(NULL, 0) returning a NULL pointer
if (size == 0 && ptr == NULL) {
size = LIBNALLOC_MIN_ALLOC;
}
void *tgt = LIBNALLOC_MALLOC(size);
if (tgt != NULL && ptr != NULL) {
struct block_header *block = (struct block_header*) ((uintptr_t) ptr - sizeof(struct block_header));
size_t old_data_size = block->size * LIBNALLOC_ALIGNMENT - 2 * sizeof(struct block_header);
memcpy(tgt, ptr, size > old_data_size ? old_data_size : size);
LIBNALLOC_FREE(ptr);
}
return tgt;
}
void LIBNALLOC_EXPORT(free)(void *ptr)
{
if (ptr == NULL) {
return;
}
struct free_block *block = (struct free_block*) ((uintptr_t) ptr - sizeof(struct block_header));
if (block->header.direct) {
struct direct_header *direct = (struct direct_header*) ((uintptr_t) block - sizeof(struct direct_header));
libnalloc_free((void*) direct->map_start, block->header.size * LIBNALLOC_ALIGNMENT / LIBNALLOC_PAGE_SIZE);
return;
}
libnalloc_lock();
block->header.free = 1;
block->data.next = NULL;
block->data.prev = NULL;
// find the chunk that owns this block
struct chunk_header *chunk = first_chunk;
struct chunk_header *prev_chunk = NULL;
while (chunk != NULL) {
if ((uintptr_t) chunk < (uintptr_t) block && (uintptr_t) chunk + chunk->size > (uintptr_t) block) {
break; // found
}
prev_chunk = chunk;
chunk = chunk->next;
}
// append block to the beginning of the chunk's linked list of free blocks
if (chunk->free_block != NULL) {
chunk->free_block->data.prev = block;
block->data.next = chunk->free_block;
}
chunk->free_block = block;
// merge with next block if it is free
struct free_block *next_block = (struct free_block*) ((uintptr_t) block + block->header.size * LIBNALLOC_ALIGNMENT);
if (next_block->header.size != 0 && next_block->header.free) {
block->header.size += next_block->header.size;
next_block->data.prev->data.next = next_block->data.next;
if (next_block->data.next != NULL) {
next_block->data.next->data.prev = next_block->data.prev;
}
}
// merge with previous block if it is free
if ((uintptr_t) block - sizeof(struct chunk_header) != (uintptr_t) chunk) {
struct block_header *prev_block_header = &block->header - 1;
if (prev_block_header->free) {
struct free_block *prev_block = (struct free_block*) ((uintptr_t) block - prev_block_header->size * LIBNALLOC_ALIGNMENT);
prev_block->header.size += block->header.size;
block->data.next->data.prev = NULL;
chunk->free_block = chunk->free_block->data.next;
block = prev_block;
}
}
// free the chunk containing this block if the chunk is fully free
if ((uintptr_t) chunk == (uintptr_t) block - sizeof(struct chunk_header) && chunk->size == block->header.size * LIBNALLOC_ALIGNMENT + sizeof(struct chunk_header) + sizeof(struct block_header)) {
// the chunk containing the block is fully free, don't free if only one chunk is allocated
if (first_chunk != chunk || chunk->next != NULL) {
if (prev_chunk != NULL) {
prev_chunk->next = chunk->next;
}
if (first_chunk == chunk) {
first_chunk = chunk->next;
}
libnalloc_free(chunk, chunk->size / LIBNALLOC_PAGE_SIZE);
libnalloc_unlock();
return;
}
}
copy_block_header_to_end(&block->header);
libnalloc_unlock();
}
int LIBNALLOC_EXPORT(posix_memalign)(void **memptr, size_t alignment, size_t size)
{
void *ptr = LIBNALLOC_ALIGNED_ALLOC(alignment, size);
*memptr = ptr;
return 0;
}
void *LIBNALLOC_EXPORT(aligned_alloc)(size_t alignment, size_t size)
{
if (size == 0 || alignment == 0) {
return NULL;
}
if (alignment <= LIBNALLOC_ALIGNMENT) {
return LIBNALLOC_MALLOC(size);
}
// easier to just do a direct allocation regardless of size
return alloc_direct(size, alignment);
}