Crate mimalloc_rust
source ·Expand description
this crate provides the best binding for mimalloc
Example Usage
first add to dependencies
[dependencies]
mimalloc-rust = "0.2"
then set the global allocator
use mimalloc_rust::*;
#[global_allocator]
static GLOBAL_MIMALLOC: GlobalMiMalloc = GlobalMiMalloc;
Allocator API!
#![feature(allocator_api)]
use std::{ffi::c_void, mem::ManuallyDrop};
use mimalloc_rust::{
heap::{HeapVisitor, MiMallocHeap},
raw::{
heap::{mi_heap_area_t, mi_heap_delete, mi_heap_new},
types::mi_heap_t,
},
with_heap, GlobalMiMalloc,
};
#[derive(Debug, Clone)]
struct TestHeap {
heap: *mut mi_heap_t,
}
use std::ops::Deref;
impl Deref for TestHeap {
type Target = *mut mi_heap_t;
fn deref(&self) -> &Self::Target {
&self.heap
}
}
impl TestHeap {
fn new() -> Self {
Self {
heap: unsafe { mi_heap_new() },
}
}
}
impl Drop for TestHeap {
fn drop(&mut self) {
unsafe { mi_heap_delete(self.heap) }
}
}
#[test]
fn test_allocator_api() {
let allocator = MiMallocHeap::new(TestHeap::new());
let mut b: Vec<u8, &MiMallocHeap<TestHeap>> = Vec::new_in(&allocator);
b.push(1);
b.push(2);
assert_eq!(b[0], 1);
assert_eq!(b[1], 2);
}
Re-exports
pub use mimalloc_rust_sys as raw;
Modules
- First-class heaps that can be destroyed in one go.
Macros
- a macro which could only be used in single thread condition
Structs
- The global allocator