Browse Source

Added debug feature

tree_alloc
Thomas Johnson 1 year ago
parent
commit
732d2f9dd2
  1. 2
      Makefile
  2. 22
      tree_alloc.c

2
Makefile

@ -4,7 +4,7 @@ CC = clang
COMMON_FLAGS = -std=gnu11
OUT_NAME = the_alloc
dev: CFLAGS= $(COMMON_FLAGS) -g -D UNIT_TESTS
dev: CFLAGS= $(COMMON_FLAGS) -g -D UNIT_TESTS -D DEBUG=1
dev: $(OUT_NAME)
valgrind ./$(OUT_NAME)

22
tree_alloc.c

@ -4,6 +4,22 @@
#include "allocator_internal.h"
#ifdef DEBUG
#include <stdio.h>
void debug_print_tree(int indent, TreeAlloc *node) {
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);
if (node->color == COLOR_RED) { printf("\e[37"); }
debug_print_tree(indent + 1, node->right);
}
}
#endif
TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size) {
return NULL;
}
@ -354,6 +370,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("=== FREESPACE TREE ===\n");
debug_print_tree(0, arena->root_freespace);
printf("=== TREEALLOC TREE ===\n");
debug_print_tree(0, arena->root_treealloc);
#endif
if (arena->root_freespace == NULL) {
// Handle being out of freespace.
if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {

Loading…
Cancel
Save