3 releases

0.3.2 Jun 4, 2024
0.3.1 Apr 24, 2024
0.3.0 Feb 26, 2024

#158 in Memory management

Download history 146/week @ 2024-07-24 115/week @ 2024-07-31 198/week @ 2024-08-07 160/week @ 2024-08-14 228/week @ 2024-08-21 122/week @ 2024-08-28 102/week @ 2024-09-04 214/week @ 2024-09-11 115/week @ 2024-09-18 129/week @ 2024-09-25 117/week @ 2024-10-02 209/week @ 2024-10-09 352/week @ 2024-10-16 74/week @ 2024-10-23 202/week @ 2024-10-30 130/week @ 2024-11-06

803 downloads per month
Used in syd

MIT license

17KB
395 lines

The Best and Highest-Leveled and Newest binding for MiMalloc Ever Existed in Rust

mimalloc 2.1.7 stable

Why create this

  • in repo https://github.com/LemonHX/mimalloc-rust/pulls there're many pull requests open for a long time, in other words it's garbage. Once these pull requests are merged, I am going to yank this crate.
  • in repo https://github.com/purpleprotocol/mimalloc_rust i didn't see any data types and it neither has all the functionalities which mimalloc has provided, in other words it's garbage.

Usage

first add to dependencies

[dependencies]
mimalloc2-rust = "0.3"

then set the global allocator

use mimalloc2_rust::*;

#[global_allocator]
static GLOBAL_MIMALLOC: GlobalMiMalloc = GlobalMiMalloc;

lib.rs:

this crate provides the best binding for mimalloc

Example Usage

first add to dependencies

[dependencies]
mimalloc2-rust = "0.3"

then set the global allocator

use mimalloc2_rust::*;

#[global_allocator]
static GLOBAL_MIMALLOC: GlobalMiMalloc = GlobalMiMalloc;

Allocator API!

#![feature(allocator_api)]
use std::{ffi::c_void, mem::ManuallyDrop};

use mimalloc2_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);
}

Dependencies

~0.5–0.8MB
~16K SLoC