Browse Source

Attempt at making insert work

tree_alloc
Cameron Weinfurt 1 year ago
parent
commit
c1ce333202
  1. 2
      main.c
  2. 21
      tree_alloc.c

2
main.c

@ -129,7 +129,7 @@ int main() {
};
dummy.next = &dummy;
dummy.prev = &dummy;
for (int ii = 0; ii < 400; ii++) {
for (int ii = 0; ii < 4000; ii++) {
act(&dummy, &arena);
}
return 0;

21
tree_alloc.c

@ -14,14 +14,14 @@ int debug_tree_black_height(TreeAlloc *node) {
if (node == NULL) {
return 1;
}
return ((node->color == COLOR_BLACK) ? 1 : 0) + debug_tree_black_height(node->left);
return (IS_BLACK_NODE(node) ? 1 : 0) + debug_tree_black_height(node->left);
}
void debug_print_node(int indent, TreeAlloc *node, int bad) {
for (int ii = 0; ii < indent; ii++)
printf(" ");
if (node->color == COLOR_RED)
if (IS_RED_NODE(node))
printf("\e[31m");
else if (bad)
printf("\e[30m]");
@ -29,7 +29,10 @@ void debug_print_node(int indent, TreeAlloc *node, int bad) {
if (bad)
printf("\e[43m");
printf("%p %lu\n", node, node->size);
if (node)
printf("%p %lu\n", node, node->size);
else
printf("(nil) 0\n");
printf("\e[37m");
if (bad)
@ -64,7 +67,7 @@ TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uin
TreeAlloc *search_by_address(TreeAlloc *root, void *address) {
TreeAlloc *head = root;
while (1) {
if (head > (TreeAlloc*) address) {
if (head >= (TreeAlloc*) address) {
if (head->left == NULL) {
return NULL;
} else {
@ -97,7 +100,7 @@ TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, u
TreeAlloc *head = root;
while (1) {
uintptr_t esize = pessimistic_size(head, padding, align);
if (esize < size) {
if (esize <= size) {
if (head->right == NULL) {
return NULL;
} else {
@ -265,6 +268,8 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
printf("=== PRE-INSERT-RIGHT ===\n");
printf("===== INSERTING =====\n");
debug_print_node(0, to_insert, 0);
printf("\tafter\n");
debug_print_node(0, after, 0);
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr, 0);
printf("===== END OF TREES =====\n");
@ -295,10 +300,15 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
printf("=== PRE-INSERT-LEFT ====\n");
printf("===== INSERTING =====\n");
debug_print_tree(0, to_insert, 0);
printf("\tbefore\n");
debug_print_node(0, before, 0);
printf("===== CURRENT TREE =====\n");
debug_print_tree(0, *root_ptr, 0);
printf("===== END OF TREES =====\n");
#endif
if (!before)
before = *root_ptr;
if (before->left != NULL) {
before = before->left;
while (before->right != NULL) {
@ -629,6 +639,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
FreeSpace *new_free = (FreeSpace*) ((void*) region + new_size);
new_free->left = NULL;
new_free->right = NULL;
new_free->parent = NULL;
new_free->type = RT_FREESPACE;
new_free->size = new_free_size;
if (arena->root_freespace == NULL) {

Loading…
Cancel
Save