1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//! Thread-local heap binding -- the TLS routing truth for Phase 9.
//!
//! Each thread lazily initialises its own [`Heap`] on first use via
//! `std::thread_local!`. The heap is released when the thread exits (the TLS
//! destructor drops the `RefCell<Option<Heap>>`). This is allocation-free
//! init: `std::thread_local!` with a `const` initialiser does NOT call the
//! global allocator (it uses a linker-provided TLS slot on all major
//! platforms).
//!
//! **M5 reentrancy-freedom:** the TLS access is a plain thread-local load (no
//! lock, no alloc). The `Heap::new()` bootstrap calls the OS aperture
//! (`mmap`/`VirtualAlloc`) but never the global allocator. So the TLS init
//! path is reentrancy-free.
use RefCell;
use Heap;
thread_local!
/// Execute `f` with a mutable reference to the current thread's [`Heap`].
///
/// Lazily bootstraps the heap on first call. Returns `None` only if the
/// primordial segment reservation fails (OOM at startup -- unrecoverable for
/// an allocator, but we propagate gracefully).
///
/// # Panics
///
/// Panics if the TLS destructor has already run (thread is shutting down and
/// the TLS slot is poisoned). This is the standard `thread_local!` behaviour
/// and is acceptable: a thread that outlives its TLS is already in an
/// exceptional state.
/// Execute `f` with a mutable reference to the current thread's [`Heap`], or
/// return `None` if the heap cannot be accessed right now WITHOUT panicking.
///
/// This was the **no-panic** variant for the Phase 11 `GlobalAlloc` face.
/// Phase 12.3 rewired `SeferAlloc` to route through the registry-backed
/// raw-pointer TLS ([`crate::global::tls_heap`]) instead of this `RefCell`
/// binding, so `with_heap_try` is no longer on the malloc path. It is
/// retained as a documented part of the explicit-`Heap` API surface (and a
/// reference implementation of the no-panic TLS pattern); the `alloc` /
/// `alloc-xthread` paths and their tests still use [`with_heap`].
///
/// Returns `None` if the heap cannot be accessed (TLS destroyed, reentrant
/// borrow, or primordial OOM). The caller MUST handle `None` by returning null
/// -- never by panicking.
pub