iterative api
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
#include <stdbool.h>
|
||||
#include "buddy.h"
|
||||
|
||||
#define NUM_PAGES 32
|
||||
#define NUM_PAGES 2
|
||||
#define PAGE_SIZE (1 << 12)
|
||||
#define MAX_ALLOCS (1 << 14)
|
||||
|
||||
@@ -30,36 +30,53 @@ static bool contains_ptr(struct alloc_info *array, int count, void *ptr)
|
||||
int main(void)
|
||||
{
|
||||
_Alignas(PAGE_SIZE) char mem[NUM_PAGES * PAGE_SIZE];
|
||||
struct buddy_alloc alloc;
|
||||
uint32_t alloc_bits[NUM_PAGES];
|
||||
init_buddy_alloc(&alloc, mem, sizeof(mem), alloc_bits, NUM_PAGES);
|
||||
|
||||
struct page_info info[NUM_PAGES];
|
||||
struct buddy_alloc alloc = buddy_startup(mem, sizeof(mem), info, NUM_PAGES);
|
||||
|
||||
struct alloc_info current_allocs[MAX_ALLOCS];
|
||||
int num_current_allocs = 0;
|
||||
|
||||
int failed = 0;
|
||||
for (;;) {
|
||||
|
||||
if (num_current_allocs > 0) {
|
||||
int i = rand() % num_current_allocs;
|
||||
struct alloc_info deallocating = current_allocs[i];
|
||||
current_allocs[i] = current_allocs[--num_current_allocs];
|
||||
buddy_free(&alloc, (void*) deallocating.ptr, deallocating.len);
|
||||
if (failed == 10 || num_current_allocs == MAX_ALLOCS) {
|
||||
failed = 0;
|
||||
while (1 + rand() % 10 < 6 && num_current_allocs > 0) {
|
||||
int i = rand() % num_current_allocs;
|
||||
struct alloc_info deallocating = current_allocs[i];
|
||||
current_allocs[i] = current_allocs[--num_current_allocs];
|
||||
fprintf(stderr, "buddy_free(%lu, %lu)\n", deallocating.len, (uintptr_t) deallocating.ptr - (uintptr_t) alloc.base);
|
||||
buddy_free(&alloc, deallocating.len, (void*) deallocating.ptr);
|
||||
}
|
||||
}
|
||||
|
||||
size_t len = 1 + (rand() % PAGE_SIZE);
|
||||
assert(len > 0 && len <= PAGE_SIZE);
|
||||
|
||||
void *ptr = buddy_malloc(&alloc, len);
|
||||
if (ptr == NULL)
|
||||
continue;
|
||||
|
||||
/*
|
||||
* Check that it isn't currently allocated
|
||||
*/
|
||||
assert(contains_ptr(current_allocs, num_current_allocs, ptr) == false);
|
||||
current_allocs[num_current_allocs++] = (struct alloc_info) {.ptr=(uintptr_t)ptr, .len=len};
|
||||
if (ptr == NULL) {
|
||||
failed++;
|
||||
//fprintf(stderr, "buddy_malloc(%lu) = NULL\n", len);
|
||||
} else {
|
||||
//buddy_dump(&alloc, stderr);
|
||||
fprintf(stderr, "buddy_malloc(%lu) = %lu\n", len, (uintptr_t) ptr - (uintptr_t) alloc.base);
|
||||
}
|
||||
|
||||
if (ptr != NULL) {
|
||||
|
||||
assert(contains_ptr(current_allocs, num_current_allocs, ptr) == false);
|
||||
current_allocs[num_current_allocs++] = (struct alloc_info) {.ptr=(uintptr_t)ptr, .len=len};
|
||||
/*
|
||||
fprintf(stderr, "Allocations:\n");
|
||||
for (int i = 0; i < num_current_allocs; i++) {
|
||||
fprintf(stderr, " len: %-4lu | ptr: %lu\n", current_allocs[i].len, (uintptr_t) current_allocs[i].ptr - (uintptr_t) alloc.base);
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
|
||||
free_buddy_alloc(&alloc);
|
||||
buddy_cleanup(&alloc);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user