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