memory.l0

memory.l0

Module: sys.memory

Source: compiler/shared/l0/stdlib/sys/memory.l0 Language: Dea/L0

Symbols

Function rt_alloc

1
extern func rt_alloc(bytes: int) -> void*?

UNSAFE: Functions in this module are unsafe and may lead to undefined behavior if used incorrectly.

Use with caution and ensure proper memory management.

Note: Allocation wrappers register returned storage with the runtime pointer access tracker. Dereferences performed by checked generated L0 code validate the target pointer before accessing memory.

Low-level byte operations in this module still operate on raw pointers and do not perform full semantic bounds checking on their own. It is the caller’s responsibility to ensure that pointers are valid and that memory is managed correctly. Misuse of these functions can lead to memory corruption, leaks, or crashes. Always validate inputs and handle memory carefully when using these functions. Allocate a new block of memory of the given size.

Parameters:

  • bytes: The number of bytes to allocate.

Returns: A pointer to the newly allocated memory, or null on failure.

Function rt_realloc

1
extern func rt_realloc(ptr: void*?, new_bytes: int) -> void*?

Resize an existing raw allocation to a new size.

Pointers returned by new , ARC storage, and registered foreign storage are rejected in checked builds. If ptr is null, this behaves like rt_alloc .

Parameters:

  • ptr: The pointer to the existing memory block, or null to allocate fresh.
  • new_bytes: The new size in bytes.

Returns: A pointer to the resized memory block, or null on failure.

Function rt_free

1
extern func rt_free(ptr: void*?) -> void

Free a raw allocation returned by rt_alloc , rt_calloc , or rt_realloc .

Pointers returned by new must use drop instead.

Parameters:

  • ptr: The pointer to the memory block to free.

Function rt_calloc

1
extern func rt_calloc(count: int, elem_size: int) -> void*?

Allocate zero-initialized memory for multiple elements.

Parameters:

  • count: The number of elements.
  • elem_size: The size of each element in bytes.

Returns: A pointer to the newly allocated memory, or null on failure.

Function rt_register_foreign

1
extern func rt_register_foreign(ptr: void*, bytes: int, read_only: bool) -> void

Register externally owned storage with the checked pointer runtime.

Repeating an identical live registration is a no-op. The caller must unregister the storage before its external lifetime ends.

Parameters:

  • ptr: Exact base pointer of the external storage.
  • bytes: Accessible byte extent, which must be positive.
  • read_only: Whether checked writes must be rejected.

Function rt_unregister_foreign

1
extern func rt_unregister_foreign(ptr: void*) -> void

Remove a live foreign-storage registration without freeing its payload.

Parameters:

  • ptr: Exact base pointer previously passed to rt_register_foreign.

Function rt_memcpy

1
extern func rt_memcpy(dest: void*, src: void*, bytes: int) -> void*

Copy bytes from one memory location to another.

Parameters:

  • dest: The destination pointer.
  • src: The source pointer.
  • bytes: The number of bytes to copy.

Returns: The destination pointer.

Function rt_memset

1
extern func rt_memset(dest: void*, value: int, bytes: int) -> void*

Set a block of memory to a specific byte value.

Parameters:

  • dest: The destination pointer.
  • value: The byte value to set.
  • bytes: The number of bytes to set.

Returns: The destination pointer.

Function rt_memcmp

1
extern func rt_memcmp(a: void*, b: void*, bytes: int) -> int

Compare two blocks of memory.

Parameters:

  • a: The first pointer.
  • b: The second pointer.
  • bytes: The number of bytes to compare.

Returns: Zero if equal, non-zero otherwise.

Function rt_array_element

1
extern func rt_array_element(array_data: void*, element_size: int, index: int) -> void*

Get a pointer to an element in an array given its index.

Parameters:

  • array_data: The pointer to the base of the array.
  • element_size: The size of each element in bytes.
  • index: The element index.

Returns: A pointer to the requested element.

Function rt_stdin_read

1
extern func rt_stdin_read(buf: byte*?, capacity: int) -> int

Read up to capacity bytes from standard input into buf .

Parameters:

  • buf: Destination buffer. A null buffer is reported as -1.
  • capacity: Maximum number of bytes to read.

Returns: Bytes read, 0 on EOF, or -1 on error (including a null buf when capacity > 0).

Function rt_stdout_write

1
extern func rt_stdout_write(buf: byte*?, len: int) -> int

Write up to len bytes from buf to standard output.

Parameters:

  • buf: Source buffer. A null buffer is reported as -1.
  • len: Maximum number of bytes to write.

Returns: Bytes written, or -1 on error (including a null buf when len > 0).

Function rt_stderr_write

1
extern func rt_stderr_write(buf: byte*?, len: int) -> int

Write up to len bytes from buf to standard error.

Parameters:

  • buf: Source buffer. A null buffer is reported as -1.
  • len: Maximum number of bytes to write.

Returns: Bytes written, or -1 on error (including a null buf when len > 0).