-
Notifications
You must be signed in to change notification settings - Fork 0
Memory Management
It's an unmanaged language, so the GC will not slow down / complicate your program. Instead, manual allocations and dellocations will complicate you code. You're welcome.
The memory allocation is implemented in the standard library, in System.Memory.bbc.
To make syntax sugars work, there are several attributes you can put on stuff:
You can allocate any value on the heap by using the following syntax:
i32* value = new i32*;Normally, the new expression would just initialize the object with zeroes (makes sense for struct types), but if you "create a new pointer", it'll allocate the object on the heap, and will initialize it with zeroes.
You can also allocate arrays with it:
i32 length = 69;
i32[]* values = new i32[length]*;If you don't specify the length with a constant value, the size of the allocated memory block will be calculated at runtime.
You can deallocate any allocated object with the delete statement like this:
delete value;Optionally this will also call the destructor defined in the value type (makes sense for struct types).
You can use the temp modifier if you don't want to mess with the delete keyword.
The temp modifier can be applied to variables and parameters.
If you define a variable with the temp modifier, it will be automatically deallocated
when goes out of scope.
void meow()
{
// Allocate an integer on the heap
temp i32* value = new i32*;
// Random calculations
*value = 64;
*value -= 3;
// The value will be deallocated here
}If you define a parameter with the temp keyword, sometimes it can be used to automatically deallocate the value.
void print(temp u16[]* text)
{
// Stuff ...
}
print("meow"); // This string will be allocated on the heap, but will also be deallocated after the function returns. This way you don't have to save the argument into a variable to deallocate it manually.[Builtin("init_heap")]You can put this attribute on functions. The function will be automatically called before anything, only if allocations was used in the program. This way, if you import the standard library, but don't use any allocations, the HEAP will remain unitialized, speeding up the startup time.
[Builtin("alloc")]You can put this attribute on functions. This function will be used by several expressions when they are being compiled. These can be new expressions, string literals, closure captures.
[Builtin("free")]You can put this attribute on functions. This function will be used primarily by the temp modifer syntax sugar. When an temp marked argument/variable goes out of scope, this function will be called with the argument/variable as an argument.
