Browse Source

Collected miscellaneous code

tree_alloc
Thomas Johnson 1 year ago
parent
commit
693056d467
  1. 7
      main.c
  2. 26
      tree_alloc.c

7
main.c

@ -11,7 +11,7 @@
void *get_new_region(uintptr_t size) {
void *m = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (m == MAP_FAILED) {
return 0;
return NULL;
}
return m;
}
@ -54,7 +54,7 @@ void delete_record(struct AllocationRecord *dummy, struct AllocationRecord *dele
struct AllocationRecord *get_record(struct AllocationRecord *dummy, int which) {
struct AllocationRecord *head = dummy->next;
while (--which) {
while (which--) {
head = head->next;
}
return head;
@ -77,6 +77,7 @@ void new_region(struct AllocationRecord *dummy, struct Arena *arena) {
return;
}
for (int ii = 0; ii < size; ii++) {
printf("writing %p\n", ((void*) region) + ii);
((char*) region)[ii] = (char) rand();
}
insert_record(dummy, region, size);
@ -106,11 +107,13 @@ int act(struct AllocationRecord *dummy, struct Arena *arena) {
printf("validation failed at id %i\n", head->id);
die = 1;
}
head = head->next;
}
return die;
}
int main() {
srand(1);
struct Arena arena = {
NULL,
NULL,

26
tree_alloc.c

@ -302,8 +302,8 @@ void insert_singleton(TreeAlloc **root_ptr, TreeAlloc *to_insert) {
void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after) {
if (after->right != NULL) {
after->right->parent = to_insert;
to_insert->right = after->right;
after->right = to_insert;
to_insert->parent = after;
}
after->right = to_insert;
to_insert->parent = after;
@ -312,8 +312,8 @@ void insert_right(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* after)
void insert_left(TreeAlloc** root_ptr, TreeAlloc* to_insert, TreeAlloc* before) {
if (before->left != NULL) {
before->left->parent = to_insert;
to_insert->left = before->left;
before->left = to_insert;
to_insert->parent = before;
}
before->left = to_insert;
to_insert->parent = before;
@ -344,6 +344,14 @@ int add_new_region(Arena *arena, uintptr_t size, uintptr_t padding, uintptr_t al
head = head->right;
}
insert_right((TreeAlloc**) arena->root_freespace, (TreeAlloc*) newreg, (TreeAlloc*) newreg);
#ifdef DEBUG
printf("= POST-REGION-CREATION =\n");
printf("==== FREESPACE TREE ====\n");
debug_print_tree(0, arena->root_freespace);
printf("==== TREEALLOC TREE ====\n");
debug_print_tree(0, arena->root_treealloc);
printf("===== END OF TREES =====\n");
#endif
}
return 1;
}
@ -355,7 +363,7 @@ void unalloc(Arena *arena, void *addr) {
debug_print_tree(0, arena->root_freespace);
printf("=== TREEALLOC TREE ===\n");
debug_print_tree(0, arena->root_treealloc);
printf("=== END OF TREES ====\n");
printf("==== END OF TREES ====\n");
#endif
if (arena->root_treealloc == NULL) {
arena->error("attempt to unallocate when there are no allocations!");
@ -414,7 +422,7 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
debug_print_tree(0, arena->root_freespace);
printf("=== TREEALLOC TREE ===\n");
debug_print_tree(0, arena->root_treealloc);
printf("=== END OF TREES ====\n");
printf("==== END OF TREES ====\n");
#endif
if (arena->root_freespace == NULL) {
// Handle being out of freespace.
@ -441,7 +449,6 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
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
@ -479,7 +486,10 @@ void *alloc(Arena *arena, uintptr_t size, uintptr_t align) {
} else {
region->before = before_alloc;
}
return align_after(region + sizeof(TreeAlloc), actual_align);
#ifdef DEBUG
printf("region is still at %p\n", region);
#endif
return align_after((void*) region + sizeof(TreeAlloc), actual_align);
}
}

Loading…
Cancel
Save