Skip to content

Commit 2acc1b0

Browse files
committed
Hooked api to allocator ffi
1 parent f519807 commit 2acc1b0

File tree

4 files changed

+58
-8
lines changed

4 files changed

+58
-8
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ edition = "2018"
88
libc = "0.2"
99

1010
[build-dependencies]
11-
cmake = "0.1"
11+
cmake = "0.1"
12+
13+
[features]
14+
no_secure = []

build.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
use cmake::Config;
22

3+
#[cfg(feature = "no_secure")]
4+
fn main() {
5+
let dst = Config::new("c_src/mimalloc")
6+
.build();
7+
println!("cargo:rustc-link-search=native={}", dst.display());
8+
//println!("cargo:rustc-link-lib=static=libmimalloc");
9+
}
10+
11+
#[cfg(not(feature = "no_secure"))]
312
fn main() {
413
let dst = Config::new("c_src/mimalloc")
514
.define("SECURE", "ON")

src/ffi.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// Copyright 2019 Octavian Oncescu
2+
23
use libc::{c_void, size_t};
34

45
extern "C" {
5-
fn mi_malloc_aligned(size: size_t, alignment: size_t) -> c_void;
6-
fn mi_free(p: *const c_void) -> c_void;
6+
pub(crate) fn mi_malloc_aligned(size: size_t, alignment: size_t) -> c_void;
7+
pub(crate) fn mi_free(p: *const c_void) -> c_void;
78
}

src/lib.rs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
11
// Copyright 2019 Octavian Oncescu
22

3-
use std::alloc::{GlobalAlloc, Layout, alloc};
3+
//! A drop-in global allocator wrapper around the [mimalloc](https://github.com/microsoft/mimalloc) allocator.
4+
//! Mimalloc is a general purpose, performance oriented allocator built by Microsoft.
5+
//!
6+
//! ## Usage
7+
//! ```rust
8+
//! use mimalloc::MiMalloc;
9+
//!
10+
//! #[global_allocator]
11+
//! static GLOBAL: MiMalloc = MiMalloc;
12+
//! ```
13+
//!
14+
//! ## Usage without secure mode
15+
//! By default this library builds mimalloc in safe-mode. This means that
16+
//! heap allocations are encrypted, but this results in a 3% increase in overhead.
17+
//!
18+
//! In `Cargo.toml`:
19+
//! ```rust
20+
//! [dependencies]
21+
//! mimalloc = { version = "*", features = ["no_secure"] }
22+
//! ```
23+
24+
use std::alloc::{GlobalAlloc, Layout};
425
use std::ptr::null_mut;
26+
use libc::c_void;
527
use ffi::*;
628

7-
pub struct MimAlloc;
29+
/// Drop-in mimalloc global allocator.
30+
///
31+
/// ## Usage
32+
/// ```rust
33+
/// use mimalloc::MiMalloc;
34+
///
35+
/// #[global_allocator]
36+
/// static GLOBAL: MiMalloc = MiMalloc;
37+
/// ```
38+
pub struct MiMalloc;
839

9-
unsafe impl GlobalAlloc for MimAlloc {
10-
unsafe fn alloc(&self, _layout: Layout) -> *mut u8 { null_mut() }
11-
unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout) {}
40+
unsafe impl GlobalAlloc for MiMalloc {
41+
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
42+
mi_malloc_aligned(layout.size(), layout.align());
43+
null_mut()
44+
}
45+
46+
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
47+
mi_free(ptr as *const c_void);
48+
}
1249
}
1350

1451
mod ffi;

0 commit comments

Comments
 (0)