From affa5e2933186970d0a3dac38d2ca8f02190e4d9 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Wed, 15 May 2024 20:15:45 -0700 Subject: basic magic buddy allocator --- main.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..b84e958 --- /dev/null +++ b/main.c @@ -0,0 +1,39 @@ +#include "buddy.h" +#include +#include +#include +#include +#include +#include + +void get_random(uint8_t *dst, size_t count) { + int fd = open("/dev/urandom", O_RDONLY); + assert(count == read(fd, dst, count)); + close(fd); +} + +// helper for getting an aligned allocation +static void *malloc_aligned(size_t size) { + uint8_t *data = malloc(size); + if (!((size_t)data % size)) return data; + data = realloc(data, size * 2); + data = data + (size - ((size_t)data % size)); + return data; +} + +void main() { + size_t region_size = 1024 * 1024; + + uint8_t magic[MAGIC_COOKIE_BYTES]; + get_random(magic, MAGIC_COOKIE_BYTES); + init_buddy(malloc_aligned(region_size), region_size, magic); + memset(magic, 0, sizeof(magic)); + + void *x = allocate(1024); + printf("Just allocated %p...\n", x); + debug_buddy(); + + printf("Now liberating %p...\n", x); + liberate(x, 1024); + debug_buddy(); +} -- cgit v1.2.3