$
#define $(T, ...)
#define $I(X)
#define $F(X)
#define $S(X)
#define $R(X)
#define $B(X)
Allocate memory for the given type T
on the stack and copy in the given arguments ...
as struct members. Shorthand constructors exist for native types:
$I -> Int
$F -> Float
$S -> String
$R -> Ref
$B -> Box
alloc
#define alloc_stack(T)
var alloc(var type);
var alloc_raw(var type);
var alloc_root(var type);
Allocate memory for a given type
. To avoid the Garbage Collector completely use alloc_raw
, to register the allocation as a root use alloc_root
. In the case of raw or root allocations the corresponding dealloc
function should be used when done. Memory allocated with alloc_stack
is not managed by the Garbage Collector.
dealloc
void dealloc(var self);
void dealloc_raw(var self);
void dealloc_root(var self);
Deallocate memory for object self
manually. If registered with the Garbage Collector then entry will be removed. If the raw
variation is used memory will be deallocated without going via the Garbage Collector.
Usage
/* Allocation deallocated by Garbage Collector */
var x = alloc(Int);
construct(x, $I(10));
Avoid Garbage Collection
/* Allocation must be manually deallocated */
var x = alloc_raw(Int);
construct(x, $I(10));
destruct(x);
dealloc_raw(x);
Memory Allocation
The Alloc
class can be used to override how memory is allocated for a given data type. By default memory is allocated using calloc
along with the Size
class to determine the amount of memory to allocate.
A custom allocator should be careful to also initialise the header for the allocated memory using the function header_init
. Cello objects without a header wont be recognised as such as so will throw errors when used with Cello functions.
Allocated memory is automatically registered with the garbage collector unless the functions alloc_raw
and dealloc_raw
are used.
struct Alloc {
var (*alloc)(void);
void (*dealloc)(var);
};