-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzmalloc.c
More file actions
301 lines (242 loc) · 8.56 KB
/
zmalloc.c
File metadata and controls
301 lines (242 loc) · 8.56 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* ZMALLOC - Dynamic Memory Allocation for Small Memory Systems
*
* Copyright © 2024 by Ivo van Poorten
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* See also:
* Dynamic Storage Allocation: A Survey and Critical Review
* by Paul R. Wilson, Mark S. Johnstone, Michael Neely, and David Boles
* International Workshop on Memory Management, September 1995
* ftp://ftp.cs.utexas.edu/pub/garbage/allocsrv.ps
*/
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
struct block_info;
struct block_info {
uintptr_t size;
struct block_info *prev;
struct block_info *next;
};
static void *base;
static struct block_info *free_list;
static const uintptr_t minfreeblocksize = sizeof(struct block_info);
#define is_free(x) (!((x)& 1))
#define is_inuse(x) ((x)& 1)
#define set_free(x) ((x)&~1)
#define set_inuse(x) ((x)| 1)
#define get_size(x) ((x)&-sizeof(uintptr_t))
static inline void *align_up(void *p, int to) {
return (void *)(((uintptr_t) p + to - 1) & -to);
}
bool zmalloc_init(void *start, size_t size) {
static_assert(sizeof(uintptr_t) == sizeof(void *), "uintptr_t mismatch");
base = align_up(start, sizeof(uintptr_t)); // align our memory pool
size -= base - start; // adjust size for skipped mememory
size &= -sizeof(uintptr_t); // reduce size to multiple of alignment
if (size < 3 * sizeof(struct block_info)) return false;
// setup sentinels, size is 0, prev = next = NULL
struct block_info *begin = base;
struct block_info *end = base + size - sizeof(struct block_info);
memset(begin, 0, sizeof(struct block_info));
memset(end, 0, sizeof(struct block_info));
size -= 2 * sizeof(struct block_info); // subtract from available memory
// one free block
struct block_info *freeb = base + sizeof(struct block_info);
freeb->size = size;
// setup free_list as begin <-> freeb <-> end
freeb->prev = free_list = begin;
freeb->next = end;
begin->next = end->prev = freeb;
return true;
}
static size_t size_requirements(size_t size) {
size += sizeof(uintptr_t);
size = (size_t) align_up((void *)size, sizeof(uintptr_t));
if (size < sizeof(struct block_info))
size = sizeof(struct block_info);
return size;
}
void *zmalloc(size_t size) {
if (!size) {
errno = ENOMEM;
return NULL;
}
size = size_requirements(size);
struct block_info *p = free_list->next; // skip sentinel
while (p && p->size < size)
p = p->next;
if (!p) {
errno = ENOMEM;
return NULL;
}
if (p->size - size > minfreeblocksize) { // split
struct block_info *freeb = (void *) p + size;
freeb->size = p->size - size;
// link free block into free_list in place of p
freeb->prev = p->prev;
freeb->next = p->next;
freeb->prev->next = freeb;
freeb->next->prev = freeb;
// reduce alloc size to size
p->size = size;
} else { // take full block
// unlink from free_list
p->prev->next = p->next;
p->next->prev = p->prev;
}
p->size = set_inuse(p->size);
return (void *) p + sizeof(uintptr_t);
}
void *zcalloc(size_t nmemb, size_t size) {
if (nmemb && size > (size_t)-1/nmemb) { // check overflow of multiplication
errno = ENOMEM;
return 0;
}
size_t nsize = nmemb * size;
void *p = zmalloc(nsize);
if (p) memset(p, 0, nsize);
return p;
}
static void link_to_free_list(struct block_info *p) {
struct block_info *nextb = (void *) p + p->size;
if (is_free(nextb->size)) { // merge with next block, link to free_list
p->next = nextb->next;
p->prev = nextb->prev;
p->next->prev = p;
p->prev->next = p;
p->size += nextb->size;
} else { // find next free block in memory, and link
nextb = (void *) p + p->size;
while (is_inuse(nextb->size))
nextb = (void *) nextb + get_size(nextb->size);
p->prev = nextb->prev;
p->prev->next = p;
p->next = nextb;
nextb->prev = p;
}
}
void __zfree_null(void);
void zfree(void *ptr) {
if (!ptr) {
__zfree_null();
return;
}
struct block_info *p = ptr - sizeof(uintptr_t);
p->size = set_free(p->size);
link_to_free_list(p);
// if previous block is adjacent, merge
struct block_info *prev = p->prev;
if ((void *) prev + prev->size == p) {
prev->next = p->next;
prev->next->prev = prev;
prev->size += p->size;
}
}
void *zrealloc(void *ptr, size_t size) {
if (!ptr)
return zmalloc(size);
if (!size) {
zfree(ptr);
return NULL;
}
size = size_requirements(size);
struct block_info *p = ptr - sizeof(uintptr_t);
if (size > get_size(p->size)) { // bigger
void *q = zmalloc(size);
if (!q) return NULL;
memcpy(q, ptr, get_size(p->size));
zfree(ptr);
return q;
} else if (get_size(p->size) >= size + minfreeblocksize) { // split
struct block_info *q = (void *) p + size;
q->size = get_size(p->size) - size;
link_to_free_list(q);
p->size = set_inuse(size);
}
return ptr;
}
static void *__zmemalign(uintptr_t alignment, uintptr_t size) {
if ((alignment & -alignment) != alignment) {
errno = EINVAL;
return NULL;
}
if (alignment <= sizeof(uintptr_t)) return zmalloc(size);
uintptr_t worst_padding = sizeof(minfreeblocksize) + \
sizeof(uintptr_t) + (alignment - 1);
if (size > SIZE_MAX - worst_padding) {
errno = ENOMEM;
return NULL;
}
struct block_info *freeb = zmalloc(size + worst_padding)-sizeof(uintptr_t);
void *end = (void *) freeb + get_size(freeb->size);
// freshly allocated, so prev is still valid (but next is not(!))
struct block_info *prev = freeb->prev;
struct block_info *next = prev->next;
void *tmp = (void *) freeb + minfreeblocksize + sizeof(uintptr_t);
struct block_info *p = align_up(tmp, alignment) - sizeof(uintptr_t);
freeb->size = (void *) p - (void *) freeb;
// insert back into free_list, coalescing is impossible
prev->next = next->prev = freeb;
freeb->prev = prev;
freeb->next = next;
p->size = set_inuse(end - (void *) p);
return (void *) p + sizeof(uintptr_t);
}
void *zaligned_alloc(size_t alignment, size_t size) {
return __zmemalign(alignment, size);
}
int zposix_memalign(void **memptr, size_t alignment, size_t size) {
if (alignment < sizeof(uintptr_t)) return EINVAL;
void *mem = __zmemalign(alignment, size);
if (!mem) return errno;
*memptr = mem;
return 0;
}
// ----------------------------------------------------------------------------
#ifdef ZMALLOC_DEBUG
#include <stdio.h>
void print_memory(void) {
int sentinels = 2;
unsigned long int total = 0, count = 0;
struct block_info *p = base, *q;
printf("Traversing whole memory pool:\n");
while (sentinels) {
if (!p->size) {
printf("Sentinel found\n");
sentinels--;
p = (void *) p + sizeof(struct block_info);
total += sizeof(struct block_info);
} else {
printf("Block %ld: size = %ld, %s\n", count++, get_size(p->size),
is_inuse(p->size) ? "used" : "free");
total += get_size(p->size);
p = (void *) p + get_size(p->size);
}
}
printf("TOTAL: %ld\n", total);
puts("--------------------------------");
printf("Traversing free_list forward:\n");
count = 0;
for (p = free_list; p; q = p, p = p->next)
printf("Block %ld: size = %ld\n", count++, p->size);
printf("traversing free_list backwards:\n");
for (p = q; p; p = p->prev)
printf("block %ld: size = %ld\n", --count, p->size);
puts("################################");
}
#endif