Browse Source

Continued progress on tree allocator

Thomas Johnson 1 year ago
parent
commit
89f8bfe3f1
2 changed files with 29 additions and 4 deletions
  1. 4
    2
      allocator_internal.h
  2. 25
    2
      tree_alloc.c

+ 4
- 2
allocator_internal.h View File

@@ -8,10 +8,12 @@ const char RT_FREESPACE = 0;
8 8
 const char RT_TREE_NODE = 1;
9 9
 const char RT_WATERMARK = 2;
10 10
 
11
-struct TopLevel {
11
+typedef struct Arena {
12 12
   struct FreeSpace *root_freespace;
13 13
   struct TreeAlloc *root_treealloc;
14
-};
14
+  void *(*get_new_region)(uintptr_t);
15
+  void (*error)(char*);
16
+} Arena;
15 17
 
16 18
 // All three of these types should be memory-compatible with each other
17 19
 

+ 25
- 2
tree_alloc.c View File

@@ -84,8 +84,15 @@ void remove_node(TreeAlloc** root_ptr, TreeAlloc* node) {
84 84
   }
85 85
 }
86 86
 
87
+// Inserts a node into an empty tree.
87 88
 // TODO: Rewrite for self-balancing tree.
88
-void insert_after(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
89
+void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
90
+  *root_ptr = to_insert;
91
+  to_insert->parent = NULL;
92
+}
93
+
94
+// TODO: Rewrite for self-balancing tree.
95
+void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
89 96
   if (after->right != NULL) {
90 97
     after->right->parent = to_insert;
91 98
     to_insert->right = after->right;
@@ -95,7 +102,7 @@ void insert_after(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
95 102
 }
96 103
 
97 104
 // TODO: Rewrite for self-balancing tree.
98
-void insert_before(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
105
+void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
99 106
   if (before->left != NULL) {
100 107
     before->left->parent = to_insert;
101 108
     to_insert->left = before->left;
@@ -104,5 +111,21 @@ void insert_before(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before
104 111
   to_insert->parent = before;
105 112
 }
106 113
 
114
+void *unalloc(Arena *arena, void *addr) {
115
+  if (arena->root_treealloc == NULL) {
116
+    arena->error("attempt to unallocate when there are no allocations!");
117
+  }
118
+  TreeAlloc *node = search_by_address(arena->root_treealloc, addr);
119
+  // TODO
120
+}
121
+
122
+void *alloc(Arena arena, uintptr_t size, uintptr_t align) {
123
+  // TODO
124
+  return NULL;
125
+}
107 126
 
127
+void *alloc_growable(Arena arena, uintptr_t size, uintptr_t align) {
128
+  // TODO
129
+  return NULL;
130
+}
108 131
 

Loading…
Cancel
Save