nupp.mem.arena

A paged allocator of struct rows whose pages never move.

Rows are handed out in order from fixed-size pages of one struct type. A page is allocated once and stays where it is, so a row reference is good for as long as the arena is; growth adds a page beside the others rather than moving anything. A reset rewinds to the first row of the first page and keeps every page, which is what makes the next epoch allocation-free.

local arena = nupp.mem.arena

local struct Impact
    x: float
    y: float
end

local impacts = arena.new(Impact, 256)
local impact = impacts:acquire() -- zero-filled
impact.x = 1.5
impacts:release(impact)
impacts:reset() -- rewinds; the page stays

Every acquired row reads as zero, whether it is fresh or one a reset made available again. A release ends a lease and nothing more: the cursor does not rewind until reset, and reset refuses while any lease is live, so a row that is out is never handed out twice.

Storage comes from the cstorage capability, so the module is available on native LuaJIT and on the Wasm and browser backends, and refused by portable. Table-backed record instances are nupp.mem.pool.

Module contents

Constructors

ConstructorDescription
newCreates an arena of witness rows with one page of pageSize rows already allocated.

Types

TypeKindDescription
ArenarecordA paged allocator of rows of one struct type.

Constructors#

arena.newconstructor#

function arena.new<S>(witness: Type<S>, pageSize: integer?): arena.Arena<S>

Creates an arena of witness rows with one page of pageSize rows already allocated.

local struct Cell
    value: int32
end

local cells = arena.new(Cell, 16)
assert(cells:pages() == 1 and cells:used() == 0)

Type parameters

NameDescription
S

Arguments

NameTypeDescription
witnessType<S>

the struct declaration every row is laid out as

pageSizeinteger?

rows per page, 256 when omitted

Returns

TypeDescription
arena.Arena<S>

the arena, holding its first page

Raises

  • when pageSize is not a positive integer

Types#

Arenarecord#

record arena.Arena<S>
    readonly pageSize: integer
    acquire: function(exclusive self: Arena<S>): S
    release: function(exclusive self: Arena<S>, row: S): nil
    reset: function(exclusive self: Arena<S>): nil
    pages: function(self: Arena<S>): integer
    used: function(self: Arena<S>): integer
    live: function(self: Arena<S>): integer
end

A paged allocator of rows of one struct type.

Type parameters

NameDescription
S

Methods

acquire#
acquire: function(exclusive self: Arena<S>): S

Hands out the next row, zero-filled.

Arguments
NameTypeDescription
exclusive selfArena<S>
Returns
TypeDescription
S
release#
release: function(exclusive self: Arena<S>, row: S): nil

Ends a lease without rewinding the cursor.

Arguments
NameTypeDescription
exclusive selfArena<S>
rowS
Returns
TypeDescription
nil
Raises
  • when no lease is live

reset#
reset: function(exclusive self: Arena<S>): nil

Rewinds every used page's cursor and keeps the pages.

Arguments
NameTypeDescription
exclusive selfArena<S>
Returns
TypeDescription
nil
Raises
  • when any lease is live

pages#
pages: function(self: Arena<S>): integer

How many pages the arena holds.

Arguments
NameTypeDescription
selfArena<S>
Returns
TypeDescription
integer
used#
used: function(self: Arena<S>): integer

How many rows were handed out since the last reset.

Arguments
NameTypeDescription
selfArena<S>
Returns
TypeDescription
integer
live#
live: function(self: Arena<S>): integer

How many rows are acquired and not yet released.

Arguments
NameTypeDescription
selfArena<S>
Returns
TypeDescription
integer

Fields

pageSize#
pageSize: integer

How many rows each page holds.