use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use std;
pub fn shallow_size_of_btreemap<K, V>(
val: &std::collections::BTreeMap<K, V>,
_ops: &mut MallocSizeOfOps,
) -> usize {
let mut size = 0;
for (_, _) in val.iter() {
size += std::mem::size_of::<(K, V)>();
}
size
}
pub fn size_of_btreemap<K, V>(
val: &std::collections::BTreeMap<K, V>,
ops: &mut MallocSizeOfOps,
) -> usize
where
K: MallocSizeOf,
V: MallocSizeOf,
{
let mut size = 0;
for (key, value) in val.iter() {
size += std::mem::size_of::<(K, V)>() + key.size_of(ops) + value.size_of(ops);
}
size
}
#[cfg(not(windows))]
pub mod platform {
use std::os::raw::c_void;
extern "C" {
#[cfg_attr(
any(target_os = "macos", target_os = "ios"),
link_name = "malloc_size"
)]
fn malloc_usable_size(ptr: *const c_void) -> usize;
}
pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize {
if ptr.is_null() {
return 0;
} else {
return malloc_usable_size(ptr);
}
}
}
#[cfg(windows)]
pub mod platform {
extern crate kernel32;
use self::kernel32::{GetProcessHeap, HeapSize, HeapValidate};
use std::os::raw::c_void;
pub unsafe extern "C" fn usable_size(mut ptr: *const c_void) -> usize {
let heap = GetProcessHeap();
if HeapValidate(heap, 0, ptr) == 0 {
ptr = *(ptr as *const *const c_void).offset(-1);
}
HeapSize(heap, 0, ptr) as usize
}
}