File tree 3 files changed +18
-16
lines changed
3 files changed +18
-16
lines changed Original file line number Diff line number Diff line change @@ -26,3 +26,6 @@ use_std_for_test_debugging = []
26
26
[target .'cfg(all(unix, not(target_arch = "wasm32")))' .dependencies .libc ]
27
27
default-features = false
28
28
version = " 0.2"
29
+
30
+ [target .'cfg(any(target_os = "linux", target_os = "macos"))' .dependencies .mmap-alloc ]
31
+ version = " 0.2"
Original file line number Diff line number Diff line change
1
+ use alloc:: allocator:: { Alloc , Layout } ;
1
2
use const_init:: ConstInit ;
2
3
use core:: cell:: UnsafeCell ;
3
4
use libc;
5
+ use mmap_alloc:: MapAllocBuilder ;
4
6
use units:: { Bytes , Pages } ;
5
7
6
8
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
+ }
23
20
}
24
21
25
22
// Cache line size on an i7. Good enough.
Original file line number Diff line number Diff line change @@ -229,6 +229,8 @@ extern crate core;
229
229
230
230
#[ cfg( all( unix, not( target_arch = "wasm32" ) ) ) ]
231
231
extern crate libc;
232
+ #[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
233
+ extern crate mmap_alloc;
232
234
233
235
#[ macro_use]
234
236
mod extra_assert;
You can’t perform that action at this time.
0 commit comments