switch to async system
[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_error( const char *fmt, ... );
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 #ifdef _WIN32
126 if( ((u32)buffer) % 8 ){
127 #else
128 if( ((u64)buffer) % 8 ){
129 #endif
130 vg_fatal_error( "unaligned buffer (%p)", buffer );
131 }
132
133 vg_linear_allocator *alloc = vg_linear_header( buffer );
134
135 if( (alloc->cur + size) > alloc->size ){
136 vg_fatal_error( "linear allocator overflow (%u + %u > %u)\n",
137 alloc->cur, size, alloc->size );
138 }
139
140 if( alloc->flags & VG_MEMORY_SYSTEM )
141 if( (alloc->allocation_count + 1) > VG_MAX_ALLOCATIONS )
142 vg_fatal_error( "Max linear allocations reached" );
143
144 void *data;
145
146 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
147 data = malloc( size );
148
149 vg_allocation_meta *meta = &alloc->alloc_table[ alloc->allocation_count ];
150 meta->type = k_allocation_type_block;
151 meta->data = data;
152 }
153 else{
154 data = buffer + alloc->cur;
155 }
156
157 u8 *bytes = data;
158 for( u32 i=0; i<size; i++ ){
159 bytes[i] = 0xfe;
160 }
161
162 alloc->allocation_count ++;
163 alloc->last_alloc = data;
164 alloc->last_alloc_size = size;
165 alloc->cur += size;
166
167 #ifdef _WIN32
168 if( ((u32)data) % 8 ){
169 #else
170 if( ((u64)data) % 8 ){
171 #endif
172 vg_fatal_error( "unaligned" );
173 }
174
175 return data;
176 }
177
178 /* resize latest block of memory from linear */
179 __attribute__((warn_unused_result))
180 VG_STATIC void *vg_linear_resize( void *buffer, void *data, u32 newsize )
181 {
182 vg_linear_allocator *alloc = vg_linear_header( buffer );
183
184 if( newsize % 8 ){
185 vg_error( "alloc(%u) is not 8 byte aligned\n", newsize );
186 vg_print_backtrace();
187 newsize = vg_align8( newsize );
188 }
189
190 if( alloc->last_alloc != data )
191 vg_fatal_error( "This block has been fixed!" );
192
193 if( (alloc->cur - alloc->last_alloc_size + newsize) > alloc->size )
194 vg_fatal_error( "Cannot resize, overflow" );
195
196 alloc->cur -= alloc->last_alloc_size;
197 alloc->cur += newsize;
198 alloc->last_alloc_size = newsize;
199
200 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
201 data = realloc( data, newsize );
202 if( !data )
203 vg_fatal_error( "realloc failed" );
204
205 alloc->alloc_table[ alloc->allocation_count-1 ].data = data;
206 alloc->last_alloc = data;
207 return data;
208 }
209 else{
210 return data;
211 }
212 }
213
214 /* its possible to delete just the last item */
215 VG_STATIC void vg_linear_del( void *buffer, void *data )
216 {
217 vg_linear_allocator *alloc = vg_linear_header( buffer );
218
219 if( alloc->last_alloc != data )
220 vg_fatal_error( "This block has been fixed!" );
221
222 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
223 vg_allocation_meta *meta = &alloc->alloc_table[alloc->allocation_count-1];
224 if( meta->type == k_allocation_type_linear )
225 vg_fatal_error( "Cannot free a linear allocator in this conext" );
226
227 free( data );
228 }
229
230 alloc->allocation_count --;
231 alloc->cur -= alloc->last_alloc_size;
232 alloc->last_alloc = NULL;
233 alloc->last_alloc_size = 0;
234 }
235
236 /* extend latest block of memory from linear */
237 __attribute__((warn_unused_result))
238 VG_STATIC void *vg_linear_extend( void *buffer, void *data, u32 extra )
239 {
240 if( !data )
241 return vg_linear_alloc( buffer, extra );
242
243 vg_linear_allocator *alloc = vg_linear_header( buffer );
244
245 if( alloc->last_alloc != data )
246 vg_fatal_error( "This block has been fixed!" );
247
248 u32 new_size = alloc->last_alloc_size + extra;
249 return vg_linear_resize( buffer, data, new_size );
250 }
251
252 /* get the current usage of allocator */
253 VG_STATIC u32 vg_linear_get_cur( void *buffer )
254 {
255 vg_linear_allocator *alloc = vg_linear_header( buffer );
256 return alloc->cur;
257 }
258
259 /* get the capacity of allocator. */
260 VG_STATIC u32 vg_linear_get_capacity( void *buffer )
261 {
262 vg_linear_allocator *alloc = vg_linear_header( buffer );
263 return alloc->size;
264 }
265
266 /* get the remaining size of the allocator */
267 VG_STATIC u32 vg_linear_remaining( void *buffer )
268 {
269 vg_linear_allocator *alloc = vg_linear_header( buffer );
270 return alloc->size - alloc->cur;
271 }
272
273 /* yeet all memory from linear allocator */
274 VG_STATIC void vg_linear_clear( void *buffer )
275 {
276 vg_linear_allocator *alloc = vg_linear_header( buffer );
277
278 /* libc mode we recursively free any allocations made */
279 if( vg_mem.use_libc_malloc && (alloc->flags & VG_MEMORY_SYSTEM) ){
280 for( u32 i=0; i<alloc->allocation_count; i++ ){
281 vg_allocation_meta *meta = &alloc->alloc_table[i];
282
283 if( meta->type == k_allocation_type_block ){
284 free( meta->data );
285 }
286 else{
287 vg_linear_clear( meta->data );
288 vg_linear_allocator *sub = vg_linear_header( meta->data );
289
290 free( sub->alloc_table );
291 free( sub );
292 }
293 }
294 }
295
296 alloc->last_alloc = NULL;
297 alloc->last_alloc_size = 0;
298 alloc->allocation_count = 0;
299 alloc->cur = 0;
300 }
301
302 /* allocate a FIXED SIZE linear allocator */
303 VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size,
304 u16 flags )
305 {
306 assert( sizeof( vg_linear_allocator ) == 32 );
307
308 vg_linear_allocator *header;
309 u32 block_size = size + sizeof(vg_linear_allocator);
310
311 /* Creating it inside an existing one */
312 if( lin_alloc ){
313 vg_linear_allocator *alloc = vg_linear_header( lin_alloc );
314
315 if( alloc->cur + block_size > alloc->size )
316 vg_fatal_error( "Out of memory" );
317
318 if( alloc->allocation_count + 1 > VG_MAX_ALLOCATIONS )
319 vg_fatal_error( "Max allocations in linear allocator" );
320
321 if( (flags && VG_MEMORY_SYSTEM) && (alloc->flags & VG_MEMORY_REALTIME) )
322 vg_fatal_error( "Cannot declare realtime allocator inside systems"
323 " allocator" );
324
325 if( vg_mem.use_libc_malloc ){
326 vg_allocation_meta *meta =
327 &alloc->alloc_table[ alloc->allocation_count ];
328
329 if( flags & VG_MEMORY_REALTIME )
330 header = malloc( block_size );
331 else
332 header = malloc( sizeof(vg_linear_allocator) );
333
334 meta->data = header+1;
335 meta->type = k_allocation_type_linear;
336 }
337 else{
338 header = lin_alloc + alloc->cur;
339 }
340
341 alloc->cur += block_size;
342 alloc->last_alloc = NULL; /* cant resize this block! */
343 alloc->last_alloc_size = block_size;
344
345 alloc->allocation_count ++;
346 }
347 else{
348 if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) )
349 header = malloc( sizeof(vg_linear_allocator) );
350 else
351 header = malloc( block_size );
352 }
353
354 header->allocation_count = 0;
355 header->cur = 0;
356 header->last_alloc = NULL;
357 header->last_alloc_size = 0;
358 header->size = size;
359 header->flags = flags;
360
361 if( vg_mem.use_libc_malloc && (flags & VG_MEMORY_SYSTEM) ){
362 u32 table_size = sizeof(vg_allocation_meta)*VG_MAX_ALLOCATIONS;
363 header->alloc_table = malloc( table_size );
364 }
365 else
366 header->alloc_table = NULL;
367
368 return header+1;
369 }
370
371 /* request all the memory we need in advance */
372 VG_STATIC void vg_set_mem_quota( u32 size )
373 {
374 vg_mem.quota = size;
375 }
376
377 VG_STATIC void vg_alloc_quota(void)
378 {
379 u32 size_scratch = 10*1024*1024;
380 u32 size = VG_MAX( vg_mem.quota, size_scratch );
381
382 vg_mem.rtmemory = vg_create_linear_allocator( NULL, size, VG_MEMORY_SYSTEM );
383 vg_mem.scratch = vg_create_linear_allocator( vg_mem.rtmemory,
384 size_scratch,
385 VG_MEMORY_SYSTEM );
386 }
387
388 #endif /* VG_MEM_H */