Browse Source

Added tree functions

tree_alloc
Thomas Johnson 1 year ago
parent
commit
64765d22b5
  1. 6
      allocator_internal.h
  2. 49
      tree_alloc.c

6
allocator_internal.h

@ -20,6 +20,8 @@ typedef struct TreeAlloc {
struct TreeAlloc *parent;
struct TreeAlloc *left;
struct TreeAlloc *right;
struct TreeAlloc *before;
struct TreeAlloc *after;
uintptr_t size;
} TreeAlloc;
@ -28,6 +30,8 @@ typedef struct FreeSpace {
struct FreeSpace *parent;
struct FreeSpace *left;
struct FreeSpace *right;
struct TreeAlloc *before;
struct TreeAlloc *after;
uintptr_t size;
} FreeSpace;
@ -36,6 +40,8 @@ typedef struct WatermarkAlloc {
struct TreeAlloc *parent;
struct TreeAlloc *left;
struct TreeAlloc *right;
struct TreeAlloc *before;
struct TreeAlloc *after;
uintptr_t size;
int num_allocs;
void *next_alloc;

49
tree_alloc.c

@ -57,3 +57,52 @@ TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, u
}
}
}
// TODO: Rewrite for self-balancing tree.
void remove_node(TreeAlloc** root_ptr, TreeAlloc* node) {
TreeAlloc *replace = NULL;
if (node->left == NULL) {
replace = node->right;
} else if (node->right != NULL) {
replace = node->right;
TreeAlloc *head = node->left;
while (head->right != NULL) {
head = head->right;
}
head->right = head->parent->right;
head->right->parent = head;
}
if (node->parent == NULL) {
replace->parent = NULL;
*root_ptr = replace;
} else {
if (node == node->parent->left) {
node->parent->left = replace;
} else {
node->parent->right = replace;
}
}
}
// TODO: Rewrite for self-balancing tree.
void insert_after(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
if (after->right != NULL) {
after->right->parent = to_insert;
to_insert->right = after->right;
}
after->right = to_insert;
to_insert->parent = after;
}
// TODO: Rewrite for self-balancing tree.
void insert_before(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
if (before->left != NULL) {
before->left->parent = to_insert;
to_insert->left = before->left;
}
before->left = to_insert;
to_insert->parent = before;
}
Loading…
Cancel
Save