A small, no_std/no_alloc-friendly allocation interface for raw buffers, with explicit layouts, split allocator
traits, and structured errors.
Version: 0.12.1
MSRV: 1.46.0 (some features require newer compilers or nightly; see Feature flags)
- Split allocator traits:
Alloc,Dealloc,Realloc, plusBasicAllocandFullAllocaliases - Mutable versions of allocator traits for allocation operations which require mutable access to the allocator
- Stateless allocator trait family (
traits::zst_alloc:ZstAlloc,ZstDealloc,ZstRealloc,ZstBasicAlloc,ZstFullAlloc) exposing associated functions instead of&selfmethods, for allocators with no per-instance state - Checked allocation traits (
alloc_checked_traitfeature) which return an error on invalid arguments instead of causing undefined behavior - Temporary/scoped allocation trait for function-scoped allocations (AllocTemp), plus a stateless counterpart
(
ZstAllocTemp) AllocDescriptortrait describing an allocator's supportedAllocFeaturesand minimum alignment- Custom
Layouttype with conversion to/fromalloc::alloc::Layout(unlessno_allocis on andstdisn't) - 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 - Data utilities for size/alignment/metadata via
traits::data::type_propsandtraits::data::marker no_stdby default;allocis used unlessno_allocis enabled (stdis used in place ofallocifstdandno_allocare both enabled)
[dependencies]
memapi2 = "0.12.1"If you want common optional features:
[dependencies]
memapi2 = { version = "0.12.1", features = ["full_std"] }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_temp_trait: scoped/temporary allocation trait (AllocTemp)alloc_checked_trait: checked allocation traits (traits::alloc_checked) which return an error on invalid arguments instead of causing undefined behaviorcatch_unwind: catch unwinds across the C boundary instack_alloc(requiresstd)wip_stdlib_data: VERY wip, do not attempt to usec_alloc: Cposix_memalign-style allocator (c_alloc::CAlloc)stack_alloc:alloca-based allocator (stack_alloc::StackAlloc, requires a C toolchain, MSRV on macOS is ~1.56)metadata: nightlycore::ptr::Pointeeandcore::ptr::metadatametadata supportno_alloc: disable thealloccrate (removesDefaultAlloc,StdLayout, and implementations for theSystemallocator, unlessstdis on)no_nightly: disable automatic nightly detection inbuild.rsfull: convenience bundle (c_alloc,alloc_checked_trait,stack_alloc)full_nightly:full+metadatafull_std:full+catch_unwind+os_err_reportingfull_nightly_std:full_nightly+full_std
- API reference: https://docs.rs/memapi2
Licensed under GPL-3.0 OR MIT. See LICENSE-GPL and LICENSE-MIT.