Skip to content

Commit e40d57f

Browse files
authored
Merge pull request #14 from joshlf/mmap-alloc
Use mmap-alloc crate for mmap and Windows VirtualAlloc allocations
2 parents 80146fd + 195bd59 commit e40d57f

File tree

3 files changed

+18
-16
lines changed

3 files changed

+18
-16
lines changed

wee_alloc/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ use_std_for_test_debugging = []
2626
[target.'cfg(all(unix, not(target_arch = "wasm32")))'.dependencies.libc]
2727
default-features = false
2828
version = "0.2"
29+
30+
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies.mmap-alloc]
31+
version = "0.2"

wee_alloc/src/imp_unix.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1+
use alloc::allocator::{Alloc, Layout};
12
use const_init::ConstInit;
23
use core::cell::UnsafeCell;
34
use libc;
5+
use mmap_alloc::MapAllocBuilder;
46
use units::{Bytes, Pages};
57

68
pub(crate) fn alloc_pages(pages: Pages) -> *mut u8 {
7-
let bytes: Bytes = pages.into();
8-
let addr = unsafe {
9-
libc::mmap(
10-
0 as *mut _,
11-
bytes.0,
12-
libc::PROT_WRITE | libc::PROT_READ,
13-
libc::MAP_ANON | libc::MAP_PRIVATE,
14-
-1,
15-
0,
16-
)
17-
};
18-
// TODO: when we can detect failure of wasm intrinsics, then both
19-
// `alloc_pages` implementations should return results, rather than
20-
// asserting against failure.
21-
assert!(addr != libc::MAP_FAILED);
22-
addr as *mut u8
9+
unsafe {
10+
let bytes: Bytes = pages.into();
11+
let layout = Layout::from_size_align_unchecked(bytes.0, 1);
12+
// TODO: when we can detect failure of wasm intrinsics, then both
13+
// `alloc_pages` implementations should return results, rather than
14+
// panicking on failure.
15+
MapAllocBuilder::default()
16+
.build()
17+
.alloc(layout)
18+
.expect("failed to allocate page")
19+
}
2320
}
2421

2522
// Cache line size on an i7. Good enough.

wee_alloc/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ extern crate core;
229229

230230
#[cfg(all(unix, not(target_arch = "wasm32")))]
231231
extern crate libc;
232+
#[cfg(any(target_os = "linux", target_os = "macos"))]
233+
extern crate mmap_alloc;
232234

233235
#[macro_use]
234236
mod extra_assert;

0 commit comments

Comments
 (0)