-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskunk_runtime.c
More file actions
153 lines (131 loc) · 3.85 KB
/
skunk_runtime.c
File metadata and controls
153 lines (131 loc) · 3.85 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <stdint.h>
#include <stdlib.h>
typedef struct SkunkArenaNode {
void *memory;
struct SkunkArenaNode *next;
} SkunkArenaNode;
typedef struct SkunkAllocator SkunkAllocator;
typedef struct SkunkArena {
SkunkArenaNode *head;
SkunkAllocator *allocator;
} SkunkArena;
struct SkunkAllocator {
int kind;
void *state;
};
enum {
SKUNK_ALLOC_SYSTEM = 0,
SKUNK_ALLOC_ARENA = 1,
};
static SkunkAllocator skunk_global_allocator = {SKUNK_ALLOC_SYSTEM, NULL};
static void *skunk_zero_alloc(size_t size) {
if (size == 0) {
size = 1;
}
return calloc(1, size);
}
void *skunk_system_allocator(void) {
return &skunk_global_allocator;
}
void *skunk_arena_init(void *backing_allocator) {
(void)backing_allocator;
SkunkArena *arena = (SkunkArena *)calloc(1, sizeof(SkunkArena));
SkunkAllocator *allocator = (SkunkAllocator *)calloc(1, sizeof(SkunkAllocator));
allocator->kind = SKUNK_ALLOC_ARENA;
allocator->state = arena;
arena->allocator = allocator;
return arena;
}
void *skunk_arena_allocator(void *arena_ptr) {
SkunkArena *arena = (SkunkArena *)arena_ptr;
if (arena == NULL) {
return NULL;
}
return arena->allocator;
}
void skunk_arena_reset(void *arena_ptr) {
SkunkArena *arena = (SkunkArena *)arena_ptr;
if (arena == NULL) {
return;
}
SkunkArenaNode *node = arena->head;
while (node != NULL) {
SkunkArenaNode *next = node->next;
free(node->memory);
free(node);
node = next;
}
arena->head = NULL;
}
void skunk_arena_deinit(void *arena_ptr) {
SkunkArena *arena = (SkunkArena *)arena_ptr;
if (arena == NULL) {
return;
}
skunk_arena_reset(arena_ptr);
free(arena->allocator);
free(arena);
}
static void *skunk_alloc_impl(void *allocator_ptr, size_t size) {
SkunkAllocator *allocator = (SkunkAllocator *)allocator_ptr;
if (allocator == NULL || allocator->kind == SKUNK_ALLOC_SYSTEM) {
return skunk_zero_alloc(size);
}
if (allocator->kind == SKUNK_ALLOC_ARENA) {
SkunkArena *arena = (SkunkArena *)allocator->state;
void *memory = skunk_zero_alloc(size);
SkunkArenaNode *node = (SkunkArenaNode *)calloc(1, sizeof(SkunkArenaNode));
node->memory = memory;
node->next = arena->head;
arena->head = node;
return memory;
}
return skunk_zero_alloc(size);
}
static int skunk_arena_release_node(SkunkArena *arena, void *memory) {
if (arena == NULL || memory == NULL) {
return 0;
}
SkunkArenaNode *previous = NULL;
SkunkArenaNode *node = arena->head;
while (node != NULL) {
if (node->memory == memory) {
if (previous == NULL) {
arena->head = node->next;
} else {
previous->next = node->next;
}
free(node);
return 1;
}
previous = node;
node = node->next;
}
return 0;
}
void *skunk_alloc_create(void *allocator_ptr, uint64_t size) {
return skunk_alloc_impl(allocator_ptr, (size_t)size);
}
void *skunk_alloc_buffer(void *allocator_ptr, uint64_t elem_size, int32_t len) {
size_t count = len < 0 ? 0 : (size_t)len;
return skunk_alloc_impl(allocator_ptr, (size_t)elem_size * count);
}
void skunk_alloc_destroy(void *allocator_ptr, void *memory) {
SkunkAllocator *allocator = (SkunkAllocator *)allocator_ptr;
if (memory == NULL) {
return;
}
if (allocator == NULL || allocator->kind == SKUNK_ALLOC_SYSTEM) {
free(memory);
return;
}
if (allocator->kind == SKUNK_ALLOC_ARENA) {
SkunkArena *arena = (SkunkArena *)allocator->state;
if (skunk_arena_release_node(arena, memory)) {
free(memory);
}
}
}
void skunk_alloc_free(void *allocator_ptr, void *memory) {
skunk_alloc_destroy(allocator_ptr, memory);
}