A small, no_std/no_alloc-friendly allocation interface for raw buffers, with explicit layouts,
split allocator traits, and structured errors.
Version: 0.9.2 MSRV: 1.46.0 (some features require newer compilers or nightly; see Feature flags)
- Split allocator traits:
Alloc,Dealloc,Grow,Shrink,Realloc, plusBasicAllocandFullAllocaliases - Mutable versions of allocator traits for allocation operations which require mutable access to the allocator
- Temporary/scoped allocation trait for function-scoped allocations
- Custom
Layouttype with conversion to/fromalloc::alloc::Layout(unlessno_allocis on) - Generic
Errortypes for allocation traits - Structured error reporting via
ErrorandCause, with optional OS error capture - Optional allocator implementations:
DefaultAlloc,std::alloc::System,c_alloc::CAlloc,stack_alloc::StackAlloc(experimental) - Data utilities for size/alignment/metadata via
data::type_propsanddata::marker no_stdby default;allocis used unlessno_allocis enabled
[dependencies]
memapi2 = "0.9.2"If you want common optional features:
[dependencies]
memapi2 = { version = "0.9.2", features = ["os_err_reporting", "alloc_mut_traits"] }use memapi2::{DefaultAlloc, Layout, traits::*};
fn main() -> Result<(), memapi2::error::Error> {
let alloc = DefaultAlloc;
let layout = Layout::from_size_align(64, 8)?;
let ptr = alloc.alloc(layout)?;
unsafe {
core::ptr::write_bytes(ptr.as_ptr(), 0xCD, layout.size());
alloc.try_dealloc(ptr, layout)?;
}
Ok(())
}std: enablestdintegration (includingstd::alloc::System)os_err_reporting: best-effort OS error reporting viaerrno(requiresstd)alloc_mut_traits: mutable allocator trait variants (AllocMut,DeallocMut, ...)alloc_temp_trait: scoped/temporary allocation trait (AllocTemp)c_alloc: Caligned_alloc-style allocator (c_alloc::CAlloc)stack_alloc:alloca-based allocator (stack_alloc::StackAlloc, experimental, requires a C toolchain)c_str: enableCStr-specific data traits inno_std(MSRV: 1.64)metadata: nightlycore::ptr::Pointeemetadata supportsized_hierarchy: nightlycore::marker::MetaSizedsupportno_alloc: disable thealloccrate (removesDefaultAlloc,StdLayout, and implementations for theSystemallocator)no_nightly: disable automatic nightly detection inbuild.rsfull_msrv: convenience bundle (os_err_reporting,c_alloc,stack_alloc,alloc_mut_traits)full:full_msrv+c_strall_nightly:metadata+sized_hierarchyfull_nightly:full+all_nightly
DefaultAllocdelegates to the global allocator. Do not set it as the global allocator itself, or you will cause infinite recursion.- This crate is low-level and uses raw pointers. Read the safety contracts on trait methods and layout constructors carefully.
- API reference: https://docs.rs/memapi2
Licensed under GPL-3.0 OR MIT. See LICENSE-GPL and LICENSE-MIT.