memory

Allocation wrappers, raw memory operations, cached small-block helpers, and heap-usage queries.


Use

"memory" use exposes raw memory bindings, cached allocation helpers, public allocation entry points, and heap-usage queries.


Allocation model


Public allocation entry points

mplMalloc (size -- ptr)

Allocates one block through the module’s public allocator.

mplRealloc (newSize oldSize ptr -- newPtr)

Resizes one block through the module’s public allocator.

mplFree (size ptr --)

Releases one block previously handled by the module’s public allocator.

Example: public allocation helpers

"memory" use
"control" use

{} () {} [
  p0: 4nx mplMalloc;
  4nx 65 p0 memset drop
  p1: 8nx 4nx p0 mplRealloc;
  p1 Nat8 addressToReference new 65n8 same printStack _:;
  8nx p1 mplFree
] "main" exportFunction

Expected Output During Compilation

TRUE

Runtime example

"memory" use
"String" use
"control" use

{} Int32 {} [
  p0: 4nx mplMalloc;
  4nx 65 p0 memset drop
  p1: 8nx 4nx p0 mplRealloc;
  p1 Nat8 addressToReference new 65n8 same print
  LF print
  8nx p1 mplFree
  0
] "main" exportFunction

Expected Output

TRUE

Raw memory functions


Cached allocation helpers

fastAllocate (size -- ptr)

Allocates memory, reusing cached blocks for sizes not greater than SL_MEMORY_MAX_CACHED_SIZE.

fastDeallocate (size ptr --)

Releases one block previously handled by fastAllocate or fastReallocate.

fastReallocate (newSize oldSize ptr -- newPtr)

Allocates one new block of newSize, copies the shared prefix of the old and new ranges, and releases the old block.


getHeapUsedSize (value -- size)

Returns the heap usage attributable to value as Natx.

Example

"memory" use
"control" use

{} () {} [
  {a: 1i32; b: 2i32;} getHeapUsedSize printStack _:;
] "main" exportFunction

Expected Output During Compilation

0nx

Debug allocation metrics

When debugMemory is TRUE, getMemoryMetrics (-- metrics) returns one record with the current allocation accounting.


See also