blob: b84e958e6b2e044ed9846d9b674be80f68849e5a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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();
}
|