Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-browser-foundation

Source of release 0.6.0

0.6.0 / src / gc / weak_block.lucb

#==============================================================================================
#
#   weak_block - WeakBlock: a block of WeakImpls
#
#   DESCRIPTION:
#       Port of Libraries/LibGC/WeakBlock.cpp (lines 1-77) and WeakBlock.h (lines 1-45). A
#       block's free impls form a freelist threaded through their pointers. An impl goes back to
#       the freelist when its cell dies, not when its reference count drops to zero, and its
#       generation then moves on so that the Weaks still holding it read none (weak.lucb).
#
#==============================================================================================

## Ported from Libraries/LibGC/WeakBlock.cpp:20 GC::WeakBlock::create.
pub func weak_block_create() -> WeakBlock*:
    let block = (WeakBlock*)map_aligned_block(weak_block_block_size)
    weak_block_construct(block)
    return block

## Ported from Libraries/LibGC/WeakBlock.cpp:32 GC::WeakBlock::WeakBlock.
pub func weak_block_construct(this: WeakBlock*):
    this.m_list_node = ListNode()
    for i in 0..<weak_block_impl_count:
        let next: void*? = &this.m_impls[i + 1] if i + 1 < weak_block_impl_count else none
        this.m_impls[i].m_generation = 0
        weak_impl_set_ptr(&this.m_impls[i], next)
        weak_impl_set_state(&this.m_impls[i], WeakImplState.freelist)
    this.m_freelist = &this.m_impls[0]

## Ported from Libraries/LibGC/WeakBlock.cpp:43 GC::WeakBlock::allocate.
pub func weak_block_allocate(this: WeakBlock*, cell: Cell*?) -> WeakImpl*?:
    let impl = this.m_freelist else return none
    this.m_freelist = (WeakImpl*?)weak_impl_ptr(impl)
    weak_impl_set_ptr(impl, cell)
    weak_impl_set_state(impl, WeakImplState.allocated)
    return impl

## Ported from Libraries/LibGC/WeakBlock.cpp:55 GC::WeakBlock::deallocate. The Weaks made with
## the impl's current generation read none from now on.
pub func weak_block_deallocate(this: WeakBlock*, impl: WeakImpl*):
    impl.m_generation += 1
    weak_impl_set_ptr(impl, this.m_freelist)
    weak_impl_set_state(impl, WeakImplState.freelist)
    this.m_freelist = impl

## Ported from Libraries/LibGC/WeakBlock.h:24 GC::WeakBlock::can_allocate.
pub func weak_block_can_allocate(this: const WeakBlock*) -> bool:
    return this.m_freelist != none

## Ported from Libraries/LibGC/WeakBlock.cpp:63 GC::WeakBlock::sweep. C++ clears the impls of
## dead cells and frees the impls no Weak holds; the port frees the impls of dead cells (their
## Weaks read none through the generation) and forgets them in `impls_by_cell`, the heap's
## impl of each cell (heap_create_weak_impl).
pub func weak_block_sweep(this: WeakBlock*, impls_by_cell: GcHashMap[WeakImpl*]*):
    for i in 0..<weak_block_impl_count:
        let impl = &this.m_impls[i]
        if weak_impl_state(impl) == WeakImplState.freelist:
            continue
        if let cell = (Cell*?)weak_impl_ptr(impl):
            if cell_is_marked(cell):
                continue
            gc_hash_map_remove(impls_by_cell, (usize)cell)
        weak_impl_set_ptr(impl, none)
        weak_block_deallocate(this, impl)

## The WeakBlock whose m_list_node is `node` (IntrusiveList's container_of).
func weak_block_from_list_node(node: ListNode*) -> WeakBlock*:
    return (WeakBlock*)((u8*)node - memory.offset_of(WeakBlock, m_list_node))