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_error( const char *fmt
, ... );
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_fatal_error( "unaligned buffer (%p)", buffer
);
133 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
135 if( (alloc
->cur
+ size
) > alloc
->size
){
136 vg_fatal_error( "linear allocator overflow (%u + %u > %u)\n",
137 alloc
->cur
, size
, alloc
->size
);
140 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
141 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
142 vg_fatal_error( "Max linear allocations reached" );
146 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
147 data
= malloc( size
);
149 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
150 meta
->type
= k_allocation_type_block
;
154 data
= buffer
+ alloc
->cur
;
158 for( u32 i
=0; i
<size
; i
++ ){
162 alloc
->allocation_count
++;
163 alloc
->last_alloc
= data
;
164 alloc
->last_alloc_size
= size
;
168 if( ((u32
)data
) % 8 ){
170 if( ((u64
)data
) % 8 ){
172 vg_fatal_error( "unaligned" );
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
)
182 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
185 vg_error( "alloc(%u) is not 8 byte aligned\n", newsize
);
186 vg_print_backtrace();
187 newsize
= vg_align8( newsize
);
190 if( alloc
->last_alloc
!= data
)
191 vg_fatal_error( "This block has been fixed!" );
193 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
194 vg_fatal_error( "Cannot resize, overflow" );
196 alloc
->cur
-= alloc
->last_alloc_size
;
197 alloc
->cur
+= newsize
;
198 alloc
->last_alloc_size
= newsize
;
200 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
201 data
= realloc( data
, newsize
);
203 vg_fatal_error( "realloc failed" );
205 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
206 alloc
->last_alloc
= data
;
214 /* its possible to delete just the last item */
215 VG_STATIC
void vg_linear_del( void *buffer
, void *data
)
217 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
219 if( alloc
->last_alloc
!= data
)
220 vg_fatal_error( "This block has been fixed!" );
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" );
230 alloc
->allocation_count
--;
231 alloc
->cur
-= alloc
->last_alloc_size
;
232 alloc
->last_alloc
= NULL
;
233 alloc
->last_alloc_size
= 0;
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
)
241 return vg_linear_alloc( buffer
, extra
);
243 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
245 if( alloc
->last_alloc
!= data
)
246 vg_fatal_error( "This block has been fixed!" );
248 u32 new_size
= alloc
->last_alloc_size
+ extra
;
249 return vg_linear_resize( buffer
, data
, new_size
);
252 /* get the current usage of allocator */
253 VG_STATIC u32
vg_linear_get_cur( void *buffer
)
255 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
259 /* get the capacity of allocator. */
260 VG_STATIC u32
vg_linear_get_capacity( void *buffer
)
262 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
266 /* get the remaining size of the allocator */
267 VG_STATIC u32
vg_linear_remaining( void *buffer
)
269 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
270 return alloc
->size
- alloc
->cur
;
273 /* yeet all memory from linear allocator */
274 VG_STATIC
void vg_linear_clear( void *buffer
)
276 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
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
];
283 if( meta
->type
== k_allocation_type_block
){
287 vg_linear_clear( meta
->data
);
288 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
290 free( sub
->alloc_table
);
296 alloc
->last_alloc
= NULL
;
297 alloc
->last_alloc_size
= 0;
298 alloc
->allocation_count
= 0;
302 /* allocate a FIXED SIZE linear allocator */
303 VG_STATIC
void *vg_create_linear_allocator( void *lin_alloc
, u32 size
,
306 assert( sizeof( vg_linear_allocator
) == 32 );
308 vg_linear_allocator
*header
;
309 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
311 /* Creating it inside an existing one */
313 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
315 if( alloc
->cur
+ block_size
> alloc
->size
)
316 vg_fatal_error( "Out of memory" );
318 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
319 vg_fatal_error( "Max allocations in linear allocator" );
321 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
322 vg_fatal_error( "Cannot declare realtime allocator inside systems"
325 if( vg_mem
.use_libc_malloc
){
326 vg_allocation_meta
*meta
=
327 &alloc
->alloc_table
[ alloc
->allocation_count
];
329 if( flags
& VG_MEMORY_REALTIME
)
330 header
= malloc( block_size
);
332 header
= malloc( sizeof(vg_linear_allocator
) );
334 meta
->data
= header
+1;
335 meta
->type
= k_allocation_type_linear
;
338 header
= lin_alloc
+ alloc
->cur
;
341 alloc
->cur
+= block_size
;
342 alloc
->last_alloc
= NULL
; /* cant resize this block! */
343 alloc
->last_alloc_size
= block_size
;
345 alloc
->allocation_count
++;
348 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
349 header
= malloc( sizeof(vg_linear_allocator
) );
351 header
= malloc( block_size
);
354 header
->allocation_count
= 0;
356 header
->last_alloc
= NULL
;
357 header
->last_alloc_size
= 0;
359 header
->flags
= flags
;
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
);
366 header
->alloc_table
= NULL
;
371 /* request all the memory we need in advance */
372 VG_STATIC
void vg_set_mem_quota( u32 size
)
377 VG_STATIC
void vg_alloc_quota(void)
379 u32 size_scratch
= 10*1024*1024;
380 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
382 vg_mem
.rtmemory
= vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
);
383 vg_mem
.scratch
= vg_create_linear_allocator( vg_mem
.rtmemory
,
388 #endif /* VG_MEM_H */