vile / scheme / goops_weak_notd_gcboehm.v
30 lines · 25 sloc · 831 bytes · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import arena
4
5struct GoopsWeakValue {
6 value Value
7 lifetime ?arena.LifetimeToken
8}
9
10fn goops_weak(value Value) GoopsWeakValue {
11 return GoopsWeakValue{ value: value, lifetime: arena.lifetime_token(value.pointer()) }
12}
13
14// Resolve once and retain the returned Value throughout its use. Arena reset
15// and free still require the host's synchronization; a lifetime token is not a pin.
16@[inline]
17fn (reference &GoopsWeakValue) get() ?Value {
18 if lifetime := reference.lifetime {
19 if !lifetime.matches(reference.value.pointer()) { return none }
20 }
21 return reference.value
22}
23
24@[inline]
25fn (mut vm VM) goops_note_weak_reference(_reference &GoopsWeakValue) {
26 if v_arena_current() != vm.allocation_owner {
27 vm.goops.temporary_references++
28 if vm.goops.temporary_references >= 256 { vm.goops_prune_references() }
29 }
30}
31