pub struct RawAlloc<A: Allocator = Global> { /* private fields */ }
Expand description
A safe wrapper around a raw allocation with known layout.
§Safety
This type ensures that:
- The wrapped pointer is always non-null and properly aligned
- Memory is automatically deallocated when dropped
- Reallocation maintains proper alignment and size constraints
§Example
use safe_allocator_api::RawAlloc;
// Create a new allocation of 1024 bytes
let layout = Layout::array::<u8>(1024).unwrap();
let mut alloc = RawAlloc::new(layout).expect("allocation failed");
// Write some data
unsafe {
core::ptr::write(alloc.as_mut_ptr(), 42u8);
}
// Automatically deallocated when dropped
Implementations§
Source§impl<A: Allocator> RawAlloc<A>
impl<A: Allocator> RawAlloc<A>
Sourcepub fn new_in(layout: Layout, allocator: A) -> Result<Self, AllocError>
pub fn new_in(layout: Layout, allocator: A) -> Result<Self, AllocError>
Creates a new allocation with the given layout using the provided allocator.
This is equivalent to calling Allocator::allocate
but provides automatic
cleanup when the allocation is no longer needed.
§Arguments
layout
- The desired memory layoutallocator
- The allocator to use
§Errors
Returns AllocError
if the allocator reports an error or if the layout
has a size of 0.
§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]
use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;
let layout = Layout::new::<u64>();
let alloc = RawAlloc::new_in(layout, Global)?;
Sourcepub fn new_zeroed_in(layout: Layout, allocator: A) -> Result<Self, AllocError>
pub fn new_zeroed_in(layout: Layout, allocator: A) -> Result<Self, AllocError>
Creates a new zeroed allocation with the given layout using the provided allocator.
This is equivalent to calling Allocator::allocate_zeroed
but provides automatic
cleanup when the allocation is no longer needed.
§Errors
Returns AllocError
if the allocator reports an error or if the layout
has a size of 0.
Sourcepub fn grow(&mut self, new_layout: Layout) -> Result<(), AllocError>
pub fn grow(&mut self, new_layout: Layout) -> Result<(), AllocError>
Attempts to grow the allocation to the new layout.
§Errors
Returns AllocError
if:
- The allocator reports an error
- The new layout has a size of 0
- The new size is smaller than the current size (use
Self::shrink
instead)
§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]
use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;
let layout = Layout::array::<u8>(100).unwrap();
let mut alloc = RawAlloc::new(layout)?;
// Grow the allocation
let new_layout = Layout::array::<u8>(200).unwrap();
alloc.grow(new_layout)?;
Sourcepub fn grow_zeroed(&mut self, new_layout: Layout) -> Result<(), AllocError>
pub fn grow_zeroed(&mut self, new_layout: Layout) -> Result<(), AllocError>
Attempts to grow the allocation to the new layout, zeroing the additional memory.
This is equivalent to Self::grow
but ensures any additional memory is zeroed.
§Errors
Returns AllocError
if:
- The allocator reports an error
- The new layout has a size of 0
- The new size is smaller than the current size (use
Self::shrink
instead)
Sourcepub fn shrink(&mut self, new_layout: Layout) -> Result<(), AllocError>
pub fn shrink(&mut self, new_layout: Layout) -> Result<(), AllocError>
Attempts to shrink the allocation to the new layout.
§Errors
Returns AllocError
if:
- The allocator reports an error
- The new layout has a size of 0
- The new size is larger than the current size (use
Self::grow
instead)
§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]
use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;
let layout = Layout::array::<u8>(200).unwrap();
let mut alloc = RawAlloc::new(layout)?;
// Shrink the allocation
let new_layout = Layout::array::<u8>(100).unwrap();
alloc.shrink(new_layout)?;
Sourcepub fn as_ptr(&self) -> *const u8
pub fn as_ptr(&self) -> *const u8
Returns a raw pointer to the allocated memory.
§Safety
The caller must ensure that the memory is accessed according to the original layout constraints.
Sourcepub fn as_mut_ptr(&mut self) -> *mut u8
pub fn as_mut_ptr(&mut self) -> *mut u8
Returns a raw mutable pointer to the allocated memory.
§Safety
The caller must ensure that the memory is accessed according to the original layout constraints.
Sourcepub fn as_slice(&self) -> &[u8] ⓘ
pub fn as_slice(&self) -> &[u8] ⓘ
Returns a slice reference to the allocated memory.
This provides a safe interface to access the allocated memory as a byte slice.
§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]
use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;
let layout = Layout::array::<u8>(100).unwrap();
let alloc = RawAlloc::new(layout)?;
let slice = alloc.as_slice();
assert_eq!(slice.len(), 100);
Sourcepub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
pub fn as_mut_slice(&mut self) -> &mut [u8] ⓘ
Returns a mutable slice reference to the allocated memory.
This provides a safe interface to access the allocated memory as a mutable byte slice.
§Example
#![cfg_attr(feature = "nightly", feature(allocator_api))]
use safe_allocator_api::prelude::*;
use safe_allocator_api::RawAlloc;
let layout = Layout::array::<u8>(100).unwrap();
let mut alloc = RawAlloc::new(layout)?;
let slice = alloc.as_mut_slice();
slice[0] = 42;
assert_eq!(slice[0], 42);
Source§impl RawAlloc
impl RawAlloc
Sourcepub fn new(layout: Layout) -> Result<Self, AllocError>
pub fn new(layout: Layout) -> Result<Self, AllocError>
Creates a new allocation with the given layout using the global allocator.
This is equivalent to calling Self::new_in
with the global allocator.
Sourcepub fn new_zeroed(layout: Layout) -> Result<Self, AllocError>
pub fn new_zeroed(layout: Layout) -> Result<Self, AllocError>
Creates a new zeroed allocation with the given layout using the global allocator.
This is equivalent to calling Self::new_zeroed_in
with the global allocator.