|
| 1 | +// Copyright 2026 Stellar Development Foundation and contributors. Licensed |
| 2 | +// under the Apache License, Version 2.0. See the COPYING file at the root |
| 3 | +// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | + |
| 5 | +//! Rust global allocator backed by the C++ tcmalloc (gperftools) allocator that |
| 6 | +//! is linked into stellar-core. This is only compiled with the `tcmalloc` |
| 7 | +//! feature, which the build system enables whenever the C++ build uses tcmalloc |
| 8 | +//! (see `USE_TCMALLOC` in configure.ac / src/Makefile.am). Builds without |
| 9 | +//! tcmalloc (sanitizer or non-Linux builds) simply use Rust's default |
| 10 | +//! allocator. |
| 11 | +//! |
| 12 | +//! The `tc_*` symbols are provided by `libtcmalloc_minimal.a` and are left |
| 13 | +//! undefined in `librust_stellar_core.a`; they are resolved at the final C++ |
| 14 | +//! link step, exactly like the `shim_*` C++ functions imported by the bridge. |
| 15 | +//! No `#[link]` attribute or build script is required. |
| 16 | +
|
| 17 | +use core::ffi::c_void; |
| 18 | +use std::alloc::{GlobalAlloc, Layout}; |
| 19 | + |
| 20 | +extern "C" { |
| 21 | + fn tc_malloc(size: usize) -> *mut c_void; |
| 22 | + fn tc_free(ptr: *mut c_void); |
| 23 | + fn tc_realloc(ptr: *mut c_void, size: usize) -> *mut c_void; |
| 24 | + fn tc_calloc(nmemb: usize, size: usize) -> *mut c_void; |
| 25 | + fn tc_memalign(alignment: usize, size: usize) -> *mut c_void; |
| 26 | +} |
| 27 | + |
| 28 | +// tcmalloc, like the system malloc, guarantees alignment suitable for any |
| 29 | +// fundamental type (alignof(max_align_t)): 16 bytes on 64-bit, 8 on 32-bit. |
| 30 | +// Requests with a larger alignment need to go through tc_memalign. |
| 31 | +#[cfg(target_pointer_width = "64")] |
| 32 | +const MIN_ALIGN: usize = 16; |
| 33 | +#[cfg(target_pointer_width = "32")] |
| 34 | +const MIN_ALIGN: usize = 8; |
| 35 | + |
| 36 | +pub struct TcMalloc; |
| 37 | + |
| 38 | +// This implementation mirrors the standard library's unix allocator ( |
| 39 | +// https://github.com/rust-lang/rust/blob/225e91c03da22cd4b9792b83c1cfc97967101614/library/std/src/sys/alloc/unix.rs). |
| 40 | +// Normally aligned allocations use tc_malloc/tc_calloc/tc_realloc, over-aligned |
| 41 | +// ones use tc_memalign, and tc_free deallocates memory from any of these. |
| 42 | +unsafe impl GlobalAlloc for TcMalloc { |
| 43 | + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { |
| 44 | + if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { |
| 45 | + tc_malloc(layout.size()) as *mut u8 |
| 46 | + } else { |
| 47 | + tc_memalign(layout.align(), layout.size()) as *mut u8 |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { |
| 52 | + if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() { |
| 53 | + tc_calloc(1, layout.size()) as *mut u8 |
| 54 | + } else { |
| 55 | + let ptr = self.alloc(layout); |
| 56 | + if !ptr.is_null() { |
| 57 | + core::ptr::write_bytes(ptr, 0, layout.size()); |
| 58 | + } |
| 59 | + ptr |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { |
| 64 | + tc_free(ptr as *mut c_void); |
| 65 | + } |
| 66 | + |
| 67 | + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { |
| 68 | + if layout.align() <= MIN_ALIGN && layout.align() <= new_size { |
| 69 | + tc_realloc(ptr as *mut c_void, new_size) as *mut u8 |
| 70 | + } else { |
| 71 | + // tc_realloc cannot preserve an over-aligned allocation's alignment, |
| 72 | + // so allocate fresh, copy, and free the old block by hand. |
| 73 | + let new_layout = Layout::from_size_align_unchecked(new_size, layout.align()); |
| 74 | + let new_ptr = self.alloc(new_layout); |
| 75 | + if !new_ptr.is_null() { |
| 76 | + let copy = core::cmp::min(layout.size(), new_size); |
| 77 | + core::ptr::copy_nonoverlapping(ptr, new_ptr, copy); |
| 78 | + self.dealloc(ptr, layout); |
| 79 | + } |
| 80 | + new_ptr |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments