Skip to content

Commit ce88089

Browse files
authored
Merge pull request #68 from nicoburns/malloc_size_of
Implement `MallocSizeOf` for `thin-vec`
2 parents 60db597 + c88c149 commit ce88089

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

.github/workflows/rust.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ jobs:
3131
- uses: actions/checkout@v2
3232
- name: Build
3333
run: cargo build --verbose
34+
- name: Build (malloc_size_of)
35+
run: cargo build --features=malloc_size_of --verbose
3436
- name: Run tests
3537
run: cargo test --verbose
3638
- name: Run tests

Cargo.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
edition = "2018"
33
name = "thin-vec"
4-
version = "0.2.13"
4+
version = "0.2.14"
55
authors = ["Aria Beingessner <[email protected]>"]
66
description = "A vec that takes up less space on the stack"
77
license = "MIT/Apache-2.0"
@@ -20,7 +20,11 @@ std = []
2020
gecko-ffi = []
2121

2222
[dependencies]
23-
serde = {version = "1.0", optional = true}
23+
serde = { version = "1.0", optional = true }
24+
malloc_size_of = { version = "0.1", default-features = false, optional = true }
2425

2526
[dev-dependencies]
2627
serde_test = "1.0"
28+
29+
[lints.rust]
30+
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(no_global_oom_handling)'] }

RELEASES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Version 0.2.14 (2025-02-19)
2+
* Add "malloc_size_of" feature for heap size measurement support
3+
14
# Version 0.2.13 (2023-12-02)
25

36
* add default-on "std" feature for no_std support

src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ use core::{fmt, mem, ptr, slice};
163163

164164
use impl_details::*;
165165

166+
#[cfg(feature = "malloc_size_of")]
167+
use malloc_size_of::{MallocShallowSizeOf, MallocSizeOf, MallocSizeOfOps};
168+
166169
// modules: a simple way to cfg a whole bunch of impl details at once
167170

168171
#[cfg(not(feature = "gecko-ffi"))]
@@ -1873,6 +1876,33 @@ impl<'de, T: serde::Deserialize<'de>> serde::Deserialize<'de> for ThinVec<T> {
18731876
}
18741877
}
18751878

1879+
#[cfg(feature = "malloc_size_of")]
1880+
impl<T> MallocShallowSizeOf for ThinVec<T> {
1881+
fn shallow_size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
1882+
if self.capacity() == 0 {
1883+
// If it's the singleton we might not be a heap pointer.
1884+
return 0;
1885+
}
1886+
1887+
assert_eq!(
1888+
std::mem::size_of::<Self>(),
1889+
std::mem::size_of::<*const ()>()
1890+
);
1891+
unsafe { ops.malloc_size_of(*(self as *const Self as *const *const ())) }
1892+
}
1893+
}
1894+
1895+
#[cfg(feature = "malloc_size_of")]
1896+
impl<T: MallocSizeOf> MallocSizeOf for ThinVec<T> {
1897+
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
1898+
let mut n = self.shallow_size_of(ops);
1899+
for elem in self.iter() {
1900+
n += elem.size_of(ops);
1901+
}
1902+
n
1903+
}
1904+
}
1905+
18761906
macro_rules! array_impls {
18771907
($($N:expr)*) => {$(
18781908
impl<A, B> PartialEq<[B; $N]> for ThinVec<A> where A: PartialEq<B> {

0 commit comments

Comments
 (0)