diff --git a/wee_alloc/src/imp_static_array.rs b/wee_alloc/src/imp_static_array.rs index 9a85366..3032554 100644 --- a/wee_alloc/src/imp_static_array.rs +++ b/wee_alloc/src/imp_static_array.rs @@ -6,17 +6,30 @@ use core::ptr::NonNull; use memory_units::{Bytes, Pages}; use spin::Mutex; + const SCRATCH_LEN_BYTES: usize = include!(concat!(env!("OUT_DIR"), "/wee_alloc_static_array_backend_size_bytes.txt")); static mut SCRATCH_HEAP: [u8; SCRATCH_LEN_BYTES] = [0; SCRATCH_LEN_BYTES]; static mut OFFSET: Mutex = Mutex::new(0); -pub(crate) unsafe fn alloc_pages(pages: Pages) -> Result, AllocErr> { +pub(crate) unsafe fn alloc_pages>(pages: Pages, align: B) -> Result, AllocErr> { let bytes: Bytes = pages.into(); let mut offset = OFFSET.lock(); - let end = bytes.0 + *offset; - if end < SCRATCH_LEN_BYTES { - let ptr = SCRATCH_HEAP[*offset..end].as_mut_ptr() as *mut u8; - *offset = end; + let mut end = bytes.0 + *offset; + let align = align.into(); + + if end < SCRATCH_LEN_BYTES { + let mut ptr: *mut u8; + let mut count = 0; + ptr = SCRATCH_HEAP[*offset..end].as_mut_ptr() as *mut u8; + if ptr as usize & (align.0 - 1) != 0{ + loop { + *offset += 1; + count += 1; + ptr = SCRATCH_HEAP[*offset..end + count].as_mut_ptr() as *mut u8; + if ptr as usize & (align.0 - 1) == 0 { break; } + } + } + *offset = end + count; NonNull::new(ptr).ok_or_else(|| AllocErr) } else { Err(AllocErr) @@ -72,6 +85,8 @@ impl Exclusive { where for<'x> F: FnOnce(&'x mut T) -> U, { + + // debug_println!("WIth exclusive access!"); let mut guard = self.inner.lock(); assert_not_in_use(self); set_in_use(self); diff --git a/wee_alloc/src/lib.rs b/wee_alloc/src/lib.rs index a5650f2..decafdd 100644 --- a/wee_alloc/src/lib.rs +++ b/wee_alloc/src/lib.rs @@ -780,7 +780,7 @@ impl<'a> AllocPolicy<'a> for LargeAllocPolicy { let size: Bytes = cmp::max(size.into(), (align + Self::MIN_CELL_SIZE) * Words(2)); let pages: Pages = (size + size_of::()).round_up_to(); - let new_pages = imp::alloc_pages(pages)?; + let new_pages = imp::alloc_pages(pages, align)?; let allocated_size: Bytes = pages.into(); let free_cell = &*FreeCell::from_uninitialized(