vile / scheme / entry_storage.v
181 lines · 168 sloc · 6.47 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5// Only cross-owner growth borrows a different backing array. Restoring that
6// header before returning lets a native callback release its own arena while an
7// enclosing Scheme entry still uses the original registry.
8struct ActiveEntry {
9 previous []&Machine
10 allocation_owner voidptr
11 restore bool
12}
13
14// One small entry covers ordinary host callbacks without retaining the high
15// water mark of an unusually deep call. Include cached activations and every
16// private buffer's capacity, not just its currently live prefix.
17const retained_entry_storage_limit = 4096
18
19fn (mut vm VM) take_entry_machine(context u64, parameters &ParameterBinding) &Machine {
20 if vm.allocation_owner == v_arena_current() && !isnil(vm.spare_machine) {
21 mut machine := vm.spare_machine
22 vm.spare_machine = unsafe { nil }
23 machine.context = context
24 machine.logical_context = context
25 machine.parameters = parameters
26 machine.parameter_base = parameters
27 return machine
28 }
29 return &Machine{ context: context, logical_context: context, parameters: parameters, parameter_base: parameters }
30}
31
32// This runs after leave_entry, including all cleanup callbacks and registry
33// restoration. The captured entry owner also covers failed preparation before
34// registry publication. Caller arenas must never become persistent VM storage.
35fn (mut vm VM) finish_entry_machine(machine &Machine, allocation_owner voidptr, published bool) {
36 mut owned := unsafe { machine }
37 if !published || allocation_owner != vm.allocation_owner || allocation_owner != v_arena_current()
38 || !isnil(vm.spare_machine) || arena.allocation_failure() != none
39 || (!isnil(vm.owner.bounded_files) && vm.owner.bounded_files.interrupted != none)
40 || !owned.entry_storage_fits() {
41 owned.release_entry_storage()
42 return
43 }
44 // Clear complete capacities so a conservative collector cannot retain
45 // abandoned operands or callers through unused storage in the warm entry.
46 clear_entry_capacity(mut owned.stack)
47 clear_entry_capacity(mut owned.frames)
48 clear_entry_capacity(mut owned.bridge_sites)
49 for head in owned.spares { clear_entry_environment_capacity(head) }
50 clear_entry_environment_capacity(owned.wide_spares)
51 unsafe {
52 owned.stack.len = 0
53 owned.frames.len = 0
54 owned.bridge_sites.len = 0
55 }
56 stack := unsafe { owned.stack }
57 frames := unsafe { owned.frames }
58 bridge_sites := unsafe { owned.bridge_sites }
59 spares := owned.spares
60 wide_spares := owned.wide_spares
61 cached := owned.cached
62 // A fresh header resets every execution register and future fields too.
63 // Only private, cleared storage is carried to the next invocation.
64 unsafe {
65 *owned = Machine{
66 stack: stack, frames: frames, bridge_sites: bridge_sites,
67 spares: spares, wide_spares: wide_spares, cached: cached,
68 goops_cache_owner: allocation_owner
69 }
70 }
71 vm.spare_machine = owned
72}
73
74fn clear_entry_capacity[T](mut items []T) {
75 if items.cap > 0 { unsafe { vmemset(items.data, 0, items.cap * int(sizeof(T))) } }
76}
77
78fn clear_entry_environment_capacity(head &Environment) {
79 mut cursor := head
80 for !isnil(cursor) {
81 clear_entry_capacity(mut cursor.slots)
82 cursor = cursor.parent
83 }
84}
85
86fn (m &Machine) entry_storage_fits() bool {
87 mut remaining := retained_entry_storage_limit - int(sizeof(Machine))
88 if remaining < 0 { return false }
89 if m.stack.cap > remaining / int(sizeof(Value)) { return false }
90 remaining -= m.stack.cap * int(sizeof(Value))
91 if m.frames.cap > remaining / int(sizeof(CallFrame)) { return false }
92 remaining -= m.frames.cap * int(sizeof(CallFrame))
93 if m.bridge_sites.cap > remaining / int(sizeof(BridgeSite)) { return false }
94 remaining -= m.bridge_sites.cap * int(sizeof(BridgeSite))
95 for head in m.spares {
96 cost := entry_environment_storage(head, remaining)
97 if cost > remaining { return false }
98 remaining -= cost
99 }
100 return entry_environment_storage(m.wide_spares, remaining) <= remaining
101}
102
103fn entry_environment_storage(head &Environment, limit int) int {
104 mut remaining := limit
105 mut cursor := head
106 for !isnil(cursor) {
107 if int(sizeof(Environment)) > remaining { return limit + 1 }
108 remaining -= int(sizeof(Environment))
109 if cursor.slots.cap > remaining / int(sizeof(Value)) { return limit + 1 }
110 remaining -= cursor.slots.cap * int(sizeof(Value))
111 cursor = cursor.parent
112 }
113 return limit - remaining
114}
115
116fn (mut vm VM) enter_context(machine &Machine) ActiveEntry {
117 mut entry := ActiveEntry{}
118 if vm.active.len == vm.active.cap {
119 current := v_arena_current()
120 if current != vm.active_allocation_owner {
121 entry = ActiveEntry{
122 previous: unsafe { vm.active }
123 allocation_owner: vm.active_allocation_owner
124 restore: true
125 }
126 // Do not realloc/free the saved backing storage, even when arrays are
127 // otherwise allowed to grow in place. The new entry owns this copy.
128 mut grown := []&Machine{len: vm.active.len, cap: vm.active.len + 1, init: unsafe { &Machine(nil) }}
129 copy(mut grown, vm.active)
130 vm.active = unsafe { grown }
131 vm.active_allocation_owner = current
132 }
133 }
134 vm.active << machine
135 return entry
136}
137
138fn (mut vm VM) leave_entry(entry ActiveEntry) {
139 mut files := vm.owner.bounded_files
140 if !isnil(files) {
141 if reason := arena.allocation_failure() { files.interrupted = reason }
142 }
143 // The outer boundary closes descriptors, even if a callback intercepted
144 // the panic. Drop each abandoned entry before its machine goes out of scope.
145 if isnil(files) || files.interrupted == none { vm.leave_context() } else { vm.pop_active_context() }
146 if entry.restore {
147 unsafe { vm.active.free() }
148 vm.active = unsafe { entry.previous }
149 vm.active_allocation_owner = entry.allocation_owner
150 }
151}
152
153// Host entries explicitly own their Machine header. Release it only after
154// leave_entry has finished callbacks and removed the borrowed active pointer.
155// Stack/frame/bridge snapshots copy their buffers; published values and escaped
156// environments own separate storage and are deliberately not traversed here.
157fn (mut m Machine) release_entry_storage() {
158 for head in m.spares { release_entry_environments(head) }
159 release_entry_environments(m.wide_spares)
160 unsafe {
161 m.stack.free()
162 m.frames.free()
163 m.bridge_sites.free()
164 free(m)
165 }
166}
167
168// Only unescaped, cleared activations enter these private free lists. A live
169// env/frame register can still alias a cached activation after the final return;
170// freeing through those registers as well would destroy it twice.
171fn release_entry_environments(head &Environment) {
172 mut cursor := head
173 for !isnil(cursor) {
174 next := cursor.parent
175 unsafe {
176 cursor.slots.free()
177 free(cursor)
178 }
179 cursor = next
180 }
181}
182