Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-base

Source of release 0.45.0

0.45.0 / src / std / ownership / pool.lucb

#==============================================================================================
#
#   pool - The temporaries pool
#
#   DESCRIPTION:
#       Owned values handed on as temporaries, released together by the next `drain`; the
#       generators emit `mark`/`drain` pairs around a statement's operands.
#
#==============================================================================================

# mark: The temporaries pool ===================================================================

struct Pending:
    var object: Object*?
    var cell: void*?
    var drop: (func(void*) -> unit)?
    var size: usize

local var pending: Pending[]
local var pending_count: usize = 0

## The current pool position, to `drain` back to later.
pub func mark() -> usize:
    return pending_count

func push_pending(p: Pending):
    if pending_count == pending.length:
        pending = grow(pending, pending_count, 64)
    pending[pending_count] = p
    pending_count += 1

## An owned object handed on as a temporary: released by the next `drain`.
pub func pool_object(o: Object*) -> Object*:
    push_pending(Pending(object = o, cell = none, drop = none, size = 0))
    return o

## An owned value of any type handed on as a temporary; `drop` releases what it holds.
pub func pool_value[T](v: T, drop: func(void*) -> unit) -> T:
    let block = memory.heap.allocate(memory.size_of(T), memory.align_of(T)) else trap("out of memory")
    memory.write[T](block.data, v)
    push_pending(Pending(object = none, cell = block.data, drop = drop, size = memory.size_of(T)))
    return v

## Releases every temporary pooled since `mark`, in the order they were pooled.
pub func drain(since: usize):
    if pending_count <= since:
        return
    let count = pending_count
    var i = since
    while i < count:
        let p = pending[i]
        if let o = p.object:
            release(o)
        elif let cell = p.cell:
            if let d = p.drop:
                d(cell)
            release_cell(cell, p.size)
        i += 1
    # a release pools nothing of its own, so nothing was added above `count`
    pending_count = since

## Free a pooled value's block of `size` bytes, which `pool_value` took from the heap.
func release_cell(cell: void*, size: usize):
    free(u8[]((u8*)cell, size)) in memory.heap

## `items` in heap storage with room for twice as many (`least` when it had none), its
## first `count` kept and its old storage freed: the growth of the runtime's own lists.
## Out of memory traps, as the bookkeeping has no failure to report into.
func grow[T](items: T[], count: usize, least: usize) -> T[]:
    let grown = new T[items.length * 2 if items.length > 0 else least] --- in memory.heap catch failure:
        trap("out of memory")
    for i in 0..<count:
        grown[i] = items[i]
    if items.length > 0:
        free(items) in memory.heap
    return grown