From f894b3983bbffd17cae05f9bcafc0b3d800c2d20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Mon, 1 Jun 2026 11:39:39 +0200 Subject: [PATCH] Fix shallow_size_of for inline arrays. Fixes #86 --- src/lib.rs | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7054205..ae5786d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2132,16 +2132,12 @@ impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for ThinVec { #[cfg(feature = "malloc_size_of")] impl MallocShallowSizeOf for ThinVec { fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { - if self.capacity() == 0 { - // If it's the singleton we might not be a heap pointer. + if self.capacity() == 0 || self.uses_stack_allocated_buffer() { + // We're not a heap pointer. return 0; } - assert_eq!( - core::mem::size_of::(), - core::mem::size_of::<*const ()>() - ); - unsafe { ops.malloc_size_of(*(self as *const Self as *const *const ())) } + unsafe { ops.malloc_size_of(self.ptr() as _) } } } @@ -4829,4 +4825,23 @@ mod std_tests { v.push(PanicBomb("normal2")); v.clear(); } + + #[cfg(all(feature = "gecko-ffi", feature = "malloc_size_of"))] + #[test] + fn malloc_size_of_auto_array() { + use malloc_size_of::{MallocShallowSizeOf, MallocSizeOfOps}; + use std::ffi::c_void; + + extern "C" { + fn malloc_usable_size(ptr: *const c_void) -> usize; + } + + unsafe extern "C" fn malloc_size_of(ptr: *const c_void) -> usize { + unsafe { malloc_usable_size(ptr) } + } + + crate::auto_thin_vec!(let t: [u8; 4]); + let mut ops = MallocSizeOfOps::new(malloc_size_of, None, None); + let _ = MallocShallowSizeOf::shallow_size_of(&**t, &mut ops); + } }