summaryrefslogtreecommitdiffstats
path: root/lib/buddymap.h
blob: 1af8dfbd87cc1eec38bb0553617e22ef418f1859 (plain) (blame)
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
#ifndef JOVE_LIB_BUDDYMAP_H
#define JOVE_LIB_BUDDYMAP_H 1

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#define BUDDY_BLOCK_BYTES sizeof(intmax_t)
#define BUDDY_BLOCK_BITS (BUDDY_BLOCK_BYTES * 8)

#define BUDDY_BITS_FOR(range) (range * 2)
#define BUDDY_BLOCKS_FOR(range) (BUDDY_BITS_FOR(range) / BUDDY_BLOCK_BITS)

struct BuddyMap
{
    size_t orders;
    size_t bits;
    size_t free;
    uintmax_t **blocks;
};

#define BUDDY_BIT_PARTIAL 0
#define BUDDY_BIT_FULL 1

bool buddy_bit_test(struct BuddyMap *map, size_t l, size_t i);
void buddy_bit_mark(struct BuddyMap *map, size_t l, size_t i);
void buddy_bit_free(struct BuddyMap *map, size_t l, size_t i);

void buddy_mark_range(struct BuddyMap *map, size_t start, size_t end);
void buddy_free_range(struct BuddyMap *map, size_t start, size_t end);

intmax_t buddy_alloc(struct BuddyMap *map, size_t length);
void buddy_free(struct BuddyMap *map, size_t i, size_t length);

#endif