6 #include "vg_platform.h"
11 #define VG_MAX_ALLOCATIONS 64
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 /* Returns allocator structure from data pointer */
100 static vg_linear_allocator
*vg_linear_header( void *data
)
102 vg_linear_allocator
*ptr
= data
;
108 /* allocate something from a linear allocator */
109 __attribute__((warn_unused_result
))
110 VG_STATIC
void *vg_linear_alloc( void *buffer
, u32 size
)
114 vg_error( "alloc(%u) is not 8 byte aligned\n", size
);
115 size
= vg_align8( size
);
118 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
120 if( (alloc
->cur
+ size
) > alloc
->size
)
122 vg_error( "%u + %u > %u\n", alloc
->cur
, size
, alloc
->size
);
123 vg_fatal_exit_loop( "linear allocator overflow" );
126 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
127 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
128 vg_fatal_exit_loop( "Max linear allocations reached" );
132 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
134 data
= malloc( size
);
136 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
137 meta
->type
= k_allocation_type_block
;
142 data
= buffer
+ alloc
->cur
;
145 alloc
->allocation_count
++;
146 alloc
->last_alloc
= data
;
147 alloc
->last_alloc_size
= size
;
154 /* resize latest block of memory from linear */
155 __attribute__((warn_unused_result
))
156 VG_STATIC
void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
)
158 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
160 if( alloc
->last_alloc
!= data
)
161 vg_fatal_exit_loop( "This block has been fixed!" );
163 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
164 vg_fatal_exit_loop( "Cannot resize, overflow" );
166 alloc
->cur
-= alloc
->last_alloc_size
;
167 alloc
->cur
+= newsize
;
168 alloc
->last_alloc_size
= newsize
;
170 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
172 data
= realloc( data
, newsize
);
174 vg_fatal_exit_loop( "realloc failed" );
176 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
177 alloc
->last_alloc
= data
;
186 /* its possible to delete just the last item */
187 VG_STATIC
void vg_linear_del( void *buffer
, void *data
)
189 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
191 if( alloc
->last_alloc
!= data
)
192 vg_fatal_exit_loop( "This block has been fixed!" );
194 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
196 vg_allocation_meta
*meta
= &alloc
->alloc_table
[alloc
->allocation_count
-1];
197 if( meta
->type
== k_allocation_type_linear
)
198 vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
203 alloc
->allocation_count
--;
204 alloc
->cur
-= alloc
->last_alloc_size
;
205 alloc
->last_alloc
= NULL
;
206 alloc
->last_alloc_size
= 0;
209 /* extend latest block of memory from linear */
210 __attribute__((warn_unused_result
))
211 VG_STATIC
void *vg_linear_extend( void *buffer
, void *data
, u32 extra
)
213 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
215 if( alloc
->last_alloc
!= data
)
216 vg_fatal_exit_loop( "This block has been fixed!" );
218 u32 new_size
= alloc
->last_alloc_size
+ extra
;
219 return vg_linear_resize( buffer
, data
, new_size
);
222 /* get the current usage of allocator */
223 VG_STATIC u32
vg_linear_get_cur( void *buffer
)
225 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
229 /* get the capacity of allocator. */
230 VG_STATIC u32
vg_linear_get_capacity( void *buffer
)
232 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
236 /* get the remaining size of the allocator */
237 VG_STATIC u32
vg_linear_remaining( void *buffer
)
239 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
240 return alloc
->size
- alloc
->cur
;
243 /* yeet all memory from linear allocator */
244 VG_STATIC
void vg_linear_clear( void *buffer
)
246 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
248 /* libc mode we recursively free any allocations made */
249 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
251 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ )
253 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
255 if( meta
->type
== k_allocation_type_block
)
261 vg_linear_clear( meta
->data
);
262 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
264 free( sub
->alloc_table
);
270 alloc
->last_alloc
= NULL
;
271 alloc
->last_alloc_size
= 0;
272 alloc
->allocation_count
= 0;
276 /* allocate a FIXED SIZE linear allocator */
277 VG_STATIC
void *vg_create_linear_allocator( void *lin_alloc
, u32 size
,
280 assert( sizeof( vg_linear_allocator
) == 32 );
282 vg_linear_allocator
*header
;
283 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
285 /* Creating it inside an existing one */
288 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
290 if( alloc
->cur
+ block_size
> alloc
->size
)
291 vg_fatal_exit_loop( "Out of memory" );
293 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
294 vg_fatal_exit_loop( "Max allocations in linear allocator" );
296 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
297 vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
300 if( vg_mem
.use_libc_malloc
)
302 vg_allocation_meta
*meta
=
303 &alloc
->alloc_table
[ alloc
->allocation_count
];
305 if( flags
& VG_MEMORY_REALTIME
)
306 header
= malloc( block_size
);
308 header
= malloc( sizeof(vg_linear_allocator
) );
310 meta
->data
= header
+1;
311 meta
->type
= k_allocation_type_linear
;
315 header
= lin_alloc
+ alloc
->cur
;
318 alloc
->cur
+= block_size
;
319 alloc
->last_alloc
= NULL
; /* cant resize this block! */
320 alloc
->last_alloc_size
= block_size
;
322 alloc
->allocation_count
++;
326 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
327 header
= malloc( sizeof(vg_linear_allocator
) );
329 header
= malloc( block_size
);
332 header
->allocation_count
= 0;
334 header
->last_alloc
= NULL
;
335 header
->last_alloc_size
= 0;
337 header
->flags
= flags
;
339 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
341 u32 table_size
= sizeof(vg_allocation_meta
)*VG_MAX_ALLOCATIONS
;
342 header
->alloc_table
= malloc( table_size
);
345 header
->alloc_table
= NULL
;
350 /* request all the memory we need in advance */
351 VG_STATIC
void vg_set_mem_quota( u32 size
)
356 VG_STATIC
void vg_alloc_quota(void)
358 u32 size_scratch
= 10*1024*1024;
359 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
361 vg_mem
.rtmemory
= vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
);
362 vg_mem
.scratch
= vg_create_linear_allocator( vg_mem
.rtmemory
,
367 #endif /* VG_MEM_H */