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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//! Building large structs directly in their final memory.
//!
//! Without the `heap` feature an `OpusDecoder` holds all of its state
//! inline: 52 KB, or 153 KB with the neural network features. An
//! `OpusEncoder` holds 150 KB, or 210 KB with `dred`. A constructor that
//! returns such a struct by value builds it on the stack and copies it out,
//! often more than once. `OpusDecoder::init` and `OpusEncoder::init`
//! instead write every field through a raw pointer into memory the caller
//! provides. The large fields are filled by functions that take a pointer
//! to the field, so no large value is ever built on the stack.
//!
//! [`write_fields!`] writes the fields. It also checks at compile time that
//! the list names every field of the struct, so that adding a field
//! without writing it in place fails to compile instead of leaving memory
//! uninitialized.
//!
//! [`write_zero`] fills a field with zero bytes, for numbers, arrays of
//! numbers and the structs that `zeroable_new!` checks. The shared tables
//! (the CELT mode, the built-in neural network models) are built in place
//! too, through `compat::OnceCell::get_or_write`.
//!
//! With the feature `heap` the large fields are boxes. `boxed` and
//! `write_box` write their contents in place too. `OpusEncoder::reset_state`
//! writes a new encoder over the old one with `HeapMemory::Reuse`, which
//! keeps the memory of every box, so a reset allocates nothing.
//!
//! These functions keep large values off the stack in an optimized build.
//! An unoptimized build may still copy some through the stack.
/// Writes every field of the struct that `$this` (a `*mut Self`) points
/// to. Each entry is either `field: value`, which writes `value`, or
/// `field = init`, which calls `init` (a closure or function) with a raw
/// pointer to the field; `init` must initialize the whole field.
///
/// The list must name every field of `Self`, with the same `#[cfg]`
/// attributes as the struct; otherwise the struct expression in the
/// unused closure below does not compile.
///
/// Must be used inside an `unsafe` block, in an `impl` of the struct, and
/// `$this` must be valid for writes of `Self`.
) => ;
=> ;
=> ;
}
pub use write_fields;
/// A value of any type, for the struct expression of [`write_fields!`]
/// that is never evaluated.
pub
use Box;
/// A `Box<T>` whose contents `init` writes in place, so that `T` is never
/// built on the stack.
///
/// # Safety
/// `init` must initialize the whole `T` that its pointer points to.
pub unsafe
/// What the function that writes an encoder in place does with the heap
/// memory of the feature `heap`.
///
/// - `Allocate`: the encoder is new, so each box is allocated.
/// - `Reuse`: the memory holds an encoder already, which is being reset.
/// Each box it holds keeps its memory, and only the contents are written
/// anew, so a reset allocates nothing, as libopus `OPUS_RESET_STATE`.
///
/// Without `heap` an encoder holds no boxes, and both write the same.
pub
/// Writes a `Box<T>` to `p` whose contents `init` writes in place.
///
/// - With [`HeapMemory::Allocate`] the box is new, as from [`boxed`], and
/// `p` may be uninitialized.
/// - With [`HeapMemory::Reuse`] `p` holds a box already. Its contents are
/// dropped, and `init` writes the new contents into the same memory, so
/// nothing is allocated.
///
/// # Safety
/// `p` must be valid for writes, and with `Reuse` it must hold a valid
/// `Box<T>`. `init` must initialize the whole `T` that its pointer points
/// to. With `Reuse` it must not panic, because the box then holds a value
/// that was dropped.
pub unsafe
/// Types for which zero bytes are a valid value: the value their
/// constructor makes, if they have one. [`write_zero`] writes it in place.
///
/// # Safety
/// Zero bytes must be a valid value of the type, and the type must have no
/// destructor.
pub unsafe
zeroable_numbers!;
// SAFETY: an array of zero values is zero bytes.
unsafe
/// Implements [`Zeroable`] for structs with a `const fn new()`, after a
/// check at compile time that `new()` returns only zero bytes. The check
/// fails to compile when a field gets a value other than zero, or a type
/// with padding (whose bytes are not all initialized).
pub use zeroable_new;
/// Whether every byte is 0, for [`zeroable_new!`].
pub const
/// Writes zero bytes to `p`: for a [`Zeroable`] type, a valid value (the
/// one its constructor makes), written in place.
///
/// # Safety
/// `p` must be valid for writes of `T`.
pub unsafe