Memory Management - banszkyy/BBLang GitHub Wiki

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.

new pointer expression

You can allocate any value on the heap by using the following syntax:

i32* value = new i32*;

This allocates a value of type i32 on the heap, initialized to zero.

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]*;

This allocates 69 sequential i32 values on the heap, initialized to zero.

If you don't specify the length with a constant value, the size of the allocated memory block will be calculated at runtime.

delete statement

You can deallocate any allocated object with the delete statement like this:

delete value;

This will deallocate value using the provided heap implementation. read more

Optionally this will also call the destructor defined in the value type (makes sense for struct types).

temp modifier

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.

temp modifier on variables

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
}

temp modifier on parameters

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.

Heap Implementation Attributes

To make syntax sugars work, there are several attributes you can put on stuff:

[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 delete statement and temp modifer syntax sugar. When you use the delete statement or when a temp marked argument/variable goes out of scope, this function will be called with the argument/variable as the argument.