Browse Source

Added test harness

tree_alloc
Thomas Johnson 1 year ago
parent
commit
a3f3a9e837
  1. 67
      main.c
  2. 2
      util.c

67
main.c

@ -4,6 +4,7 @@
#include <stddef.h>
#include <unistd.h>
#include <sys/mman.h>
#include <string.h>
#include "alloc_api.h"
@ -23,9 +24,10 @@ struct AllocationRecord {
uintptr_t size;
void *allocation;
void *reference;
int id;
struct AllocationRecord *next;
struct AllocationRecord *prev;
}
};
void insert_record(struct AllocationRecord *dummy, void *allocation, uintptr_t size) {
struct AllocationRecord *record = malloc(sizeof(struct AllocationRecord));
@ -34,9 +36,11 @@ void insert_record(struct AllocationRecord *dummy, void *allocation, uintptr_t s
record->reference = malloc(size);
record->next = dummy;
record->prev = dummy->prev;
record->id = dummy->id;
dummy->prev->next = record;
dummy->prev = record;
dummy->size += 1;
dummy->id += 1;
memcpy(record->reference, allocation, size);
}
@ -48,6 +52,14 @@ void delete_record(struct AllocationRecord *dummy, struct AllocationRecord *dele
dummy->size -= 1;
}
struct AllocationRecord *get_record(struct AllocationRecord *dummy, int which) {
struct AllocationRecord *head = dummy->next;
while (--which) {
head = head->next;
}
return head;
}
int validate_record(struct AllocationRecord *record) {
if (memcmp(record->allocation, record->reference, record->size) != 0) {
return 0;
@ -55,8 +67,48 @@ int validate_record(struct AllocationRecord *record) {
return 1;
}
void new_region(struct AllocationRecord *dummy, struct Arena *arena) {
int size = 1 + (rand() % 8192);
int align = 1 << (rand() % 4);
void *region = alloc(arena, size, align);
if (region == NULL) {
printf("memory allocator broke, we have 1L though\n");
return;
}
for (int ii = 0; ii < size; ii++) {
((char*) region)[ii] = (char) rand();
}
insert_record(dummy, region, size);
}
void delete_region(struct AllocationRecord *dummy, struct Arena *arena) {
if (dummy->size == 0) {
return;
}
uintptr_t which = rand() % dummy->size;
struct AllocationRecord *zap = get_record(dummy, which);
unalloc(arena, zap->allocation);
delete_record(dummy, zap);
}
int act(struct AllocationRecord *dummy, struct Arena *arena) {
if (rand() & 1) {
new_region(dummy, arena);
} else {
delete_region(dummy, arena);
}
int die = 0;
struct AllocationRecord *head = dummy->next;
while (head != dummy) {
if (!validate_record(head)) {
printf("validation failed at id %i\n", head->id);
die = 1;
}
}
return die;
}
int main() {
printf("Hello, World!\n");
struct Arena arena = {
NULL,
NULL,
@ -65,5 +117,16 @@ int main() {
};
void *reg = alloc(&arena, 20, 4);
unalloc(&arena, reg);
struct AllocationRecord dummy = {
.size = 0,
.allocation = NULL,
.reference = NULL,
.id = 0,
};
dummy.next = &dummy;
dummy.prev = &dummy;
for (int ii = 0; ii < 100; ii++) {
act(&dummy, &arena);
}
return 0;
}

2
util.c

@ -15,7 +15,7 @@ void* align_after(void* address, int align) {
rv = (void*) (addr + align - align % offset);
}
#ifdef DEBUG
printf("aligning %p at alignment %lx to %p\n", address, align, rv);
printf("aligning %p at alignment %x to %p\n", address, align, rv);
#endif
return rv;
}

Loading…
Cancel
Save