-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathallocator.c
More file actions
356 lines (284 loc) · 9.37 KB
/
allocator.c
File metadata and controls
356 lines (284 loc) · 9.37 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
#define _GNU_SOURCE
#include "allocator.h"
#include <sys/mman.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <stdio.h>
#include <errno.h>
/* Configuration constants */
#define MIN_BLOCK_SIZE 32
#define ALIGNMENT 16
#define NUM_SIZE_CLASSES 10
#define MMAP_THRESHOLD (128 * 1024) /* Use mmap for allocations > 128KB */
#define BRK_INCREMENT (64 * 1024) /* Grow heap by 64KB chunks */
/* Block header structure */
typedef struct block_header {
size_t size; /* Size of block (including header) */
struct block_header* next; /* Next block in free list */
struct block_header* prev; /* Previous block in free list */
int is_free; /* 1 if free, 0 if allocated */
int is_mmap; /* 1 if allocated via mmap */
} block_header_t;
/* Segregated free lists - bins for different size classes */
static block_header_t* free_lists[NUM_SIZE_CLASSES] = {NULL};
/* Statistics */
static mem_stats_t stats = {0};
/* Heap boundaries */
static void* heap_start = NULL;
static void* heap_end = NULL;
/* Helper function: Align size to ALIGNMENT boundary */
static inline size_t align_size(size_t size) {
return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);
}
/* Helper function: Get size class index for a given size */
static int get_size_class(size_t size) {
if (size <= 32) return 0;
if (size <= 64) return 1;
if (size <= 128) return 2;
if (size <= 256) return 3;
if (size <= 512) return 4;
if (size <= 1024) return 5;
if (size <= 2048) return 6;
if (size <= 4096) return 7;
if (size <= 8192) return 8;
return 9; /* Large allocations */
}
/* Remove block from free list */
static void remove_from_free_list(block_header_t* block) {
int class_idx = get_size_class(block->size);
if (block->prev) {
block->prev->next = block->next;
} else {
free_lists[class_idx] = block->next;
}
if (block->next) {
block->next->prev = block->prev;
}
block->next = NULL;
block->prev = NULL;
}
/* Add block to free list */
static void add_to_free_list(block_header_t* block) {
int class_idx = get_size_class(block->size);
block->next = free_lists[class_idx];
block->prev = NULL;
if (free_lists[class_idx]) {
free_lists[class_idx]->prev = block;
}
free_lists[class_idx] = block;
block->is_free = 1;
}
/* Coalesce adjacent free blocks */
static block_header_t* coalesce(block_header_t* block) {
if (!block || block->is_mmap) {
return block;
}
void* block_end = (void*)block + block->size;
/* Check if next block exists within heap bounds */
if ((void*)block_end >= heap_end || (void*)block_end < heap_start) {
return block;
}
block_header_t* next_block = (block_header_t*)block_end;
/* Additional safety check: ensure next block is within valid range */
if ((void*)next_block + sizeof(block_header_t) > heap_end) {
return block;
}
/* Check if next block is free */
if (next_block->is_free && !next_block->is_mmap) {
/* Coalesce with next block */
remove_from_free_list(next_block);
block->size += next_block->size;
stats.num_coalesces++;
/* Recursively coalesce */
return coalesce(block);
}
return block;
}
/* Split block if it's too large */
static void split_block(block_header_t* block, size_t size) {
size_t total_size = align_size(size + sizeof(block_header_t));
if (block->size >= total_size + sizeof(block_header_t) + MIN_BLOCK_SIZE) {
/* Create new free block from remainder */
block_header_t* new_block = (block_header_t*)((void*)block + total_size);
new_block->size = block->size - total_size;
new_block->is_free = 1;
new_block->is_mmap = 0;
new_block->next = NULL;
new_block->prev = NULL;
block->size = total_size;
add_to_free_list(new_block);
stats.num_splits++;
}
}
/* Expand heap using brk */
static void* expand_heap(size_t size) {
size_t alloc_size = size < BRK_INCREMENT ? BRK_INCREMENT : align_size(size);
void* old_brk = sbrk(0);
if (old_brk == (void*)-1) {
return NULL;
}
void* new_brk = sbrk(alloc_size);
if (new_brk == (void*)-1) {
return NULL;
}
if (heap_start == NULL) {
heap_start = old_brk;
}
heap_end = sbrk(0);
/* Create new free block */
block_header_t* block = (block_header_t*)old_brk;
block->size = alloc_size;
block->is_free = 1;
block->is_mmap = 0;
block->next = NULL;
block->prev = NULL;
return block;
}
/* Find suitable free block */
static block_header_t* find_free_block(size_t size) {
int start_class = get_size_class(size);
/* Search in appropriate size class and larger ones */
for (int i = start_class; i < NUM_SIZE_CLASSES; i++) {
block_header_t* current = free_lists[i];
while (current) {
if (current->is_free && current->size >= size) {
return current;
}
current = current->next;
}
}
return NULL;
}
/* Thread-unsafe malloc implementation */
void* mem_malloc(size_t size) {
if (size == 0) {
return NULL;
}
size_t total_size = align_size(size + sizeof(block_header_t));
block_header_t* block;
/* Use mmap for large allocations */
if (total_size >= MMAP_THRESHOLD) {
void* ptr = mmap(NULL, total_size, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED) {
return NULL;
}
block = (block_header_t*)ptr;
block->size = total_size;
block->is_free = 0;
block->is_mmap = 1;
block->next = NULL;
block->prev = NULL;
stats.total_allocated += total_size;
stats.current_usage += total_size;
stats.num_allocations++;
return (void*)((char*)block + sizeof(block_header_t));
}
/* Try to find free block */
block = find_free_block(total_size);
if (!block) {
/* No suitable free block, expand heap */
block = expand_heap(total_size);
if (!block) {
return NULL;
}
}
/* Remove from free list */
remove_from_free_list(block);
/* Split if block is too large */
split_block(block, size);
block->is_free = 0;
stats.total_allocated += block->size;
stats.current_usage += block->size;
stats.num_allocations++;
return (void*)((char*)block + sizeof(block_header_t));
}
/* Thread-unsafe free implementation */
void mem_free(void* ptr) {
if (!ptr) {
return;
}
block_header_t* block = (block_header_t*)((char*)ptr - sizeof(block_header_t));
if (block->is_mmap) {
/* Unmap large allocation */
stats.total_freed += block->size;
stats.current_usage -= block->size;
stats.num_frees++;
munmap(block, block->size);
return;
}
stats.total_freed += block->size;
stats.current_usage -= block->size;
stats.num_frees++;
/* Coalesce with adjacent free blocks */
block->is_free = 1;
block = coalesce(block);
/* Add to appropriate free list */
add_to_free_list(block);
}
/* Thread-unsafe calloc implementation */
void* mem_calloc(size_t nmemb, size_t size) {
if (nmemb == 0 || size == 0) {
return NULL;
}
/* Check for overflow */
size_t total = nmemb * size;
if (total / nmemb != size) {
return NULL; /* Overflow */
}
void* ptr = mem_malloc(total);
if (ptr) {
memset(ptr, 0, total);
}
return ptr;
}
/* Thread-unsafe realloc implementation */
void* mem_realloc(void* ptr, size_t size) {
if (!ptr) {
return mem_malloc(size);
}
if (size == 0) {
mem_free(ptr);
return NULL;
}
block_header_t* block = (block_header_t*)((char*)ptr - sizeof(block_header_t));
size_t old_size = block->size - sizeof(block_header_t);
if (old_size >= size) {
/* Current block is large enough */
return ptr;
}
/* Allocate new block and copy data */
void* new_ptr = mem_malloc(size);
if (!new_ptr) {
return NULL;
}
memcpy(new_ptr, ptr, old_size);
mem_free(ptr);
return new_ptr;
}
/* Get statistics */
mem_stats_t mem_get_stats(void) {
return stats;
}
/* Print statistics */
void mem_print_stats(void) {
printf("Memory Allocator Statistics:\n");
printf(" Total allocated: %zu bytes\n", stats.total_allocated);
printf(" Total freed: %zu bytes\n", stats.total_freed);
printf(" Current usage: %zu bytes\n", stats.current_usage);
printf(" Number of allocations: %zu\n", stats.num_allocations);
printf(" Number of frees: %zu\n", stats.num_frees);
printf(" Number of splits: %zu\n", stats.num_splits);
printf(" Number of coalesces: %zu\n", stats.num_coalesces);
}
/* Reset allocator state (for testing) */
void mem_reset(void) {
/* Reset statistics */
memset(&stats, 0, sizeof(stats));
/* Clear free lists */
for (int i = 0; i < NUM_SIZE_CLASSES; i++) {
free_lists[i] = NULL;
}
/* Note: We don't reset heap_start/heap_end as brk() is global */
}