summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2024-05-15 20:15:45 -0700
committerMatthew Sotoudeh <matthew@masot.net>2024-05-15 20:15:45 -0700
commitaffa5e2933186970d0a3dac38d2ca8f02190e4d9 (patch)
treed61e7385564729c887ab4c7c18d6444f7ebacbad /main.c
basic magic buddy allocator
Diffstat (limited to 'main.c')
-rw-r--r--main.c39
1 files changed, 39 insertions, 0 deletions
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 <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <assert.h>
+#include <unistd.h>
+#include <stdio.h>
+
+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();
+}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback