summaryrefslogtreecommitdiff
path: root/buddy.h
blob: ad4f4aa0b7eed4875f0a77d3e614e08f2f928e9e (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
#pragma once
#include <stdint.h>
#include <stddef.h>

#define MAGIC_COOKIE_BYTES 32
#define ADDRESS_BITS (8 * sizeof(void*))

struct buddy {
    uint8_t magic[MAGIC_COOKIE_BYTES];
    struct free_block *(avail[ADDRESS_BITS]);
    size_t root_logsize;
    void *base;
};

// NOTE: after init_buddy, *state should never move.
void init_buddy(uint8_t *base, size_t size, uint8_t magic[MAGIC_COOKIE_BYTES],
                struct buddy *state);

void *allocate(size_t size, struct buddy *state);
void  liberate(void *base, size_t size, struct buddy *state);

void debug_buddy(struct buddy *state);

void *reallocate(void *old, size_t old_size, size_t new_size,
                 struct buddy *state);
int reserve(void *start, size_t size, struct buddy *state);

// grow can also be used to move the buddy. grow can never fail.
void grow_buddy(uint8_t *new_base, size_t new_size, struct buddy *state);
// 0 -> failure. always in-place. to do out-of-place, first shrink then grow.
int shrink_buddy(size_t new_size, struct buddy *state);
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback