-
Notifications
You must be signed in to change notification settings - Fork 1
/
buddy.h
47 lines (38 loc) · 1.04 KB
/
buddy.h
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
40
41
42
43
44
45
46
47
#ifndef BUDDY_H
#define BUDDY_H
#include <sys/queue.h>
/**
* @file buddy.h
* @brief Buddy-system memory allocator
*/
typedef enum purpose { FREE, USED } purpose_t;
#define BUDDY_ALIGN_PREF (32 - 2 * sizeof(void*) - sizeof(uint32_t) - sizeof(purpose_t))
/**
* Metadata about this particular block
* (and stored at the beginning of this block).
* One per allocated block of memory.
* Should be 32 bytes to not screw up alignment.
*/
typedef struct buddy_list
{
// Should be two pointers
LIST_ENTRY(buddy_list) next_freelist;
uint32_t size;
purpose_t use;
char padding[BUDDY_ALIGN_PREF];
} buddy_list_t;
typedef enum valid { VALID, INVALID } valid_t;
/**
* Bucket of 2^order sized free memory blocks.
*/
typedef struct buddy_list_bucket
{
LIST_HEAD(buddy_list_head, buddy_list) ptr;
unsigned int count;
unsigned int order;
valid_t is_valid;
} buddy_list_bucket_t;
buddy_list_bucket_t * create_buddy_table(unsigned int power_of_two);
void *buddy_alloc(unsigned size);
void buddy_free(void *ptr);
#endif /* BUDDY_H */