|
|
@ -7,12 +7,13 @@ |
|
|
|
#ifdef DEBUG |
|
|
|
#include <stdio.h> |
|
|
|
|
|
|
|
void debug_print_tree(int indent, TreeAlloc *node) { |
|
|
|
void debug_print_tree(int indent, void *p) { |
|
|
|
TreeAlloc *node = (TreeAlloc*) p; |
|
|
|
if (node != NULL) { |
|
|
|
debug_print_tree(indent + 1, node->left); |
|
|
|
for (int ii = 0; ii < indent; ii++) { printf(" "); } |
|
|
|
if (node->color == COLOR_RED) { printf("\e[31"); } |
|
|
|
printf("%p %u\n", node, node->size); |
|
|
|
printf("%p %lu\n", node, node->size); |
|
|
|
if (node->color == COLOR_RED) { printf("\e[37"); } |
|
|
|
debug_print_tree(indent + 1, node->right); |
|
|
|
} |
|
|
@ -306,6 +307,7 @@ int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t al |
|
|
|
newreg->right = NULL; |
|
|
|
realsize -= (void*) newreg - (void*) reg; |
|
|
|
realsize -= realsize % alignof(WatermarkAlloc); |
|
|
|
newreg->size = realsize; |
|
|
|
if (arena->root_freespace == NULL) { |
|
|
|
insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg); |
|
|
|
} else { |
|
|
@ -319,6 +321,14 @@ int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t al |
|
|
|
} |
|
|
|
|
|
|
|
void unalloc(Arena *arena, void *addr) { |
|
|
|
#ifdef DEBUG |
|
|
|
printf("==== UNALLOCATING ====\n"); |
|
|
|
printf("=== FREESPACE TREE ===\n"); |
|
|
|
debug_print_tree(0, arena->root_freespace); |
|
|
|
printf("=== TREEALLOC TREE ===\n"); |
|
|
|
debug_print_tree(0, arena->root_treealloc); |
|
|
|
printf("=== END OF TREES ====\n"); |
|
|
|
#endif |
|
|
|
if (arena->root_treealloc == NULL) { |
|
|
|
arena->error("attempt to unallocate when there are no allocations!"); |
|
|
|
return; |
|
|
@ -371,10 +381,12 @@ void unalloc(Arena *arena, void *addr) { |
|
|
|
void *alloc(Arena *arena, uintptr_t size, uintptr_t align) { |
|
|
|
uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align); |
|
|
|
#ifdef DEBUG |
|
|
|
printf("==== ALLOCATING =====\n"); |
|
|
|
printf("=== FREESPACE TREE ===\n"); |
|
|
|
debug_print_tree(0, arena->root_freespace); |
|
|
|
printf("=== TREEALLOC TREE ===\n"); |
|
|
|
debug_print_tree(0, arena->root_treealloc); |
|
|
|
printf("=== END OF TREES ====\n"); |
|
|
|
#endif |
|
|
|
if (arena->root_freespace == NULL) { |
|
|
|
// Handle being out of freespace. |
|
|
@ -406,12 +418,6 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) { |
|
|
|
TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region); |
|
|
|
insert_right(&arena->root_treealloc, region, insert_point); |
|
|
|
} |
|
|
|
if (arena->root_treealloc == NULL) { |
|
|
|
insert_singleton(&arena->root_treealloc, region); |
|
|
|
} else { |
|
|
|
TreeAlloc *insert_point = search_by_address((TreeAlloc*) arena->root_treealloc, region); |
|
|
|
insert_right(&arena->root_treealloc, region, insert_point); |
|
|
|
} |
|
|
|
if (new_free_size >= sizeof(FreeSpace)) { |
|
|
|
// If there's enough free space after the allocation, use it! |
|
|
|
region->size = new_size; // Safe because the allocated region tree is not sorted by size. |
|
|
|