Browse Source

Added tree functions

Thomas Johnson 1 year ago
parent
commit
64765d22b5
2 changed files with 55 additions and 0 deletions
  1. 6
    0
      allocator_internal.h
  2. 49
    0
      tree_alloc.c

+ 6
- 0
allocator_internal.h View File

@@ -20,6 +20,8 @@ typedef struct TreeAlloc {
20 20
   struct TreeAlloc *parent;
21 21
   struct TreeAlloc *left;
22 22
   struct TreeAlloc *right;
23
+  struct TreeAlloc *before;
24
+  struct TreeAlloc *after;
23 25
   uintptr_t size;
24 26
 } TreeAlloc;
25 27
 
@@ -28,6 +30,8 @@ typedef struct FreeSpace {
28 30
   struct FreeSpace *parent;
29 31
   struct FreeSpace *left;
30 32
   struct FreeSpace *right;
33
+  struct TreeAlloc *before;
34
+  struct TreeAlloc *after;
31 35
   uintptr_t size;
32 36
 } FreeSpace;
33 37
 
@@ -36,6 +40,8 @@ typedef struct WatermarkAlloc {
36 40
   struct TreeAlloc *parent;
37 41
   struct TreeAlloc *left;
38 42
   struct TreeAlloc *right;
43
+  struct TreeAlloc *before;
44
+  struct TreeAlloc *after;
39 45
   uintptr_t size;
40 46
   int num_allocs;
41 47
   void *next_alloc;

+ 49
- 0
tree_alloc.c View File

@@ -57,3 +57,52 @@ TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, u
57 57
     }
58 58
   }
59 59
 }
60
+
61
+// TODO: Rewrite for self-balancing tree.
62
+void remove_node(TreeAlloc** root_ptr, TreeAlloc* node) {
63
+  TreeAlloc *replace = NULL;
64
+  if (node->left == NULL) {
65
+    replace = node->right;
66
+  } else if (node->right != NULL) {
67
+    replace = node->right;
68
+    TreeAlloc *head = node->left;
69
+    while (head->right != NULL) {
70
+      head = head->right;
71
+    }
72
+    head->right = head->parent->right;
73
+    head->right->parent = head;
74
+  }
75
+  if (node->parent == NULL) {
76
+    replace->parent = NULL;
77
+    *root_ptr = replace;
78
+  } else {
79
+    if (node == node->parent->left) {
80
+      node->parent->left = replace;
81
+    } else {
82
+      node->parent->right = replace;
83
+    }
84
+  }
85
+}
86
+
87
+// TODO: Rewrite for self-balancing tree.
88
+void insert_after(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
89
+  if (after->right != NULL) {
90
+    after->right->parent = to_insert;
91
+    to_insert->right = after->right;
92
+  }
93
+  after->right = to_insert;
94
+  to_insert->parent = after;
95
+}
96
+
97
+// TODO: Rewrite for self-balancing tree.
98
+void insert_before(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
99
+  if (before->left != NULL) {
100
+    before->left->parent = to_insert;
101
+    to_insert->left = before->left;
102
+  }
103
+  before->left = to_insert;
104
+  to_insert->parent = before;
105
+}
106
+
107
+
108
+

Loading…
Cancel
Save