use std::alloc::{GlobalAlloc, Layout};
use std::ptr;
static ALLOCATOR: emballoc::Allocator<{ 128 * 1024 * 1024 }> = emballoc::Allocator::new();
#[cfg(all(target_arch = "x86_64", target_os = "linux"))] #[test]
fn ensure_that_allocator_memory_is_not_initialized() {
unsafe {
let layout = Layout::new::<u64>();
let ptr = ALLOCATOR.alloc(layout);
ALLOCATOR.dealloc(ptr, layout);
}
let memory_map = MemoryMap::new();
let bss_start = memory_map.bss_start;
let data_end = memory_map.data_end;
assert_eq!(bss_start, data_end, "test assumes bss directly after data");
let addr_allocator = ptr::addr_of!(ALLOCATOR) as usize;
assert!(addr_allocator >= bss_start, "allocator is placed in .data");
}
struct MemoryMap {
data_end: usize,
bss_start: usize,
}
impl MemoryMap {
pub fn new() -> Self {
extern "C" {
static __bss_start: usize;
static _edata: usize;
}
Self {
data_end: unsafe { ptr::addr_of!(__bss_start) } as usize,
bss_start: unsafe { ptr::addr_of!(_edata) } as usize,
}
}
}