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
//! [`GlobalAlloc`] adapter, behind the `alloc` feature.
//!
//! With the `alloc` feature enabled, [`CadAlloc<C>`] implements
//! [`core::alloc::GlobalAlloc`], so a concrete configuration can back the
//! `alloc` crate as the program's `#[global_allocator]`:
//!
//! ```ignore
//! use cadalloc::{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; // a real, mapped region
//! const HEAP_SIZE: u64 = 1 << 20;
//! }
//!
//! #[global_allocator]
//! static GLOBAL: CadAlloc<MyConfig> = CadAlloc::new();
//!
//! fn main() {
//! // MUST run before the first allocation (see below).
//! GLOBAL.init().expect("prepare the heap");
//! let v = vec![1u8, 2, 3];
//! assert_eq!(v.len(), 3);
//! }
//! ```
//!
//! # You must call [`init`](CadAlloc::init) first
//!
//! The `#[global_allocator]` static is used the moment anything allocates, and
//! [`init`](CadAlloc::init) — which stamps the heap and positions the bump
//! pointer — is **not** run automatically. Call it once, from a single thread,
//! before the first allocation (in practice, the first line of `main` or an
//! equivalent startup hook). Allocating before `init` fails (returns null),
//! which the `alloc` crate turns into an allocation-error abort. There is no
//! lazy initialization: it could not be done race-free within this crate's
//! single-`init` contract.
//!
//! # Alignment
//!
//! `cadalloc` guarantees payloads are aligned to `MIN_ALIGN` (always `>= 16`),
//! and nothing stronger. A request whose [`Layout`] demands
//! `align > MIN_ALIGN` therefore **cannot be satisfied and returns null** (an
//! allocation-error abort at the `alloc`-crate level). Every primitive type and
//! most aggregates need `align <= 16`, so this only bites over-aligned types
//! (e.g. some SIMD vectors, `#[repr(align(N))]` with large `N`). Raise
//! `MIN_ALIGN` in the configuration to widen the ceiling if such types matter.
use crateCadAlloc;
use crateConfig;
use crateSlice;
use ;
// SAFETY: the `GlobalAlloc` contract is upheld by delegating to the allocator's
// own methods over the heap fixed by `C`:
//
// - `alloc` returns either null or a `MIN_ALIGN`-aligned block of at least
// `layout.size()` bytes; it refuses (returns null) any `layout` whose
// alignment exceeds `MIN_ALIGN`, so a non-null result always meets the
// requested alignment.
// - `dealloc`/`realloc` receive a pointer previously returned by `alloc` on the
// same allocator; `free`/`realloc` recover the true block size from its header
// (the slice length is ignored), so the `Layout` need only supply the pointer.
unsafe