Scoop API

scoop:scoop.core:0.1.0

src/gc.scoop

Selected production source · src/gc.scoop

1// GC facilities (spec 14.1, M12 DESIGN section 4.2). Public operations are
2// ordinary unsafe core functions; pressure uses a direct C leaf binding.
4/**
5 * An outstanding explicit pin that keeps a managed object at a stable address until unpinned.
6 * Copying the token does not acquire another pin.
7 */
8public struct PinnedPtr<T : ref>(val raw: ULong)
10/**
11 * An outstanding native handle keeping a managed reference alive without promising a stable object
12 * address. Copying the token does not retain it.
13 */
14public struct GcHandle<T : ref>(val raw: ULong)
16@Unsafe
17@Intrinsic("gc_pin_raw")
18internal fun <T : ref> _pin(v: T): ULong
20@Unsafe
21@Intrinsic("gc_unpin_raw")
22internal fun <T : ref> _unpin(raw: ULong): T
24@Unsafe
25@Intrinsic("gc_get_handle_raw")
26internal fun <T : ref> _getGcHandle(v: T): ULong
28@Unsafe
29@Intrinsic("gc_release_handle_raw")
30internal fun <T : ref> _releaseGcHandle(raw: ULong): T
32/**
33 * Acquires an explicit pin on a managed object. Balance each acquired pin with exactly one unpin.
34 */
35@Unsafe
36public fun <T : ref> pin(v: T): PinnedPtr<T> = PinnedPtr<T>(_pin(v))
38/**
39 * Releases this acquired pin and returns the managed reference; do not reuse the released token.
40 */
41@Unsafe
42public fun <T : ref> unpin(p: PinnedPtr<T>): T = _unpin<T>(p.raw)
44/**
45 * Acquires a stable native handle that roots the managed reference until released.
46 */
47@Unsafe
48public fun <T : ref> getGcHandle(v: T): GcHandle<T> = GcHandle<T>(_getGcHandle(v))
50/**
51 * Releases this acquired handle and returns the managed reference; do not reuse the released
52 * token.
53 */
54@Unsafe
55public fun <T : ref> releaseGcHandle(h: GcHandle<T>): T = _releaseGcHandle<T>(h.raw)
57// Test-only hooks (not part of the spec): force a collection and
58// read the live object count.
59/**
60 * Forces a collection for diagnostics and runtime tests.
61 */
62@Intrinsic("rt_gc_collect")
63public fun gcCollect()
65/**
66 * Returns the runtime's live managed-object count for diagnostics and tests.
67 */
68@Intrinsic("rt_gc_stats")
69public fun gcStats(): ULong
71// A leaf hint only: it never collects, allocates, or accesses thread state.
72/**
73 * Adds a signed native-memory pressure hint. This leaf operation does not itself allocate or
74 * collect; release external memory with a matching negative adjustment.
75 */
76@GCLeaf
77@Extern(name = "scoop_rt_gc_adjust_pressure")
78public fun gcAdjustPressure(delta: Long)