Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
unify linux and macos into a single module
  • Loading branch information
nivkner committed Oct 26, 2021
commit 8dc1a86ce0f250900abffd65936ade62069f000b
8 changes: 2 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ pub struct Dlmalloc<A = System>(dlmalloc::Dlmalloc<A>);
#[path = "wasm.rs"]
mod sys;

#[cfg(target_os = "macos")]
#[path = "macos.rs"]
mod sys;

#[cfg(target_os = "linux")]
#[path = "linux.rs"]
#[cfg(any(target_os = "linux", target_os = "macos"))]
#[path = "unix.rs"]
mod sys;

#[cfg(not(any(target_os = "linux", target_os = "macos", target_arch = "wasm32")))]
Expand Down
109 changes: 0 additions & 109 deletions src/macos.rs

This file was deleted.

14 changes: 13 additions & 1 deletion src/linux.rs → src/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ unsafe impl Allocator for System {
0 as *mut _,
size,
libc::PROT_WRITE | libc::PROT_READ,
libc::MAP_ANONYMOUS | libc::MAP_PRIVATE,
libc::MAP_ANON | libc::MAP_PRIVATE,
-1,
0,
)
Expand All @@ -36,6 +36,7 @@ unsafe impl Allocator for System {
}
}

#[cfg(target_os = "linux")]
fn remap(&self, ptr: *mut u8, oldsize: usize, newsize: usize, can_move: bool) -> *mut u8 {
let flags = if can_move { libc::MREMAP_MAYMOVE } else { 0 };
let ptr = unsafe { libc::mremap(ptr as *mut _, oldsize, newsize, flags) };
Expand All @@ -46,6 +47,12 @@ unsafe impl Allocator for System {
}
}

#[cfg(target_os = "macos")]
fn remap(&self, _ptr: *mut u8, _oldsize: usize, _newsize: usize, _can_move: bool) -> *mut u8 {
ptr::null_mut()
}

#[cfg(target_os = "linux")]
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool {
unsafe {
let rc = libc::mremap(ptr as *mut _, oldsize, newsize, 0);
Expand All @@ -56,6 +63,11 @@ unsafe impl Allocator for System {
}
}

#[cfg(target_os = "macos")]
fn free_part(&self, ptr: *mut u8, oldsize: usize, newsize: usize) -> bool {
unsafe { libc::munmap(ptr.offset(newsize as isize) as *mut _, oldsize - newsize) == 0 }
}

fn free(&self, ptr: *mut u8, size: usize) -> bool {
unsafe { libc::munmap(ptr as *mut _, size) == 0 }
}
Expand Down