module scheme import arena // Only cross-owner growth borrows a different backing array. Restoring that // header before returning lets a native callback release its own arena while an // enclosing Scheme entry still uses the original registry. struct ActiveEntry { previous []&Machine allocation_owner voidptr restore bool } // One small entry covers ordinary host callbacks without retaining the high // water mark of an unusually deep call. Include cached activations and every // private buffer's capacity, not just its currently live prefix. const retained_entry_storage_limit = 4096 fn (mut vm VM) take_entry_machine(context u64, parameters &ParameterBinding) &Machine { if vm.allocation_owner == v_arena_current() && !isnil(vm.spare_machine) { mut machine := vm.spare_machine vm.spare_machine = unsafe { nil } machine.context = context machine.logical_context = context machine.parameters = parameters machine.parameter_base = parameters return machine } return &Machine{ context: context, logical_context: context, parameters: parameters, parameter_base: parameters } } // This runs after leave_entry, including all cleanup callbacks and registry // restoration. The captured entry owner also covers failed preparation before // registry publication. Caller arenas must never become persistent VM storage. fn (mut vm VM) finish_entry_machine(machine &Machine, allocation_owner voidptr, published bool) { mut owned := unsafe { machine } if !published || allocation_owner != vm.allocation_owner || allocation_owner != v_arena_current() || !isnil(vm.spare_machine) || arena.allocation_failure() != none || (!isnil(vm.owner.bounded_files) && vm.owner.bounded_files.interrupted != none) || !owned.entry_storage_fits() { owned.release_entry_storage() return } // Clear complete capacities so a conservative collector cannot retain // abandoned operands or callers through unused storage in the warm entry. clear_entry_capacity(mut owned.stack) clear_entry_capacity(mut owned.frames) clear_entry_capacity(mut owned.bridge_sites) for head in owned.spares { clear_entry_environment_capacity(head) } clear_entry_environment_capacity(owned.wide_spares) unsafe { owned.stack.len = 0 owned.frames.len = 0 owned.bridge_sites.len = 0 } stack := unsafe { owned.stack } frames := unsafe { owned.frames } bridge_sites := unsafe { owned.bridge_sites } spares := owned.spares wide_spares := owned.wide_spares cached := owned.cached // A fresh header resets every execution register and future fields too. // Only private, cleared storage is carried to the next invocation. unsafe { *owned = Machine{ stack: stack, frames: frames, bridge_sites: bridge_sites, spares: spares, wide_spares: wide_spares, cached: cached, goops_cache_owner: allocation_owner } } vm.spare_machine = owned } fn clear_entry_capacity[T](mut items []T) { if items.cap > 0 { unsafe { vmemset(items.data, 0, items.cap * int(sizeof(T))) } } } fn clear_entry_environment_capacity(head &Environment) { mut cursor := head for !isnil(cursor) { clear_entry_capacity(mut cursor.slots) cursor = cursor.parent } } fn (m &Machine) entry_storage_fits() bool { mut remaining := retained_entry_storage_limit - int(sizeof(Machine)) if remaining < 0 { return false } if m.stack.cap > remaining / int(sizeof(Value)) { return false } remaining -= m.stack.cap * int(sizeof(Value)) if m.frames.cap > remaining / int(sizeof(CallFrame)) { return false } remaining -= m.frames.cap * int(sizeof(CallFrame)) if m.bridge_sites.cap > remaining / int(sizeof(BridgeSite)) { return false } remaining -= m.bridge_sites.cap * int(sizeof(BridgeSite)) for head in m.spares { cost := entry_environment_storage(head, remaining) if cost > remaining { return false } remaining -= cost } return entry_environment_storage(m.wide_spares, remaining) <= remaining } fn entry_environment_storage(head &Environment, limit int) int { mut remaining := limit mut cursor := head for !isnil(cursor) { if int(sizeof(Environment)) > remaining { return limit + 1 } remaining -= int(sizeof(Environment)) if cursor.slots.cap > remaining / int(sizeof(Value)) { return limit + 1 } remaining -= cursor.slots.cap * int(sizeof(Value)) cursor = cursor.parent } return limit - remaining } fn (mut vm VM) enter_context(machine &Machine) ActiveEntry { mut entry := ActiveEntry{} if vm.active.len == vm.active.cap { current := v_arena_current() if current != vm.active_allocation_owner { entry = ActiveEntry{ previous: unsafe { vm.active } allocation_owner: vm.active_allocation_owner restore: true } // Do not realloc/free the saved backing storage, even when arrays are // otherwise allowed to grow in place. The new entry owns this copy. mut grown := []&Machine{len: vm.active.len, cap: vm.active.len + 1, init: unsafe { &Machine(nil) }} copy(mut grown, vm.active) vm.active = unsafe { grown } vm.active_allocation_owner = current } } vm.active << machine return entry } fn (mut vm VM) leave_entry(entry ActiveEntry) { mut files := vm.owner.bounded_files if !isnil(files) { if reason := arena.allocation_failure() { files.interrupted = reason } } // The outer boundary closes descriptors, even if a callback intercepted // the panic. Drop each abandoned entry before its machine goes out of scope. if isnil(files) || files.interrupted == none { vm.leave_context() } else { vm.pop_active_context() } if entry.restore { unsafe { vm.active.free() } vm.active = unsafe { entry.previous } vm.active_allocation_owner = entry.allocation_owner } } // Host entries explicitly own their Machine header. Release it only after // leave_entry has finished callbacks and removed the borrowed active pointer. // Stack/frame/bridge snapshots copy their buffers; published values and escaped // environments own separate storage and are deliberately not traversed here. fn (mut m Machine) release_entry_storage() { for head in m.spares { release_entry_environments(head) } release_entry_environments(m.wide_spares) unsafe { m.stack.free() m.frames.free() m.bridge_sites.free() free(m) } } // Only unescaped, cleared activations enter these private free lists. A live // env/frame register can still alias a cached activation after the final return; // freeing through those registers as well would destroy it twice. fn release_entry_environments(head &Environment) { mut cursor := head for !isnil(cursor) { next := cursor.parent unsafe { cursor.slots.free() free(cursor) } cursor = next } }