### BEMalloc::init Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Starts the allocator's background job. This function should be called within the `main()` function to ensure the allocator is properly initialized. ```APIDOC ## BEMalloc::init ### Description Starts an allocator background job. Should be called in `main()` function. ### Method `init()` ### Usage This method should be invoked in the `main` function of your application. ``` -------------------------------- ### BEMalloc::init Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Starts the background job for the BEMalloc allocator. This should be called within the `main()` function of your application. ```APIDOC ## BEMalloc::init ### Description Starts an allocator background job. This function should be called in the `main()` function. ### Method `init()` ### Usage ```rust fn main() { BEMalloc::init(); // ... rest of your application } ``` ``` -------------------------------- ### BEMalloc Build Script Configuration Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=std%3A%3Avec This example shows how to configure your `build.rs` file to handle potential build dependencies and linking requirements for BEMalloc, particularly for mimalloc on different platforms. ```APIDOC ## Build Script Configuration ### Description This snippet provides guidance for your `build.rs` file to work around mimalloc build dependencies and ensure correct linking on various platforms. ### Usage Add the following to your `build.rs`: ```rust,ignore use std::borrow::Cow; use std::env; fn main() { let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!"); let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!"); // on armv6 we need to link with libatomic if target_os == "linux" && target_arch == "arm" { // Embrace the atomic capability library across various platforms. // For instance, on certain platforms, llvm has relocated the atomic of the arm32 architecture to libclang_rt.builtins.a // while some use libatomic.a, and others use libatomic_ops.a. let atomic_name = match env::var("DEP_ATOMIC") { Ok(atomic_name) => Cow::Owned(atomic_name), Err(_) => Cow::Borrowed("atomic"), }; println!("cargo:rustc-link-lib={atomic_name}"); } // Link with libs needed on Windows if target_os == "windows" { // https://github.com/microsoft/mimalloc/blob/af21001f7a65eafb8fb16460b018ebf9d75e2ad8/CMakeLists.txt#L487 for lib in ["psapi", "shell32", "user32", "advapi32", "bcrypt"] { println!("cargo:rustc-link-lib={lib}"); } } } ``` ``` -------------------------------- ### Initialize BEMalloc Background Job Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Starts the background job for the BEMalloc allocator. This function should be called within the main function of your program. ```rust pub fn init() ``` -------------------------------- ### Global Allocator Setup Source: https://docs.rs/malloc-best-effort/0.1.4/index.html Integrate the BEMalloc allocator as the global allocator in your Rust project by placing this code in `src/main.rs`. Ensure `BEMalloc::init()` is called. ```rust use malloc_best_effort::BEMalloc; #[global_allocator] static GLOBAL: BEMalloc = BEMalloc::new(); fn main() { BEMalloc::init(); // Rest of main } ``` -------------------------------- ### BEMalloc Usage Example Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=std%3A%3Avec This snippet demonstrates how to integrate BEMalloc as the global allocator in your Rust project's `src/main.rs` file. It also shows the necessary initialization step. ```APIDOC ## BEMalloc Usage ### Description This section shows how to use `BEMalloc` as the global allocator in your Rust application. ### Usage Add the following to your `src/main.rs`: ```rust,ignore use malloc_best_effort::BEMalloc; #[global_allocator] static GLOBAL: BEMalloc = BEMalloc::new(); fn main() { BEMalloc::init(); // Rest of main } ``` ### Initialization Ensure `BEMalloc::init()` is called within your `main` function. ``` -------------------------------- ### Get Type ID with Any Trait Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Retrieves the `TypeId` of the current instance. This is part of the blanket implementation of the `Any` trait. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### BEMalloc Methods Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=std%3A%3Avec Provides details on how to create and initialize a BEMalloc allocator instance. ```APIDOC ## pub const fn new() -> Self ### Description Creates a new instance of the BEMalloc allocator. ### Returns A new `BEMalloc` instance. ## pub fn init() ### Description Starts the allocator's background job. This function should be called within the `main()` function. ### Usage Call this function early in your program's execution, typically at the beginning of `main()`. ``` -------------------------------- ### Build Script Configuration for Allocator Dependencies Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html This build script snippet configures linker flags for specific target OS and architectures, particularly for Linux ARM and Windows. It handles linking with atomic libraries and Windows-specific libraries required by mimalloc. ```rust use std::borrow::Cow; use std::env; fn main() { let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!"); let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!"); // on armv6 we need to link with libatomic if target_os == "linux" && target_arch == "arm" { // Embrace the atomic capability library across various platforms. // For instance, on certain platforms, llvm has relocated the atomic of the arm32 architecture to libclang_rt.builtins.a // while some use libatomic.a, and others use libatomic_ops.a. let atomic_name = match env::var("DEP_ATOMIC") { Ok(atomic_name) => Cow::Owned(atomic_name), Err(_) => Cow::Borrowed("atomic"), }; println!("cargo:rustc-link-lib={atomic_name}"); } // Link with libs needed on Windows if target_os == "windows" { // https://github.com/microsoft/mimalloc/blob/af21001f7a65eafb8fb16460b018ebf9d75e2ad8/CMakeLists.txt#L487 for lib in ["psapi", "shell32", "user32", "advapi32", "bcrypt"] { println!("cargo:rustc-link-lib={lib}"); } } } ``` -------------------------------- ### Initialize Allocator Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Initializes the allocator background job. This should be called in the main function. ```rust pub fn init() { init_impl(); } ``` -------------------------------- ### Initialize Allocator Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search= Initializes the allocator. This function should be called in the main function. ```rust pub fn init() { init_impl(); } ``` -------------------------------- ### BEMalloc::new Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Creates a new instance of the BEMalloc allocator. This is the constructor for the allocator. ```APIDOC ## BEMalloc::new ### Description Creates a new instance of the allocator. ### Method `new()` ### Returns - `Self`: A new instance of `BEMalloc`. ``` -------------------------------- ### BEMalloc::new Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Creates a new, independent instance of the BEMalloc allocator. ```APIDOC ## BEMalloc::new ### Description Creates a new instance of the BEMalloc allocator. ### Method `new()` ### Returns - `Self`: A new instance of `BEMalloc`. ``` -------------------------------- ### Workaround for mimalloc Build Dependencies Source: https://docs.rs/malloc-best-effort/0.1.4/index.html Use this code in `build.rs` to manage build dependencies for mimalloc, particularly for ARM architectures on Linux and linking necessary libraries on Windows. ```rust use std::borrow::Cow; use std::env; fn main() { let target_os = env::var("CARGO_CFG_TARGET_OS").expect("target_os not defined!"); let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("target_arch not defined!"); // on armv6 we need to link with libatomic if target_os == "linux" && target_arch == "arm" { // Embrace the atomic capability library across various platforms. // For instance, on certain platforms, llvm has relocated the atomic of the arm32 architecture to libclang_rt.builtins.a // while some use libatomic.a, and others use libatomic_ops.a. let atomic_name = match env::var("DEP_ATOMIC") { Ok(atomic_name) => Cow::Owned(atomic_name), Err(_) => Cow::Borrowed("atomic"), }; println!("cargo:rustc-link-lib={atomic_name}"); } // Link with libs needed on Windows if target_os == "windows" { // https://github.com/microsoft/mimalloc/blob/af21001f7a65eafb8fb16460b018ebf9d75e2ad8/CMakeLists.txt#L487 for lib in ["psapi", "shell32", "user32", "advapi32", "bcrypt"] { println!("cargo:rustc-link-lib={lib}"); } } } ``` -------------------------------- ### BEMalloc GlobalAlloc Implementation Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=std%3A%3Avec This section details the implementation of the `GlobalAlloc` trait for `BEMalloc`, showing how it delegates allocation, deallocation, zeroed allocation, and reallocation calls to its underlying allocator implementation. ```APIDOC ## GlobalAlloc Implementation for BEMalloc ### Description This documentation covers the `GlobalAlloc` trait implementation for the `BEMalloc` struct. It outlines the methods for memory allocation and deallocation. ### Trait `unsafe impl GlobalAlloc for BEMalloc` ### Methods - **`alloc(&self, layout: Layout) -> *mut u8`** Allocates memory for a given layout. - **`dealloc(&self, ptr: *mut u8, layout: Layout)`** Deallocates memory at the given pointer with the specified layout. - **`alloc_zeroed(&self, layout: Layout) -> *mut u8`** Allocates memory and initializes it to zero. - **`realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8`** Reallocates memory block to a new size. ``` -------------------------------- ### BEMalloc Implementation for Linux x86_64/aarch64 Little-Endian Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html This module defines the `BEMallocImpl` using `tcmalloc_better` and provides an `init_impl` function that calls `process_background_actions_thread` when the `std` feature is enabled. Otherwise, `init_impl` does nothing. ```rust mod tcmalloc { pub(crate) use tcmalloc_better::TCMalloc as BEMallocImpl; #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] #[inline] pub(crate) fn init_impl() { BEMallocImpl::process_background_actions_thread(); } #[cfg(not(feature = "std"))] #[cfg_attr(docsrs, doc(cfg(not(feature = "std"))))] #[inline] pub(crate) fn init_impl() {} } ``` -------------------------------- ### Test: Free Allocated Memory with Init Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Verifies memory deallocation after the allocator has been initialized. ```rust unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = BEMalloc::new(); BEMalloc::init(); let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } ``` -------------------------------- ### BEMalloc Default Implementation (Mimalloc) Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Uses `mimalloc` as the default allocator implementation when not targeting specific Linux configurations. This implementation does not require explicit initialization. ```rust pub(crate) use mimalloc::MiMalloc as BEMallocImpl; #[inline] pub(crate) fn init_impl() {} ``` -------------------------------- ### BEMalloc Implementation for Non-Linux x86_64/aarch64 Little-Endian Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html This module defines the `BEMallocImpl` using `mimalloc` and provides an `init_impl` function for targets not matching the specific Linux configuration. This is used when the `std` feature is not enabled. ```rust mod mimalloc { pub(crate) use mimalloc::MiMalloc as BEMallocImpl; #[inline] pub(crate) fn init_impl() {} } ``` -------------------------------- ### GlobalAlloc Trait Implementation Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=std%3A%3Avec Details the core memory allocation and deallocation functions provided by the GlobalAlloc trait. ```APIDOC ## unsafe fn alloc(&self, layout: Layout) -> *mut u8 ### Description Allocates a block of memory according to the specified `layout`. ### Parameters * `layout` (Layout) - Describes the size and alignment requirements for the memory block. ### Returns A raw pointer (`*mut u8`) to the beginning of the allocated memory block. ## unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) ### Description Deallocates a previously allocated block of memory. ### Parameters * `ptr` (*mut u8) - A pointer to the beginning of the memory block to deallocate. * `layout` (Layout) - The layout that was used when the memory block was allocated. ## unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 ### Description Allocates a block of memory as described by `layout`, ensuring that the memory contents are initialized to zero. ### Parameters * `layout` (Layout) - Describes the size and alignment requirements for the memory block. ### Returns A raw pointer (`*mut u8`) to the beginning of the allocated and zeroed memory block. ## unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 ### Description Shrinks or grows an existing memory block to a new size. ### Parameters * `ptr` (*mut u8) - A pointer to the beginning of the memory block to reallocate. * `layout` (Layout) - The layout of the original memory block. * `new_size` (usize) - The desired new size of the memory block in bytes. ### Returns A raw pointer (`*mut u8`) to the beginning of the reallocated memory block. This may be the same as `ptr` or a new location. ``` -------------------------------- ### Allocate Zeroed Memory with BEMalloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Allocates memory and ensures its contents are initialized to zero. Behaves like `alloc` but with zero-initialization. ```rust unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 ``` -------------------------------- ### BEMalloc Implementation for Linux x86_64/aarch64 Little Endian Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Uses `tcmalloc_better` as the allocator implementation for specific Linux configurations. Includes conditional initialization logic. ```rust pub(crate) use tcmalloc_better::TCMalloc as BEMallocImpl; #[cfg(feature = "std")] #[cfg_attr(docsrs, doc(cfg(feature = "std")))] #[inline] pub(crate) fn init_impl() { BEMallocImpl::process_background_actions_thread(); } #[cfg(not(feature = "std"))] #[cfg_attr(docsrs, doc(cfg(not(feature = "std"))))] #[inline] pub(crate) fn init_impl() {} ``` -------------------------------- ### BEMalloc Structure and GlobalAlloc Implementation Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Details the `BEMalloc` struct, its implementation of the `GlobalAlloc` trait, and its `Default` trait. ```APIDOC ## BEMalloc ### Description `BEMalloc` is a struct designed to be registered as the global memory allocator. It wraps an underlying allocator implementation (`BEMallocImpl`) that is chosen based on the target platform. ### Struct Definition ```rust pub struct BEMalloc { alloc_impl: BEMallocImpl, } ``` ### GlobalAlloc Implementation `BEMalloc` implements the `GlobalAlloc` trait, providing the standard methods for memory allocation and deallocation: - `alloc(&self, layout: Layout) -> *mut u8` - `dealloc(&self, ptr: *mut u8, layout: Layout)` - `alloc_zeroed(&self, layout: Layout) -> *mut u8` - `realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8` These methods delegate to the underlying `alloc_impl`. ### Default Implementation `BEMalloc` also implements the `Default` trait, allowing it to be created using `BEMalloc::default()`, which is equivalent to `BEMalloc::new()`. ### Constructor - `pub const fn new() -> Self`: Creates a new instance of the `BEMalloc` allocator. ``` -------------------------------- ### GlobalAlloc::alloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html Allocates a block of memory according to the specified layout. This is an unsafe function required by the GlobalAlloc trait. ```APIDOC ## GlobalAlloc::alloc ### Description Allocates memory as described by the given `layout`. ### Safety This is an `unsafe` function. The caller must ensure that the layout is valid and that memory is managed correctly. ### Parameters - `layout` (`Layout`): Describes the size and alignment of the memory to be allocated. ### Returns - `*mut u8`: A raw pointer to the beginning of the allocated memory block. ``` -------------------------------- ### GlobalAlloc::alloc_zeroed Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html Allocates a block of memory and ensures its contents are initialized to zero. This is an unsafe function required by the GlobalAlloc trait. ```APIDOC ## GlobalAlloc::alloc_zeroed ### Description Behaves like `alloc`, but also ensures that the contents are set to zero before being returned. ### Safety This is an `unsafe` function. The caller must ensure that the layout is valid and that memory is managed correctly. ### Parameters - `layout` (`Layout`): Describes the size and alignment of the memory to be allocated. ### Returns - `*mut u8`: A raw pointer to the beginning of the zeroed allocated memory block. ``` -------------------------------- ### Test: Free Zeroed Allocated Memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Verifies that memory allocated with `alloc_zeroed` can be correctly deallocated. ```rust unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } ``` -------------------------------- ### Test: Free Allocated Memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Verifies that memory allocated with `alloc` can be correctly deallocated. ```rust unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } ``` -------------------------------- ### GlobalAlloc for BEMalloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Implementation of the `GlobalAlloc` trait for `BEMalloc`, providing core memory allocation and deallocation functionalities. ```APIDOC ## GlobalAlloc for BEMalloc ### Description Provides the core memory management functions for the `BEMalloc` allocator by implementing the `GlobalAlloc` trait. ### Methods #### `alloc(&self, layout: Layout) -> *mut u8` Allocates memory as described by the given `layout`. #### `dealloc(&self, ptr: *mut u8, layout: Layout)` Deallocates the block of memory at the given `ptr` pointer with the given `layout`. #### `alloc_zeroed(&self, layout: Layout) -> *mut u8` Behaves like `alloc`, but also ensures that the contents are set to zero before being returned. #### `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`. ``` -------------------------------- ### Try Conversion from Another Type Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=u32+-%3E+bool Attempts to perform a conversion from type `U` to type `T`. Returns a `Result` indicating success or failure. Relies on `U: Into`. ```rust type Error = Infallible ``` ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### Convert From Self with From Trait Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Returns the argument unchanged, as part of the `From` for `T` implementation. ```rust fn from(t: T) -> T ``` -------------------------------- ### Test: Free Reallocated Memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Verifies that memory reallocated with `realloc` can be correctly deallocated. ```rust unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let new_size = 16; let new_layout = Layout::from_size_align(new_size, layout.align()).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, new_size); alloc.dealloc(ptr, new_layout); } ``` -------------------------------- ### GlobalAlloc for BEMalloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Implements the `GlobalAlloc` trait for BEMalloc, providing core memory allocation and deallocation functionalities. ```APIDOC ## GlobalAlloc for BEMalloc ### Description Implements the `GlobalAlloc` trait, enabling BEMalloc to be used as the global memory allocator for Rust. ### Methods #### `alloc(&self, layout: Layout) -> *mut u8` Allocates memory according to the specified `layout`. #### `dealloc(&self, ptr: *mut u8, layout: Layout)` Deallocates a block of memory pointed to by `ptr` with the given `layout`. #### `alloc_zeroed(&self, layout: Layout) -> *mut u8` Allocates memory and ensures its contents are initialized to zero. #### `realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8` Shrinks or grows an existing memory block to `new_size` bytes, as described by `ptr` and `layout`. ``` -------------------------------- ### Rust BEMalloc: Test memory deallocation with initialization Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Tests memory deallocation after initializing the BEMalloc allocator. This ensures that the allocator functions correctly when its initialization routine has been called. ```rust #[test] fn it_frees_allocated_memory_with_init() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = BEMalloc::new(); BEMalloc::init(); let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } } ``` -------------------------------- ### BEMalloc::try_from Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=u32+-%3E+bool Attempts to convert a u32 value into a bool. This is part of the TryFrom trait implementation for BEMalloc. ```APIDOC ## BEMalloc::try_from ### Description Attempts to convert a u32 value into a bool. This is part of the TryFrom trait implementation for BEMalloc. ### Method try_from ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example None ### Response #### Success Response (Result) - **Success**: Returns `Ok(bool)` if the conversion is successful. - **Error**: Returns `Err(Infallible)` if the conversion fails (though `Infallible` indicates this specific conversion will not fail). #### Response Example ```json { "example": "Ok(true)" } ``` ``` -------------------------------- ### BEMalloc Global Allocator Implementation Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Defines the `BEMalloc` struct which wraps an underlying allocator implementation (`BEMallocImpl`). It implements the `GlobalAlloc` trait, providing `alloc`, `dealloc`, `alloc_zeroed`, and `realloc` methods that delegate to the wrapped allocator. ```rust use core::alloc::{GlobalAlloc, Layout}; /// A memory allocator that can be registered as the standard library’s default /// through the `#[global_allocator]` attribute. pub struct BEMalloc { alloc_impl: BEMallocImpl, } unsafe impl GlobalAlloc for BEMalloc { #[inline] unsafe fn alloc(&self, layout: Layout) -> *mut u8 { unsafe { self.alloc_impl.alloc(layout) } } #[inline] unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { unsafe { self.alloc_impl.dealloc(ptr, layout) } } #[inline] unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { unsafe { self.alloc_impl.alloc_zeroed(layout) } } #[inline] unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { unsafe { self.alloc_impl.realloc(ptr, layout, new_size) } } } impl Default for BEMalloc { #[inline] fn default() -> Self { Self::new() } } impl BEMalloc { /// Create a new instance of allocator. #[inline] pub const fn new() -> Self { Self { alloc_impl: BEMallocImpl, } } } ``` -------------------------------- ### Allocate Memory with BEMalloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Allocates a block of memory according to the specified layout. This is a core function for the GlobalAlloc trait implementation. ```rust unsafe fn alloc(&self, layout: Layout) -> *mut u8 ``` -------------------------------- ### Try Convert Into Another Type with TryInto Trait Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Attempts to convert the current type into another type `U`. Returns a `Result` indicating success or failure. ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Try Conversion to Another Type Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=u32+-%3E+bool Attempts to convert the current type into another type `U` using `U::try_from(self)`. Returns a `Result` indicating success or failure. ```rust type Error = >::Error ``` ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Rust BEMalloc: Test basic memory deallocation Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Tests the functionality of allocating and deallocating a small block of memory using BEMalloc. This is a fundamental test for the allocator's core operations. ```rust #[test] fn it_frees_allocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } } ``` -------------------------------- ### Create New BEMalloc Instance Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Creates a new, independent instance of the BEMalloc allocator. This function is part of the BEMalloc struct's direct implementations. ```rust pub const fn new() -> Self ``` -------------------------------- ### Rust BEMalloc: Test deallocation of large zeroed memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Tests the deallocation of a large memory block that was allocated and zeroed. This verifies the allocator's performance with large, zero-initialized memory regions. ```rust #[test] fn it_frees_zero_allocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } } ``` -------------------------------- ### Rust BEMalloc: Test deallocation of large memory block Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Tests the deallocation of a large memory block (1MB) using BEMalloc. This verifies the allocator's ability to handle larger memory requests and deallocations efficiently. ```rust #[test] fn it_frees_allocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } } ``` -------------------------------- ### Rust BEMalloc: Test reallocation of memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Tests the reallocation of a small memory block using BEMalloc. This verifies the `realloc` functionality, ensuring memory can be resized and its contents preserved. ```rust #[test] fn it_frees_reallocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let new_size = 16; let new_layout = Layout::from_size_align(new_size, layout.align()).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, new_size); alloc.dealloc(ptr, new_layout); } } ``` -------------------------------- ### Rust BEMalloc: Test deallocation of zeroed memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Tests the deallocation of memory that was allocated using `alloc_zeroed`. This ensures that memory initialized to zero is correctly managed and deallocated. ```rust #[test] fn it_frees_zero_allocated_memory() { unsafe { let layout = Layout::from_size_align(8, 8).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } } ``` -------------------------------- ### Default Trait Implementation Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search=std%3A%3Avec Provides the default value for the BEMalloc type. ```APIDOC ## fn default() -> Self ### Description Returns the default value for the `BEMalloc` type. ### Returns A new `BEMalloc` instance representing the default state. ``` -------------------------------- ### Reallocate Memory with BEMalloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Shrinks or grows an existing memory block to a new size. It takes the current pointer, layout, and the desired new size. ```rust unsafe fn realloc( &self, ptr: *mut u8, layout: Layout, new_size: usize, ) -> *mut u8 ``` -------------------------------- ### Rust BEMalloc: Test reallocation of large memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html?search=u32+-%3E+bool Tests the reallocation of a large memory block using BEMalloc. This checks the `realloc` function's behavior with significant memory sizes, ensuring it handles resizing efficiently. ```rust #[test] fn it_frees_reallocated_big_memory() { unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let new_size = 2 << 20; let new_layout = Layout::from_size_align(new_size, layout.align()).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, new_size); alloc.dealloc(ptr, new_layout); } } ``` -------------------------------- ### Test: Free Zeroed Large Allocated Memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Tests the deallocation of a large block of memory allocated with `alloc_zeroed`. ```rust unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc_zeroed(layout); alloc.dealloc(ptr, layout); } ``` -------------------------------- ### Test: Free Reallocated Large Memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Tests the deallocation of a large block of memory after reallocation. ```rust unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let new_size = 2 << 20; let new_layout = Layout::from_size_align(new_size, layout.align()).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); let ptr = alloc.realloc(ptr, layout, new_size); alloc.dealloc(ptr, new_layout); } ``` -------------------------------- ### Try Convert From Another Type with TryFrom Trait Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Attempts to convert a value of type `U` into type `T`. Returns a `Result` indicating success or failure. ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### Default BEMalloc Instance Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Provides the default value for the BEMalloc type, as required by the Default trait implementation. ```rust fn default() -> Self ``` -------------------------------- ### Test: Free Large Allocated Memory Source: https://docs.rs/malloc-best-effort/0.1.4/src/malloc_best_effort/lib.rs.html Tests the deallocation of a large block of memory. ```rust unsafe { let layout = Layout::from_size_align(1 << 20, 32).unwrap(); let alloc = BEMalloc::new(); let ptr = alloc.alloc(layout); alloc.dealloc(ptr, layout); } ``` -------------------------------- ### Default::default Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html Returns the default value for the BEMalloc type. This implementation allows BEMalloc to be used with Rust's default value mechanisms. ```APIDOC ## Default::default ### Description Returns the “default value” for a type. ### Returns - `Self`: The default instance of BEMalloc. ``` -------------------------------- ### GlobalAlloc::realloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html Shrinks or grows an allocated block of memory to a new size. This is an unsafe function required by the GlobalAlloc trait. ```APIDOC ## GlobalAlloc::realloc ### Description 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`. ### Safety This is an `unsafe` function. The caller must ensure that `ptr` is a valid pointer previously allocated by this allocator and that `layout` describes the original allocation. ### Parameters - `ptr` (`*mut u8`): A pointer to the memory block to reallocate. - `layout` (`Layout`): The layout of the original memory block. - `new_size` (`usize`): The desired new size in bytes for the memory block. ### Returns - `*mut u8`: A raw pointer to the beginning of the reallocated memory block. This may be the same as `ptr` or a new location. ``` -------------------------------- ### GlobalAlloc::dealloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html Deallocates a previously allocated block of memory. This is an unsafe function required by the GlobalAlloc trait. ```APIDOC ## GlobalAlloc::dealloc ### Description Deallocates the block of memory at the given `ptr` pointer with the given `layout`. ### Safety This is an `unsafe` function. The caller must ensure that `ptr` is a valid pointer previously allocated by this allocator and that `layout` matches the original allocation. ### Parameters - `ptr` (`*mut u8`): A pointer to the memory block to deallocate. - `layout` (`Layout`): The layout of the memory block being deallocated. ``` -------------------------------- ### BEMalloc Struct Definition Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Defines the BEMalloc struct, which serves as a private memory allocator. It is intended to be registered globally. ```rust pub struct BEMalloc { /* private fields */ } ``` -------------------------------- ### Deallocate Memory with BEMalloc Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Deallocates a previously allocated block of memory at the given pointer and layout. This is essential for memory management. ```rust unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) ``` -------------------------------- ### Borrow Immutably with Borrow Trait Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Provides an immutable borrow to the contained type. This is part of the blanket implementation of the `Borrow` trait. ```rust fn borrow(&self) -> &T ``` -------------------------------- ### Convert Into Another Type with Into Trait Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Converts the current type into another type `U` if `U` implements `From`. This is part of the blanket `Into` implementation. ```rust fn into(self) -> U ``` -------------------------------- ### Borrow Mutably with BorrowMut Trait Source: https://docs.rs/malloc-best-effort/0.1.4/malloc_best_effort/struct.BEMalloc.html?search= Provides a mutable borrow to the contained type. This is part of the blanket implementation of the `BorrowMut` trait. ```rust fn borrow_mut(&mut self) -> &mut T ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.