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
);
125 if( ((u64
)buffer
) % 8 ){
126 vg_error( "buffer: %p\n", buffer
);
127 vg_fatal_exit_loop( "unaligned buffer" );
130 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
132 if( (alloc
->cur
+ size
) > alloc
->size
){
133 vg_error( "%u + %u > %u\n", alloc
->cur
, size
, alloc
->size
);
134 vg_fatal_exit_loop( "linear allocator overflow" );
137 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
138 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
139 vg_fatal_exit_loop( "Max linear allocations reached" );
143 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
144 data
= malloc( size
);
146 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
147 meta
->type
= k_allocation_type_block
;
151 data
= buffer
+ alloc
->cur
;
155 for( u32 i
=0; i
<size
; i
++ ){
159 alloc
->allocation_count
++;
160 alloc
->last_alloc
= data
;
161 alloc
->last_alloc_size
= size
;
164 if( ((u64
)data
) % 8 ){
165 vg_fatal_exit_loop( "unaligned" );
171 /* resize latest block of memory from linear */
172 __attribute__((warn_unused_result
))
173 VG_STATIC
void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
)
175 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
178 vg_error( "alloc(%u) is not 8 byte aligned\n", newsize
);
179 vg_print_backtrace();
180 newsize
= vg_align8( newsize
);
183 if( alloc
->last_alloc
!= data
)
184 vg_fatal_exit_loop( "This block has been fixed!" );
186 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
187 vg_fatal_exit_loop( "Cannot resize, overflow" );
189 alloc
->cur
-= alloc
->last_alloc_size
;
190 alloc
->cur
+= newsize
;
191 alloc
->last_alloc_size
= newsize
;
193 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
194 data
= realloc( data
, newsize
);
196 vg_fatal_exit_loop( "realloc failed" );
198 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
199 alloc
->last_alloc
= data
;
207 /* its possible to delete just the last item */
208 VG_STATIC
void vg_linear_del( void *buffer
, void *data
)
210 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
212 if( alloc
->last_alloc
!= data
)
213 vg_fatal_exit_loop( "This block has been fixed!" );
215 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
216 vg_allocation_meta
*meta
= &alloc
->alloc_table
[alloc
->allocation_count
-1];
217 if( meta
->type
== k_allocation_type_linear
)
218 vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
223 alloc
->allocation_count
--;
224 alloc
->cur
-= alloc
->last_alloc_size
;
225 alloc
->last_alloc
= NULL
;
226 alloc
->last_alloc_size
= 0;
229 /* extend latest block of memory from linear */
230 __attribute__((warn_unused_result
))
231 VG_STATIC
void *vg_linear_extend( void *buffer
, void *data
, u32 extra
)
234 return vg_linear_alloc( buffer
, extra
);
236 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
238 if( alloc
->last_alloc
!= data
)
239 vg_fatal_exit_loop( "This block has been fixed!" );
241 u32 new_size
= alloc
->last_alloc_size
+ extra
;
242 return vg_linear_resize( buffer
, data
, new_size
);
245 /* get the current usage of allocator */
246 VG_STATIC u32
vg_linear_get_cur( void *buffer
)
248 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
252 /* get the capacity of allocator. */
253 VG_STATIC u32
vg_linear_get_capacity( void *buffer
)
255 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
259 /* get the remaining size of the allocator */
260 VG_STATIC u32
vg_linear_remaining( void *buffer
)
262 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
263 return alloc
->size
- alloc
->cur
;
266 /* yeet all memory from linear allocator */
267 VG_STATIC
void vg_linear_clear( void *buffer
)
269 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
271 /* libc mode we recursively free any allocations made */
272 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) ){
273 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ ){
274 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
276 if( meta
->type
== k_allocation_type_block
){
280 vg_linear_clear( meta
->data
);
281 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
283 free( sub
->alloc_table
);
289 alloc
->last_alloc
= NULL
;
290 alloc
->last_alloc_size
= 0;
291 alloc
->allocation_count
= 0;
295 /* allocate a FIXED SIZE linear allocator */
296 VG_STATIC
void *vg_create_linear_allocator( void *lin_alloc
, u32 size
,
299 assert( sizeof( vg_linear_allocator
) == 32 );
301 vg_linear_allocator
*header
;
302 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
304 /* Creating it inside an existing one */
306 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
308 if( alloc
->cur
+ block_size
> alloc
->size
)
309 vg_fatal_exit_loop( "Out of memory" );
311 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
312 vg_fatal_exit_loop( "Max allocations in linear allocator" );
314 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
315 vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
318 if( vg_mem
.use_libc_malloc
){
319 vg_allocation_meta
*meta
=
320 &alloc
->alloc_table
[ alloc
->allocation_count
];
322 if( flags
& VG_MEMORY_REALTIME
)
323 header
= malloc( block_size
);
325 header
= malloc( sizeof(vg_linear_allocator
) );
327 meta
->data
= header
+1;
328 meta
->type
= k_allocation_type_linear
;
331 header
= lin_alloc
+ alloc
->cur
;
334 alloc
->cur
+= block_size
;
335 alloc
->last_alloc
= NULL
; /* cant resize this block! */
336 alloc
->last_alloc_size
= block_size
;
338 alloc
->allocation_count
++;
341 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
342 header
= malloc( sizeof(vg_linear_allocator
) );
344 header
= malloc( block_size
);
347 header
->allocation_count
= 0;
349 header
->last_alloc
= NULL
;
350 header
->last_alloc_size
= 0;
352 header
->flags
= flags
;
354 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) ){
355 u32 table_size
= sizeof(vg_allocation_meta
)*VG_MAX_ALLOCATIONS
;
356 header
->alloc_table
= malloc( table_size
);
359 header
->alloc_table
= NULL
;
364 /* request all the memory we need in advance */
365 VG_STATIC
void vg_set_mem_quota( u32 size
)
370 VG_STATIC
void vg_alloc_quota(void)
372 u32 size_scratch
= 10*1024*1024;
373 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
375 vg_mem
.rtmemory
= vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
);
376 vg_mem
.scratch
= vg_create_linear_allocator( vg_mem
.rtmemory
,
381 #endif /* VG_MEM_H */