5 #include "vg_platform.h"
10 #define VG_MAX_ALLOCATIONS 64
11 #define VG_FUZZ_ALLOCATIONS
13 typedef struct vg_linear_allocator vg_linear_allocator
;
14 typedef struct vg_allocation_meta vg_allocation_meta
;
26 struct vg_allocation_meta
31 k_allocation_type_block
= 0,
32 k_allocation_type_linear
= 1
37 #define VG_MEMORY_SYSTEM 0x1 /* systems memory, slow and low counts */
38 #define VG_MEMORY_REALTIME 0x2 /* per-frame. no max allocs, only size. fast */
40 /* systems memory cannot be declared inside realtime memory regions */
43 * Stored just behind the array. 32 bytes.
46 struct vg_linear_allocator
72 vg_allocation_meta
*alloc_table
;
82 /* 32 bit pointers! */
88 VG_STATIC
void vg_error(const char *fmt
, ...);
89 VG_STATIC
void vg_info(const char *fmt
, ...);
91 /* Returns allocator structure from data pointer */
92 static vg_linear_allocator
*vg_linear_header( void *data
)
94 vg_linear_allocator
*ptr
= data
;
100 /* allocate something from a linear allocator */
101 __attribute__((warn_unused_result
))
102 VG_STATIC
void *vg_linear_alloc( void *buffer
, u32 size
)
104 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
106 if( (alloc
->cur
+ size
) > alloc
->size
)
108 vg_error( "%u + %u > %u\n", alloc
->cur
, size
, alloc
->size
);
109 vg_fatal_exit_loop( "linear allocator overflow" );
112 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
113 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
114 vg_fatal_exit_loop( "Max linear allocations reached" );
118 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
120 data
= malloc( size
);
122 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
123 meta
->type
= k_allocation_type_block
;
128 data
= buffer
+ alloc
->cur
;
131 alloc
->allocation_count
++;
132 alloc
->last_alloc
= data
;
133 alloc
->last_alloc_size
= size
;
140 /* resize latest block of memory from linear */
141 __attribute__((warn_unused_result
))
142 VG_STATIC
void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
)
144 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
146 if( alloc
->last_alloc
!= data
)
147 vg_fatal_exit_loop( "This block has been fixed!" );
149 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
150 vg_fatal_exit_loop( "Cannot resize, overflow" );
152 alloc
->cur
-= alloc
->last_alloc_size
;
153 alloc
->cur
+= newsize
;
154 alloc
->last_alloc_size
= newsize
;
156 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
158 data
= realloc( data
, newsize
);
160 vg_fatal_exit_loop( "realloc failed" );
162 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
163 alloc
->last_alloc
= data
;
172 /* its possible to delete just the last item */
173 VG_STATIC
void vg_linear_del( void *buffer
, void *data
)
175 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
177 if( alloc
->last_alloc
!= data
)
178 vg_fatal_exit_loop( "This block has been fixed!" );
180 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
182 vg_allocation_meta
*meta
= &alloc
->alloc_table
[alloc
->allocation_count
-1];
183 if( meta
->type
== k_allocation_type_linear
)
184 vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
189 alloc
->allocation_count
--;
190 alloc
->cur
-= alloc
->last_alloc_size
;
191 alloc
->last_alloc
= NULL
;
192 alloc
->last_alloc_size
= 0;
195 /* extend latest block of memory from linear */
196 __attribute__((warn_unused_result
))
197 VG_STATIC
void *vg_linear_extend( void *buffer
, void *data
, u32 extra
)
199 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
201 if( alloc
->last_alloc
!= data
)
202 vg_fatal_exit_loop( "This block has been fixed!" );
204 u32 new_size
= alloc
->last_alloc_size
+ extra
;
205 return vg_linear_resize( buffer
, data
, new_size
);
208 /* get the current usage of allocator */
209 VG_STATIC u32
vg_linear_get_cur( void *buffer
)
211 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
215 /* get the capacity of allocator. */
216 VG_STATIC u32
vg_linear_get_capacity( void *buffer
)
218 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
222 /* get the remaining size of the allocator */
223 VG_STATIC u32
vg_linear_remaining( void *buffer
)
225 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
226 return alloc
->size
- alloc
->cur
;
229 /* yeet all memory from linear allocator */
230 VG_STATIC
void vg_linear_clear( void *buffer
)
232 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
234 /* libc mode we recursively free any allocations made */
235 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
237 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ )
239 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
241 if( meta
->type
== k_allocation_type_block
)
247 vg_linear_clear( meta
->data
);
248 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
250 free( sub
->alloc_table
);
256 alloc
->last_alloc
= NULL
;
257 alloc
->last_alloc_size
= 0;
258 alloc
->allocation_count
= 0;
262 /* allocate a FIXED SIZE linear allocator */
263 VG_STATIC
void *vg_create_linear_allocator( void *lin_alloc
, u32 size
,
266 assert( sizeof( vg_linear_allocator
) == 32 );
268 vg_linear_allocator
*header
;
269 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
271 /* Creating it inside an existing one */
274 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
276 if( alloc
->cur
+ block_size
> alloc
->size
)
277 vg_fatal_exit_loop( "Out of memory" );
279 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
280 vg_fatal_exit_loop( "Max allocations in linear allocator" );
282 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
283 vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
286 if( vg_mem
.use_libc_malloc
)
288 vg_allocation_meta
*meta
=
289 &alloc
->alloc_table
[ alloc
->allocation_count
];
291 if( flags
& VG_MEMORY_REALTIME
)
292 header
= malloc( block_size
);
294 header
= malloc( sizeof(vg_linear_allocator
) );
296 meta
->data
= header
+1;
297 meta
->type
= k_allocation_type_linear
;
301 header
= lin_alloc
+ alloc
->cur
;
304 alloc
->cur
+= block_size
;
305 alloc
->last_alloc
= NULL
; /* cant resize this block! */
306 alloc
->last_alloc_size
= block_size
;
308 alloc
->allocation_count
++;
312 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
313 header
= malloc( sizeof(vg_linear_allocator
) );
315 header
= malloc( block_size
);
318 header
->allocation_count
= 0;
320 header
->last_alloc
= NULL
;
321 header
->last_alloc_size
= 0;
323 header
->flags
= flags
;
325 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
327 u32 table_size
= sizeof(vg_allocation_meta
)*VG_MAX_ALLOCATIONS
;
328 header
->alloc_table
= malloc( table_size
);
331 header
->alloc_table
= NULL
;
336 /* request all the memory we need in advance */
337 VG_STATIC
void vg_set_mem_quota( u32 size
)
342 VG_STATIC
void vg_alloc_quota(void)
344 u32 size_scratch
= 10*1024*1024;
345 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
347 vg_mem
.rtmemory
= vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
);
348 vg_mem
.scratch
= vg_create_linear_allocator( vg_mem
.rtmemory
,
353 #endif /* VG_MEM_H */