Skip to content

Commit

Permalink
Merge branch 'purpleprotocol:master' into fix-pr-106
Browse files Browse the repository at this point in the history
  • Loading branch information
nathaniel-daniel authored May 20, 2024
2 parents 8f2a80c + 679b170 commit bb3dce1
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 5 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ jobs:

- name: Test libmimalloc-sys crate bindings (no secure)
run: cargo run -p libmimalloc-sys-test

- name: Build (extended)
run: cargo build --features extended

- name: Test (extended)
run: cargo test --features extended

- name: Test libmimalloc-sys crate bindings (extended)
run: cargo run --features extended -p libmimalloc-sys-test

lint:
name: Rustfmt / Clippy
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ debug = ["libmimalloc-sys/debug"]
debug_in_debug = ["libmimalloc-sys/debug_in_debug"]
local_dynamic_tls = ["libmimalloc-sys/local_dynamic_tls"]
no_thp = ["libmimalloc-sys/no_thp"]
extended = ["libmimalloc-sys/extended"]
6 changes: 6 additions & 0 deletions libmimalloc-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ fn main() {

let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!");
let target_family = env::var("CARGO_CFG_TARGET_FAMILY").expect("target_family not defined!");
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!");

if env::var_os("CARGO_FEATURE_OVERRIDE").is_some() {
// Overriding malloc is only available on windows in shared mode, but we
Expand Down Expand Up @@ -53,4 +54,9 @@ fn main() {
}

build.compile("mimalloc");

// on armv6 we need to link with libatomic
if target_os == "linux" && target_arch == "arm" {
println!("cargo:rustc-link-lib=dylib=atomic");
}
}
21 changes: 21 additions & 0 deletions src/extended.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::MiMalloc;

impl MiMalloc {
/// Get the mimalloc version.
///
/// For mimalloc version 1.8.6, this will return 186.
pub fn version(&self) -> u32 {
unsafe { ffi::mi_version() as u32 }
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn it_gets_version() {
let version = MiMalloc.version();
assert!(version != 0);
}
}
15 changes: 10 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@
//! static GLOBAL: MiMalloc = MiMalloc;
//! ```
//!
//! ## Usage without secure mode
//! By default this library builds mimalloc in secure mode. This means that
//! heap allocations are encrypted, but this results in a 3% increase in overhead.
//! ## Usage with secure mode
//! Using secure mode adds guard pages,
//! randomized allocation, encrypted free lists, etc. The performance penalty is usually
//! around 10% according to [mimalloc's](https://github.com/microsoft/mimalloc)
//! own benchmarks.
//!
//! To disable secure mode, in `Cargo.toml`:
//! To enable secure mode, put in `Cargo.toml`:
//! ```rust,ignore
//! [dependencies]
//! mimalloc = { version = "*", default-features = false }
//! mimalloc = { version = "*", features = ["secure"] }
//! ```
extern crate libmimalloc_sys as ffi;

#[cfg(feature = "extended")]
mod extended;

use core::alloc::{GlobalAlloc, Layout};
use core::ffi::c_void;
use ffi::*;
Expand Down

0 comments on commit bb3dce1

Please sign in to comment.