Browse Source

Finished OOM handler and fixed some definite bugs

Thomas Johnson 5 months ago
parent
commit
e86edef1ea
1 changed files with 20 additions and 10 deletions
  1. 20
    10
      tree_alloc.c

+ 20
- 10
tree_alloc.c View File

@@ -209,19 +209,29 @@ void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before)
209 209
 	repair_tree_after_insert(to_insert);
210 210
 }
211 211
 
212
-int *add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t align) {
213
-    uintptr_t realsize = size + align + padding - 1;
212
+int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t align) {
213
+    uintptr_t realsize = size + align + alignof(WatermarkAlloc) + padding - 1;
214 214
     if (realsize < MIN_NEW_MEM_SIZE) {
215 215
       realsize = MIN_NEW_MEM_SIZE;
216 216
     }
217
-    FreeSpace *newreg = (FreeSpace*) arena->get_new_region(size);
218
-    if (newreg == NULL) {
217
+    FreeSpace *reg = (FreeSpace*) arena->get_new_region(realsize);
218
+    if (reg == NULL) {
219 219
       arena->error("can't allocate a new memory region!");
220 220
       return 0;
221
+    }
222
+    FreeSpace *newreg = align_after(reg, alignof(WatermarkAlloc));
223
+    newreg->left = NULL;
224
+    newreg->right = NULL;
225
+    realsize -= (void*) newreg - (void*) reg;
226
+    realsize -= realsize % alignof(WatermarkAlloc);
227
+    if (arena->root_freespace == NULL) {
228
+      insert_singleton((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) newreg);
221 229
     } else {
222
-      newreg = align_after(newreg, alignof(WatermarkAlloc));
223
-      newreg->left = NULL;
224
-      newreg->right = NULL;
230
+      FreeSpace *head = arena->root_freespace;
231
+      while (head->right != NULL) {
232
+        head = head->right;
233
+      }
234
+      insert_right((TreeAlloc**) arena->root_freespace, (TreeAlloc*) newreg, (TreeAlloc*) newreg);
225 235
     }
226 236
     return 1;
227 237
 }
@@ -277,18 +287,18 @@ void unalloc(Arena *arena, void *addr) {
277 287
 }
278 288
 
279 289
 void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
290
+  uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align);
280 291
   if (arena->root_freespace == NULL) {
281 292
     // Handle being out of freespace.
282
-    if (!add_new_region(arena, size, sizeof(TreeAlloc), align)) {
293
+    if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
283 294
       return NULL;
284 295
     }
285 296
     return alloc(arena, size, align);
286 297
   } else {
287
-    uintptr_t actual_align = lcm(alignof(struct WatermarkAlloc), align);
288 298
     TreeAlloc *region = search_by_size((TreeAlloc*) arena->root_freespace, sizeof(TreeAlloc), actual_align, size);
289 299
     if (region == NULL) {
290 300
       // Handle insufficient freespace or fragmentation.
291
-      if (!add_new_region(arena, size, sizeof(TreeAlloc), align)) {
301
+      if (!add_new_region(arena, size, sizeof(TreeAlloc), actual_align)) {
292 302
         return NULL;
293 303
       }
294 304
       return alloc(arena, size, align);

Loading…
Cancel
Save