use std::{
alloc::{GlobalAlloc, Layout, System},
ptr::NonNull,
};
use crate::generated::fixed_size_constants::{BLOCK_ALIGN, BLOCK_SIZE};
use super::super::Arena;
const ALLOC_SIZE: usize = BLOCK_SIZE + BLOCK_ALIGN;
const ALLOC_ALIGN: usize = 16;
const _: () = assert!(BLOCK_ALIGN.is_multiple_of(ALLOC_ALIGN));
const ALLOC_LAYOUT: Layout = match Layout::from_size_align(ALLOC_SIZE, ALLOC_ALIGN) {
Ok(layout) => layout,
Err(_) => unreachable!(),
};
const _: () = assert!(ALLOC_LAYOUT.size() > 0);
impl<const MIN_ALIGN: usize> Arena<MIN_ALIGN> {
pub fn new_fixed_size() -> Option<Self> {
let alloc_ptr = unsafe { System.alloc(ALLOC_LAYOUT) };
let alloc_ptr = NonNull::new(alloc_ptr)?;
let alloc_addr = alloc_ptr.addr().get();
let offset = alloc_addr.wrapping_neg() % BLOCK_ALIGN;
let chunk_ptr = unsafe { alloc_ptr.add(offset) };
debug_assert!(chunk_ptr.addr().get().is_multiple_of(BLOCK_ALIGN));
let arena = unsafe { Self::from_raw_parts(chunk_ptr, BLOCK_SIZE, alloc_ptr, ALLOC_LAYOUT) };
Some(arena)
}
}