]>
Commit | Line | Data |
---|---|---|
abe05a73 XL |
1 | extern crate libc; |
2 | ||
3 | use core::ptr; | |
4 | ||
5 | pub unsafe fn alloc(size: usize) -> (*mut u8, usize, u32) { | |
f9f354fc XL |
6 | let addr = libc::mmap( |
7 | 0 as *mut _, | |
8 | size, | |
9 | libc::PROT_WRITE | libc::PROT_READ, | |
10 | libc::MAP_ANONYMOUS | libc::MAP_PRIVATE, | |
11 | -1, | |
12 | 0, | |
13 | ); | |
abe05a73 XL |
14 | if addr == libc::MAP_FAILED { |
15 | (ptr::null_mut(), 0, 0) | |
16 | } else { | |
17 | (addr as *mut u8, size, 0) | |
18 | } | |
19 | } | |
20 | ||
f9f354fc | 21 | pub unsafe fn remap(ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 { |
abe05a73 XL |
22 | let flags = if can_move { libc::MREMAP_MAYMOVE } else { 0 }; |
23 | let ptr = libc::mremap(ptr as *mut _, oldsize, newsize, flags); | |
24 | if ptr == libc::MAP_FAILED { | |
25 | ptr::null_mut() | |
26 | } else { | |
27 | ptr as *mut u8 | |
28 | } | |
29 | } | |
30 | ||
31 | pub unsafe fn free_part(ptr: *mut u8, oldsize: usize, newsize: usize) -> bool { | |
32 | let rc = libc::mremap(ptr as *mut _, oldsize, newsize, 0); | |
33 | if rc != libc::MAP_FAILED { | |
f9f354fc | 34 | return true; |
abe05a73 XL |
35 | } |
36 | libc::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0 | |
37 | } | |
38 | ||
39 | pub unsafe fn free(ptr: *mut u8, size: usize) -> bool { | |
40 | libc::munmap(ptr as *mut _, size) == 0 | |
41 | } | |
42 | ||
43 | pub fn can_release_part(_flags: u32) -> bool { | |
44 | true | |
45 | } | |
46 | ||
0731742a | 47 | #[cfg(feature = "global")] |
abe05a73 XL |
48 | static mut LOCK: libc::pthread_mutex_t = libc::PTHREAD_MUTEX_INITIALIZER; |
49 | ||
0731742a | 50 | #[cfg(feature = "global")] |
abe05a73 | 51 | pub fn acquire_global_lock() { |
f9f354fc | 52 | unsafe { assert_eq!(libc::pthread_mutex_lock(&mut LOCK), 0) } |
abe05a73 XL |
53 | } |
54 | ||
0731742a | 55 | #[cfg(feature = "global")] |
abe05a73 | 56 | pub fn release_global_lock() { |
f9f354fc | 57 | unsafe { assert_eq!(libc::pthread_mutex_unlock(&mut LOCK), 0) } |
abe05a73 XL |
58 | } |
59 | ||
60 | pub fn allocates_zeros() -> bool { | |
61 | true | |
62 | } | |
63 | ||
64 | pub fn page_size() -> usize { | |
65 | 4096 | |
66 | } |