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
;
83 VG_STATIC
void vg_error(const char *fmt
, ...);
84 VG_STATIC
void vg_info(const char *fmt
, ...);
86 /* Returns allocator structure from data pointer */
87 static vg_linear_allocator
*vg_linear_header( void *data
)
89 vg_linear_allocator
*ptr
= data
;
95 /* allocate something from a linear allocator */
96 __attribute__((warn_unused_result
))
97 VG_STATIC
void *vg_linear_alloc( void *buffer
, u32 size
)
99 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
101 if( (alloc
->cur
+ size
) > alloc
->size
)
103 vg_error( "%u + %u > %u\n", alloc
->cur
, size
, alloc
->size
);
104 vg_fatal_exit_loop( "linear allocator overflow" );
107 if( alloc
->flags
& VG_MEMORY_SYSTEM
)
108 if( (alloc
->allocation_count
+ 1) > VG_MAX_ALLOCATIONS
)
109 vg_fatal_exit_loop( "Max linear allocations reached" );
113 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
115 data
= malloc( size
);
117 vg_allocation_meta
*meta
= &alloc
->alloc_table
[ alloc
->allocation_count
];
118 meta
->type
= k_allocation_type_block
;
123 data
= buffer
+ alloc
->cur
;
126 alloc
->allocation_count
++;
127 alloc
->last_alloc
= data
;
128 alloc
->last_alloc_size
= size
;
135 /* resize latest block of memory from linear */
136 __attribute__((warn_unused_result
))
137 VG_STATIC
void *vg_linear_resize( void *buffer
, void *data
, u32 newsize
)
139 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
141 if( alloc
->last_alloc
!= data
)
142 vg_fatal_exit_loop( "This block has been fixed!" );
144 if( (alloc
->cur
- alloc
->last_alloc_size
+ newsize
) > alloc
->size
)
145 vg_fatal_exit_loop( "Cannot resize, overflow" );
147 alloc
->cur
-= alloc
->last_alloc_size
;
148 alloc
->cur
+= newsize
;
149 alloc
->last_alloc_size
= newsize
;
151 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
153 data
= realloc( data
, newsize
);
155 vg_fatal_exit_loop( "realloc failed" );
157 alloc
->alloc_table
[ alloc
->allocation_count
-1 ].data
= data
;
158 alloc
->last_alloc
= data
;
167 /* its possible to delete just the last item */
168 VG_STATIC
void vg_linear_del( void *buffer
, void *data
)
170 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
172 if( alloc
->last_alloc
!= data
)
173 vg_fatal_exit_loop( "This block has been fixed!" );
175 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
177 vg_allocation_meta
*meta
= &alloc
->alloc_table
[alloc
->allocation_count
-1];
178 if( meta
->type
== k_allocation_type_linear
)
179 vg_fatal_exit_loop( "Cannot free a linear allocator in this conext" );
184 alloc
->allocation_count
--;
185 alloc
->cur
-= alloc
->last_alloc_size
;
186 alloc
->last_alloc
= NULL
;
187 alloc
->last_alloc_size
= 0;
190 /* extend latest block of memory from linear */
191 __attribute__((warn_unused_result
))
192 VG_STATIC
void *vg_linear_extend( void *buffer
, void *data
, u32 extra
)
194 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
196 if( alloc
->last_alloc
!= data
)
197 vg_fatal_exit_loop( "This block has been fixed!" );
199 u32 new_size
= alloc
->last_alloc_size
+ extra
;
200 return vg_linear_resize( buffer
, data
, new_size
);
203 /* get the current usage of allocator */
204 VG_STATIC u32
vg_linear_get_cur( void *buffer
)
206 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
210 /* get the capacity of allocator. */
211 VG_STATIC u32
vg_linear_get_capacity( void *buffer
)
213 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
217 /* get the remaining size of the allocator */
218 VG_STATIC u32
vg_linear_remaining( void *buffer
)
220 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
221 return alloc
->size
- alloc
->cur
;
224 /* yeet all memory from linear allocator */
225 VG_STATIC
void vg_linear_clear( void *buffer
)
227 vg_linear_allocator
*alloc
= vg_linear_header( buffer
);
229 /* libc mode we recursively free any allocations made */
230 if( vg_mem
.use_libc_malloc
&& (alloc
->flags
& VG_MEMORY_SYSTEM
) )
232 for( u32 i
=0; i
<alloc
->allocation_count
; i
++ )
234 vg_allocation_meta
*meta
= &alloc
->alloc_table
[i
];
236 if( meta
->type
== k_allocation_type_block
)
242 vg_linear_clear( meta
->data
);
243 vg_linear_allocator
*sub
= vg_linear_header( meta
->data
);
245 free( sub
->alloc_table
);
251 alloc
->last_alloc
= NULL
;
252 alloc
->last_alloc_size
= 0;
253 alloc
->allocation_count
= 0;
257 /* allocate a FIXED SIZE linear allocator */
258 VG_STATIC
void *vg_create_linear_allocator( void *lin_alloc
, u32 size
,
261 assert( sizeof( vg_linear_allocator
) == 32 );
263 vg_linear_allocator
*header
;
264 u32 block_size
= size
+ sizeof(vg_linear_allocator
);
266 /* Creating it inside an existing one */
269 vg_linear_allocator
*alloc
= vg_linear_header( lin_alloc
);
271 if( alloc
->cur
+ block_size
> alloc
->size
)
272 vg_fatal_exit_loop( "Out of memory" );
274 if( alloc
->allocation_count
+ 1 > VG_MAX_ALLOCATIONS
)
275 vg_fatal_exit_loop( "Max allocations in linear allocator" );
277 if( (flags
&& VG_MEMORY_SYSTEM
) && (alloc
->flags
& VG_MEMORY_REALTIME
) )
278 vg_fatal_exit_loop( "Cannot declare realtime allocator inside systems"
281 if( vg_mem
.use_libc_malloc
)
283 vg_allocation_meta
*meta
=
284 &alloc
->alloc_table
[ alloc
->allocation_count
];
286 if( flags
& VG_MEMORY_REALTIME
)
287 header
= malloc( block_size
);
289 header
= malloc( sizeof(vg_linear_allocator
) );
291 meta
->data
= header
+1;
292 meta
->type
= k_allocation_type_linear
;
296 header
= lin_alloc
+ alloc
->cur
;
299 alloc
->cur
+= block_size
;
300 alloc
->last_alloc
= NULL
; /* cant resize this block! */
301 alloc
->last_alloc_size
= block_size
;
303 alloc
->allocation_count
++;
307 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
308 header
= malloc( sizeof(vg_linear_allocator
) );
310 header
= malloc( block_size
);
313 header
->allocation_count
= 0;
315 header
->last_alloc
= NULL
;
316 header
->last_alloc_size
= 0;
318 header
->flags
= flags
;
320 if( vg_mem
.use_libc_malloc
&& (flags
& VG_MEMORY_SYSTEM
) )
322 u32 table_size
= sizeof(vg_allocation_meta
)*VG_MAX_ALLOCATIONS
;
323 header
->alloc_table
= malloc( table_size
);
326 header
->alloc_table
= NULL
;
331 /* request all the memory we need in advance */
332 VG_STATIC
void vg_set_mem_quota( u32 size
)
337 VG_STATIC
void vg_alloc_quota(void)
339 u32 size_scratch
= 10*1024*1024;
340 u32 size
= VG_MAX( vg_mem
.quota
, size_scratch
);
342 vg_mem
.rtmemory
= vg_create_linear_allocator( NULL
, size
, VG_MEMORY_SYSTEM
);
343 vg_mem
.scratch
= vg_create_linear_allocator( vg_mem
.rtmemory
,
348 #endif /* VG_MEM_H */