pub struct LayoutTracker { /* private fields */ }
Available on crate feature alloc only.
Expand description

An adapter for GlobalAlloc instances to implement MallocLikeAllocator.

This struct also supports introspecting the current allocation table with Self::current_allocations():

 use vectorscan::alloc::{LayoutTracker, MallocLikeAllocator};
 use std::{slice, alloc::System};

 let tracker = LayoutTracker::new(System.into());
 let p1 = tracker.allocate(32).unwrap();
 let p2 = tracker.allocate(64).unwrap();

 let allocs = tracker.current_allocations();
 assert_eq!(allocs.len(), 2);
 let (p1_p, p1_layout) = allocs[0];
 let (p2_p, p2_layout) = allocs[1];
 // The pointer value is the same as that returned by the allocate method:
 assert!(p1_p == p1);
 assert!(p2_p == p2);
 // The requested alignment is always 8:
 assert!(p1_layout.align() == 8);
 assert!(p2_layout.align() == 8);
 // The size itself may differ per allocation, though:
 assert!(p1_layout.size() == 32);
 assert!(p2_layout.size() == 64);

 // Note that modifying pointers in use by other threads may cause race conditions
 // and undefined behavior!
 let s1 = unsafe { slice::from_raw_parts_mut(p1_p.as_ptr(), p1_layout.size()) };
 s1[..5].copy_from_slice(b"hello");
 let s2 = unsafe { slice::from_raw_parts_mut(p2_p.as_ptr(), p2_layout.size()) };
 s2[..5].copy_from_slice(&s1[..5]);
 assert_eq!(&s2[..5], b"hello");

 // Free memory when done:
 tracker.deallocate(p1);
 tracker.deallocate(p2);

Implementations§

source§

impl LayoutTracker

source

pub fn new(allocator: Arc<impl GlobalAlloc + 'static>) -> Self

Create a new allocation mapping which records the Layout argument for each allocation that goes to the underlying allocator.

source

pub fn current_allocations(&self) -> Vec<(NonNull<u8>, Layout)>

Get a copy of the current live allocation mapping given a reference to this allocator.

This struct makes use of an RwLock to allow concurrent access. Note that pointers may not be safe to dereference if they are freed after this method is called!

Trait Implementations§

source§

impl MallocLikeAllocator for LayoutTracker

LayoutTracker implements three additional guarantees over the base MallocLikeAllocator:

  • 0-size allocation requests always return None instead of allocating.
  • all allocations are aligned to at least 8 bytes.
  • attempted double frees will panic instead.
source§

fn allocate(&self, size: usize) -> Option<NonNull<u8>>

Allocate a region of memory of at least size bytes. Read more
source§

fn deallocate(&self, ptr: NonNull<u8>)

Free up a previously-allocated memory region at ptr. Read more
source§

impl Send for LayoutTracker

source§

impl Sync for LayoutTracker

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.