import "util/struct";
Provides macros for manual allocation and deallocation of structs stored in a shared array.
This module requires using of classes for the macros that use struct types.
The macros access the array by reference, that way it can resize it by creating a new shared array and copying the existing content to it. The array doesn't need to be initialized, it will be automatically created on a first allocation. The array always use 4 bytes per element.
Any allocated space must be freed with the corresponding macro of the alloc/free pair. Array and raw allocations must free or resize the whole allocated space. There are no checks for faulty operations (such as double or mismatched freeing).
When passing the arrays between tasks it is required to send a new reference of the shared array before use. This is because the allocation can resize the array by creating a new array. The previous array will then contain old data and shouldn't be used anymore.
The allocator uses the first-fit algorithm with the free ranges sorted by their offset. This allow to merge free ranges and reduces the overall array size. The overhead is just a single element at the start of the array that is used as a pointer to the first free range. The downside of the allocator is that it can be slower and more fragmented in the case that many too small free ranges are at the beginning. If this is an issue you can pack differently sized structs in different shared arrays.
macro struct_init(&arr, capacity: Integer)
macro struct_alloc(&arr, type)
type
must be a struct type. Returns
the struct.
macro struct_free(&arr, type, str)
type
must be a struct type.
macro struct_alloc_array(&arr, type, num_elems: Integer)
type
must be a struct type.
Returns the first struct.
macro struct_realloc_array(&arr, type, &str, &num_elems: Integer, new_num_elems: Integer)
type
must be a struct type
and the passed struct must be the first struct in the array.
macro struct_free_array(&arr, type, str, num_elems: Integer)
type
must be a struct type
and the passed struct must be the first struct in the array.
macro struct_raw_alloc(&arr, size: Integer): Integer
macro struct_raw_realloc(&arr, &size: Integer, &off: Integer, new_size: Integer)
macro struct_raw_free(&arr, size, off: Integer)
function struct_get_free_size(arr): Integer
function struct_get_occupancy(arr): Float
function struct_dump(arr)