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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//! The configuration surface: the single trait a user implements to stand up an
//! allocator, plus compile-time validation of its constants.
//!
//! Everything the allocator needs to be tuned is gathered into one
//! [`Config`] trait. Implementing it is the whole configuration step:
//!
//! ```
//! use cadalloc::{Config, CoreAtomics};
//!
//! struct MyConfig;
//!
//! impl Config for MyConfig {
//! type Atomics = CoreAtomics;
//!
//! const MIN_ALIGN: u64 = 16;
//! const LNR_FLOOR: u64 = 32;
//! const EXP_FLOOR: u64 = 256;
//! const EXP_CEIL: u64 = 65536;
//! }
//!
//! // The constants are checked at compile time; this fails to build if they
//! // violate the power-of-two or ordering rules.
//! const _: () = cadalloc::assert_config_valid::<MyConfig>();
//! ```
//!
//! ## Size classes
//!
//! A request size maps to one of three regions, delimited by the four
//! constants (all of which must be powers of two):
//!
//! * **Linear** — from [`LNR_FLOOR`](Config::LNR_FLOOR) up to
//! [`EXP_FLOOR`](Config::EXP_FLOOR), classes step by
//! [`MIN_ALIGN`](Config::MIN_ALIGN). Small allocations, tightly spaced.
//! * **Exponential** — from [`EXP_FLOOR`](Config::EXP_FLOOR) up to
//! [`EXP_CEIL`](Config::EXP_CEIL), each power-of-two octave `[2^n, 2^(n+1))`
//! is split into two classes: `2^n` and `3 * 2^(n-1)` (i.e. `1.5 * 2^n`).
//! * **Oversized** — anything larger than [`EXP_CEIL`](Config::EXP_CEIL) spills
//! into a single oversized bucket.
//!
//! [`MIN_ALIGN`](Config::MIN_ALIGN) is also the minimum alignment of *every*
//! allocation, so it must be greater than `2^3` (i.e. at least 16).
//!
//! ## Heap region
//!
//! [`HEAP_BASE`](Config::HEAP_BASE) / [`HEAP_SIZE`](Config::HEAP_SIZE) describe
//! the region the allocator manages *when it is known at compile time* — the
//! common embedded case of a statically reserved buffer. When the region is
//! only known at runtime, leave those at `0` ("not applicable") and override
//! [`heap_base`](Config::heap_base) / [`heap_size`](Config::heap_size). The
//! free functions [`const_heap_base`] / [`const_heap_size`] read the
//! compile-time values in `const` contexts.
use crateAtomics;
/// The complete configuration of a `cadalloc` allocator.
///
/// A `Config` is a zero-sized policy type carrying only associated constants,
/// an [`Atomics`] backend, and (optionally) heap accessors. See the
/// [module documentation](self) for the size-class scheme and an example.
///
/// The four size constants plus `SPLIT_MIN` must all be **powers of two**, and
/// they must satisfy
/// `2 * MIN_ALIGN <= LNR_FLOOR <= EXP_FLOOR <= EXP_CEIL`, `SPLIT_MIN <= EXP_CEIL`,
/// with `MIN_ALIGN > 8`. (`CARVE_MAX` is unconstrained.) These rules are checked
/// at compile time by [`assert_config_valid`] and by the
/// [`VALIDATE`](Config::VALIDATE) associated constant.
/// Forces compile-time validation of a [`Config`]'s constants.
///
/// Call this in a `const` context to turn an invalid configuration into a build
/// error at the point of use:
///
/// ```
/// # use cadalloc::{Config, CoreAtomics};
/// # struct MyConfig;
/// # impl Config for MyConfig {
/// # type Atomics = CoreAtomics;
/// # const MIN_ALIGN: u64 = 16;
/// # const LNR_FLOOR: u64 = 32;
/// # const EXP_FLOOR: u64 = 256;
/// # const EXP_CEIL: u64 = 65536;
/// # }
/// const _: () = cadalloc::assert_config_valid::<MyConfig>();
/// ```
pub const
/// Returns the compile-time heap base of `C`, usable in `const` contexts.
///
/// This is [`Config::HEAP_BASE`]; it is `0` when the base is only known at
/// runtime, in which case use [`Config::heap_base`] instead.
pub const
/// Returns the compile-time heap size of `C`, usable in `const` contexts.
///
/// This is [`Config::HEAP_SIZE`]; it is `0` when the size is only known at
/// runtime, in which case use [`Config::heap_size`] instead.
pub const