numalloc 0.1.0

A blazing-fast, NUMA-aware memory allocator written in pure Rust
Documentation
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::Cell;
use std::ptr::NonNull;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicUsize, Ordering};

use crate::freelist::FreeBlock;
use crate::heap::GlobalHeap;
use crate::platform;
use crate::size_class::{self, SMALL_LIMIT};
use crate::thread_heap::{MADVISE_THRESHOLD, PerThreadHeap, REFILL_BATCH, max_thread_cache};

// ---------------------------------------------------------------------------
// Per-thread heap guard (cleanup on thread exit)
// ---------------------------------------------------------------------------

/// Thin wrapper around a [`PerThreadHeap`] pointer that drains cached freelist
/// blocks back to the per-node Treiber stacks when the owning thread exits.
/// This prevents progressive region exhaustion caused by short-lived threads
/// stranding blocks in their thread-local caches.
struct ThreadHeapSlot {
    inner: Cell<Option<NonNull<PerThreadHeap>>>,
}

impl ThreadHeapSlot {
    const fn new() -> Self {
        Self {
            inner: Cell::new(None),
        }
    }

    #[inline]
    fn get(&self) -> Option<NonNull<PerThreadHeap>> {
        self.inner.get()
    }

    #[inline]
    fn set(&self, val: Option<NonNull<PerThreadHeap>>) {
        self.inner.set(val);
    }
}

impl Drop for ThreadHeapSlot {
    fn drop(&mut self) {
        if let Some(mut th_ptr) = self.inner.get() {
            // SAFETY: `th_ptr` was allocated via `System.alloc` in
            // `NumaAlloc::thread_heap` and points to a valid `PerThreadHeap`.
            // The `GlobalHeap` is stored in a `static OnceLock` and outlives
            // all non-main threads; for the main thread, thread-locals are
            // destroyed before statics.
            unsafe {
                th_ptr.as_mut().drain_to_node_heap();
                System.dealloc(th_ptr.as_ptr() as *mut u8, Layout::new::<PerThreadHeap>());
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Per-instance thread-local storage
// ---------------------------------------------------------------------------

thread_local! {
    /// Pointer to the current thread's [`PerThreadHeap`].
    /// Allocated via the **system** allocator to avoid bootstrap recursion.
    /// The [`ThreadHeapSlot`] wrapper ensures cleanup on thread exit.
    static TH_PTR: ThreadHeapSlot = const { ThreadHeapSlot::new() };
}

// ---------------------------------------------------------------------------
// Large-object header (for mmap'd allocations)
// ---------------------------------------------------------------------------

#[repr(C)]
struct LargeHeader {
    original_ptr: NonNull<u8>,
    alloc_size: usize,
}

// ---------------------------------------------------------------------------
// NumaAlloc
// ---------------------------------------------------------------------------

/// NUMA-aware memory allocator.
///
/// Each instance owns an independent [`GlobalHeap`] and round-robin counter,
/// so multiple allocators do not compete for the same resources.
///
/// Use as `#[global_allocator]` or call [`GlobalAlloc`] methods directly.
///
/// ```rust,ignore
/// #[global_allocator]
/// static ALLOC: numalloc::NumaAlloc = numalloc::NumaAlloc::new();
/// ```
pub struct NumaAlloc {
    heap: OnceLock<GlobalHeap>,
    /// Round-robin counter for assigning threads to NUMA nodes.
    next_node: AtomicUsize,
}

// Safety: `OnceLock` and `AtomicUsize` are both `Send + Sync`.  The
// `GlobalHeap` stored inside the `OnceLock` is also `Send + Sync` (see
// heap.rs).
unsafe impl Send for NumaAlloc {}
unsafe impl Sync for NumaAlloc {}

impl Default for NumaAlloc {
    fn default() -> Self {
        Self::new()
    }
}

impl NumaAlloc {
    pub const fn new() -> Self {
        Self {
            heap: OnceLock::new(),
            next_node: AtomicUsize::new(0),
        }
    }

    fn heap(&self) -> &GlobalHeap {
        self.heap.get_or_init(|| {
            let topo = platform::detect_topology();
            GlobalHeap::new(topo.num_nodes).expect("numalloc: failed to mmap heap region")
        })
    }

    /// Obtain (or lazily create) the calling thread's [`PerThreadHeap`].
    ///
    /// The heap struct is allocated from the **system** allocator so that the
    /// very first allocation of a new thread doesn't recurse into NUMAlloc.
    fn thread_heap(&self) -> NonNull<PerThreadHeap> {
        // Fast path: try_with avoids panicking when TLS is being destroyed.
        if let Ok(Some(ptr)) = TH_PTR.try_with(ThreadHeapSlot::get) {
            return ptr;
        }

        // Slow path — first allocation on this thread.
        let heap = self.heap();
        let node = self.next_node.fetch_add(1, Ordering::Relaxed) % heap.num_nodes();

        // Bind thread to its NUMA node (no-op on non-Linux).
        platform::bind_thread_to_node(node);

        // Allocate PerThreadHeap from the system allocator.
        let layout = Layout::new::<PerThreadHeap>();
        let raw = unsafe { System.alloc(layout) } as *mut PerThreadHeap;
        let Some(nn) = NonNull::new(raw) else {
            std::alloc::handle_alloc_error(layout);
        };
        unsafe {
            nn.as_ptr()
                .write(PerThreadHeap::new(node, NonNull::from(heap)));
        }

        let _ = TH_PTR.try_with(|slot| slot.set(Some(nn)));
        nn
    }
}

unsafe impl GlobalAlloc for NumaAlloc {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        let effective_size = layout.size().max(layout.align());

        // --- large object path ---
        if effective_size > SMALL_LIMIT {
            return unsafe { self.alloc_large(layout) };
        }

        let class_idx = match size_class::size_class_index(effective_size) {
            Some(i) => i,
            None => return std::ptr::null_mut(),
        };

        let th = unsafe { self.thread_heap().as_mut() };
        let heap = self.heap();
        let node = th.node_id;
        let fl = th.freelist_mut(class_idx);

        // 1. Try per-thread freelist.
        if let Some(block) = fl.pop() {
            return block.as_ptr().cast();
        }

        // 2. Refill from per-node freelist.
        //    Pop individually (each is one CAS) but batch-insert into the
        //    thread freelist via push_chain for O(1) insertion.
        let node_fl = heap.node_region(node).node_heap.freelist(class_idx);
        if let Some(first) = node_fl.pop() {
            unsafe { first.as_ref().write_next(None) };
            let mut tail = first;
            let mut count = 1usize;
            while count < REFILL_BATCH {
                let Some(b) = node_fl.pop() else { break };
                unsafe { b.as_ref().write_next(None) };
                unsafe { tail.as_ref().write_next(Some(b)) };
                tail = b;
                count += 1;
            }
            fl.push_chain(first, tail, count);
            return fl.pop().unwrap().as_ptr().cast();
        }

        // 3. Allocate a new bag and carve it into objects.
        let region = heap.node_region(node);
        let bag_size = size_class::bag_size_for_class(class_idx);
        let Some(bag) = region.allocate_bag(bag_size) else {
            // Region exhausted — fall back to mmap for this allocation.
            return unsafe { self.alloc_large(layout) };
        };

        let obj_size = size_class::size_for_class(class_idx);
        let count = bag_size / obj_size;
        for i in 0..count {
            let obj =
                unsafe { NonNull::new_unchecked(bag.as_ptr().add(i * obj_size) as *mut FreeBlock) };
            fl.push(obj);
        }

        fl.pop().unwrap().as_ptr().cast()
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        let Some(ptr) = NonNull::new(ptr) else { return };
        let effective_size = layout.size().max(layout.align());

        // --- large object path ---
        if effective_size > SMALL_LIMIT {
            unsafe { dealloc_large(ptr, Some(self)) };
            return;
        }

        let heap = self.heap();

        if !heap.is_owned(ptr) {
            // Pointer not from our region — treat as large (mmap'd).
            unsafe { dealloc_large(ptr, Some(self)) };
            return;
        }

        let class_idx = match size_class::size_class_index(effective_size) {
            Some(i) => i,
            None => return,
        };

        let origin_node = match heap.node_for_ptr(ptr) {
            Some(n) => n,
            None => return,
        };

        let th = unsafe { self.thread_heap().as_mut() };
        let current_node = th.node_id;
        let block = ptr.cast::<FreeBlock>();

        if origin_node == current_node {
            // Local deallocation — push to per-thread freelist (no sync).
            let fl = th.freelist_mut(class_idx);
            fl.push(block);

            // Drain excess to per-node heap.
            let cache_limit = max_thread_cache(class_idx);
            if fl.count() > cache_limit
                && let Some((head, tail, _)) = fl.drain(cache_limit / 2)
            {
                heap.node_region(current_node)
                    .node_heap
                    .freelist(class_idx)
                    .push_chain(head, tail);
            }
        } else {
            // Remote deallocation — push directly to origin node's per-node
            // freelist (lock-free, one CAS).
            heap.node_region(origin_node)
                .node_heap
                .freelist(class_idx)
                .push(block);
        }
    }

    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        let effective_size = layout.size().max(layout.align());

        if effective_size > SMALL_LIMIT {
            // Large path: mmap already returns zeroed pages — the header is
            // written *before* the returned pointer, so the payload is clean.
            return unsafe { self.alloc(layout) };
        }

        // Small path: memory may come from a freelist (stale data), so zero it.
        let ptr = unsafe { self.alloc(layout) };
        if !ptr.is_null() {
            unsafe { std::ptr::write_bytes(ptr, 0, layout.size()) };
        }
        ptr
    }

    unsafe fn realloc(&self, ptr: *mut u8, old_layout: Layout, new_size: usize) -> *mut u8 {
        let old_effective = old_layout.size().max(old_layout.align());
        let new_effective = new_size.max(old_layout.align());

        // If both old and new land in the same small size class the existing
        // allocation already has enough room — return the pointer as-is.
        if old_effective <= SMALL_LIMIT
            && new_effective <= SMALL_LIMIT
            && let (Some(old_cls), Some(new_cls)) = (
                size_class::size_class_index(old_effective),
                size_class::size_class_index(new_effective),
            )
            && old_cls == new_cls
        {
            return ptr;
        }

        // General case: allocate → copy → deallocate.
        let new_layout = unsafe { Layout::from_size_align_unchecked(new_size, old_layout.align()) };
        let new_ptr = unsafe { self.alloc(new_layout) };
        if new_ptr.is_null() {
            return new_ptr;
        }
        let copy_size = old_layout.size().min(new_size);
        unsafe {
            std::ptr::copy_nonoverlapping(ptr, new_ptr, copy_size);
            self.dealloc(ptr, old_layout);
        }
        new_ptr
    }
}

// ---------------------------------------------------------------------------
// Large-object helpers (mmap/munmap)
// ---------------------------------------------------------------------------

impl NumaAlloc {
    /// Compute the total mmap size needed for a large allocation.
    #[inline]
    fn large_alloc_size(layout: &Layout) -> usize {
        let page_size = platform::page_size();
        let header_size = std::mem::size_of::<LargeHeader>();
        let align = layout.align().max(std::mem::align_of::<LargeHeader>());
        let alloc_size = header_size + (align - 1) + layout.size();
        (alloc_size + page_size - 1) & !(page_size - 1)
    }

    /// Place the [`LargeHeader`] and return the payload pointer for a given
    /// raw mmap base, alloc size, and requested layout.
    #[inline]
    unsafe fn prepare_large_payload(
        raw: NonNull<u8>,
        alloc_size: usize,
        layout: &Layout,
    ) -> *mut u8 {
        let header_size = std::mem::size_of::<LargeHeader>();
        let align = layout.align().max(std::mem::align_of::<LargeHeader>());
        let payload_addr = (raw.as_ptr() as usize + header_size + align - 1) & !(align - 1);

        // SAFETY: payload_addr - header_size is within the mmap region and
        // correctly aligned for LargeHeader.
        let header_ptr =
            unsafe { NonNull::new_unchecked((payload_addr - header_size) as *mut LargeHeader) };
        unsafe {
            header_ptr.as_ptr().write(LargeHeader {
                original_ptr: raw,
                alloc_size,
            });
        }
        payload_addr as *mut u8
    }

    /// Allocate a large object backed by its own `mmap` region.
    ///
    /// A [`LargeHeader`] is placed just before the returned pointer so that
    /// [`dealloc_large`] can recover the original mmap address and size.
    unsafe fn alloc_large(&self, layout: Layout) -> *mut u8 {
        let alloc_size = Self::large_alloc_size(&layout);
        let th = unsafe { self.thread_heap().as_mut() };

        // Fast path: check per-thread large cache.
        if let Some((raw, _)) = th.large_cache_take(alloc_size) {
            return unsafe { Self::prepare_large_payload(raw, alloc_size, &layout) };
        }

        // Slow path: mmap a fresh region.
        let Some(raw) = (unsafe { platform::mmap_anonymous(alloc_size) }) else {
            return std::ptr::null_mut();
        };

        // Bind to the current thread's NUMA node.
        let node = th.node_id;
        unsafe {
            platform::bind_to_node(raw, alloc_size, node);
        }

        unsafe { Self::prepare_large_payload(raw, alloc_size, &layout) }
    }

    /// Try to cache a freed large mapping; returns `false` if the cache is
    /// full (caller should `munmap`).
    #[inline]
    fn try_cache_large(&self, original: NonNull<u8>, alloc_size: usize) -> bool {
        if let Ok(Some(mut th)) = TH_PTR.try_with(ThreadHeapSlot::get) {
            let th = unsafe { th.as_mut() };
            if th.large_cache_put(original, alloc_size) {
                // Only release physical pages for large regions; for smaller
                // ones the madvise syscall overhead exceeds the savings.
                if alloc_size >= MADVISE_THRESHOLD {
                    // SAFETY: original/alloc_size describe a valid mmap region.
                    unsafe {
                        platform::madvise_dontneed(original, alloc_size);
                    }
                }
                return true;
            }
        }
        false
    }
}

/// Free a large object previously returned by [`NumaAlloc::alloc_large`].
///
/// # Safety
/// `ptr` must have been returned by `alloc_large`.  `allocator` is used to
/// attempt caching; pass `None` to force immediate `munmap`.
unsafe fn dealloc_large(ptr: NonNull<u8>, allocator: Option<&NumaAlloc>) {
    let header_size = std::mem::size_of::<LargeHeader>();
    // SAFETY: ptr was returned by prepare_large_payload; subtracting
    // header_size yields the LargeHeader that was written at allocation time.
    let header_ptr =
        unsafe { NonNull::new_unchecked(ptr.as_ptr().sub(header_size) as *mut LargeHeader) };
    let header = unsafe { header_ptr.as_ref() };
    let original = header.original_ptr;
    let size = header.alloc_size;

    // Try to cache for reuse.
    if let Some(alloc) = allocator
        && alloc.try_cache_large(original, size)
    {
        return;
    }

    unsafe {
        platform::munmap(original, size);
    }
}