| @@ -5,7 +5,7 @@ |
| 5 | 5 | //! iterative so dropping a long list cannot overflow a small fiber stack, |
| 6 | 6 | //! and shared (atomic) counts. |
| 7 | 7 | //! |
| 8 | | −//! Blocks come from [`BLOCKS`]: mimalloc with the `mimalloc` feature (the |
| 8 | +//! Blocks come from [`blocks`]: mimalloc with the `mimalloc` feature (the |
| 9 | 9 | //! default), the system allocator without it. Only blocks: the runtime's |
| 10 | 10 | //! own Rust allocations (queues, mailboxes, worklists) keep Rust's global |
| 11 | 11 | //! allocator. A block may be freed on another thread than the one that |
| @@ -13,7 +13,7 @@ |
| 13 | 13 | //! Sanitizer builds link a runtime built without the feature, so ASan sees |
| 14 | 14 | //! every block (`scripts/ci.sh`). |
| 15 | 15 | |
| 16 | | −use std::alloc::{GlobalAlloc, Layout, handle_alloc_error}; |
| 16 | +use std::alloc::{Layout, handle_alloc_error}; |
| 17 | 17 | use std::sync::atomic::{AtomicI32, Ordering}; |
| 18 | 18 | |
| 19 | 19 | use crate::abi::{ |
| @@ -30,10 +30,66 @@ pub struct DropStack { |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | /// The allocator of blocks (see the module documentation). |
| 33 | +/// |
| 34 | +/// mimalloc is called directly: its `GlobalAlloc` (the `mimalloc` crate) |
| 35 | +/// always takes the aligned allocation path, and blocks need no more than |
| 36 | +/// the 8-byte alignment every mimalloc allocation has. Freeing needs no |
| 37 | +/// size, so the free path skips computing the block's layout (issue #61). |
| 33 | 38 | #[cfg(feature = "mimalloc")] |
| 34 | | −static BLOCKS: mimalloc::MiMalloc = mimalloc::MiMalloc; |
| 39 | +mod blocks { |
| 40 | + use std::alloc::Layout; |
| 41 | + |
| 42 | + /// # Safety |
| 43 | + /// `layout.size()` must be non-zero. |
| 44 | + pub(super) unsafe fn alloc(layout: Layout) -> *mut u8 { |
| 45 | + // SAFETY: mimalloc returns 16-byte aligned memory, or null. |
| 46 | + unsafe { libmimalloc_sys::mi_malloc(layout.size()) }.cast() |
| 47 | + } |
| 48 | + |
| 49 | + /// # Safety |
| 50 | + /// `b` must be a live block, from [`alloc`]. |
| 51 | + pub(super) unsafe fn free(b: *mut crate::abi::Block) { |
| 52 | + // SAFETY: forwarded caller guarantee. |
| 53 | + unsafe { libmimalloc_sys::mi_free(b.cast()) } |
| 54 | + } |
| 55 | + |
| 56 | + /// # Safety |
| 57 | + /// `b` must be a live block from [`alloc`] of layout `old`, and `size` |
| 58 | + /// non-zero. |
| 59 | + pub(super) unsafe fn realloc(b: *mut u8, old: Layout, size: usize) -> *mut u8 { |
| 60 | + let _ = old; |
| 61 | + // SAFETY: forwarded caller guarantee; the result keeps mimalloc's |
| 62 | + // alignment. |
| 63 | + unsafe { libmimalloc_sys::mi_realloc(b.cast(), size) }.cast() |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 35 | 67 | #[cfg(not(feature = "mimalloc"))] |
| 36 | | −static BLOCKS: std::alloc::System = std::alloc::System; |
| 68 | +mod blocks { |
| 69 | + use std::alloc::{GlobalAlloc, Layout, System}; |
| 70 | + |
| 71 | + /// # Safety |
| 72 | + /// `layout.size()` must be non-zero. |
| 73 | + pub(super) unsafe fn alloc(layout: Layout) -> *mut u8 { |
| 74 | + // SAFETY: forwarded caller guarantee. |
| 75 | + unsafe { System.alloc(layout) } |
| 76 | + } |
| 77 | + |
| 78 | + /// # Safety |
| 79 | + /// `b` must be a live block, from [`alloc`]. |
| 80 | + pub(super) unsafe fn free(b: *mut crate::abi::Block) { |
| 81 | + // SAFETY: forwarded caller guarantee; the layout is the block's. |
| 82 | + unsafe { System.dealloc(b.cast(), super::layout_of(b)) } |
| 83 | + } |
| 84 | + |
| 85 | + /// # Safety |
| 86 | + /// `b` must be a live block from [`alloc`] of layout `old`, and `size` |
| 87 | + /// non-zero. |
| 88 | + pub(super) unsafe fn realloc(b: *mut u8, old: Layout, size: usize) -> *mut u8 { |
| 89 | + // SAFETY: forwarded caller guarantee. |
| 90 | + unsafe { System.realloc(b, old, size) } |
| 91 | + } |
| 92 | +} |
| 37 | 93 | |
| 38 | 94 | const HEADER_SIZE: usize = size_of::<Header>(); |
| 39 | 95 | const WORD: usize = size_of::<Value>(); |
| @@ -123,7 +179,7 @@ pub(crate) unsafe fn scanned_fields(b: *mut Block) -> (*mut Value, usize) { |
| 123 | 179 | pub(crate) fn alloc_bytes(_ctx: *mut Ctx, size: usize) -> *mut Block { |
| 124 | 180 | let layout = Layout::from_size_align(size, WORD).expect("tilrt: block too large"); |
| 125 | 181 | // SAFETY: layout has non-zero size (at least the header). |
| 126 | | − let b = unsafe { BLOCKS.alloc(layout) }.cast::<Block>(); |
| 182 | + let b = unsafe { blocks::alloc(layout) }.cast::<Block>(); |
| 127 | 183 | if b.is_null() { |
| 128 | 184 | handle_alloc_error(layout); |
| 129 | 185 | } |
| @@ -143,7 +199,7 @@ pub(crate) fn alloc_bytes(_ctx: *mut Ctx, size: usize) -> *mut Block { |
| 143 | 199 | pub(crate) unsafe fn realloc_block(b: *mut Block, old_size: usize, size: usize) -> *mut Block { |
| 144 | 200 | let old = Layout::from_size_align(old_size, WORD).expect("tilrt: block too large"); |
| 145 | 201 | // SAFETY: forwarded caller guarantee. |
| 146 | | − let new = unsafe { BLOCKS.realloc(b.cast(), old, size) }.cast::<Block>(); |
| 202 | + let new = unsafe { blocks::realloc(b.cast(), old, size) }.cast::<Block>(); |
| 147 | 203 | if new.is_null() { |
| 148 | 204 | handle_alloc_error(Layout::from_size_align(size, WORD).expect("tilrt: block too large")); |
| 149 | 205 | } |
| @@ -249,6 +305,18 @@ pub unsafe extern "C" fn tl_drop_slow(ctx: *mut Ctx, b: *mut Block) { |
| 249 | 305 | if !unsafe { release(b) } { |
| 250 | 306 | return; |
| 251 | 307 | } |
| 308 | + // A block without children or finalizer (a Float, a String, a |
| 309 | + // nullary-free leaf) is freed at once, without the worklist: half the |
| 310 | + // drops of numeric code. |
| 311 | + // SAFETY: `b` is live until freed. |
| 312 | + unsafe { |
| 313 | + let (scan, tag) = ((*b).header.scan_fsize, (*b).header.tag); |
| 314 | + if scan == 0 && plain(tag) { |
| 315 | + blocks::free(b); |
| 316 | + stats::free(); |
| 317 | + return; |
| 318 | + } |
| 319 | + } |
| 252 | 320 | // SAFETY: the running actor's context always has a live actor. |
| 253 | 321 | let stack = unsafe { crate::sched::drop_stack(ctx) }; |
| 254 | 322 | stack.pending.push(b); |
| @@ -273,11 +341,20 @@ pub unsafe extern "C" fn tl_free(_ctx: *mut Ctx, b: *mut Block) { |
| 273 | 341 | // SAFETY: caller guarantees `b` is live and unique. |
| 274 | 342 | unsafe { |
| 275 | 343 | debug_assert_eq!((*b).header.rc, 1, "tilrt: tl_free on a shared block"); |
| 276 | | − BLOCKS.dealloc(b.cast(), layout_of(b)); |
| 344 | + blocks::free(b); |
| 277 | 345 | } |
| 278 | 346 | stats::free(); |
| 279 | 347 | } |
| 280 | 348 | |
| 349 | +/// Whether freeing a block with this tag needs nothing but its children |
| 350 | +/// released: not a region, a handler frame or a resource. |
| 351 | +fn plain(tag: u16) -> bool { |
| 352 | + !matches!( |
| 353 | + tag, |
| 354 | + crate::abi::TAG_REGION | crate::abi::TAG_FRAME | crate::abi::TAG_RESOURCE |
| 355 | + ) |
| 356 | +} |
| 357 | + |
| 281 | 358 | /// Release `b`'s children (queuing those that die) and deallocate `b`. |
| 282 | 359 | /// |
| 283 | 360 | /// # Safety |
| @@ -306,7 +383,7 @@ unsafe fn free_block(b: *mut Block, pending: &mut Vec<*mut Block>) { |
| 306 | 383 | pending.push(child); |
| 307 | 384 | } |
| 308 | 385 | } |
| 309 | | − BLOCKS.dealloc(b.cast(), layout_of(b)); |
| 386 | + blocks::free(b); |
| 310 | 387 | } |
| 311 | 388 | stats::free(); |
| 312 | 389 | } |