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
//! Layout constants for the Mnemosyne memory allocator.
/// The size of a segment (2MB).
pub const SEGMENT_SIZE: usize = 2 * 1024 * 1024;
/// The alignment of a segment (2MB).
pub const SEGMENT_ALIGN: usize = SEGMENT_SIZE;
/// The size of a page (64KB).
pub const PAGE_SIZE: usize = 64 * 1024;
/// The shift amount corresponding to PAGE_SIZE (e.g. 16 for 64KB).
pub const PAGE_SHIFT: usize = PAGE_SIZE.trailing_zeros as usize;
/// The alignment of a page (64KB).
pub const PAGE_ALIGN: usize = PAGE_SIZE;
/// The number of pages per segment (32).
pub const PAGES_PER_SEGMENT: usize = SEGMENT_SIZE / PAGE_SIZE;
/// The maximum size of a small allocation class (16KB).
///
/// Raised from 8KB (MN-461): a request above this ceiling takes the
/// large/huge path, which maps `size + SEGMENT_ALIGN + PAGE_SIZE` per
/// allocation, so every 8-16KB object reserved its own ~2MB segment and
/// paid 41 ns against 2.6 ns on the class path. Four classes (10240,
/// 12288, 14336, 16384) now pack four to twelve blocks per 64KB page.
pub const MAX_SMALL_ALLOC_SIZE: usize = 16 * 1024;
/// The smallest size-class block, in bytes.
///
/// This is the allocation fast-path alignment ceiling: requests whose
/// alignment is no larger than this value can be served from small pages.
pub const MIN_BLOCK_SIZE: usize = 16;
/// Maximum single allocation payload size accepted by public allocation entry points.
///
/// This mirrors Rust `Layout`'s pointer-offset safety bound: allocated object
/// sizes must not exceed `isize::MAX`.
pub const MAX_ALLOC_SIZE: usize = isizeMAX as usize;
/// The total number of small size classes.
///
/// Raised from 48 to 52 (MN-REF-1): the 8–16 KB range is now subdivided
/// into 1024-byte steps instead of 2048-byte steps, reducing worst-case
/// internal fragmentation from 25% to 12.5% for scientific workloads that
/// allocate in the 8–16 KB band (FFT buffers, simulation cells, etc.).
pub const NUM_SIZE_CLASSES: usize = 52;
/// The maximum number of segments retained in the global pool (compile-time limit).
pub const MAX_RETAINED_SEGMENTS_LIMIT: usize = 1024;
// Compile-time structural invariant checks.
//
// These `const _: () = assert!(...)` items are evaluated by the compiler
// before any code is generated, so any constant drift that breaks an
// allocator-wide layout assumption produces a hard build failure rather
// than a silent runtime fault. They cost zero bytes and zero instructions.
/// `SEGMENT_SIZE` and `PAGE_SIZE` must be powers of two so the bitmask-based
/// address rounding (`addr & !(SEGMENT_SIZE - 1)`, `addr & !(PAGE_SIZE - 1)`)
/// produces the correct base address.
const _: = assert!;
const _: = assert!;
/// `SEGMENT_ALIGN` is referenced as both an alignment cap (rejecting larger
/// alignments in `is_valid_alloc_request`) and as the rounding modulus for
/// page-to-segment recovery. The two roles must agree on the same value.
const _: = assert!;
const _: = assert!;
/// `PAGES_PER_SEGMENT * PAGE_SIZE == SEGMENT_SIZE` is the array-stride
/// derivation used by `Segment::initialize` and the small-free classifier.
const _: = assert!;
const _: = assert!;
/// At least one page must be available for small allocations after Page 0
/// is reserved for segment metadata.
const _: = assert!;
/// Every small allocation must fit inside a single page so `Page::initialize_free_list`
/// can lay out at least one block of every size class.
const _: = assert!;
/// `MAX_ALLOC_SIZE` is the public payload bound; it must be at least one
/// segment so `allocate_large_or_huge` can serve any request the
/// small-allocation path rejects.
const _: = assert!;
/// `NUM_SIZE_CLASSES` is the array dimension for per-class metadata
/// (`active_pages`, `full_pages`, `size_class_occupancy`); it must be
/// non-zero for the arrays to hold any state.
const _: = assert!;
const _: = assert!;
/// `MIN_BLOCK_SIZE` must divide `PAGE_SIZE` exactly so the densest page is
/// fully tiled with no trailing partial block, keeping the block-count
/// derivation above exact.
const _: = assert!;
/// `MIN_BLOCK_SIZE` must be a power of two for alignment arithmetic.
const _: = assert!;
/// `MAX_SMALL_ALLOC_SIZE` must be a power of two to simplify Lemire
/// reciprocal table bounds and keep the size-class table regular.
const _: = assert!;