Skip to content

Memory Rt

Spice
import "std/runtime/memory_rt";

Functions

sAlloc

Spice
public f<Result<heap byte*>> sAlloc(unsigned long size)

Allocates a new block of memory of the given size.

Parameters

Name Type Description
size unsigned long The size of the block to allocate.

Returns: Result<heap byte*> — A pointer to the allocated block, or an error if the allocation failed.

sAllocUnsafe

Spice
public f<heap byte*> sAllocUnsafe(unsigned long size)

Allocates a new block of memory of the given size. This panics in case of an out of memory situation.

Parameters

Name Type Description
size unsigned long The size of the block to allocate.

Returns: heap byte* — A pointer to the allocated block

sRealloc

Spice
public f<Result<heap byte*>> sRealloc(heap byte* ptr, unsigned long size)

Reallocates a block of memory to the given size. Subsequently moves the data from the old memory space to the new one.

Parameters

Name Type Description
ptr heap byte* The pointer to the block to reallocate.
size unsigned long The new size of the block.

Returns: Result<heap byte*> — A pointer to the reallocated block, or an error if the reallocation failed.

sReallocUnsafe

Spice
public f<heap byte*> sReallocUnsafe(heap byte* ptr, unsigned long size)

Reallocates a block of memory to the given size. Subsequently moves the data from the old memory space to the new one. This panics in case of invalid input or an out of memory situation.

Parameters

Name Type Description
ptr heap byte* The pointer to the block to reallocate.
size unsigned long The new size of the block.

Returns: heap byte* — A pointer to the reallocated block, or an error if the reallocation failed.

sCopy

Spice
public f<Result<heap byte*>> sCopy(heap byte* oldPtr, heap byte* newPtr, unsigned long size)

Copies a block of memory to a new block of memory.

Parameters

Name Type Description
oldPtr heap byte* The pointer to the block to copy.
newPtr heap byte* The pointer to the new block to copy to.
size unsigned long The size of the block to copy.

Returns: Result<heap byte*> — A pointer to the copied block, or an error if the copy failed.

sCopyUnsafe

Spice
public f<heap byte*> sCopyUnsafe(heap byte* oldPtr, heap byte* newPtr, unsigned long size)

Copies a block of memory to a new block of memory.

Parameters

Name Type Description
oldPtr heap byte* The pointer to the block to copy.
newPtr heap byte* The pointer to the new block to copy to.
size unsigned long The size of the block to copy.

Returns: heap byte* — A pointer to the copied block, or an error if the copy failed.

sMove

Spice
public inline f<heap T*> sMove<T>(heap T*& oldPtr)

Moves the ownership of a heap pointer from oldPtr to the return value. After this operation, oldPtr is set to nil to prevent double frees.

Parameters

Name Type Description
oldPtr heap T*& The pointer to move ownership from

Returns: heap T* — The moved pointer

sDestruct

Spice
public p sDestruct<T>(T& obj)

Destroys the given object by calling its dtor if available

Parameters

Name Type Description
obj T& The object to destroy

sDealloc

Spice
public p sDealloc(heap byte*& ptr)

Frees a block of memory. The pointer is zeroed out after freeing the memory to prevent accidental double frees.

Parameters

Name Type Description
ptr heap byte*& The pointer to the block to free.

sDelete

Spice
public p sDelete<T>(heap T*& ptr)

Destroys the given heap-allocated instance and frees the memory.

Parameters

Name Type Description
ptr heap T*& The pointer to the heap-allocated instance

sYieldOwnership

Spice
public f<T*> sYieldOwnership<T>(heap T*& ptr)

Yields the ownership of the given heap pointer. After this operation, the original pointer is set to nil to prevent double frees.

Parameters

Name Type Description
ptr heap T*& The pointer to yield ownership from

Returns: T* — The yielded pointer

sCompare

Spice
public f<bool> sCompare<T>(const heap T* a, const heap T* b, unsigned long size)

Compares the memory behind pointer A and pointer B for equality.

Parameters

Name Type Description
a const heap T* Pointer A
b const heap T* Pointer B
size unsigned long

Returns: bool — Is memory equal