6 #include "vg_platform.h"
12 static void vg_print_backtrace(void);
14 #define VG_MAX_ALLOCATIONS 128
15 #define VG_FUZZ_ALLOCATIONS
17 typedef struct vg_linear_allocator vg_linear_allocator
;
18 typedef struct vg_allocation_meta vg_allocation_meta
;
29 struct vg_allocation_meta
35 k_allocation_type_block
= 0,
36 k_allocation_type_linear
= 1
41 #define VG_MEMORY_SYSTEM 0x1 /* systems memory, slow and low counts */
42 #define VG_MEMORY_REALTIME 0x2 /* per-frame. no max allocs, only size. fast */
44 /* systems memory cannot be declared inside realtime memory regions */
47 * Stored just behind the array. 32 bytes.
50 struct vg_linear_allocator
58 vg_allocation_meta
*alloc_table
;
61 /* 32 bit pointers! */
67 static u32
vg_align8( u32 s
);
68 static u32
vg_align4( u32 s
);
70 /* allocate something from a linear allocator */
71 __attribute__((warn_unused_result
))
72 static void *_vg_linear_alloc( void *buffer
, u32 size
,
73 const char *constr_name
);
75 /* resize latest block of memory from linear */
76 __attribute__((warn_unused_result
))
77 static void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
);
79 /* its possible to delete just the last item */
80 static void vg_linear_del( void *buffer
, void *data
);
82 /* extend latest block of memory from linear */
83 __attribute__((warn_unused_result
))
84 static void *_vg_linear_extend( void *buffer
, void *data
, u32 extra
,
85 const char *constr_name
);
87 /* get the current usage of allocator */
88 static u32
vg_linear_get_cur( void *buffer
);
90 /* get the capacity of allocator. */
91 static u32
vg_linear_get_capacity( void *buffer
);
93 /* get the remaining size of the allocator */
94 static u32
vg_linear_remaining( void *buffer
);
96 /* yeet all memory from linear allocator */
97 static void vg_linear_clear( void *buffer
);
99 /* request all the memory we need in advance */
100 static void vg_set_mem_quota( u32 size
);
102 /* essentially init() */
103 static void vg_alloc_quota(void);
105 /* print out tree of current memory used. only works with libc mode */
106 static void vg_mem_log( void *lin_alloc
, int depth
, const char *name
);
108 #define VG_MEM_MCSTR(S) VG_MEM_MCSTR2(S)
109 #define VG_MEM_MCSTR2(S) #S
111 #define vg_linear_alloc(...) \
112 _vg_linear_alloc( __VA_ARGS__, __FILE__":"VG_MEM_MCSTR(__LINE__) )
113 #define vg_linear_extend(...) \
114 _vg_linear_extend( __VA_ARGS__, __FILE__":"VG_MEM_MCSTR(__LINE__) )
115 #define vg_create_linear_allocator(...) \
116 _vg_create_linear_allocator( __VA_ARGS__, __FILE__":"VG_MEM_MCSTR(__LINE__) )
119 * ----------------------------------------
122 static void vg_fatal_error( const char *fmt
, ... );
125 static void vg_error(const char *fmt
, ...);
126 static void vg_info(const char *fmt
, ...);
129 static u32
vg_align8( u32 s
)
131 u32 m
= (s
+ 7) >> 3;
135 static u32
vg_align4( u32 s
)
137 u32 m
= (s
+ 3) >> 2;
141 /* Returns allocator structure from data pointer */
142 static vg_linear_allocator
*vg_linear_header( void *data
)
144 vg_linear_allocator
*ptr
= data
;
150 /* allocate something from a linear allocator */
151 __attribute__((warn_unused_result
))
152 static void *_vg_linear_alloc( void *buffer
, u32 size
,
153 const char *constr_name
)
156 vg_error( "alloc(%u) is not 8 byte aligned\n", size
);
157 vg_print_backtrace();
158 size
= vg_align8( size
);
160 if( ((u64
)buffer
) % 8 ){
161 vg_fatal_error( "unaligned buffer (%p)", buffer
);
164 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
166 if( (alloc
->cur
+ size
) > alloc
->size
){
167 vg_fatal_error( "linear allocator overflow (%u + %u > %u)\n",
168 alloc
->cur
, size
, alloc
->size
);
171 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
172 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
173 vg_fatal_error( "Max linear allocations reached" );
177 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
178 data
= malloc( size
);
180 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
181 meta
->type
= k_allocation_type_block
;
184 meta
->name
= constr_name
;
187 data
= buffer
+ alloc
->cur
;
191 for( u32 i
=0; i
<size
; i
++ ){
195 alloc
->allocation_count
++;
196 alloc
->last_alloc
= data
;
197 alloc
->last_alloc_size
= size
;
200 if( ((u64
)data
) % 8 ){
201 vg_fatal_error( "unaligned" );
207 /* resize latest block of memory from linear */
208 __attribute__((warn_unused_result
))
209 static void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
)
211 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
214 vg_error( "alloc(%u) is not 8 byte aligned\n", newsize
);
215 vg_print_backtrace();
216 newsize
= vg_align8( newsize
);
219 if( alloc
->last_alloc
!= data
)
220 vg_fatal_error( "This block has been fixed!" );
222 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
223 vg_fatal_error( "Cannot resize, overflow" );
225 alloc
->cur
-= alloc
->last_alloc_size
;
226 alloc
->cur
+= newsize
;
227 alloc
->last_alloc_size
= newsize
;
229 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
230 data
= realloc( data
, newsize
);
232 vg_fatal_error( "realloc failed" );
234 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
235 alloc
->last_alloc
= data
;
243 /* its possible to delete just the last item */
244 static void vg_linear_del( void *buffer
, void *data
)
246 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
248 if( alloc
->last_alloc
!= data
){
249 vg_fatal_error( "This block has been fixed! Last alloc: %p, this: %p\n",
250 alloc
->last_alloc
, data
);
253 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
254 vg_allocation_meta
*meta
= &alloc
->alloc_table
[alloc
->allocation_count
-1];
255 if( meta
->type
== k_allocation_type_linear
)
256 vg_fatal_error( "Cannot free a linear allocator in this conext" );
261 alloc
->allocation_count
--;
262 alloc
->cur
-= alloc
->last_alloc_size
;
263 alloc
->last_alloc
= NULL
;
264 alloc
->last_alloc_size
= 0;
267 /* extend latest block of memory from linear */
268 __attribute__((warn_unused_result
))
269 static void *_vg_linear_extend( void *buffer
, void *data
, u32 extra
,
270 const char *constr_name
)
273 return _vg_linear_alloc( buffer
, vg_align8(extra
), constr_name
);
275 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
277 if( alloc
->last_alloc
!= data
)
278 vg_fatal_error( "This block has been fixed!" );
280 u32 new_size
= alloc
->last_alloc_size
+ extra
;
281 return vg_linear_resize( buffer
, data
, vg_align8(new_size
) );
284 /* get the current usage of allocator */
285 static u32
vg_linear_get_cur( void *buffer
)
287 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
291 /* get the capacity of allocator. */
292 static u32
vg_linear_get_capacity( void *buffer
)
294 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
298 /* get the remaining size of the allocator */
299 static u32
vg_linear_remaining( void *buffer
)
301 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
302 return alloc
->size
- alloc
->cur
;
305 /* yeet all memory from linear allocator */
306 static void vg_linear_clear( void *buffer
)
308 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
310 /* libc mode we recursively free any allocations made */
311 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
312 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ ){
313 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
315 if( meta
->type
== k_allocation_type_block
){
319 vg_linear_clear( meta
->data
);
320 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
322 free( sub
->alloc_table
);
328 alloc
->last_alloc
= NULL
;
329 alloc
->last_alloc_size
= 0;
330 alloc
->allocation_count
= 0;
334 /* allocate a FIXED SIZE linear allocator
336 * FIXME: there was a bug in vg's code that caused a race condition between
337 * two system allocations. make this IMPOSSIBLE by requiring a lock
338 * on the allocater to be passed between threads.
340 * luckily that bug only exists when using development tools, but still!
342 * this should then only be checked and turned on in debugging.
345 static void *_vg_create_linear_allocator( void *lin_alloc
, u32 size
,
346 u16 flags
, const char *constr_name
)
348 assert( sizeof( vg_linear_allocator
) == 32 );
350 vg_linear_allocator
*header
;
351 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
353 /* Creating it inside an existing one */
355 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
357 if( alloc
->cur
+ block_size
> alloc
->size
)
358 vg_fatal_error( "Out of memory" );
360 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
361 vg_fatal_error( "Max allocations in linear allocator" );
363 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
364 vg_fatal_error( "Cannot declare realtime allocator inside systems"
367 if( vg_mem
.use_libc_malloc
){
368 vg_allocation_meta
*meta
=
369 &alloc
->alloc_table
[ alloc
->allocation_count
];
371 if( flags
& VG_MEMORY_REALTIME
)
372 header
= malloc( block_size
);
374 header
= malloc( sizeof(vg_linear_allocator
) );
376 meta
->data
= header
+1;
377 meta
->type
= k_allocation_type_linear
;
379 meta
->name
= constr_name
;
382 header
= lin_alloc
+ alloc
->cur
;
385 alloc
->cur
+= block_size
;
386 alloc
->last_alloc
= header
;
387 alloc
->last_alloc_size
= block_size
;
388 alloc
->allocation_count
++;
391 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
392 header
= malloc( sizeof(vg_linear_allocator
) );
394 header
= malloc( block_size
);
397 header
->allocation_count
= 0;
399 header
->last_alloc
= NULL
;
400 header
->last_alloc_size
= 0;
402 header
->flags
= flags
;
404 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) ){
405 u32 table_size
= sizeof(vg_allocation_meta
)*VG_MAX_ALLOCATIONS
;
406 header
->alloc_table
= malloc( table_size
);
409 header
->alloc_table
= NULL
;
414 /* request all the memory we need in advance */
415 static void vg_set_mem_quota( u32 size
)
420 static void vg_alloc_quota(void)
422 u32 size_scratch
= 10*1024*1024;
423 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
425 vg_mem
.rtmemory
= _vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
,
427 vg_mem
.scratch
= _vg_create_linear_allocator( vg_mem
.rtmemory
,
433 static void vg_mem_log( void *lin_alloc
, int depth
, const char *name
)
435 if( vg_mem
.use_libc_malloc
){
436 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
439 f32 p
= ((float)alloc
->cur
/ (float)alloc
->size
) * 100.0f
;
441 for( int i
=0; i
<depth
; i
++ ) printf( " " );
442 printf( "LA(%s): %u bytes, %f%% used\n", name
, s
, p
);
444 if( alloc
->flags
& VG_MEMORY_SYSTEM
){
445 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ ){
446 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
448 if( meta
->type
== k_allocation_type_block
){
449 for( int i
=0; i
<depth
+1; i
++ ) printf( " " );
450 printf( "B(%s): %u bytes\n", meta
->name
, meta
->size
);
453 vg_mem_log( meta
->data
, depth
+1, meta
->name
);
458 for( int i
=0; i
<depth
+1; i
++ ) printf( " " );
459 printf( "<opaque memory> (UNTRACKED)\n" );
463 vg_error( "allocations are not tracked (turn on libc mode)\n" );
467 #endif /* VG_MEM_H */