Browse Source

Fixed more bugs

tree_alloc
Thomas Johnson 1 year ago
parent
commit
b1bc888c6d
  1. 7
      tree_alloc.c
  2. 19
      util.c

7
tree_alloc.c

@ -404,7 +404,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
return alloc(arena, size, align);
}
remove_node((TreeAlloc**) &arena->root_freespace, region);
void *true_end = align_after(align_after(region + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
void *true_end = align_after(align_after(((void*) region) + sizeof(TreeAlloc), actual_align) + size, alignof(WatermarkAlloc));
// The size of the new allocation (adjusted for region header and alignment
uintptr_t new_size = true_end - (void*) region;
// 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) {
region->right = NULL;
region->left = NULL;
region->type = RT_TREE_NODE;
#ifdef DEBUG
printf("sizeof(TreeAlloc): %lu\n", (uintptr_t) sizeof(TreeAlloc));
printf("start: %p, end: %p, adjusted end: %p\n", region, ((void*) region) + size, true_end);
printf("size: %lu -> %lu\n", size, new_size);
#endif
if (arena->root_treealloc == NULL) {
insert_singleton((TreeAlloc**) &arena->root_treealloc, region);
} else {

19
util.c

@ -1,14 +1,23 @@
#include <stdint.h>
#ifdef DEBUG
#include <stdio.h>
#endif
void* align_after(void* address, int align) {
uintptr_t addr = (uintptr_t) address;
uintptr_t offset = addr % align;
void *rv;
if (offset == 0) {
return address;
rv = address;
} else {
return (void*) (addr + align - align % offset);
rv = (void*) (addr + align - align % offset);
}
#ifdef DEBUG
printf("aligning %p at alignment %lx to %p\n", address, align, rv);
#endif
return rv;
}
uintptr_t gcd(uintptr_t a, uintptr_t b) {
@ -22,6 +31,10 @@ uintptr_t gcd(uintptr_t a, uintptr_t b) {
}
uintptr_t lcm(uintptr_t a, uintptr_t b) {
return (a / gcd(a, b)) * b;
uintptr_t lcm = (a / gcd(a, b)) * b;
#ifdef DEBUG
printf("so apparently lcm(%lu, %lu) = %lu\n", a, b, lcm);
#endif
return lcm;
}
Loading…
Cancel
Save