diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index ad106c5..317da29 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -48,6 +48,10 @@ jobs: if: matrix.toolchain != '1.36.0' run: cargo test --verbose --features serde + - name: Cargo test w/ malloc_size_of + if: matrix.toolchain != '1.36.0' + run: cargo test --verbose --features malloc_size_of + - name: Cargo check w/o default features if: matrix.toolchain == 'nightly' run: cargo check --verbose --no-default-features diff --git a/Cargo.toml b/Cargo.toml index ae2a9fc..f7afed4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ debugger_visualizer = [] [dependencies] serde = { version = "1", optional = true, default-features = false } +malloc_size_of = { version = "0.1", optional = true, default-features = false } arbitrary = { version = "1", optional = true } [dev_dependencies] diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock index 5253e47..fb73e46 100644 --- a/fuzz/Cargo.lock +++ b/fuzz/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "afl" @@ -87,9 +87,9 @@ dependencies = [ [[package]] name = "honggfuzz" -version = "0.5.55" +version = "0.5.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "848e9c511092e0daa0a35a63e8e6e475a3e8f870741448b9f6028d69b142f18e" +checksum = "7c76b6234c13c9ea73946d1379d33186151148e0da231506b964b44f3d023505" dependencies = [ "arbitrary", "lazy_static", @@ -105,15 +105,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.146" +version = "0.2.169" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f92be4933c13fd498862a9e02a3055f8a8d9c039ce33db97306fd5a6caa7f29b" +checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" [[package]] name = "memmap2" -version = "0.5.10" +version = "0.9.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "83faa42c0a078c393f6b29d5db232d8be22776a891f8f56e5284faee4a20b327" +checksum = "fd3f7eed9d3848f8b98834af67102b720745c4ec028fcd0aa0239277e7de374f" dependencies = [ "libc", ] @@ -159,7 +159,7 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "smallvec" -version = "1.10.0" +version = "1.13.2" [[package]] name = "smallvec-fuzz" diff --git a/src/lib.rs b/src/lib.rs index 1ea3deb..af976eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -126,6 +126,9 @@ use core::ops::{self, Range, RangeBounds}; use core::ptr::{self, NonNull}; use core::slice::{self, SliceIndex}; +#[cfg(feature = "malloc_size_of")] +use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps}; + #[cfg(feature = "serde")] use serde::{ de::{Deserialize, Deserializer, SeqAccess, Visitor}, @@ -1971,6 +1974,32 @@ where } } +#[cfg(feature = "malloc_size_of")] +impl MallocShallowSizeOf for SmallVec { + fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + if self.spilled() { + unsafe { ops.malloc_size_of(self.as_ptr()) } + } else { + 0 + } + } +} + +#[cfg(feature = "malloc_size_of")] +impl MallocSizeOf for SmallVec +where + A: Array, + A::Item: MallocSizeOf, +{ + fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize { + let mut n = self.shallow_size_of(ops); + for elem in self.iter() { + n += elem.size_of(ops); + } + n + } +} + #[cfg(feature = "specialization")] trait SpecFrom { fn spec_from(slice: S) -> SmallVec;