-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ce-heap.h
130 lines (89 loc) · 3.07 KB
/
ce-heap.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#pragma once
#include <ce-base.h>
#include <ce-macros.h>
CE_HEADER_BEGIN
#define CE_HEAP_MAGIC 0xc0c0c0c0c0c0c0c0
#define CE_HEAP_DEAD 0xdeaddeaddeaddead
#define CE_HEAP_ALIGN (64)
#define CE_HEAP_PAGE_SIZE (4096)
#define CE_HEAP_MIN_REQU (4096 * 4)
#define CE_HEAP_ALIGNED(X) (((X) + (CE_HEAP_ALIGN - 1)) & ~(CE_HEAP_ALIGN - 1))
#define CE_HEAP_PAGE_ALIGNED(X) \
(((X) + (CE_HEAP_PAGE_SIZE - 1)) & ~(CE_HEAP_PAGE_SIZE - 1))
typedef struct ce_heap_node ce_heap_node;
typedef struct ce_heap_major ce_heap_major;
typedef struct ce_heap_minor ce_heap_minor;
struct ce_heap_node
{
union
{
ce_u64 magic;
ce_u8 m[8];
};
ce_heap_node *prev;
ce_heap_node *next;
};
#define CE_HEAP_NODE(T) \
union \
{ \
ce_heap_node base; \
struct \
{ \
ce_u64 magic; \
T *prev; \
T *next; \
}; \
}
struct ce_heap_major
{
CE_HEAP_NODE(ce_heap_major);
ce_usize size;
ce_usize used;
ce_heap_minor *minor;
};
struct ce_heap_minor
{
CE_HEAP_NODE(ce_heap_minor);
ce_usize size;
ce_usize used;
ce_heap_major *major;
};
typedef void *ce_heap_alloc_fn(void *ctx, ce_usize size);
typedef void ce_heap_free_fn(void *ctx, void *ptr, ce_usize size);
typedef struct
{
void *ctx;
ce_heap_alloc_fn *alloc;
ce_heap_free_fn *free;
ce_heap_major *root;
ce_heap_major *best;
} ce_heap;
/* ---- Internal functions -------------------------------------------------- */
/* Heap hook functions */
void *ce_heap_alloc_block(ce_heap *heap, ce_usize size);
void ce_heap_free_block(ce_heap *heap, void *ptr, ce_usize size);
/* Heap node functions */
bool ce_heap_node_check(ce_heap *heap, ce_heap_node *node);
void ce_heap_node_append(ce_heap_node *node, ce_heap_node *other);
void ce_heap_node_prepend(ce_heap_node *node, ce_heap_node *other);
void ce_heap_node_remove(ce_heap_node *node);
/* Heap major functions */
ce_usize ce_heap_major_avail(ce_heap_major *maj);
ce_heap_major *ce_heap_major_create(ce_heap *heap, ce_usize size);
ce_heap_minor *ce_heap_major_alloc(ce_heap *heap, ce_heap_major *maj,
ce_usize size);
void ce_heap_major_free(ce_heap *heap, ce_heap_major *maj);
/* Heap minor functions */
ce_usize ce_heap_minor_avail(ce_heap_minor *min);
ce_heap_minor *ce_heap_minor_create(ce_heap_major *maj, ce_usize size);
ce_heap_minor *ce_heap_minor_split(ce_heap_minor *min, ce_usize size);
void ce_heap_minor_free(ce_heap *heap, ce_heap_minor *min);
void ce_heap_minor_resize(ce_heap_minor *min, ce_usize size);
ce_heap_minor *ce_heap_minor_from(void *ptr);
void *ce_heap_minor_to(ce_heap_minor *min);
/* ---- Public functions ---------------------------------------------------- */
void *ce_heap_alloc(ce_heap *heap, ce_usize size);
void *ce_heap_realloc(ce_heap *heap, void *ptr, ce_usize size);
void *ce_heap_calloc(ce_heap *heap, ce_usize num, ce_usize size);
void ce_heap_free(ce_heap *heap, void *ptr);
CE_HEADER_END