new
#define new(T, ...)
#define new_raw(T, ...)
#define new_root(T, ...)
var new_with(var type, var args);
var new_raw_with(var type, var args);
var new_root_with(var type, var args);
Construct a new object of a given type
. Use new_raw
to avoid the Garbage Collector completely, and new_root
to register the allocation as a Garbage Collection root. In the case of raw and root allocations they must be destructed with the corresponding deletion functions.
del
void del(var self);
void del_raw(var self);
void del_root(var self);
Destruct the object self
manually. If registered with the Garbage Collector then entry will be removed. If del_raw
is used thenthe destruction will be done without going via the Garbage Collector.
construct
#define construct(self, ...)
var construct_with(var self, var args);
Call the constructor on object self
which has already been allocated.
destruct
var destruct(var self);
Call the destructor on object self
without deallocating the memory for it.
Usage
var x = new(Int, $I(1));
show(x); /* 1 */
show(type_of(x)); /* Int */
var y = alloc(Float);
construct(y, $F(1.0));
show(y); /* 1.0 */
destruct(y);
Construction and Destruction
The New
class allows the user to define constructors and destructors for a type, accessible via new
and del
. Objects allocated with new
are allocated on the heap and also registered with the Garbage Collector this means technically it isn't required to call del
on them as they will be cleaned up at a later date.
The new_root
function can be called to register a variable with the Garbage Collector but to indicate that it will be manually destructed with del_root
by the user. This should be used for variables that wont be reachable by the Garbage Collector such as those in the data segment or only accessible via vanilla C structures.
The new_raw
and del_raw
functions can be called to construct and destruct objects without going via the Garbage Collector.
It is also possible to simply call the construct
and destruct
functions if you wish to construct an already allocated object.
Constructors should assume that memory is zero'd for an object but nothing else.
struct New {
void (*construct_with)(var, var);
void (*destruct)(var);
};