summaryrefslogtreecommitdiff
path: root/magic_buddy.c
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2024-05-16 02:23:46 -0700
committerMatthew Sotoudeh <matthew@masot.net>2024-05-16 02:23:46 -0700
commited1ed2fc98cc795acd9bdc76be6d1df138c505c7 (patch)
treea815094c8577705b6bb71c8a23fd8fc2c5cdf0b7 /magic_buddy.c
parentaffa5e2933186970d0a3dac38d2ca8f02190e4d9 (diff)
no longer require aligned base address
Diffstat (limited to 'magic_buddy.c')
-rw-r--r--magic_buddy.c9
1 files changed, 7 insertions, 2 deletions
diff --git a/magic_buddy.c b/magic_buddy.c
index 52cf764..18c4e23 100644
--- a/magic_buddy.c
+++ b/magic_buddy.c
@@ -13,6 +13,7 @@ struct free_list {
static struct free_list *(AVAIL[ADDRESS_BITS]);
static size_t ROOT_LOGSIZE;
+static void *BASE;
// log2 of size rounded up (ceil) or down (!ceil) to the nearest power of 2
static size_t size2log(size_t size, int ceil) {
@@ -29,9 +30,11 @@ static size_t size2log(size_t size, int ceil) {
void init_buddy(uint8_t *base, size_t size,
uint8_t magic[MAGIC_COOKIE_BYTES]) {
+ BASE = base;
+
size_t logsize = size2log(size, 0);
ROOT_LOGSIZE = logsize;
- assert(((size_t)base & ((1 << logsize) - 1)) == 0);
+
AVAIL[logsize] = (struct free_list *)base;
AVAIL[logsize]->next = 0;
AVAIL[logsize]->pprev = &(AVAIL[logsize]);
@@ -39,7 +42,9 @@ void init_buddy(uint8_t *base, size_t size,
}
static struct free_list *buddy_of(struct free_list *block, size_t logsize) {
- return (void *)((size_t)block ^ (1 << logsize));
+ size_t virt_block = (size_t)block - (size_t)BASE;
+ size_t virt_buddy = virt_block ^ (1 << logsize);
+ return (void*)((uint8_t*)BASE + virt_buddy);
}
static int isfree(struct free_list *block) {
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback