use crate::{LocalAllocatorSelector, ThreadAllocator, ThreadAllocatorStats};
use mnemosyne_arena::HasSegmentPool;
use mnemosyne_core::types::{Segment, locate_segment};
#[inline(always)]
pub unsafe fn usable_size(ptr: *mut u8) -> usize {
if ptr.is_null() {
return 0;
}
let (segment, page_index) = unsafe { locate_segment(ptr) };
let page = unsafe { (*segment).pages.get_unchecked(page_index) };
let size = page.block_size;
if size > 0 {
return size;
}
unsafe { huge_allocation_size(ptr) }
}
#[inline]
pub(crate) unsafe fn huge_allocation_size(ptr: *mut u8) -> usize {
let segment = unsafe { *((ptr as *mut *mut Segment).sub(1)) };
let size = unsafe { (*segment).pages[0].alloc_count };
if size > 0 {
size
} else {
unsafe { (*segment).huge_mapping_suffix_from(ptr) }
}
}
pub fn thread_allocator_stats<B: HasSegmentPool + LocalAllocatorSelector<B>>()
-> ThreadAllocatorStats {
B::with_allocator(|alloc| alloc.stats()).unwrap_or_else(|| ThreadAllocatorStats {
cross_thread_reclaimed_blocks: ThreadAllocator::<B>::cross_thread_reclaimed_blocks(),
..ThreadAllocatorStats::default()
})
}