1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
#![no_std]

extern crate loca;

use loca::*;

use c::*;

mod c {
    extern crate libc;

    use self::libc::*;

    // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
    // file at the top-level directory of this distribution and at
    // http://rust-lang.org/COPYRIGHT.
    //
    // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
    // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
    // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
    // option. This file may not be copied, modified, or distributed
    // except according to those terms.

    // Note that the symbols here are prefixed by default on macOS and Windows (we
    // don't explicitly request it), and on Android and DragonFly we explicitly
    // request it as unprefixing cause segfaults (mismatches in allocators).
    extern "C" {
        #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
                       target_os = "dragonfly", target_os = "windows", target_env = "musl"),
                   link_name = "je_mallocx")]
        pub fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
        #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
                       target_os = "dragonfly", target_os = "windows", target_env = "musl"),
                   link_name = "je_rallocx")]
        pub fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
        #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
                       target_os = "dragonfly", target_os = "windows", target_env = "musl"),
                   link_name = "je_xallocx")]
        pub fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
        #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
                       target_os = "dragonfly", target_os = "windows", target_env = "musl"),
                   link_name = "je_sdallocx")]
        pub fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
        #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
                       target_os = "dragonfly", target_os = "windows", target_env = "musl"),
                   link_name = "je_nallocx")]
        pub fn nallocx(size: size_t, flags: c_int) -> size_t;
    }
}

#[derive(Clone, Copy, Default, Debug)]
pub struct Jemalloc;

use AllocErr::*;

const MALLOCX_ZERO: u32 = 0x40;

unsafe impl Alloc for Jemalloc {
    #[inline]
    unsafe fn alloc(&mut self, l: Layout) -> Result<*mut u8, AllocErr> {
        let ptr = mallocx(l.size(), l.align().trailing_zeros() as _) as *mut u8;
        if ptr.is_null() { Err(Exhausted { request: l }) } else { Ok(ptr) }
    }

    #[inline]
    unsafe fn alloc_zeroed(&mut self, l: Layout) -> Result<*mut u8, AllocErr> {
        let ptr = mallocx(l.size(), (l.align().trailing_zeros() | MALLOCX_ZERO) as _) as *mut u8;
        if ptr.is_null() { Err(Exhausted { request: l }) } else { Ok(ptr) }
    }

    #[inline]
    unsafe fn realloc(&mut self, ptr: *mut u8, old_l: Layout, new_l: Layout) -> Result<*mut u8, AllocErr> {
        if old_l.align() != new_l.align() {
            return Err(AllocErr::Unsupported { details: "can't reallocate with nonsame alignment" });
        }

        let ptr = rallocx(ptr as _, new_l.size(), new_l.align().trailing_zeros() as _) as *mut u8;
        if ptr.is_null() { Err(Exhausted { request: new_l }) } else { Ok(ptr) }
    }

    #[inline]
    unsafe fn dealloc(&mut self, ptr: *mut u8, l: Layout) {
        sdallocx(ptr as _, l.size(), l.align().trailing_zeros() as _)
    }

    #[inline]
    unsafe fn resize_in_place(&mut self, ptr: *mut u8, old_l: Layout, new_l: Layout) -> Result<(), CannotReallocInPlace> {
        if old_l.align() != new_l.align() {
            return Err(CannotReallocInPlace);
        }

        if new_l.size() ==
           xallocx(ptr as _, new_l.size(), 0, new_l.align().trailing_zeros() as _) { Ok(()) }
        else { Err(CannotReallocInPlace) }
    }

    #[inline]
    fn usable_size(&self, l: Layout) -> (usize, usize) {
        (l.size(), ::core::cmp::max(l.size(), unsafe {
            nallocx(l.size(), l.align().trailing_zeros() as _)
        }))
    }
}