Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add different mode for malloc_size_of_is_0 macro dealing with generics #334

Merged
merged 5 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion parity-util-mem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ The format is based on [Keep a Changelog].

## [Unreleased]

## [0.5.1] - 2019-02-05
- Add different mode for malloc_size_of_is_0 macro dealing with generics #334. [#332](https://github.com/paritytech/parity-common/pull/334)

## [0.5.0] - 2019-02-05
- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332
- Bump parking_lot to 0.10. [#332](https://github.com/paritytech/parity-common/pull/332)

## [0.4.2] - 2020-02-04
- Implementation of `MallocSizeOf` for `BTreeSet`. [#325](https://github.com/paritytech/parity-common/pull/325)
Expand Down
2 changes: 1 addition & 1 deletion parity-util-mem/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "parity-util-mem"
version = "0.5.0"
version = "0.5.1"
authors = ["Parity Technologies <admin@parity.io>"]
repository = "https://github.com/paritytech/parity-common"
description = "Collection of memory related utilities"
Expand Down
92 changes: 72 additions & 20 deletions parity-util-mem/src/malloc_size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,28 +564,68 @@ impl<T: MallocSizeOf> MallocSizeOf for parking_lot::RwLock<T> {
}
}

/// Implement notion of 0 allocation size for some type(s).
///
/// if used for generics, by default it will require that generaic arguments
/// should implement `MallocSizeOf`. This can be avoided with passing "any: "
/// in front of type list.
///
/// ```rust
/// use parity_util_mem::{malloc_size, malloc_size_of_is_0};
///
/// struct Data<P> {
/// phantom: std::marker::PhantomData<P>,
/// }
///
/// malloc_size_of_is_0!(any: Data<P>);
///
/// // MallocSizeOf is NOT implemented for [u8; 333]
/// assert_eq!(malloc_size(&Data::<[u8; 333]> { phantom: std::marker::PhantomData }), 0);
/// ```
///
/// and when no "any: "
///
/// use parity_util_mem::{malloc_size, malloc_size_of_is_0};
///
/// struct Data<T> { pub T }
///
/// // generic argument (`T`) must be `impl MallocSizeOf`
/// malloc_size_of_is_0!(Data<u8>);
///
/// assert_eq!(malloc_size(&Data(0u8), 0);
/// ```
#[macro_export]
macro_rules! malloc_size_of_is_0(
($($ty:ty),+) => (
$(
impl $crate::MallocSizeOf for $ty {
#[inline(always)]
fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize {
0
}
}
)+
);
($($ty:ident<$($gen:ident),+>),+) => (
$(
impl<$($gen: $crate::MallocSizeOf),+> $crate::MallocSizeOf for $ty<$($gen),+> {
#[inline(always)]
fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize {
0
}
}
)+
);
($($ty:ty),+) => (
$(
impl $crate::MallocSizeOf for $ty {
#[inline(always)]
fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize {
0
}
}
)+
);
(any: $($ty:ident<$($gen:ident),+>),+) => (
NikVolf marked this conversation as resolved.
Show resolved Hide resolved
$(
impl<$($gen),+> $crate::MallocSizeOf for $ty<$($gen),+> {
#[inline(always)]
fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize {
0
}
}
)+
);
($($ty:ident<$($gen:ident),+>),+) => (
$(
impl<$($gen: $crate::MallocSizeOf),+> $crate::MallocSizeOf for $ty<$($gen),+> {
#[inline(always)]
fn size_of(&self, _: &mut $crate::MallocSizeOfOps) -> usize {
0
}
}
)+
);
NikVolf marked this conversation as resolved.
Show resolved Hide resolved
);

malloc_size_of_is_0!(bool, char, str);
Expand Down Expand Up @@ -763,4 +803,16 @@ mod tests {
// ~36 per value
assert!(crate::malloc_size(&set) > 3000);
}

#[test]
fn special_malloc_size_of_0() {
struct Data<P> {
phantom: std::marker::PhantomData<P>,
}

malloc_size_of_is_0!(any: Data<P>);

// MallocSizeOf is not implemented for [u8; 333]
assert_eq!(crate::malloc_size(&Data::<[u8; 333]> { phantom: std::marker::PhantomData }), 0);
}
}