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
#![no_std]

extern crate loca;

use core::ptr::NonNull;
use loca::*;

use c::*;

mod c {
    // 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: usize, flags: usize) -> *mut u8;
        #[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 u8, size: usize, flags: usize) -> *mut u8;
        #[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 u8, size: usize, extra: usize, flags: usize) -> usize;
        #[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 u8, size: usize, flags: usize);
        #[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: usize, flags: usize) -> usize;
    }
}

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

use AllocErr::*;

const MALLOCX_ZERO: usize = 0x40;

unsafe impl Alloc for Jemalloc {
    #[inline]
    unsafe fn alloc(&mut self, l: Layout) -> Result<NonNull<u8>, AllocErr> {
        NonNull::new(mallocx(l.size(), l.align().get().trailing_zeros() as _))
            .ok_or(Exhausted { request: l })
    }

    #[inline]
    unsafe fn alloc_zeroed(&mut self, l: Layout) -> Result<NonNull<u8>, AllocErr> {
        NonNull::new(mallocx(l.size(), l.align().get().trailing_zeros() as usize | MALLOCX_ZERO))
            .ok_or(Exhausted { request: l })
    }

    #[inline]
    unsafe fn realloc(&mut self, ptr: NonNull<u8>, old_l: Layout, new_size: usize) -> Result<NonNull<u8>, AllocErr> {
        NonNull::new(rallocx(ptr.as_ptr(), new_size, old_l.align().get().trailing_zeros() as _))
            .ok_or(Exhausted { request: Layout::from_size_align(new_size, old_l.align().get()).unwrap() })
    }

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

    #[inline]
    unsafe fn resize_in_place(&mut self, ptr: NonNull<u8>, old_l: Layout, new_size: usize) -> Result<(), CannotReallocInPlace> {
        if new_size ==
           xallocx(ptr.as_ptr(), new_size, 0, old_l.align().get().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().get().trailing_zeros() as _)
        }))
    }
}