5 releases

0.2.1 Apr 27, 2023
0.2.0 May 27, 2022
0.1.5 Dec 22, 2021
0.1.4 Dec 22, 2021
0.1.1 Aug 13, 2021

#63 in Memory management

Download history 5097/week @ 2023-12-23 12923/week @ 2023-12-30 5435/week @ 2024-01-06 16156/week @ 2024-01-13 13834/week @ 2024-01-20 10213/week @ 2024-01-27 11389/week @ 2024-02-03 6710/week @ 2024-02-10 5893/week @ 2024-02-17 3705/week @ 2024-02-24 7570/week @ 2024-03-02 7607/week @ 2024-03-09 6379/week @ 2024-03-16 6186/week @ 2024-03-23 10113/week @ 2024-03-30 8219/week @ 2024-04-06

31,427 downloads per month
Used in 25 crates (5 directly)

MIT license

475KB
9K SLoC

C 8K SLoC // 0.2% comments Rust 1K SLoC // 0.0% comments

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

mimalloc 1.7.9 stable

ci doc.rs crates.io

Why create this

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]
mimalloc-rust = "0.2.1"

then set the global allocator

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

Dependencies