6 #include "vg_platform.h"
11 VG_STATIC
void vg_print_backtrace(void);
13 #define VG_MAX_ALLOCATIONS 128
14 #define VG_FUZZ_ALLOCATIONS
16 typedef struct vg_linear_allocator vg_linear_allocator
;
17 typedef struct vg_allocation_meta vg_allocation_meta
;
29 struct vg_allocation_meta
34 k_allocation_type_block
= 0,
35 k_allocation_type_linear
= 1
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 */
43 /* systems memory cannot be declared inside realtime memory regions */
46 * Stored just behind the array. 32 bytes.
49 struct vg_linear_allocator
75 vg_allocation_meta
*alloc_table
;
85 /* 32 bit pointers! */
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
, ...);
95 VG_STATIC u32
vg_align8( u32 s
)
101 VG_STATIC u32
vg_align4( u32 s
)
103 u32 m
= (s
+ 3) >> 2;
107 /* Returns allocator structure from data pointer */
108 static vg_linear_allocator
*vg_linear_header( void *data
)
110 vg_linear_allocator
*ptr
= data
;
116 /* allocate something from a linear allocator */
117 __attribute__((warn_unused_result
))
118 VG_STATIC
void *vg_linear_alloc( void *buffer
, u32 size
)
121 vg_error( "alloc(%u) is not 8 byte aligned\n", size
);
122 vg_print_backtrace();
123 size
= vg_align8( size
);
126 if( ((u32
)buffer
) % 8 ){
128 if( ((u64
)buffer
) % 8 ){
130 vg_error( "buffer: %p\n", buffer
);
131 vg_fatal_exit_loop( "unaligned buffer" );
134 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
136 if( (alloc
->cur
+ size
) > alloc
->size
){
137 vg_error( "%u + %u > %u\n", alloc
->cur
, size
, alloc
->size
);
138 vg_fatal_exit_loop( "linear allocator overflow" );
141 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
142 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
143 vg_fatal_exit_loop( "Max linear allocations reached" );
147 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
148 data
= malloc( size
);
150 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
151 meta
->type
= k_allocation_type_block
;
155 data
= buffer
+ alloc
->cur
;
159 for( u32 i
=0; i
<size
; i
++ ){
163 alloc
->allocation_count
++;
164 alloc
->last_alloc
= data
;
165 alloc
->last_alloc_size
= size
;
169 if( ((u32
)data
) % 8 ){
171 if( ((u64
)data
) % 8 ){
173 vg_fatal_exit_loop( "unaligned" );
179 /* resize latest block of memory from linear */
180 __attribute__((warn_unused_result
))
181 VG_STATIC
void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
)
183 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
186 vg_error( "alloc(%u) is not 8 byte aligned\n", newsize
);
187 vg_print_backtrace();
188 newsize
= vg_align8( newsize
);
191 if( alloc
->last_alloc
!= data
)
192 vg_fatal_exit_loop( "This block has been fixed!" );
194 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
195 vg_fatal_exit_loop( "Cannot resize, overflow" );
197 alloc
->cur
-= alloc
->last_alloc_size
;
198 alloc
->cur
+= newsize
;
199 alloc
->last_alloc_size
= newsize
;
201 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
202 data
= realloc( data
, newsize
);
204 vg_fatal_exit_loop( "realloc failed" );
206 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
207 alloc
->last_alloc
= data
;
215 /* its possible to delete just the last item */
216 VG_STATIC
void vg_linear_del( void *buffer
, void *data
)
218 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
220 if( alloc
->last_alloc
!= data
)
221 vg_fatal_exit_loop( "This block has been fixed!" );
223 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
224 vg_allocation_meta
*meta
= &alloc
->alloc_table
[alloc
->allocation_count
-1];
225 if( meta
->type
== k_allocation_type_linear
)
226 vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
231 alloc
->allocation_count
--;
232 alloc
->cur
-= alloc
->last_alloc_size
;
233 alloc
->last_alloc
= NULL
;
234 alloc
->last_alloc_size
= 0;
237 /* extend latest block of memory from linear */
238 __attribute__((warn_unused_result
))
239 VG_STATIC
void *vg_linear_extend( void *buffer
, void *data
, u32 extra
)
242 return vg_linear_alloc( buffer
, extra
);
244 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
246 if( alloc
->last_alloc
!= data
)
247 vg_fatal_exit_loop( "This block has been fixed!" );
249 u32 new_size
= alloc
->last_alloc_size
+ extra
;
250 return vg_linear_resize( buffer
, data
, new_size
);
253 /* get the current usage of allocator */
254 VG_STATIC u32
vg_linear_get_cur( void *buffer
)
256 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
260 /* get the capacity of allocator. */
261 VG_STATIC u32
vg_linear_get_capacity( void *buffer
)
263 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
267 /* get the remaining size of the allocator */
268 VG_STATIC u32
vg_linear_remaining( void *buffer
)
270 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
271 return alloc
->size
- alloc
->cur
;
274 /* yeet all memory from linear allocator */
275 VG_STATIC
void vg_linear_clear( void *buffer
)
277 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
279 /* libc mode we recursively free any allocations made */
280 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
281 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ ){
282 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
284 if( meta
->type
== k_allocation_type_block
){
288 vg_linear_clear( meta
->data
);
289 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
291 free( sub
->alloc_table
);
297 alloc
->last_alloc
= NULL
;
298 alloc
->last_alloc_size
= 0;
299 alloc
->allocation_count
= 0;
303 /* allocate a FIXED SIZE linear allocator */
304 VG_STATIC
void *vg_create_linear_allocator( void *lin_alloc
, u32 size
,
307 assert( sizeof( vg_linear_allocator
) == 32 );
309 vg_linear_allocator
*header
;
310 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
312 /* Creating it inside an existing one */
314 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
316 if( alloc
->cur
+ block_size
> alloc
->size
)
317 vg_fatal_exit_loop( "Out of memory" );
319 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
320 vg_fatal_exit_loop( "Max allocations in linear allocator" );
322 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
323 vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
326 if( vg_mem
.use_libc_malloc
){
327 vg_allocation_meta
*meta
=
328 &alloc
->alloc_table
[ alloc
->allocation_count
];
330 if( flags
& VG_MEMORY_REALTIME
)
331 header
= malloc( block_size
);
333 header
= malloc( sizeof(vg_linear_allocator
) );
335 meta
->data
= header
+1;
336 meta
->type
= k_allocation_type_linear
;
339 header
= lin_alloc
+ alloc
->cur
;
342 alloc
->cur
+= block_size
;
343 alloc
->last_alloc
= NULL
; /* cant resize this block! */
344 alloc
->last_alloc_size
= block_size
;
346 alloc
->allocation_count
++;
349 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
350 header
= malloc( sizeof(vg_linear_allocator
) );
352 header
= malloc( block_size
);
355 header
->allocation_count
= 0;
357 header
->last_alloc
= NULL
;
358 header
->last_alloc_size
= 0;
360 header
->flags
= flags
;
362 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) ){
363 u32 table_size
= sizeof(vg_allocation_meta
)*VG_MAX_ALLOCATIONS
;
364 header
->alloc_table
= malloc( table_size
);
367 header
->alloc_table
= NULL
;
372 /* request all the memory we need in advance */
373 VG_STATIC
void vg_set_mem_quota( u32 size
)
378 VG_STATIC
void vg_alloc_quota(void)
380 u32 size_scratch
= 10*1024*1024;
381 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
383 vg_mem
.rtmemory
= vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
);
384 vg_mem
.scratch
= vg_create_linear_allocator( vg_mem
.rtmemory
,
389 #endif /* VG_MEM_H */