Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest jemallocator and jemalloc-sys #20848

Closed
wants to merge 6 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Next

Update to latest jemallocator and jemalloc-sys

  • Loading branch information
gnzlbg committed May 22, 2018
commit 92430894eee925c58cb94919256c3efff08dcdf9

Some generated files are not rendered by default. Learn more.

@@ -9,13 +9,13 @@ publish = false
path = "lib.rs"

[features]
unstable = ["kernel32-sys", "jemalloc-sys"]
unstable = ["kernel32-sys", "jemallocator"]

[dependencies]
libc = "0.2" # Only used when 'unstable' is disabled, but looks like Cargo cannot express that.

[target.'cfg(not(windows))'.dependencies]
jemalloc-sys = { version = "0.1.4", optional = true }
jemallocator = { version = "0.1.8", optional = true }

[target.'cfg(windows)'.dependencies]
kernel32-sys = { version = "0.2.1", optional = true }
@@ -12,93 +12,22 @@ static ALLOC: Allocator = Allocator;

pub use platform::*;


#[cfg(all(feature = "unstable", not(windows)))]
mod platform {
extern crate jemalloc_sys as ffi;
extern crate jemallocator;
pub use self::jemallocator::Jemalloc as Allocator;

use std::alloc::{GlobalAlloc, Layout, Opaque, System};
use std::os::raw::{c_int, c_void};
use std::os::raw::{void};

/// Get the size of a heap block.
pub unsafe extern "C" fn usable_size(ptr: *const c_void) -> usize {
ffi::malloc_usable_size(ptr as *const _)
jemallocator::usable_size(ptr as *const _)
}

/// Memory allocation APIs compatible with libc
pub mod libc_compat {
pub use super::ffi::{malloc, realloc, free};
}

pub struct Allocator;

// The minimum alignment guaranteed by the architecture. This value is used to
// add fast paths for low alignment values.
#[cfg(all(any(target_arch = "arm",
target_arch = "mips",
target_arch = "mipsel",
target_arch = "powerpc")))]
const MIN_ALIGN: usize = 8;
#[cfg(all(any(target_arch = "x86",
target_arch = "x86_64",
target_arch = "aarch64",
target_arch = "powerpc64",
target_arch = "powerpc64le",
target_arch = "mips64",
target_arch = "s390x",
target_arch = "sparc64")))]
const MIN_ALIGN: usize = 16;

fn layout_to_flags(align: usize, size: usize) -> c_int {
// If our alignment is less than the minimum alignment they we may not
// have to pass special flags asking for a higher alignment. If the
// alignment is greater than the size, however, then this hits a sort of odd
// case where we still need to ask for a custom alignment. See #25 for more
// info.
if align <= MIN_ALIGN && align <= size {
0
} else {
// Equivalent to the MALLOCX_ALIGN(a) macro.
align.trailing_zeros() as _
}
}

unsafe impl GlobalAlloc for Allocator {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut Opaque {
let flags = layout_to_flags(layout.align(), layout.size());
ffi::mallocx(layout.size(), flags) as *mut Opaque
}

#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut Opaque {
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
ffi::calloc(1, layout.size()) as *mut Opaque
} else {
let flags = layout_to_flags(layout.align(), layout.size()) | ffi::MALLOCX_ZERO;
ffi::mallocx(layout.size(), flags) as *mut Opaque
}
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut Opaque, layout: Layout) {
let flags = layout_to_flags(layout.align(), layout.size());
ffi::sdallocx(ptr as *mut _, layout.size(), flags)
}

#[inline]
unsafe fn realloc(&self,
ptr: *mut Opaque,
layout: Layout,
new_size: usize) -> *mut Opaque {
let flags = layout_to_flags(layout.align(), new_size);
ffi::rallocx(ptr as *mut _, new_size, flags) as *mut Opaque
}

#[inline]
fn oom(&self) -> ! {
System.oom()
}
pub use super::jemallocator::ffi::{malloc, realloc, free};
}
}

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.