memory related functions
[vg.git] / vg_mem.h
1 #ifndef VG_MEM_H
2 #define VG_MEM_H
3
4 #include "vg.h"
5 #include "vg_stdint.h"
6 #include "vg_platform.h"
7
8 #include <stdlib.h>
9 #include <malloc.h>
10
11 VG_STATIC void vg_print_backtrace(void);
12
13 #define VG_MAX_ALLOCATIONS 128
14 #define VG_FUZZ_ALLOCATIONS
15
16 typedef struct vg_linear_allocator vg_linear_allocator;
17 typedef struct vg_allocation_meta vg_allocation_meta;
18
19 struct
20 {
21 void *rtmemory,
22 *scratch;
23
24 int use_libc_malloc;
25 u32 quota;
26 }
27 static vg_mem;
28
29 struct vg_allocation_meta
30 {
31 void *data;
32 enum allocation_type
33 {
34 k_allocation_type_block = 0,
35 k_allocation_type_linear = 1
36 }
37 type;
38 };
39
40 #define VG_MEMORY_SYSTEM 0x1 /* systems memory, slow and low counts */
41 #define VG_MEMORY_REALTIME 0x2 /* per-frame. no max allocs, only size. fast */
42
43 /* systems memory cannot be declared inside realtime memory regions */
44
45 /*
46 * Stored just behind the array. 32 bytes.
47 */
48 #pragma pack(push,1)
49 struct vg_linear_allocator
50 {
51 u32 size;
52 /* */
53 /* */
54 /* */
55 u32 cur;
56 /* */
57 /* */
58 /* */
59 u16 allocation_count;
60 /* */
61 u16 flags;
62 /* */
63 u32 last_alloc_size;
64 /* */
65 /* */
66 /* */
67 void *last_alloc;
68 /* */
69 /* */
70 /* */
71 /* */
72 /* */
73 /* */
74 /* */
75 vg_allocation_meta *alloc_table;
76 /* */
77 /* */
78 /* */
79 /* */
80 /* */
81 /* */
82 /* */
83
84 #ifdef _WIN32
85 /* 32 bit pointers! */
86 u8 padding[ 8 ];
87 #endif
88 };
89 #pragma pack(pop)
90
91 VG_STATIC void vg_fatal_exit_loop( const char *error );
92 VG_STATIC void vg_error(const char *fmt, ...);
93 VG_STATIC void vg_info(const char *fmt, ...);
94
95 VG_STATIC u32 vg_align8( u32 s )
96 {
97 u32 m = (s + 7) >> 3;
98 return m << 3;
99 }
100
101 VG_STATIC u32 vg_align4( u32 s )
102 {
103 u32 m = (s + 3) >> 2;
104 return m << 2;
105 }
106
107 /* Returns allocator structure from data pointer */
108 static vg_linear_allocator *vg_linear_header( void *data )
109 {
110 vg_linear_allocator *ptr = data;
111 ptr --;
112
113 return ptr;
114 }
115
116 /* allocate something from a linear allocator */
117 __attribute__((warn_unused_result))
118 VG_STATIC void *vg_linear_alloc( void *buffer, u32 size )
119 {
120 if( size % 8 ){
121 vg_error( "alloc(%u) is not 8 byte aligned\n", size );
122 vg_print_backtrace();
123 size = vg_align8( size );
124 }
125 if( ((u64)buffer) % 8 ){
126 vg_error( "buffer: %p\n", buffer );
127 vg_fatal_exit_loop( "unaligned buffer" );
128 }
129
130 vg_linear_allocator *alloc = vg_linear_header( buffer );
131
132 if( (alloc->cur + size) > alloc->size ){
133 vg_error( "%u + %u > %u\n", alloc->cur, size, alloc->size );
134 vg_fatal_exit_loop( "linear allocator overflow" );
135 }
136
137 if( alloc->flags & VG_MEMORY_SYSTEM )
138 if( (alloc->allocation_count + 1) > VG_MAX_ALLOCATIONS )
139 vg_fatal_exit_loop( "Max linear allocations reached" );
140
141 void *data;
142
143 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
144 data = malloc( size );
145
146 vg_allocation_meta *meta = &alloc->alloc_table[ alloc->allocation_count ];
147 meta->type = k_allocation_type_block;
148 meta->data = data;
149 }
150 else{
151 data = buffer + alloc->cur;
152 }
153
154 u8 *bytes = data;
155 for( u32 i=0; i<size; i++ ){
156 bytes[i] = 0xae;
157 }
158
159 alloc->allocation_count ++;
160 alloc->last_alloc = data;
161 alloc->last_alloc_size = size;
162 alloc->cur += size;
163
164 if( ((u64)data) % 8 ){
165 vg_fatal_exit_loop( "unaligned" );
166 }
167
168 return data;
169 }
170
171 /* resize latest block of memory from linear */
172 __attribute__((warn_unused_result))
173 VG_STATIC void *vg_linear_resize( void *buffer, void *data, u32 newsize )
174 {
175 vg_linear_allocator *alloc = vg_linear_header( buffer );
176
177 if( newsize % 8 ){
178 vg_error( "alloc(%u) is not 8 byte aligned\n", newsize );
179 vg_print_backtrace();
180 newsize = vg_align8( newsize );
181 }
182
183 if( alloc->last_alloc != data )
184 vg_fatal_exit_loop( "This block has been fixed!" );
185
186 if( (alloc->cur - alloc->last_alloc_size + newsize) > alloc->size )
187 vg_fatal_exit_loop( "Cannot resize, overflow" );
188
189 alloc->cur -= alloc->last_alloc_size;
190 alloc->cur += newsize;
191 alloc->last_alloc_size = newsize;
192
193 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
194 data = realloc( data, newsize );
195 if( !data )
196 vg_fatal_exit_loop( "realloc failed" );
197
198 alloc->alloc_table[ alloc->allocation_count-1 ].data = data;
199 alloc->last_alloc = data;
200 return data;
201 }
202 else{
203 return data;
204 }
205 }
206
207 /* its possible to delete just the last item */
208 VG_STATIC void vg_linear_del( void *buffer, void *data )
209 {
210 vg_linear_allocator *alloc = vg_linear_header( buffer );
211
212 if( alloc->last_alloc != data )
213 vg_fatal_exit_loop( "This block has been fixed!" );
214
215 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
216 vg_allocation_meta *meta = &alloc->alloc_table[alloc->allocation_count-1];
217 if( meta->type == k_allocation_type_linear )
218 vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
219
220 free( data );
221 }
222
223 alloc->allocation_count --;
224 alloc->cur -= alloc->last_alloc_size;
225 alloc->last_alloc = NULL;
226 alloc->last_alloc_size = 0;
227 }
228
229 /* extend latest block of memory from linear */
230 __attribute__((warn_unused_result))
231 VG_STATIC void *vg_linear_extend( void *buffer, void *data, u32 extra )
232 {
233 if( !data )
234 return vg_linear_alloc( buffer, extra );
235
236 vg_linear_allocator *alloc = vg_linear_header( buffer );
237
238 if( alloc->last_alloc != data )
239 vg_fatal_exit_loop( "This block has been fixed!" );
240
241 u32 new_size = alloc->last_alloc_size + extra;
242 return vg_linear_resize( buffer, data, new_size );
243 }
244
245 /* get the current usage of allocator */
246 VG_STATIC u32 vg_linear_get_cur( void *buffer )
247 {
248 vg_linear_allocator *alloc = vg_linear_header( buffer );
249 return alloc->cur;
250 }
251
252 /* get the capacity of allocator. */
253 VG_STATIC u32 vg_linear_get_capacity( void *buffer )
254 {
255 vg_linear_allocator *alloc = vg_linear_header( buffer );
256 return alloc->size;
257 }
258
259 /* get the remaining size of the allocator */
260 VG_STATIC u32 vg_linear_remaining( void *buffer )
261 {
262 vg_linear_allocator *alloc = vg_linear_header( buffer );
263 return alloc->size - alloc->cur;
264 }
265
266 /* yeet all memory from linear allocator */
267 VG_STATIC void vg_linear_clear( void *buffer )
268 {
269 vg_linear_allocator *alloc = vg_linear_header( buffer );
270
271 /* libc mode we recursively free any allocations made */
272 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
273 for( u32 i=0; i<alloc->allocation_count; i++ ){
274 vg_allocation_meta *meta = &alloc->alloc_table[i];
275
276 if( meta->type == k_allocation_type_block ){
277 free( meta->data );
278 }
279 else{
280 vg_linear_clear( meta->data );
281 vg_linear_allocator *sub = vg_linear_header( meta->data );
282
283 free( sub->alloc_table );
284 free( sub );
285 }
286 }
287 }
288
289 alloc->last_alloc = NULL;
290 alloc->last_alloc_size = 0;
291 alloc->allocation_count = 0;
292 alloc->cur = 0;
293 }
294
295 /* allocate a FIXED SIZE linear allocator */
296 VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
297 u16 flags )
298 {
299 assert( sizeof( vg_linear_allocator ) == 32 );
300
301 vg_linear_allocator *header;
302 u32 block_size = size + sizeof(vg_linear_allocator);
303
304 /* Creating it inside an existing one */
305 if( lin_alloc ){
306 vg_linear_allocator *alloc = vg_linear_header( lin_alloc );
307
308 if( alloc->cur + block_size > alloc->size )
309 vg_fatal_exit_loop( "Out of memory" );
310
311 if( alloc->allocation_count + 1 > VG_MAX_ALLOCATIONS )
312 vg_fatal_exit_loop( "Max allocations in linear allocator" );
313
314 if( (flags && VG_MEMORY_SYSTEM) && (alloc->flags & VG_MEMORY_REALTIME) )
315 vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
316 " allocator" );
317
318 if( vg_mem.use_libc_malloc ){
319 vg_allocation_meta *meta =
320 &alloc->alloc_table[ alloc->allocation_count ];
321
322 if( flags & VG_MEMORY_REALTIME )
323 header = malloc( block_size );
324 else
325 header = malloc( sizeof(vg_linear_allocator) );
326
327 meta->data = header+1;
328 meta->type = k_allocation_type_linear;
329 }
330 else{
331 header = lin_alloc + alloc->cur;
332 }
333
334 alloc->cur += block_size;
335 alloc->last_alloc = NULL; /* cant resize this block! */
336 alloc->last_alloc_size = block_size;
337
338 alloc->allocation_count ++;
339 }
340 else{
341 if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) )
342 header = malloc( sizeof(vg_linear_allocator) );
343 else
344 header = malloc( block_size );
345 }
346
347 header->allocation_count = 0;
348 header->cur = 0;
349 header->last_alloc = NULL;
350 header->last_alloc_size = 0;
351 header->size = size;
352 header->flags = flags;
353
354 if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) ){
355 u32 table_size = sizeof(vg_allocation_meta)*VG_MAX_ALLOCATIONS;
356 header->alloc_table = malloc( table_size );
357 }
358 else
359 header->alloc_table = NULL;
360
361 return header+1;
362 }
363
364 /* request all the memory we need in advance */
365 VG_STATIC void vg_set_mem_quota( u32 size )
366 {
367 vg_mem.quota = size;
368 }
369
370 VG_STATIC void vg_alloc_quota(void)
371 {
372 u32 size_scratch = 10*1024*1024;
373 u32 size = VG_MAX( vg_mem.quota, size_scratch );
374
375 vg_mem.rtmemory = vg_create_linear_allocator( NULL, size, VG_MEMORY_SYSTEM );
376 vg_mem.scratch = vg_create_linear_allocator( vg_mem.rtmemory,
377 size_scratch,
378 VG_MEMORY_SYSTEM );
379 }
380
381 #endif /* VG_MEM_H */