Simplify buddy_startup and remove buddy_cleanup
This commit is contained in:
@@ -1,63 +1,21 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "buddy.h"
|
||||
|
||||
#define NUM_PAGES 16
|
||||
|
||||
#define MIN_ALLOC_SIZE (1U << BUDDY_ALLOC_MIN_BLOCK_LOG2)
|
||||
#define PAGE_SIZE (1U << BUDDY_ALLOC_MAX_BLOCK_LOG2)
|
||||
#define POOL_SIZE (NUM_PAGES * PAGE_SIZE)
|
||||
#define MAX_ALLOCS (POOL_SIZE / MIN_ALLOC_SIZE)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
_Alignas(PAGE_SIZE) char mem[POOL_SIZE];
|
||||
struct page_info info[NUM_PAGES];
|
||||
struct buddy_alloc alloc = buddy_startup(mem, POOL_SIZE, info, NUM_PAGES);
|
||||
char mem[1<<20];
|
||||
for (int i = 0; i < 100; i++) {
|
||||
|
||||
void *allocs[MAX_ALLOCS];
|
||||
void *base = mem;
|
||||
size_t size = sizeof(mem);
|
||||
|
||||
size_t performed = 0;
|
||||
for (size_t i = 0; i < MAX_ALLOCS; i++) {
|
||||
void *ptr = buddy_malloc(&alloc, MIN_ALLOC_SIZE);
|
||||
if (ptr == NULL)
|
||||
break;
|
||||
|
||||
performed++;
|
||||
size_t pad = rand() % size;
|
||||
base += pad;
|
||||
size -= pad;
|
||||
|
||||
allocs[i] = ptr;
|
||||
size_t len = rand() % (size+1);
|
||||
|
||||
*(size_t*) ptr = i;
|
||||
struct buddy *b = buddy_startup(base, len);
|
||||
}
|
||||
|
||||
if (performed == MAX_ALLOCS) {
|
||||
|
||||
for (size_t i = 0; i < MAX_ALLOCS; i++) {
|
||||
if (*(size_t*) allocs[i] != i)
|
||||
fprintf(stderr, "%p allocated twice!\n", allocs[i]);
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MAX_ALLOCS; i++) {
|
||||
buddy_free(&alloc, MIN_ALLOC_SIZE, allocs[MAX_ALLOCS-i-1]);
|
||||
}
|
||||
|
||||
performed = 0;
|
||||
for (size_t i = 0; i < MAX_ALLOCS; i++) {
|
||||
void *ptr = buddy_malloc(&alloc, MIN_ALLOC_SIZE);
|
||||
performed++;
|
||||
|
||||
allocs[i] = ptr;
|
||||
|
||||
*(size_t*) ptr = i + MAX_ALLOCS;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < MAX_ALLOCS; i++) {
|
||||
if (*(size_t*) allocs[i] != MAX_ALLOCS + i)
|
||||
fprintf(stderr, "%p reallocation error!\n", allocs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(stderr, "performed=%d, expected=%d\n", (int) performed, (int) MAX_ALLOCS);
|
||||
|
||||
buddy_cleanup(&alloc);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user