Browse Source

Fixed a major bug involving and pointers

Thomas Johnson 5 months ago
parent
commit
4473209226
2 changed files with 45 additions and 2 deletions
  1. 4
    2
      main.c
  2. 41
    0
      tree_alloc.c

+ 4
- 2
main.c View File

@@ -68,8 +68,9 @@ int validate_record(struct AllocationRecord *record) {
68 68
 }
69 69
 
70 70
 void new_region(struct AllocationRecord *dummy, struct Arena *arena) {
71
-  int size = 1 + (rand() % 8192);
72
-  int align = 1 << (rand() % 4);
71
+  uintptr_t size = 1 + (rand() % 8192);
72
+  uintptr_t align = 1 << (rand() % 4);
73
+  printf("alloc'ing a region of size %lu with alignment %lu, id %i\n", size, align, dummy->id);
73 74
   void *region = alloc(arena, size, align);
74 75
   if (region == NULL) {
75 76
     printf("memory allocator broke, we have 1L though\n");
@@ -87,6 +88,7 @@ void delete_region(struct AllocationRecord *dummy, struct Arena *arena) {
87 88
   }
88 89
   uintptr_t which = rand() % dummy->size;
89 90
   struct AllocationRecord *zap = get_record(dummy, which);
91
+  printf("dealloc'ing a region of size %lu, id %i\n", zap->size, zap->id);
90 92
   unalloc(arena, zap->allocation);
91 93
   delete_record(dummy, zap);
92 94
 }

+ 41
- 0
tree_alloc.c View File

@@ -80,6 +80,34 @@ TreeAlloc *search_by_size(TreeAlloc *root, uintptr_t padding, uintptr_t align, u
80 80
   }
81 81
 }
82 82
 
83
+TreeAlloc *succ(TreeAlloc *el) {
84
+  if (el->right != NULL) {
85
+    el = el->right;
86
+    while (el->left != NULL) {
87
+      el = el->left;
88
+    }
89
+    return el;
90
+  }
91
+  while (el->parent != NULL && el == el->parent->right) {
92
+    el = el->parent;
93
+  }
94
+  return el->parent;
95
+}
96
+
97
+TreeAlloc *pred(TreeAlloc *el) {
98
+  if (el->left != NULL) {
99
+    el = el->left;
100
+    while (el->right != NULL) {
101
+      el = el->right;
102
+    }
103
+    return el;
104
+  }
105
+  while (el->parent != NULL && el == el->parent->left) {
106
+    el = el->parent;
107
+  }
108
+  return el->parent;
109
+}
110
+
83 111
 TreeAlloc *get_sibling(TreeAlloc *ta) {
84 112
 	TreeAlloc *p = ta->parent;
85 113
 	if (!p)
@@ -437,6 +465,19 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
437 465
         FreeSpace *insert_point = (FreeSpace*) search_by_size((TreeAlloc*) arena->root_freespace, 0, 1, new_free_size);
438 466
         insert_left((TreeAlloc**) &arena->root_freespace, (TreeAlloc*) new_free, (TreeAlloc*) insert_point);
439 467
       }
468
+      // Set the region following this one to be the new free space
469
+      region->after = (TreeAlloc*) new_free;
470
+    } else {
471
+      // There isn't a free space after this one, so put the `next` pointer at the next allocated
472
+      // region.
473
+      region->after = succ(region);
474
+    }
475
+    // Also make sure the `before` pointer is correct.
476
+    TreeAlloc *before_alloc = pred(region);
477
+    if (before_alloc == NULL || ((void*) before_alloc) + before_alloc->size < (void*) region) {
478
+      region->before = search_by_address((TreeAlloc*) arena->root_freespace, region);
479
+    } else {
480
+      region->before = before_alloc;
440 481
     }
441 482
     return align_after(region + sizeof(TreeAlloc), actual_align);
442 483
   }

Loading…
Cancel
Save