pub struct TrackedAllocation<T> { /* private fields */ }Expand description
A tracked allocation that stores its layout for safe deallocation.
This wrapper ensures that memory is deallocated with the same layout that was used for allocation, preventing undefined behavior.
§Example
use ruvector_security::TrackedAllocation;
// Allocate memory for 100 f32 values
let mut alloc = TrackedAllocation::<f32>::new(100).unwrap();
// Write data
unsafe {
for i in 0..100 {
*alloc.as_mut_ptr().add(i) = i as f32;
}
}
// Memory is automatically deallocated when `alloc` is droppedImplementations§
Source§impl<T> TrackedAllocation<T>
impl<T> TrackedAllocation<T>
Sourcepub fn new(count: usize) -> SecurityResult<Self>
pub fn new(count: usize) -> SecurityResult<Self>
Allocate memory for count elements of type T.
§Errors
Returns an error if:
countis zero- The allocation size overflows
- The allocator fails
Sourcepub fn new_zeroed(count: usize) -> SecurityResult<Self>
pub fn new_zeroed(count: usize) -> SecurityResult<Self>
Allocate and zero-initialize memory.
Sourcepub fn as_mut_ptr(&mut self) -> *mut T
pub fn as_mut_ptr(&mut self) -> *mut T
Get a mutable raw pointer to the allocation.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Check if the allocation is empty (always false for valid allocations).
Sourcepub unsafe fn as_slice_mut(&mut self) -> &mut [T]
pub unsafe fn as_slice_mut(&mut self) -> &mut [T]
Convert to a mutable slice.
§Safety
The caller must ensure the memory has been properly initialized.
Sourcepub fn copy_from_slice(&mut self, src: &[T])where
T: Copy,
pub fn copy_from_slice(&mut self, src: &[T])where
T: Copy,
Copy data from a slice into the allocation.
§Panics
Panics if the slice length doesn’t match the allocation length.
Sourcepub fn into_raw_parts(self) -> (*mut T, Layout, usize)
pub fn into_raw_parts(self) -> (*mut T, Layout, usize)
Take ownership and return raw parts.
After calling this, the caller is responsible for deallocation.
Use TrackedAllocation::from_raw_parts to reconstruct.