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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//! `PrivData` — the per-library private data the BEAM hands back via
//! `enif_priv_data`.
//!
//! Hot code upgrade loads a second build of a NIF library beside the first.
//! The two builds may differ in compiler, allocator, or std layout, so the
//! only fields one build may read from another build's `PrivData` are the two
//! in the frozen `#[repr(C)]` header: `magic` and `user_priv_data`, at their
//! fixed offsets. Everything after the header (the resource-type [registry])
//! is build-private: each build constructs its own and drops it with its own
//! allocator in its own `unload`. See `docs/UPGRADE.md`.
//!
//! [registry]: ResourceRegistry
use TypeId;
use HashMap;
use c_void;
/// Layout-version tag for the frozen header. The trailing digit is bumped only
/// if the two header fields below ever change; it subsumes any separate
/// version field. Both sides of any upgrade are otter, so a mismatch is a
/// non-case in practice — the check exists purely to refuse to interpret
/// genuinely foreign private data.
pub const PRIV_MAGIC: u64 = u64from_be_bytes;
/// Library-private data owned by otter for the lifetime of one build of the
/// library.
///
/// The first two fields form the frozen cross-build header (see module docs);
/// `registry` is build-private and never read across the upgrade boundary.
/// Maps each registered resource type to its BEAM-side `enif_ffi::ResourceType`
/// pointer. Build-private; reconstructed fresh by every build.
// ---------------------------------------------------------------------------
// Lifecycle helpers used by generated load/unload scaffolding
// ---------------------------------------------------------------------------
/// Allocate a fresh [`PrivData`] and publish it into the BEAM's private-data
/// slot, so that registration during the load callback can populate it via
/// `enif_priv_data`.
///
/// Returns the raw pointer so a vetoed load can hand it back to
/// [`discard_priv_data`].
///
/// # Safety
///
/// `slot` must be the `*mut *mut c_void` priv-data slot passed to the load
/// callback by the BEAM.
pub unsafe
/// Free a [`PrivData`] published by [`install_priv_data`] and clear the slot.
/// Called when the user's load/upgrade callback vetoes.
///
/// # Safety
///
/// `pd` must have come from [`install_priv_data`] with this same `slot`, and
/// must not have been freed already.
pub unsafe
/// Free a [`PrivData`] in the unload callback, which receives the pointer by
/// value rather than through a slot.
///
/// # Safety
///
/// `pd` must have come from [`install_priv_data`] and must not have been freed
/// already.
pub unsafe
// ---------------------------------------------------------------------------
// Tier-2 (`raw`) user priv_data access
// ---------------------------------------------------------------------------
/// Pointer to the `user_priv_data` field of `pd`, for the tier-2 `_raw`
/// callbacks to read and write the user's own `void*`.
///
/// # Safety
///
/// `pd` must point at a live [`PrivData`].
pub unsafe
/// Pointer to the *old* build's `user_priv_data` field, reached through the
/// frozen `#[repr(C)]` header at `*old_slot`. Returns null if the old slot
/// holds no recognizable otter [`PrivData`] (null, or a magic mismatch) — the
/// caller then substitutes a scratch slot so the user always gets a valid
/// `void**`. This is the one place a build writes another build's `PrivData`,
/// and it touches only the frozen header word.
///
/// # Safety
///
/// `old_slot` must be the `*mut *mut c_void` old-priv-data slot the BEAM passes
/// to the upgrade callback.
pub unsafe