Skip to content

Commit b01c874

Browse files
authored
test: complete no-concurrency Lean model of refcounting (#14910)
This PR extends the existing Lean specifications of `lean_inc_ref_n` and `lean_dec_ref` with a corollary that for any sequence of such ops, the implementation is a refinement of the behavior of ideal, unbounded reference counting.
1 parent 2a7175c commit b01c874

3 files changed

Lines changed: 564 additions & 249 deletions

File tree

src/include/lean/lean.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,12 @@ The reference counter `m_rc` field also encodes whether the object is single thr
125125
reference counting is not needed (== 0). We don't use reference counting for objects stored in compact regions, or
126126
marked as persistent.
127127
128-
Single-threaded counts grow upward (0, 1, 2, ...); multi-threaded counts grow downward (a count of N is stored as
129-
-N and adjusted atomically). To stay memory-safe when a count would exceed the 32-bit range, we reserve a band of
130-
deeply negative values as "sticky": a single-threaded count that overflows past INT_MAX wraps directly into it, and
131-
a multi-threaded count descending toward INT_MIN is caught in it before it can wrap. Once in the sticky range the
132-
object is frozen: it is never freed and its count is no longer adjusted. See `LEAN_RC_STICKY` / `LEAN_RC_STICKY_DROP`
133-
for the exact thresholds. This trades an unbounded but astronomically rare memory leak for memory safety under
134-
reference-count over/underflow.
128+
To stay memory-safe when a count would exceed the 32-bit range, we reserve a band of deeply negative
129+
values as "sticky": a single-threaded count that overflows past INT_MAX wraps directly into it, and
130+
a multi-threaded count descending toward INT_MIN is caught in it before it can wrap. Once in the
131+
sticky range the object is frozen: it is never freed and its count is no longer adjusted. See
132+
`LEAN_RC_STICKY` / `LEAN_RC_STICKY_DROP` for the exact thresholds. This trades an unbounded but
133+
practically exceedingly unlikely memory leak for memory safety under reference-count over/underflow.
135134
136135
For "small" objects stored in compact regions, the field `m_cs_sz` contains the object size. For "small" objects not
137136
stored in compact regions, we use the page information to retrieve its size so that we can reuse

0 commit comments

Comments
 (0)