blob: 4c55ae421048be34cf1fd780cec6319c87d08c7d (
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
40
41
42
43
44
45
46
47
48
49
50
|
#ifndef HEAP_H
#define HEAP_H
#include <stddef.h>
#include <toxic/config.h>
/*
* Heap table consists of an array of 1-byte long entries describing
* each PAGE_SIZE region inside the heap.
*/
/*
* Each entry in the heap table is 1byte long, where:
*
* bit 7: Set when next page in this heap belongs to the same allocation
* bit 6: Set when the current page in the first in this allocation map
* bit 5-1: Reserved
* bit 0: Used flag
*/
#define HEAP_ENTRY_INUSE 0x1
#define HEAP_ENTRY_FREE 0x0
#define HEAP_ENTRY_HAS_NEXT 0x80 /* 0b1000000 */
#define HEAP_ENTRY_IS_FIRST 0x40 /* 0b01000000 */
#define HEAP_ENTRY_USED_MASK ((unsigned char) 0x01)
/*
* Define a macro for entry size so in future
* I can turn this into a proper structure
*/
#define HEAP_ENTRY_SIZE (sizeof(unsigned char))
struct heap_table {
unsigned char *entries;
size_t count;
};
/* Heap descriptor */
struct heap {
struct heap_table *table;
void *start_addr;
};
int heap_create(struct heap *heap, void *start, void *end,
struct heap_table *tbl);
void * heap_malloc(struct heap *heap, size_t size);
void heap_free(struct heap *heap, void *ptr);
#endif /* HEAP_H */
|