### MiMalloc Global Allocator Source: https://docs.rs/mimalloc/latest/mimalloc/struct.MiMalloc This section details the `MiMalloc` struct and its implementation as a global allocator in Rust, along with usage examples. ```APIDOC ## Struct MiMalloc ### Description Drop-in mimalloc global allocator. ### Usage ```rust use mimalloc::MiMalloc; #[global_allocator] static GLOBAL: MiMalloc = MiMalloc; ``` ## Trait Implementations ### impl GlobalAlloc for MiMalloc #### `unsafe fn alloc(&self, layout: Layout) -> *mut u8` Allocates memory as described by the given `layout`. #### `unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8` Behaves like `alloc`, but also ensures that the contents are set to zero before being returned. #### `unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout)` Deallocates the block of memory at the given `ptr` pointer with the given `layout`. #### `unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8` Shrinks or grows a block of memory to the given `new_size` in bytes. The block is described by the given `ptr` pointer and `layout`. ``` -------------------------------- ### Enable Secure Mode for mimalloc in Rust Source: https://docs.rs/mimalloc/latest/index This example shows how to enable the secure mode for the mimalloc allocator in a Rust project by specifying the 'secure' feature flag in the `Cargo.toml` file. Secure mode enhances memory safety with features like guard pages and randomization, potentially with a slight performance cost. ```toml [dependencies] mimalloc = { version = "*", features = ["secure"] } ``` -------------------------------- ### Rust mimalloc Allocation and Deallocation Tests Source: https://docs.rs/mimalloc/latest/src/mimalloc/lib.rs Contains unit tests for the `MiMalloc` global allocator in Rust. These tests verify the functionality of allocating, zero-allocating, and reallocating memory blocks of various sizes, ensuring that memory can be safely deallocated afterwards. ```rust use super::*; use core::alloc::Layout; #[test] fn it_frees_allocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_allocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_zero_allocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_zero_allocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_reallocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, 16); alloc.dealloc(ptr, layout); } } #[test] fn it_frees_reallocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = MiMalloc; let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, 2 << 20); alloc.dealloc(ptr, layout); } } ``` -------------------------------- ### MiMalloc GlobalAlloc Alloc Method Source: https://docs.rs/mimalloc/latest/mimalloc/struct.MiMalloc Demonstrates the 'alloc' method for the MiMalloc global allocator. This function is responsible for allocating memory according to the specified layout. ```rust unsafe fn alloc(&self, layout: Layout) -> *mut u8 ``` -------------------------------- ### MiMalloc GlobalAlloc Alloc Zeroed Method Source: https://docs.rs/mimalloc/latest/mimalloc/struct.MiMalloc Illustrates the 'alloc_zeroed' method for the MiMalloc global allocator. This method allocates memory and ensures its contents are initialized to zero. ```rust unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 ``` -------------------------------- ### MiMalloc GlobalAlloc Realloc Method Source: https://docs.rs/mimalloc/latest/mimalloc/struct.MiMalloc Details the 'realloc' method for the MiMalloc global allocator. This method resizes a memory block to a new size. ```rust unsafe fn realloc( &self, ptr: *mut u8, layout: Layout, new_size: usize, ) -> *mut u8 ``` -------------------------------- ### Rust mimalloc Global Allocator Implementation Source: https://docs.rs/mimalloc/latest/src/mimalloc/lib.rs Provides the core implementation of the `MiMalloc` struct in Rust, which adheres to the `GlobalAlloc` trait. It includes unsafe implementations for `alloc`, `alloc_zeroed`, `dealloc`, and `realloc` using the underlying `mimalloc` C functions. ```rust use core::alloc::{GlobalAlloc, Layout}; use core::ffi::c_void; use libmimalloc_sys::*; pub struct MiMalloc; unsafe impl GlobalAlloc for MiMalloc { #[inline] unsafe fn alloc(&self, layout: Layout) -> *mut u8 { mi_malloc_aligned(layout.size(), layout.align()) as *mut u8 } #[inline] unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { mi_zalloc_aligned(layout.size(), layout.align()) as *mut u8 } #[inline] unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { mi_free(ptr as *mut c_void); } #[inline] unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { mi_realloc_aligned(ptr as *mut c_void, new_size, layout.align()) as *mut u8 } } ``` -------------------------------- ### MiMalloc GlobalAlloc Dealloc Method Source: https://docs.rs/mimalloc/latest/mimalloc/struct.MiMalloc Shows the 'dealloc' method for the MiMalloc global allocator. This function deallocates a previously allocated block of memory. ```rust unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) ``` -------------------------------- ### Set mimalloc as Global Allocator in Rust Source: https://docs.rs/mimalloc/latest/index This code snippet demonstrates how to integrate the mimalloc crate as the global allocator in a Rust project by adding the `#[global_allocator]` attribute. It requires the `mimalloc` crate to be added as a dependency. ```rust use mimalloc::MiMalloc; #[global_allocator] static GLOBAL: MiMalloc = MiMalloc; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.