forked from RandyGaul/cute_headers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyalloc.h
321 lines (262 loc) · 8.27 KB
/
tinyalloc.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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
tinyalloc.h - v1.01
To create implementation (the function definitions)
#define TINYALLOC_IMPLEMENTATION
in *one* C/CPP file (translation unit) that includes this file
SUMMARY:
This header contains a collection of allocators. None of the allocators are
very fancy, and all have particular limitations making them useful only in
specific scenarios. None of the allocators do any special alignment support.
There are a few kinds:
1. taStack - stack based allocator
2. taFrame - frame based scratch allocator
3. TINYALLOC_ALLOC/TINYALLOC_FREE - malloc/free leak checker
And here are their descriptions:
1. Given an initial chunk of memory, implements a stack based allocator inside
the chunk. Each allocation is laid contiguously after the last. Deallocation must
occur in *reverse* order to allocation. Mostly useful for graph traversals.
2. Given an initial chunk of memory, acts like the stack allocator, but only
implements a single deallocation function. Upon deallocation, the entire stack is
cleared, typically by moving a single pointer. Mostly useful for loading level
assets, or for quick "temporary scratch-space" in the middle of an algorithm.
3. Thin wrapper around malloc/free for recording and reporting memory leaks. Call
TINYALLOC_CHECK_FOR_LEAKS to find and printf any un-free'd memory. Define TINYALLOC_LEAK_CHECK
to 0 in order to turn of leak checking and use raw malloc/free.
Revision history:
1.0 (11/01/2017) initial release
1.01 (11/11/2017) added leak checker for malloc/free
*/
#if !defined(TINYALLOC_H)
typedef struct taStack taStack;
taStack* taStackCreate(void* memory_chunk, size_t size);
void* taStackAlloc(taStack* stack, size_t size);
int taStackFree(taStack* stack, void* memory);
size_t taStackBytesLeft(taStack* stack);
typedef struct taFrame taFrame;
taFrame* taFrameCreate(void* memory_chunk, size_t size);
void* taFrameAlloc(taFrame* frame, size_t size);
void taFrameFree(taFrame* frame);
// define these to your own user definition as necessary
#if !defined(TINYALLOC_ALLOC)
#define TINYALLOC_ALLOC(size, ctx) taLeakCheckAlloc((size), (char*)__FILE__, __LINE__)
#endif
#if !defined(TINYALLOC_FREE)
#define TINYALLOC_FREE(mem, ctx) taLeakCheckFree(mem)
#endif
#if !defined(TINYALLOC_CALLOC)
#define TINYALLOC_CALLOC(count, elementSize, ctx) taLeakCheckCalloc(count, elementSize, (char*)__FILE__, __LINE__)
#endif
// 1 - use the leak checker
// 0 - use plain malloc/free
#define TINYALLOC_LEAK_CHECK 1
void* taLeakCheckAlloc(size_t size, char* file, int line);
void* taLeakCheckCalloc(size_t count, size_t elementSize, char* file, int line);
void taLeakCheckFree(void* mem);
int TINYALLOC_CHECK_FOR_LEAKS();
int TINYALLOC_BYTES_IN_USE();
// define these to your own user definition as necessary
#if !defined(TINYALLOC_MALLOC_FUNC)
#define TINYALLOC_MALLOC_FUNC malloc
#endif
#if !defined(TINYALLOC_FREE_FUNC)
#define TINYALLOC_FREE_FUNC free
#endif
#if !defined(TINYALLOC_CALLOC_FUNC)
#define TINYALLOC_CALLOC_FUNC calloc
#endif
#define TINYALLOC_H
#endif
#ifdef TINYALLOC_IMPLEMENTATION
#ifndef TINYALLOC_IMPLEMENTATION_ONCE
#define TINYALLOC_IMPLEMENTATION_ONCE
struct taStack
{
void* memory;
size_t capacity;
size_t bytes_left;
};
#define TINYALLOC_PTR_ADD(ptr, size) ((void*)(((char*)ptr) + (size)))
#define TINYALLOC_PTR_SUB(ptr, size) ((void*)(((char*)ptr) - (size)))
taStack* taStackCreate(void* memory_chunk, size_t size)
{
taStack* stack = (taStack*)memory_chunk;
if (size < sizeof(taStack)) return 0;
*(size_t*)TINYALLOC_PTR_ADD(memory_chunk, sizeof(taStack)) = 0;
stack->memory = TINYALLOC_PTR_ADD(memory_chunk, sizeof(taStack) + sizeof(size_t));
stack->capacity = size - sizeof(taStack) - sizeof(size_t);
stack->bytes_left = stack->capacity;
return stack;
}
void* taStackAlloc(taStack* stack, size_t size)
{
if (stack->bytes_left - sizeof(size_t) < size) return 0;
void* user_mem = stack->memory;
*(size_t*)TINYALLOC_PTR_ADD(user_mem, size) = size;
stack->memory = TINYALLOC_PTR_ADD(user_mem, size + sizeof(size_t));
stack->bytes_left -= sizeof(size_t) + size;
return user_mem;
}
int taStackFree(taStack* stack, void* memory)
{
if (!memory) return 0;
size_t size = *(size_t*)TINYALLOC_PTR_SUB(stack->memory, sizeof(size_t));
void* prev = TINYALLOC_PTR_SUB(stack->memory, size + sizeof(size_t));
if (prev != memory) return 0;
stack->memory = prev;
stack->bytes_left += sizeof(size_t) + size;
return 1;
}
size_t taStackBytesLeft(taStack* stack)
{
return stack->bytes_left;
}
struct taFrame
{
void* original;
void* ptr;
size_t capacity;
size_t bytes_left;
};
taFrame* taFrameCreate(void* memory_chunk, size_t size)
{
taFrame* frame = (taFrame*)memory_chunk;
frame->original = TINYALLOC_PTR_ADD(memory_chunk, sizeof(taFrame));
frame->ptr = frame->original;
frame->capacity = frame->bytes_left = size - sizeof(taFrame);
return frame;
}
void* taFrameAlloc(taFrame* frame, size_t size)
{
if (frame->bytes_left < size) return 0;
void* user_mem = frame->ptr;
frame->ptr = TINYALLOC_PTR_ADD(frame->ptr, size);
frame->bytes_left -= size;
return user_mem;
}
void taFrameFree(taFrame* frame)
{
frame->ptr = frame->original;
frame->bytes_left = frame->capacity;
}
#include <stdlib.h> // malloc, free
#if TINYALLOC_LEAK_CHECK
#include <stdio.h>
typedef struct taAllocInfo taAllocInfo;
struct taAllocInfo
{
char* file;
size_t size;
int line;
struct taAllocInfo* next;
struct taAllocInfo* prev;
};
static taAllocInfo* taAllocHead()
{
static taAllocInfo info;
static int init;
if (!init)
{
info.next = &info;
info.prev = &info;
init = 1;
}
return &info;
}
void* taLeakCheckAlloc(size_t size, char* file, int line)
{
taAllocInfo* mem = (taAllocInfo*)TINYALLOC_MALLOC_FUNC(sizeof(taAllocInfo) + size);
if (!mem) return 0;
mem->file = file;
mem->line = line;
mem->size = size;
taAllocInfo* head = taAllocHead();
mem->prev = head;
mem->next = head->next;
head->next->prev = mem;
head->next = mem;
return mem + 1;
}
#if !defined(TINYALLOC_MEMSET)
#include <string.h> // memset
#define TINYALLOC_MEMSET memset
#endif
void* taLeakCheckCalloc(size_t count, size_t elementSize, char* file, int line)
{
size_t size = count * elementSize;
void* mem = taLeakCheckAlloc(size, file, line);
TINYALLOC_MEMSET(mem, 0, size);
return mem;
}
void taLeakCheckFree(void* mem)
{
if (!mem) return;
taAllocInfo* info = (taAllocInfo*)mem - 1;
info->prev->next = info->next;
info->next->prev = info->prev;
TINYALLOC_FREE_FUNC(info);
}
int TINYALLOC_CHECK_FOR_LEAKS()
{
taAllocInfo* head = taAllocHead();
taAllocInfo* next = head->next;
int leaks = 0;
while (next != head)
{
printf("LEAKED %llu bytes from file \"%s\" at line %d from address %p.\n", next->size, next->file, next->line, next + 1);
next = next->next;
leaks = 1;
}
if (leaks) printf("WARNING: Memory leaks detected (see above).\n");
else printf("SUCCESS: No memory leaks detected.\n");
return leaks;
}
int TINYALLOC_BYTES_IN_USE()
{
taAllocInfo* head = taAllocHead();
taAllocInfo* next = head->next;
int bytes = 0;
while (next != head)
{
bytes += (int)next->size;
next = next->next;
}
return bytes;
}
#else
#if !defined(TINYALLOC_UNUSED)
#if defined(_MSC_VER)
#define TINYALLOC_UNUSED(x) (void)x
#else
#define TINYALLOC_UNUSED(x) (void)(sizeof(x))
#endif
#endif
inline void* taLeakCheckAlloc(size_t size, char* file, int line)
{
TINYALLOC_UNUSED(file);
TINYALLOC_UNUSED(line);
return TINYALLOC_MALLOC_FUNC(size);
}
void* taLeakCheckCalloc(size_t count, size_t elementSize, char* file, int line)
{
TINYALLOC_UNUSED(file);
TINYALLOC_UNUSED(line);
return TINYALLOC_CALLOC_FUNC(count, size);
}
inline void taLeakCheckFree(void* mem)
{
return TINYALLOC_FREE_FUNC(mem);
}
inline int TINYALLOC_CHECK_FOR_LEAKS() { return 0; }
inline int TINYALLOC_BYTES_IN_USE() { return 0; }
#endif // TINYALLOC_LEAK_CHECK
#endif // TINYALLOC_IMPLEMENTATION_ONCE
#endif // TINYALLOC_IMPLEMENTATION
/*
This is free and unencumbered software released into the public domain.
Our intent is that anyone is free to copy and use this software,
for any purpose, in any form, and by any means.
The authors dedicate any and all copyright interest in the software
to the public domain, at their own expense for the betterment of mankind.
The software is provided "as is", without any kind of warranty, including
any implied warranty. If it breaks, you get to keep both pieces.
*/