Skip to content

Commit 74669d2

Browse files
authored
Merge pull request #3 from Speedy37/master
Build on windows + ci enhancements
2 parents 98273b2 + c7be312 commit 74669d2

File tree

7 files changed

+116
-83
lines changed

7 files changed

+116
-83
lines changed

.travis.yml

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
1-
dist: trusty
2-
sudo: require
3-
4-
addons:
5-
apt:
6-
sources:
7-
- ubuntu-toolchain-r-test
8-
packages:
9-
- gcc-6
10-
- g++-6
11-
- cmake
12-
131
language: rust
142

15-
cache: cargo
3+
sudo: required
164

175
rust:
186
- stable
7+
- beta
198
- nightly
209

10+
os:
11+
- windows
12+
- linux
13+
- osx
14+
15+
dist: xenial
16+
2117
matrix:
2218
allow_failures:
2319
- rust: nightly
24-
fast_finish: true
20+
21+
addons:
22+
apt:
23+
packages:
24+
- cmake
25+
- gcc
26+
27+
language: rust
28+
29+
cache: cargo
2530

2631
script:
27-
- cargo test --all
28-
- cargo test --all --features no_secure
32+
- cargo test --verbose --all
33+
- cargo test --verbose --all --no-default-features
34+
- cargo test --release --verbose --all
35+
- cargo test --release --verbose --all --no-default-features

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "mimalloc"
33
version = "0.1.2"
4-
authors = ["Octavian Oncescu <octavonce@gmail.com>"]
4+
authors = [
5+
"Octavian Oncescu <octavonce@gmail.com>",
6+
"Vincent Rouillé <vincent@speedy37.fr>",
7+
]
58
edition = "2018"
69
repository = "https://github.com/purpleprotocol/mimalloc_rust"
710
keywords = ["mimalloc", "allocator", "encrypted-heap", "performance"]
@@ -10,6 +13,9 @@ description = "Performance and security oriented drop-in allocator"
1013
license = "MIT"
1114
readme = "README.md"
1215

16+
[workspace]
17+
members = ["libmimalloc-sys" ]
18+
1319
[badges]
1420
travis-ci = { repository = "purpleprotocol/mimalloc_rust" }
1521

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,30 @@
11
# Mimalloc Rust
2+
23
[![Build Status](https://travis-ci.org/purpleprotocol/mimalloc_rust.svg?branch=master)](https://travis-ci.org/purpleprotocol/mimalloc_rust) [![Latest Version]][crates.io] [![Documentation]][docs.rs]
34

45
A drop-in global allocator wrapper around the [mimalloc](https://github.com/microsoft/mimalloc) allocator.
56
Mimalloc is a general purpose, performance oriented allocator built by Microsoft.
67

78
## Usage
9+
810
```rust
911
use mimalloc::MiMalloc;
1012

1113
#[global_allocator]
1214
static GLOBAL: MiMalloc = MiMalloc;
1315
```
1416

17+
## Requirements
18+
19+
[CMake](https://cmake.org/) and a __C__ compiler are required for building
20+
[mimalloc](https://github.com/microsoft/mimalloc) with cargo.
21+
1522
## Usage without secure mode
16-
By default this library builds mimalloc in secure mode. This means that
17-
heap allocations are encrypted, but this results in a 3% increase in overhead.
23+
24+
By default this library builds mimalloc in secure mode. This add guard pages,
25+
randomized allocation, encrypted free lists, etc. The performance penalty should
26+
only be around 3% according to [mimalloc](https://github.com/microsoft/mimalloc)
27+
own benchmarks.
1828

1929
To disable secure mode, in `Cargo.toml`:
2030
```rust
@@ -25,4 +35,4 @@ mimalloc = { version = "*", default-features = false }
2535
[crates.io]: https://crates.io/crates/mimalloc
2636
[Latest Version]: https://img.shields.io/crates/v/mimalloc.svg
2737
[Documentation]: https://docs.rs/mimalloc/badge.svg
28-
[docs.rs]: https://docs.rs/mimalloc
38+
[docs.rs]: https://docs.rs/mimalloc

libmimalloc-sys/build.rs

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,45 @@
11
use cmake::Config;
22

3-
#[cfg(not(feature = "secure"))]
43
fn main() {
5-
let mut dst = Config::new("c_src/mimalloc")
6-
.build();
4+
let mut cfg = &mut Config::new("c_src/mimalloc");
75

8-
dst.push("build");
6+
cfg = cfg.define("MI_OVERRIDE", "OFF");
97

10-
println!("cargo:rustc-link-search=native={}", dst.display());
11-
if cfg!(debug_assertions) {
12-
println!("cargo:rustc-link-lib=static=mimalloc-debug");
13-
} else {
14-
println!("cargo:rustc-link-lib=static=mimalloc");
8+
if cfg!(feature = "secure") {
9+
cfg = cfg.define("MI_SECURE", "OFF");
1510
}
16-
}
1711

18-
#[cfg(feature = "secure")]
19-
fn main() {
20-
let mut dst = Config::new("c_src/mimalloc")
21-
.define("SECURE", "ON")
22-
.build();
12+
if cfg!(all(windows, target_env = "msvc")) {
13+
// cc::get_compiler have /nologo /MD default flags that are cmake::Config
14+
// defaults to. Those flags prevents mimalloc from building on windows
15+
// extracted from default cmake configuration on windows
16+
if cfg!(debug_assertions) {
17+
// CMAKE_C_FLAGS + CMAKE_C_FLAGS_DEBUG
18+
cfg = cfg.cflag("/DWIN32 /D_WINDOWS /W3 /MDd /Zi /Ob0 /Od /RTC1");
19+
} else {
20+
// CMAKE_C_FLAGS + CMAKE_C_FLAGS_RELEASE
21+
cfg = cfg.cflag("/DWIN32 /D_WINDOWS /W3 /MD /O2 /Ob2 /DNDEBUG");
22+
}
23+
}
2324

24-
dst.push("build");
25-
26-
println!("cargo:rustc-link-search=native={}", dst.display());
27-
if cfg!(debug_assertions) {
28-
println!("cargo:rustc-link-lib=static=mimalloc-debug");
25+
let (out_dir, out_name) = if cfg!(all(windows, target_env = "msvc")) {
26+
if cfg!(debug_assertions) {
27+
("./build/Debug", "mimalloc-static-debug")
28+
} else {
29+
("./build/Release", "mimalloc-static")
30+
}
2931
} else {
30-
println!("cargo:rustc-link-lib=static=mimalloc-secure");
31-
}
32-
}
32+
if cfg!(debug_assertions) {
33+
("./build", "mimalloc-debug")
34+
} else {
35+
("./build", "mimalloc")
36+
}
37+
};
38+
39+
// Build mimalloc-static
40+
let mut dst = cfg.build_target("mimalloc-static").build();
41+
dst.push(out_dir);
42+
43+
println!("cargo:rustc-link-search=native={}", dst.display());
44+
println!("cargo:rustc-link-lib={}", out_name);
45+
}

libmimalloc-sys/src/lib.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,35 @@
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;
9-
pub fn mi_zalloc_aligned(size: size_t, alignment: size_t) -> *const c_void;
10-
pub fn mi_malloc_aligned(size: size_t, alignment: size_t) -> *const c_void;
11-
pub fn mi_realloc_aligned(p: *const c_void, size: size_t, alignment: size_t) -> *const c_void;
12-
pub fn mi_free(p: *const c_void) -> c_void;
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;
9+
pub fn mi_zalloc_aligned(size: size_t, alignment: size_t) -> *const c_void;
10+
pub fn mi_malloc_aligned(size: size_t, alignment: size_t) -> *const c_void;
11+
pub fn mi_realloc_aligned(p: *const c_void, size: size_t, alignment: size_t) -> *const c_void;
12+
pub fn mi_free(p: *const c_void) -> c_void;
1313
}
1414

1515
#[cfg(test)]
1616
mod tests {
17-
use super::*;
17+
use super::*;
1818

19-
#[test]
20-
fn it_frees_memory_malloc() {
21-
let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
22-
unsafe { mi_free(ptr as *const c_void) };
23-
}
19+
#[test]
20+
fn it_frees_memory_malloc() {
21+
let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
22+
unsafe { mi_free(ptr as *const c_void) };
23+
}
2424

25-
#[test]
26-
fn it_frees_memory_zalloc() {
27-
let ptr = unsafe { mi_zalloc_aligned(8, 8) } as *mut u8;
28-
unsafe { mi_free(ptr as *const c_void) };
29-
}
25+
#[test]
26+
fn it_frees_memory_zalloc() {
27+
let ptr = unsafe { mi_zalloc_aligned(8, 8) } as *mut u8;
28+
unsafe { mi_free(ptr as *const c_void) };
29+
}
3030

31-
#[test]
32-
fn it_frees_memory_realloc() {
33-
let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
34-
let ptr = unsafe { mi_realloc_aligned(ptr as *const c_void, 8, 8) } as *mut u8;
35-
unsafe { mi_free(ptr as *const c_void) };
36-
}
37-
}
31+
#[test]
32+
fn it_frees_memory_realloc() {
33+
let ptr = unsafe { mi_malloc_aligned(8, 8) } as *mut u8;
34+
let ptr = unsafe { mi_realloc_aligned(ptr as *const c_void, 8, 8) } as *mut u8;
35+
unsafe { mi_free(ptr as *const c_void) };
36+
}
37+
}

src/lib.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44

55
//! A drop-in global allocator wrapper around the [mimalloc](https://github.com/microsoft/mimalloc) allocator.
66
//! Mimalloc is a general purpose, performance oriented allocator built by Microsoft.
7-
//!
7+
//!
88
//! ## Usage
99
//! ```rust,ignore
1010
//! use mimalloc::MiMalloc;
1111
//!
1212
//! #[global_allocator]
1313
//! static GLOBAL: MiMalloc = MiMalloc;
1414
//! ```
15-
//!
15+
//!
1616
//! ## Usage without secure mode
1717
//! By default this library builds mimalloc in secure mode. This means that
1818
//! heap allocations are encrypted, but this results in a 3% increase in overhead.
19-
//!
19+
//!
2020
//! To disable secure mode, in `Cargo.toml`:
2121
//! ```rust,ignore
2222
//! [dependencies]
@@ -26,9 +26,8 @@
2626
extern crate libmimalloc_sys as ffi;
2727

2828
use core::alloc::{GlobalAlloc, Layout};
29-
use core::ptr;
30-
use libc::c_void;
3129
use ffi::*;
30+
use libc::c_void;
3231

3332
// Copied from https://github.com/rust-lang/rust/blob/master/src/libstd/sys_common/alloc.rs
3433
#[cfg(all(any(
@@ -52,7 +51,7 @@ const MIN_ALIGN: usize = 8;
5251
const MIN_ALIGN: usize = 16;
5352

5453
/// Drop-in mimalloc global allocator.
55-
///
54+
///
5655
/// ## Usage
5756
/// ```rust,ignore
5857
/// use mimalloc::MiMalloc;
@@ -68,10 +67,9 @@ unsafe impl GlobalAlloc for MiMalloc {
6867
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
6968
mi_malloc(layout.size()) as *mut u8
7069
} else {
71-
#[cfg(target_os = "macos")]
72-
{
70+
if cfg!(target_os = "macos") {
7371
if layout.align() > (1 << 31) {
74-
return ptr::null_mut()
72+
return core::ptr::null_mut();
7573
}
7674
}
7775

@@ -84,17 +82,16 @@ unsafe impl GlobalAlloc for MiMalloc {
8482
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
8583
mi_zalloc(layout.size()) as *mut u8
8684
} else {
87-
#[cfg(target_os = "macos")]
88-
{
85+
if cfg!(target_os = "macos") {
8986
if layout.align() > (1 << 31) {
90-
return ptr::null_mut()
87+
return core::ptr::null_mut();
9188
}
9289
}
9390

9491
mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8
9592
}
9693
}
97-
94+
9895
#[inline]
9996
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
10097
mi_free(ptr as *const c_void);
@@ -147,4 +144,4 @@ mod tests {
147144
alloc.dealloc(ptr, layout);
148145
}
149146
}
150-
}
147+
}

0 commit comments

Comments
 (0)