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