Browse Source

Collected miscellaneous code

Thomas Johnson 5 months ago
parent
commit
693056d467
2 changed files with 23 additions and 10 deletions
  1. 5
    2
      main.c
  2. 18
    8
      tree_alloc.c

+ 5
- 2
main.c View File

@@ -11,7 +11,7 @@
11 11
 void *get_new_region(uintptr_t size) {
12 12
   void *m = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
13 13
   if (m == MAP_FAILED) {
14
-    return 0;
14
+    return NULL;
15 15
   }
16 16
   return m;
17 17
 }
@@ -54,7 +54,7 @@ void delete_record(struct AllocationRecord *dummy, struct AllocationRecord *dele
54 54
 
55 55
 struct AllocationRecord *get_record(struct AllocationRecord *dummy, int which) {
56 56
   struct AllocationRecord *head = dummy->next;
57
-  while (--which) {
57
+  while (which--) {
58 58
     head = head->next;
59 59
   }
60 60
   return head;
@@ -77,6 +77,7 @@ void new_region(struct AllocationRecord *dummy, struct Arena *arena) {
77 77
     return;
78 78
   }
79 79
   for (int ii = 0; ii < size; ii++) {
80
+    printf("writing %p\n", ((void*) region) + ii);
80 81
     ((char*) region)[ii] = (char) rand();
81 82
   }
82 83
   insert_record(dummy, region, size);
@@ -106,11 +107,13 @@ int act(struct AllocationRecord *dummy, struct Arena *arena) {
106 107
       printf("validation failed at id %i\n", head->id);
107 108
       die = 1;
108 109
     }
110
+    head = head->next;
109 111
   }
110 112
   return die;
111 113
 }
112 114
 
113 115
 int main() {
116
+  srand(1);
114 117
   struct Arena arena = {
115 118
     NULL,
116 119
     NULL,

+ 18
- 8
tree_alloc.c View File

@@ -302,8 +302,8 @@ void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
302 302
 
303 303
 void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
304 304
   if (after->right != NULL) {
305
-    after->right->parent = to_insert;
306
-    to_insert->right = after->right;
305
+    after->right = to_insert;
306
+    to_insert->parent = after;
307 307
   }
308 308
   after->right = to_insert;
309 309
   to_insert->parent = after;
@@ -312,8 +312,8 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
312 312
 
313 313
 void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
314 314
   if (before->left != NULL) {
315
-    before->left->parent = to_insert;
316
-    to_insert->left = before->left;
315
+    before->left = to_insert;
316
+    to_insert->parent = before;
317 317
   }
318 318
   before->left = to_insert;
319 319
   to_insert->parent = before;
@@ -344,6 +344,14 @@ int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t al
344 344
         head = head->right;
345 345
       }
346 346
       insert_right((TreeAlloc**) arena->root_freespace, (TreeAlloc*) newreg, (TreeAlloc*) newreg);
347
+#ifdef DEBUG
348
+      printf("= POST-REGION-CREATION =\n");
349
+      printf("==== FREESPACE TREE ====\n");
350
+      debug_print_tree(0, arena->root_freespace);
351
+      printf("==== TREEALLOC TREE ====\n");
352
+      debug_print_tree(0, arena->root_treealloc);
353
+      printf("===== END OF TREES =====\n");
354
+#endif
347 355
     }
348 356
     return 1;
349 357
 }
@@ -355,7 +363,7 @@ void unalloc(Arena *arena, void *addr) {
355 363
   debug_print_tree(0, arena->root_freespace);
356 364
   printf("=== TREEALLOC TREE ===\n");
357 365
   debug_print_tree(0, arena->root_treealloc);
358
-  printf("=== END OF TREES ====\n");
366
+  printf("==== END OF TREES ====\n");
359 367
 #endif
360 368
   if (arena->root_treealloc == NULL) {
361 369
     arena->error("attempt to unallocate when there are no allocations!");
@@ -414,7 +422,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
414 422
   debug_print_tree(0, arena->root_freespace);
415 423
   printf("=== TREEALLOC TREE ===\n");
416 424
   debug_print_tree(0, arena->root_treealloc);
417
-  printf("=== END OF TREES ====\n");
425
+  printf("==== END OF TREES ====\n");
418 426
 #endif
419 427
   if (arena->root_freespace == NULL) {
420 428
     // Handle being out of freespace.
@@ -441,7 +449,6 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
441 449
     region->left = NULL;
442 450
     region->type = RT_TREE_NODE;
443 451
 #ifdef DEBUG
444
-    printf("sizeof(TreeAlloc): %lu\n", (uintptr_t) sizeof(TreeAlloc));
445 452
     printf("start: %p, end: %p, adjusted end: %p\n", region, ((void*) region) + size, true_end);
446 453
     printf("size: %lu -> %lu\n", size, new_size);
447 454
 #endif
@@ -479,7 +486,10 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
479 486
     } else {
480 487
       region->before = before_alloc;
481 488
     }
482
-    return align_after(region + sizeof(TreeAlloc), actual_align);
489
+#ifdef DEBUG
490
+    printf("region is still at %p\n", region);
491
+#endif
492
+    return align_after((void*) region + sizeof(TreeAlloc), actual_align);
483 493
   }
484 494
 }
485 495
 

Loading…
Cancel
Save