Skip to content

Commit 4008c0a

Browse files
committed
More fixes
1 parent 71fe2a5 commit 4008c0a

File tree

6 files changed

+28
-27
lines changed

6 files changed

+28
-27
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ libc = "0.2"
1818
libmimalloc-sys = { path = "libmimalloc-sys", version = "0.1.0" }
1919

2020
[features]
21-
no_secure = ["libmimalloc-sys/no_secure"]
21+
default = ["secure"]
22+
secure = ["libmimalloc-sys/secure"]

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ static GLOBAL: MiMalloc = MiMalloc;
1616
By default this library builds mimalloc in safe-mode. This means that
1717
heap allocations are encrypted, but this results in a 3% increase in overhead.
1818

19-
In `Cargo.toml`:
19+
To disable secure mode, in `Cargo.toml`:
2020
```rust
2121
[dependencies]
22-
mimalloc = { version = "*", features = ["no_secure"] }
22+
mimalloc = { version = "*", default-features = false }
2323
```
2424

2525
[crates.io]: https://crates.io/crates/mimalloc

libmimalloc-sys/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ libc = "0.2"
1616
cmake = "0.1"
1717

1818
[features]
19-
no_secure = []
19+
secure = []

libmimalloc-sys/build.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cmake::Config;
22

3-
#[cfg(feature = "no_secure")]
3+
#[cfg(not(feature = "secure"))]
44
fn main() {
55
let mut dst = Config::new("c_src/mimalloc")
66
.build();
@@ -15,7 +15,7 @@ fn main() {
1515
}
1616
}
1717

18-
#[cfg(not(feature = "no_secure"))]
18+
#[cfg(feature = "secure")]
1919
fn main() {
2020
let mut dst = Config::new("c_src/mimalloc")
2121
.define("SECURE", "ON")

libmimalloc-sys/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
use libc::{c_void, size_t};
44

55
extern "C" {
6+
pub fn mi_zalloc(size: size_t) -> *const c_void;
7+
pub fn mi_malloc(size: size_t) -> *const c_void;
8+
pub fn mi_realloc(p: *const c_void, size: size_t) -> *const c_void;
69
pub fn mi_zalloc_aligned(size: size_t, alignment: size_t) -> *const c_void;
710
pub fn mi_malloc_aligned(size: size_t, alignment: size_t) -> *const c_void;
811
pub fn mi_realloc_aligned(p: *const c_void, size: size_t, alignment: size_t) -> *const c_void;

src/lib.rs

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

3+
#![feature(no_std)]
4+
#![no_std]
5+
36
//! A drop-in global allocator wrapper around the [mimalloc](https://github.com/microsoft/mimalloc) allocator.
47
//! Mimalloc is a general purpose, performance oriented allocator built by Microsoft.
58
//!
@@ -15,15 +18,15 @@
1518
//! By default this library builds mimalloc in safe-mode. This means that
1619
//! heap allocations are encrypted, but this results in a 3% increase in overhead.
1720
//!
18-
//! In `Cargo.toml`:
21+
//! To disable secure mode, in `Cargo.toml`:
1922
//! ```rust,ignore
2023
//! [dependencies]
21-
//! mimalloc = { version = "*", features = ["no_secure"] }
24+
//! mimalloc = { version = "*", default-features = false }
2225
//! ```
2326
2427
extern crate libmimalloc_sys as ffi;
2528

26-
use std::alloc::{GlobalAlloc, Layout};
29+
use core::alloc::{GlobalAlloc, Layout};
2730
use libc::c_void;
2831
use ffi::*;
2932

@@ -62,24 +65,20 @@ pub struct MiMalloc;
6265
unsafe impl GlobalAlloc for MiMalloc {
6366
#[inline]
6467
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
65-
let align = if layout.align() > MIN_ALIGN {
66-
layout.align()
68+
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
69+
mi_malloc(layout.size()) as *mut u8
6770
} else {
68-
MIN_ALIGN
69-
};
70-
71-
mi_malloc_aligned(layout.size(), align) as *mut u8
71+
mi_malloc_aligned(layout.size(), layout.align()) as *mut u8
72+
}
7273
}
7374

7475
#[inline]
7576
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
76-
let align = if layout.align() > MIN_ALIGN {
77-
layout.align()
77+
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
78+
mi_zalloc(layout.size()) as *mut u8
7879
} else {
79-
MIN_ALIGN
80-
};
81-
82-
mi_zalloc_aligned(layout.size(), align) as *mut u8
80+
mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8
81+
}
8382
}
8483

8584
#[inline]
@@ -89,13 +88,11 @@ unsafe impl GlobalAlloc for MiMalloc {
8988

9089
#[inline]
9190
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
92-
let align = if layout.align() > MIN_ALIGN {
93-
layout.align()
91+
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
92+
mi_realloc(ptr as *const c_void, new_size) as *mut u8
9493
} else {
95-
MIN_ALIGN
96-
};
97-
98-
mi_realloc_aligned(ptr as *const c_void, new_size, align) as *mut u8
94+
mi_realloc_aligned(ptr as *const c_void, new_size, layout.align()) as *mut u8
95+
}
9996
}
10097
}
10198

0 commit comments

Comments
 (0)