iota_util_mem/
allocators.rs1#[cfg(not(feature = "std"))]
14use core::ffi::c_void;
15#[cfg(feature = "std")]
16use std::os::raw::c_void;
17
18#[cfg(feature = "std")]
19use crate::malloc_size::MallocUnconditionalSizeOf;
20use crate::malloc_size::{MallocSizeOf, MallocSizeOfOps, VoidPtrToSizeFn};
21
22mod usable_size {
23
24 use super::*;
25
26 cfg_if::cfg_if! {
27
28 if #[cfg(any(
29 target_arch = "wasm32",
30 feature = "estimate-heapsize",
31 ))] {
32
33 pub unsafe extern "C" fn malloc_usable_size(_ptr: *const c_void) -> usize {
39 unreachable!("estimate heapsize only")
40 }
41
42 } else if #[cfg(any(
43 target_os = "linux",
44 target_os = "android",
45 target_os = "freebsd",
46 ))] {
47 extern "C" {
49 pub fn malloc_usable_size(ptr: *const c_void) -> usize;
50 }
51
52 } else {
53 pub unsafe extern "C" fn malloc_usable_size(_ptr: *const c_void) -> usize {
55 unreachable!("estimate heapsize or feature allocator needed")
56 }
57
58 }
59
60 }
61
62 #[inline]
64 pub fn new_enclosing_size_fn() -> Option<VoidPtrToSizeFn> {
65 None
66 }
67}
68
69pub fn new_malloc_size_ops() -> MallocSizeOfOps {
71 MallocSizeOfOps::new(
72 usable_size::malloc_usable_size,
73 usable_size::new_enclosing_size_fn(),
74 None,
75 )
76}
77
78pub trait MallocSizeOfExt: MallocSizeOf {
83 fn malloc_size_of(&self) -> usize {
86 let mut ops = new_malloc_size_ops();
87 <Self as MallocSizeOf>::size_of(self, &mut ops)
88 }
89}
90
91impl<T: MallocSizeOf> MallocSizeOfExt for T {}
92
93#[cfg(feature = "std")]
94impl<T: MallocSizeOf> MallocSizeOf for std::sync::Arc<T> {
95 fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
96 self.unconditional_size_of(ops)
97 }
98}