6 #include "vg_platform.h"
11 #define VG_MAX_ALLOCATIONS 128
12 #define VG_FUZZ_ALLOCATIONS
14 typedef struct vg_linear_allocator vg_linear_allocator
;
15 typedef struct vg_allocation_meta vg_allocation_meta
;
27 struct vg_allocation_meta
32 k_allocation_type_block
= 0,
33 k_allocation_type_linear
= 1
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 */
41 /* systems memory cannot be declared inside realtime memory regions */
44 * Stored just behind the array. 32 bytes.
47 struct vg_linear_allocator
73 vg_allocation_meta
*alloc_table
;
83 /* 32 bit pointers! */
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
, ...);
93 VG_STATIC u32
vg_align8( u32 s
)
99 VG_STATIC u32
vg_align4( u32 s
)
101 u32 m
= (s
+ 3) >> 2;
105 /* Returns allocator structure from data pointer */
106 static vg_linear_allocator
*vg_linear_header( void *data
)
108 vg_linear_allocator
*ptr
= data
;
114 /* allocate something from a linear allocator */
115 __attribute__((warn_unused_result
))
116 VG_STATIC
void *vg_linear_alloc( void *buffer
, u32 size
)
120 vg_error( "alloc(%u) is not 8 byte aligned\n", size
);
121 size
= vg_align8( size
);
124 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
126 if( (alloc
->cur
+ size
) > alloc
->size
)
128 vg_error( "%u + %u > %u\n", alloc
->cur
, size
, alloc
->size
);
129 vg_fatal_exit_loop( "linear allocator overflow" );
132 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
133 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
134 vg_fatal_exit_loop( "Max linear allocations reached" );
138 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
140 data
= malloc( size
);
142 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
143 meta
->type
= k_allocation_type_block
;
148 data
= buffer
+ alloc
->cur
;
151 alloc
->allocation_count
++;
152 alloc
->last_alloc
= data
;
153 alloc
->last_alloc_size
= size
;
160 /* resize latest block of memory from linear */
161 __attribute__((warn_unused_result
))
162 VG_STATIC
void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
)
164 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
166 if( alloc
->last_alloc
!= data
)
167 vg_fatal_exit_loop( "This block has been fixed!" );
169 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
170 vg_fatal_exit_loop( "Cannot resize, overflow" );
172 alloc
->cur
-= alloc
->last_alloc_size
;
173 alloc
->cur
+= newsize
;
174 alloc
->last_alloc_size
= newsize
;
176 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
178 data
= realloc( data
, newsize
);
180 vg_fatal_exit_loop( "realloc failed" );
182 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
183 alloc
->last_alloc
= data
;
192 /* its possible to delete just the last item */
193 VG_STATIC
void vg_linear_del( void *buffer
, void *data
)
195 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
197 if( alloc
->last_alloc
!= data
)
198 vg_fatal_exit_loop( "This block has been fixed!" );
200 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
202 vg_allocation_meta
*meta
= &alloc
->alloc_table
[alloc
->allocation_count
-1];
203 if( meta
->type
== k_allocation_type_linear
)
204 vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
209 alloc
->allocation_count
--;
210 alloc
->cur
-= alloc
->last_alloc_size
;
211 alloc
->last_alloc
= NULL
;
212 alloc
->last_alloc_size
= 0;
215 /* extend latest block of memory from linear */
216 __attribute__((warn_unused_result
))
217 VG_STATIC
void *vg_linear_extend( void *buffer
, void *data
, u32 extra
)
220 return vg_linear_alloc( buffer
, extra
);
222 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
224 if( alloc
->last_alloc
!= data
)
225 vg_fatal_exit_loop( "This block has been fixed!" );
227 u32 new_size
= alloc
->last_alloc_size
+ extra
;
228 return vg_linear_resize( buffer
, data
, new_size
);
231 /* get the current usage of allocator */
232 VG_STATIC u32
vg_linear_get_cur( void *buffer
)
234 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
238 /* get the capacity of allocator. */
239 VG_STATIC u32
vg_linear_get_capacity( void *buffer
)
241 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
245 /* get the remaining size of the allocator */
246 VG_STATIC u32
vg_linear_remaining( void *buffer
)
248 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
249 return alloc
->size
- alloc
->cur
;
252 /* yeet all memory from linear allocator */
253 VG_STATIC
void vg_linear_clear( void *buffer
)
255 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
257 /* libc mode we recursively free any allocations made */
258 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
260 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ )
262 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
264 if( meta
->type
== k_allocation_type_block
)
270 vg_linear_clear( meta
->data
);
271 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
273 free( sub
->alloc_table
);
279 alloc
->last_alloc
= NULL
;
280 alloc
->last_alloc_size
= 0;
281 alloc
->allocation_count
= 0;
285 /* allocate a FIXED SIZE linear allocator */
286 VG_STATIC
void *vg_create_linear_allocator( void *lin_alloc
, u32 size
,
289 assert( sizeof( vg_linear_allocator
) == 32 );
291 vg_linear_allocator
*header
;
292 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
294 /* Creating it inside an existing one */
297 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
299 if( alloc
->cur
+ block_size
> alloc
->size
)
300 vg_fatal_exit_loop( "Out of memory" );
302 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
303 vg_fatal_exit_loop( "Max allocations in linear allocator" );
305 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
306 vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
309 if( vg_mem
.use_libc_malloc
)
311 vg_allocation_meta
*meta
=
312 &alloc
->alloc_table
[ alloc
->allocation_count
];
314 if( flags
& VG_MEMORY_REALTIME
)
315 header
= malloc( block_size
);
317 header
= malloc( sizeof(vg_linear_allocator
) );
319 meta
->data
= header
+1;
320 meta
->type
= k_allocation_type_linear
;
324 header
= lin_alloc
+ alloc
->cur
;
327 alloc
->cur
+= block_size
;
328 alloc
->last_alloc
= NULL
; /* cant resize this block! */
329 alloc
->last_alloc_size
= block_size
;
331 alloc
->allocation_count
++;
335 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
336 header
= malloc( sizeof(vg_linear_allocator
) );
338 header
= malloc( block_size
);
341 header
->allocation_count
= 0;
343 header
->last_alloc
= NULL
;
344 header
->last_alloc_size
= 0;
346 header
->flags
= flags
;
348 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
350 u32 table_size
= sizeof(vg_allocation_meta
)*VG_MAX_ALLOCATIONS
;
351 header
->alloc_table
= malloc( table_size
);
354 header
->alloc_table
= NULL
;
359 /* request all the memory we need in advance */
360 VG_STATIC
void vg_set_mem_quota( u32 size
)
365 VG_STATIC
void vg_alloc_quota(void)
367 u32 size_scratch
= 10*1024*1024;
368 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
370 vg_mem
.rtmemory
= vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
);
371 vg_mem
.scratch
= vg_create_linear_allocator( vg_mem
.rtmemory
,
376 #endif /* VG_MEM_H */