Browse Source

Fixed more bugs

Thomas Johnson 5 months ago
parent
commit
b1bc888c6d
2 changed files with 22 additions and 4 deletions
  1. 6
    1
      tree_alloc.c
  2. 16
    3
      util.c

+ 6
- 1
tree_alloc.c View File

@@ -404,7 +404,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
404 404
       return alloc(arena, size, align);
405 405
     }
406 406
     remove_node((TreeAlloc**) &arena->root_freespace, region);
407
-    void *true_end = align_after(align_after(region + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
407
+    void *true_end = align_after(align_after(((void*) region) + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
408 408
     // The size of the new allocation (adjusted for region header and alignment
409 409
     uintptr_t new_size = true_end - (void*) region;
410 410
     // The size of the free space region following the new allocation
@@ -412,6 +412,11 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
412 412
     region->right = NULL;
413 413
     region->left = NULL;
414 414
     region->type = RT_TREE_NODE;
415
+#ifdef DEBUG
416
+    printf("sizeof(TreeAlloc): %lu\n", (uintptr_t) sizeof(TreeAlloc));
417
+    printf("start: %p, end: %p, adjusted end: %p\n", region, ((void*) region) + size, true_end);
418
+    printf("size: %lu -> %lu\n", size, new_size);
419
+#endif
415 420
     if (arena->root_treealloc == NULL) {
416 421
       insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
417 422
     } else {

+ 16
- 3
util.c View File

@@ -1,14 +1,23 @@
1 1
 
2 2
 #include <stdint.h>
3 3
 
4
+#ifdef DEBUG
5
+#include <stdio.h>
6
+#endif
7
+
4 8
 void* align_after(void* address, int align) {
5 9
   uintptr_t addr = (uintptr_t) address;
6 10
   uintptr_t offset = addr % align;
11
+  void *rv;
7 12
   if (offset == 0) {
8
-    return address;
13
+    rv = address;
9 14
   } else {
10
-    return (void*) (addr + align - align % offset);
15
+    rv = (void*) (addr + align - align % offset);
11 16
   }
17
+#ifdef DEBUG
18
+  printf("aligning %p at alignment %lx to %p\n", address, align, rv);
19
+#endif
20
+  return rv;
12 21
 }
13 22
 
14 23
 uintptr_t gcd(uintptr_t a, uintptr_t b) {
@@ -22,6 +31,10 @@ uintptr_t gcd(uintptr_t a, uintptr_t b) {
22 31
 }
23 32
 
24 33
 uintptr_t lcm(uintptr_t a, uintptr_t b) {
25
-  return (a / gcd(a, b)) * b;
34
+  uintptr_t lcm = (a / gcd(a, b)) * b;
35
+#ifdef DEBUG
36
+  printf("so apparently lcm(%lu, %lu) = %lu\n", a, b, lcm);
37
+#endif
38
+  return lcm;
26 39
 }
27 40
 

Loading…
Cancel
Save