Browse Source

Added some of allocator

Thomas Johnson 6 months ago
parent
commit
cead24e00f
3 changed files with 41 additions and 2 deletions
  1. 8
    0
      allocator_internal.h
  2. 19
    2
      tree_alloc.c
  3. 14
    0
      util.c

+ 8
- 0
allocator_internal.h View File

@@ -17,6 +17,12 @@ typedef struct Arena {
17 17
 
18 18
 // All three of these types should be memory-compatible with each other
19 19
 
20
+// Before I forget to write it down: It is assumed that the beginning of every allocation region (or
21
+// free space region) is aligned to the alignment of all three of these types (or WatermarkAlloc since
22
+// it has at least all the fields of the other one). This allows for the allocation record to appear
23
+// at the beginning of the region and still be aligned. As a consequence, all sizes are a multiple of
24
+// the alignment.
25
+
20 26
 typedef struct TreeAlloc {
21 27
   char type;  // Should be RT_TREE_NODE
22 28
   struct TreeAlloc *parent;
@@ -48,6 +54,8 @@ typedef struct WatermarkAlloc {
48 54
 } WatermarkAlloc;
49 55
 
50 56
 void* align_after(void* address, uintptr_t align);
57
+uintptr_t lcm(uintptr_t a, uintptr_t b);
58
+uintptr_t gcd(uintptr_t a, uintptr_t b);
51 59
 TreeAlloc *insert_node_at(void *address, uintptr_t padding, uintptr_t align, uintptr_t size);
52 60
 TreeAlloc *search_by_address(TreeAlloc *root, void *address);
53 61
 TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, uintptr_t size);

+ 19
- 2
tree_alloc.c View File

@@ -159,11 +159,28 @@ void unalloc(Arena *arena, void *addr) {
159 159
       insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) start, insert_point);
160 160
     }
161 161
   }
162
-  // TODO
163 162
 }
164 163
 
165 164
 void *alloc(Arena arena, uintptr_t size, uintptr_t align) {
166
-  // TODO
165
+  if (arena->root_freespace == NULL) {
166
+    // TODO: handle out-of-memory
167
+  } else {
168
+    uintptr_t actual_align = lcm(alignof(WatermarkAlloc), align);
169
+    TreeAlloc *insert_point = search_by_size(arena->root_freespace, sizeof(TreeAlloc), actual_align, size);
170
+    if (insert_point == NULL) {
171
+      // TODO: handle memory fragmentation (or OOM)
172
+    }
173
+    remove_node(&arena->root_freespace, insert_point);
174
+    true_end = align_after(align_after(insert_point + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
175
+    // The size of the 
176
+    uintptr_t new_size = true_end - insert_point;
177
+    uintptr_t new_free_size = insert_point.size - new_size;
178
+    if (new_free_size < sizeof(FreeSpace)) {
179
+      // TODO: Handle insufficient size for the new free region (by not having one)
180
+    } else {
181
+      // TODO: Add free region and allocation record
182
+    }
183
+  }
167 184
   return NULL;
168 185
 }
169 186
 

+ 14
- 0
util.c View File

@@ -11,3 +11,17 @@ void* align_after(void* address, int align) {
11 11
   }
12 12
 }
13 13
 
14
+uintptr_t gcd(uintptr_t a, uintptr_t b) {
15
+  if (b > a) {
16
+    return gcd(b, a);
17
+  }
18
+  if (b == 0) {
19
+    return a;
20
+  }
21
+  return gcd(b, a % b);
22
+}
23
+
24
+uintptr_t lcm(uintptr_t a, uintptr_t b) {
25
+  return (a / gcd(a, b)) * b;
26
+}
27
+

Loading…
Cancel
Save