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
//! Exporting a `cadalloc` allocator to C and other non-Rust callers.
//!
//! The public methods on [`CadAlloc`](crate::CadAlloc) are generic over the
//! [`Config`](crate::Config), so they monomorphize to a distinct function per
//! configuration and cannot themselves carry a stable, unmangled symbol name —
//! `#[no_mangle]` does not apply to a generic function. To link from C you pick
//! one concrete configuration and emit thin `extern "C"` shims for it, which is
//! exactly what [`export_c_api!`](crate::export_c_api) does.
//!
//! # ABI
//!
//! - [`Slice`](crate::Slice) is `#[repr(C)]` — `struct { uint64_t ptr, len; }` —
//! and is passed and returned by value.
//! - `alloc(size) -> Slice` returns [`Slice::NULL`](crate::Slice::NULL)
//! (a zero `ptr`) on failure.
//! - `realloc(Slice, new_size) -> Slice` returns the resized payload, or the
//! null slice on failure (leaving the original valid); a null input acts like
//! `alloc`.
//! - `free(Slice)` returns nothing; freeing the null slice is a no-op.
//! - `init() -> int32_t` and `verify() -> int32_t` return `0` on success, or a
//! positive code otherwise:
//! - `init`: `1` = misaligned heap, `2` = heap too small.
//! - `verify`: `1` = bad/absent marker, `2` = configuration mismatch.
//!
//! The allocator is stateless (its state lives in the heap), so the shims
//! construct the handle on each call for free.
//!
//! # Example
//!
//! ```
//! 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 HEAP_BASE: u64 = 0x2000_0000;
//! const HEAP_SIZE: u64 = 1 << 20;
//! }
//!
//! cadalloc::export_c_api! {
//! config: MyConfig,
//! init: cad_init,
//! alloc: cad_alloc,
//! realloc: cad_realloc,
//! free: cad_free,
//! verify: cad_verify,
//! }
//! ```
//!
//! This emits `cad_init`, `cad_alloc`, `cad_realloc`, `cad_free`, and
//! `cad_verify` as unmangled `extern "C"` symbols bound to `MyConfig`.
//!
//! # Calling convention
//!
//! The default calling convention is `"C"`. Add an `abi:` line (any ABI string
//! Rust's `extern` accepts, e.g. `"system"`, `"sysv64"`, `"aapcs"`) to override
//! it:
//!
//! ```
//! # 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 HEAP_BASE: u64 = 0x2000_0000;
//! # const HEAP_SIZE: u64 = 1 << 20;
//! # }
//! cadalloc::export_c_api! {
//! config: MyConfig,
//! abi: "system",
//! init: cad_init,
//! alloc: cad_alloc,
//! realloc: cad_realloc,
//! free: cad_free,
//! verify: cad_verify,
//! }
//! ```
/// Emits unmangled `extern "C"` entry points for one concrete
/// [`Config`](crate::Config).
///
/// See the [module documentation](crate::ffi) for the ABI and status codes. All
/// five names are required and given in the order shown:
///
/// ```
/// # 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 HEAP_BASE: u64 = 0x2000_0000;
/// # const HEAP_SIZE: u64 = 1 << 20;
/// # }
/// cadalloc::export_c_api! {
/// config: MyConfig,
/// init: cad_init,
/// alloc: cad_alloc,
/// realloc: cad_realloc,
/// free: cad_free,
/// verify: cad_verify,
/// }
/// ```