Skip to content

Commit

Permalink
Proposed bug fix for issue rustwasm#85
Browse files Browse the repository at this point in the history
  • Loading branch information
aclifford committed Oct 16, 2019
1 parent e782897 commit 43ed066
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions wee_alloc/src/imp_static_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize> = Mutex::new(0);

pub(crate) unsafe fn alloc_pages(pages: Pages) -> Result<NonNull<u8>, AllocErr> {
pub(crate) unsafe fn alloc_pages<B: Into<Bytes>>(pages: Pages, align: B) -> Result<NonNull<u8>, 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)
Expand Down Expand Up @@ -72,6 +85,8 @@ impl<T> Exclusive<T> {
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);
Expand Down
2 changes: 1 addition & 1 deletion wee_alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<CellHeader>()).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(
Expand Down

0 comments on commit 43ed066

Please sign in to comment.