-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloc.c
583 lines (518 loc) · 14.6 KB
/
alloc.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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include <pthread.h>
#include <stdint.h>
typedef enum rbcolor { BLACK = 0, RED = 1 } rbcolor_t;
typedef struct metadata {
size_t size;
size_t free;
struct metadata *next, *prev;
} metadata_t;
typedef size_t t_key;
typedef metadata_t t_value;
typedef struct rbnode {
size_t size;
size_t free;
metadata_t *next, *prev;
t_key key;
t_value **tab_values;
size_t size_tab;
size_t n_active;
rbcolor_t color;
struct rbnode *left, *right;
} rbnode_t;
typedef struct {
rbnode_t *root_rbtree;
metadata_t *last_node;
void *end_in_page;
void *first_block;
int page_size;
pthread_mutex_t mutex;
size_t page_remaining;
} malloc_t;
#if __SIZE_WIDTH__ == 64
#define YFREE 0xDEADBEEF5EBA571E
#define NFREE 0x5EBA571EDEADBEEF
#define ALIGN_BYTES(x) ((((x - 1) >> 4) << 4) + 16)
#else
#define YFREE 0x5EBA571E
#define NFREE 0xDEADBEEF
#define ALIGN_BYTES(x) ((((x - 1) >> 3) << 3) + 8)
#endif
#define SIZE_TAB_VALUES (256)
#define META_SIZE ALIGN_BYTES((sizeof(metadata_t)))
#define GET_PAYLOAD(x) ((void *) ((size_t) x + META_SIZE))
#define GET_NODE(x) ((void *) ((size_t) x - META_SIZE))
#define SIZE_DEFAULT_BLOCK (32)
#define IS_VALID(x) \
(((metadata_t *) x)->free == YFREE || ((metadata_t *) x)->free == NFREE)
#define IS_FREE(x) ((x) ? (((metadata_t *) x)->free == YFREE) : (0))
#define IS_RED(x) ((x) ? (((rbnode_t *) x)->color == RED) : (0))
#define MY_COMPARE(k1, k2) (((k1 == k2) ? (0) : ((k1 < k2) ? (-1) : (1))))
extern const char *__progname;
void *calloc(size_t nmemb, size_t size);
void *realloc(void *ptr, size_t size);
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
static inline void flip_color(rbnode_t *node)
{
assert(node);
node->color = !(node->color);
node->left->color = !(node->left->color);
node->right->color = !(node->right->color);
}
static rbnode_t *rotate_left(rbnode_t *left)
{
if (!left)
return NULL;
rbnode_t *right = left->right;
left->right = right->left;
right->left = left;
right->color = left->color;
left->color = RED;
return right;
}
static rbnode_t *rotate_right(rbnode_t *right)
{
if (!right)
return NULL;
rbnode_t *left = right->left;
right->left = left->right;
left->right = right;
left->color = right->color;
right->color = RED;
return left;
}
static rbnode_t *balance(rbnode_t *node)
{
if (IS_RED(node->right))
node = rotate_left(node);
if (IS_RED(node->left) && IS_RED(node->left->left))
node = rotate_right(node);
if (IS_RED(node->left) && IS_RED(node->right))
flip_color(node);
return node;
}
static rbnode_t *move_red_to_left(rbnode_t *node)
{
flip_color(node);
if (node && node->right && IS_RED(node->right->left)) {
node->right = rotate_right(node->right);
node = rotate_left(node);
flip_color(node);
}
return node;
}
static rbnode_t *move_red_to_right(rbnode_t *node)
{
flip_color(node);
if (node && node->left && IS_RED(node->left->left)) {
node = rotate_right(node);
flip_color(node);
}
return node;
}
static bool resize_tab_values(metadata_t **old, rbnode_t *node);
static bool insert_node(rbnode_t *node, metadata_t *new)
{
size_t i = 0;
metadata_t **tmp = node->tab_values;
size_t size = node->size_tab;
if (node->n_active == size) {
i = node->n_active;
if (!resize_tab_values(tmp, node))
return false;
} else {
while (i < size && tmp[i])
i++;
}
node->n_active++;
;
node->tab_values[i] = new;
return true;
}
static rbnode_t *new_rbtree(metadata_t *node);
static rbnode_t *insert_this(rbnode_t *node, metadata_t *new)
{
if (!node)
return new_rbtree(new);
int res = MY_COMPARE(new->size, node->key);
if (res == 0) {
if (!insert_node(node, new))
return NULL;
} else if (res < 0)
node->left = insert_this(node->left, new);
else
node->right = insert_this(node->right, new);
if (IS_RED(node->right) && !IS_RED(node->left))
node = rotate_left(node);
if (IS_RED(node->left) && IS_RED(node->left->left))
node = rotate_right(node);
if (IS_RED(node->left) && IS_RED(node->right))
flip_color(node);
return node;
}
static rbnode_t *insert_in_freed_list(rbnode_t *node, metadata_t *new)
{
node = insert_this(node, new);
if (node)
node->color = BLACK;
new->free = YFREE;
return node;
}
static rbnode_t g_rbnode_basis = {
0, YFREE, NULL, NULL, 0, NULL, SIZE_TAB_VALUES, 1, RED, NULL, NULL,
};
static void *get_heap(size_t size);
static void *alloc_tab(size_t size)
{
void *new;
size_t true_size = ALIGN_BYTES(META_SIZE + (sizeof(new) * size));
new = get_heap(true_size);
if (new)
memset(GET_PAYLOAD(new), 0, true_size - META_SIZE);
return new;
}
static rbnode_t *new_rbtree(metadata_t *node)
{
rbnode_t *new;
if (!(new = get_heap(ALIGN_BYTES(sizeof(*new)))))
return NULL;
memcpy(&(new->size_tab), &(g_rbnode_basis.size_tab), sizeof(size_t) * 5);
new->key = node->size;
if ((new->tab_values = GET_PAYLOAD(alloc_tab(SIZE_TAB_VALUES))) ==
(void *) META_SIZE)
return NULL;
new->tab_values[0] = node;
return new;
}
static bool resize_tab_values(metadata_t **old, rbnode_t *node)
{
metadata_t **new;
size_t size = (node->size_tab) << 1;
if ((new = GET_PAYLOAD(alloc_tab(size))) == (void *) META_SIZE)
return false;
memcpy(new, old, (size >> 1) * sizeof(*new));
((metadata_t *) GET_NODE(node->tab_values))->free = YFREE;
node->tab_values = new;
node->size_tab = size;
return true;
}
static rbnode_t *remove_min(rbnode_t *node)
{
if (!node)
return NULL;
if (!node->left) {
node->free = YFREE;
return NULL;
}
if (!IS_RED(node->left) && !IS_RED(node->left->left))
node = move_red_to_left(node);
node->left = remove_min(node->left);
return balance(node);
}
static inline rbnode_t *min(rbnode_t *node)
{
if (!node)
return NULL;
while (node->left)
node = node->left;
return node;
}
static rbnode_t *remove_node(rbnode_t *node, t_key key, rbnode_t *tmp);
static rbnode_t *remove_key(rbnode_t *node, t_key key)
{
if (!node)
return NULL;
rbnode_t *tmp = NULL;
if (MY_COMPARE(key, node->key) == -1) {
if (node->left) {
if (!IS_RED(node->left) && !IS_RED(node->left->left))
node = move_red_to_left(node);
node->left = remove_key(node->left, key);
}
} else if (!(node = remove_node(node, key, tmp)))
return NULL;
return balance(node);
}
static rbnode_t *remove_node(rbnode_t *node, t_key key, rbnode_t *tmp)
{
if (IS_RED(node->left))
node = rotate_right(node);
if (!MY_COMPARE(key, node->key) && !node->right) {
node->free = YFREE;
return NULL;
}
if (node->right) {
if (!IS_RED(node->right) && !IS_RED(node->right->left))
node = move_red_to_right(node);
if (!MY_COMPARE(key, node->key)) {
tmp = min(node->right);
node->tab_values = tmp->tab_values;
node->size_tab = tmp->size_tab;
node->key = tmp->key;
node->right = remove_min(node->right);
node->n_active = tmp->n_active;
} else
node->right = remove_key(node->right, key);
}
return node;
}
static rbnode_t *remove_k(rbnode_t *node, t_key key)
{
node = remove_key(node, key);
if (node)
node->color = BLACK;
return node;
}
static rbnode_t *get_key(rbnode_t *node, t_key key)
{
while (node) {
int cmp;
if (!(cmp = MY_COMPARE(key, node->key)))
return (node);
node = ((cmp < 0) ? node->left : node->right);
}
return NULL;
}
static rbnode_t *remove_from_freed_list(rbnode_t *node, metadata_t *meta)
{
rbnode_t *tmp;
if ((tmp = get_key(node, meta->size))) {
meta->free = NFREE;
metadata_t **tab = tmp->tab_values;
size_t size = tmp->size_tab;
for (size_t i = 0; i < size; i++) {
if (tab[i] == meta) {
tab[i] = NULL;
tmp->n_active--;
if (tmp->n_active == 0)
return remove_k(node, meta->size);
return node;
}
}
}
return NULL;
}
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
static malloc_t g_info = {
.root_rbtree = NULL,
.last_node = NULL,
.end_in_page = NULL,
.first_block = NULL,
.page_size = 0,
.mutex = PTHREAD_MUTEX_INITIALIZER,
.page_remaining = 0,
};
static inline rbnode_t *find_best(rbnode_t *node, size_t size)
{
rbnode_t *tmp = NULL;
while (node) {
if (node->key >= size) {
tmp = node;
node = node->left;
} else
node = node->right;
}
return tmp;
}
static metadata_t *search_freed_block(rbnode_t *node, size_t size)
{
rbnode_t *tmp = find_best(node, size);
if (tmp) {
size_t size_tab = tmp->size_tab;
metadata_t **tab = tmp->tab_values;
for (size_t i = 0; i < size_tab; i++) {
if (tab[i])
return tab[i];
}
}
return NULL;
}
static void *split_block(metadata_t *node, size_t size)
{
g_info.root_rbtree = remove_from_freed_list(g_info.root_rbtree, node);
if (node->size > size + sizeof(size_t) &&
node->size - size > sizeof(rbnode_t) + SIZE_DEFAULT_BLOCK) {
metadata_t *new = (void *) node + size;
new->size = node->size - size;
new->free = YFREE;
new->prev = node;
new->next = node->next;
node->next = new;
node->size = size;
if (new->next)
new->next->prev = new;
g_info.root_rbtree = insert_in_freed_list(g_info.root_rbtree, new);
}
return node;
}
void *malloc(size_t size)
{
metadata_t *tmp;
void *ptr;
pthread_mutex_lock(&g_info.mutex);
if (size < SIZE_DEFAULT_BLOCK)
size = SIZE_DEFAULT_BLOCK;
size = ALIGN_BYTES(size) + META_SIZE;
if (!g_info.page_size)
g_info.page_size = getpagesize();
if ((tmp = search_freed_block(g_info.root_rbtree, size)))
ptr = split_block(tmp, size);
else
ptr = get_heap(size);
pthread_mutex_unlock(&g_info.mutex);
return ptr ? (GET_PAYLOAD(ptr)) : NULL;
}
static void invalid_pointer(void *ptr)
{
printf("Error in '%s': free(): invalid pointer: %p\n",
((__progname) ? (__progname) : ("Unknow")), ptr);
abort();
}
static void double_free(void *ptr)
{
printf("Error in '%s': free(): double free: %p\n",
((__progname) ? (__progname) : ("Unknow")), ptr);
abort();
}
static metadata_t *fusion(metadata_t *first, metadata_t *second)
{
first->size += second->size;
first->next = second->next;
if (first->next)
first->next->prev = first;
else
g_info.last_node = first;
return first;
}
static inline metadata_t *try_fusion(metadata_t *node)
{
while (IS_FREE(node->prev)) {
g_info.root_rbtree =
remove_from_freed_list(g_info.root_rbtree, node->prev);
node = fusion(node->prev, node);
}
while (IS_FREE(node->next)) {
g_info.root_rbtree =
remove_from_freed_list(g_info.root_rbtree, node->next);
node = fusion(node, node->next);
}
return node;
}
static inline void change_break(metadata_t *node)
{
size_t pages_to_remove;
if (node->prev) {
node->prev->next = NULL;
g_info.last_node = node->prev;
g_info.end_in_page = (void *) g_info.last_node + g_info.last_node->size;
} else {
g_info.end_in_page = g_info.last_node;
g_info.last_node = NULL;
}
g_info.page_remaining += node->size;
pages_to_remove = g_info.page_remaining / g_info.page_size;
/* FIXME: sbrk is deprecated */
brk((sbrk(0) - (pages_to_remove * g_info.page_size)));
g_info.page_remaining =
g_info.page_remaining - (pages_to_remove * g_info.page_size);
}
void free(void *ptr)
{
if (!ptr)
return;
pthread_mutex_lock(&g_info.mutex);
metadata_t *node = GET_NODE(ptr);
if (ptr < g_info.first_block || ptr > g_info.end_in_page || !IS_VALID(node))
invalid_pointer(ptr);
if (node->free == YFREE)
double_free(ptr);
node = try_fusion(node);
if (!node->next)
change_break(node);
else
g_info.root_rbtree = insert_in_freed_list(g_info.root_rbtree, node);
pthread_mutex_unlock(&g_info.mutex);
}
void *calloc(size_t nmemb, size_t size)
{
if (!nmemb || !size)
return NULL;
void *ptr;
if (!(ptr = malloc(size * nmemb)))
return NULL;
pthread_mutex_lock(&g_info.mutex);
memset(ptr, 0, ALIGN_BYTES(size * nmemb));
pthread_mutex_unlock(&g_info.mutex);
return ptr;
}
void *free_realloc(void *ptr)
{
free(ptr);
return NULL;
}
void *realloc(void *ptr, size_t size)
{
if (!ptr)
return malloc(size);
if (!size)
return free_realloc(ptr);
ptr = (void *) ptr - META_SIZE;
metadata_t *tmp = (metadata_t *) ptr;
metadata_t *new = ptr;
if (size + META_SIZE > tmp->size) {
if (!(new = malloc(size)))
return NULL;
size = ALIGN_BYTES(size);
pthread_mutex_lock(&g_info.mutex);
memcpy(new, (void *) ptr + META_SIZE,
(size <= tmp->size) ? (size) : (tmp->size));
pthread_mutex_unlock(&g_info.mutex);
free((void *) ptr + META_SIZE);
} else
new = GET_PAYLOAD(new);
return new;
}
static size_t get_new_page(size_t size)
{
size_t pages = ((size / g_info.page_size) + 1) * g_info.page_size;
/* FIXME: sbrk is deprecated */
if (!g_info.end_in_page) {
if ((g_info.end_in_page = sbrk(0)) == (void *) -1)
return (size_t) -1;
g_info.first_block = g_info.end_in_page;
}
if (sbrk(pages) == (void *) -1) {
errno = ENOMEM;
return (size_t) -1;
}
return pages;
}
static void *get_in_page(size_t size)
{
metadata_t *new = g_info.end_in_page;
new->size = size;
new->free = NFREE;
new->next = NULL;
new->prev = g_info.last_node;
if (g_info.last_node)
g_info.last_node->next = new;
g_info.last_node = new;
g_info.end_in_page = (void *) new + size;
return new;
}
static void *get_heap(size_t size)
{
size_t tmp;
if (g_info.page_remaining < size) {
if ((tmp = get_new_page(size)) == (size_t) -1)
return NULL;
g_info.page_remaining += tmp;
}
g_info.page_remaining -= size;
return get_in_page(size);
}