[−][src]Trait heaparray::base::BaseArrayPtr
Trait representing an unsafe reference to an array.
Should be the same size as the underlying pointer.
Implementing this Type
Let a
be an instance of A
, which is a concrete implementation of
BaseArrayPtr<E, L>
. The following must hold on a
:
a.dealloc(len)
is safe to call on the result ofA::alloc(len)
a.elem_ptr(idx)
anda.lbl_ptr()
must return properly aligned pointers for the typesE
andL
respectivelya.lbl_ptr()
must return the same value for the lifetime ofa
for alllet a = A::alloc(len)
, or at least untila.dealloc()
is called.a.elem_ptr(idx)
must return the same value for each value ofidx
for the lifetime ofa
for alllet a = A::alloc(len)
, or at least untila.dealloc()
is called.A::alloc(len).elem_ptr(idx)
returns a pointer to allocated memory for allidx < len
- The difference in addresses between
a.elem_ptr(idx + 1)
anda.elem_ptr(idx)
is exactlycore::mem::size_of::<E>()
; i.e. the objects thata.elem_ptr
points to are in an array A::alloc(len).lbl_ptr()
returns a pointer to allocated memorya._init()
is safe to call on the result ofA::alloc(len)
a._drop()
is safe to call on any result ofA::alloc(len)
for which_init()
has been called exactly onceA::from_ptr(A::alloc(len).as_ptr())
is safe; i.e.A::from_ptr
andA::as_ptr
must agree on the raw pointer representation ofA
Use of API by BaseArray
Let A
be a concrete implementation of BaseArrayPtr
. At initialization via
BaseArray::new
, BaseArray::new_lazy
, or BaseArray::alloc
, BaseArray
does the following:
- Call
A::alloc(len)
- Call
a._init()
on the newly created instance - Optionally call constructor methods (depending on which method)
- Label is initialized first by calling
a.lbl_ptr()
and writing to it - Elements are initialized by calling
a.elem_ptr(idx)
and writing to it for eachidx < len
- Label is initialized first by calling
At destruction via BaseArray::drop
or BaseArray::drop_lazy
, BaseArray
does the following:
- Optionally call destructors (depending on which method)
- Label is destructed first, in place
- Elements are destructed in ascending order
- Call
a._drop()
- Call
a.dealloc()
On accessing an element, BaseArray
calls elem_ptr
, and on accessing the
label, BaseArray
calls lbl_ptr
Required methods
unsafe fn alloc(len: usize) -> Self
Allocate the memory necessary for a new instance of len
elements, without
initializing it
unsafe fn dealloc(&mut self, len: usize)
Deallocate the memory for an instance of len
elements, without running
destructors
unsafe fn from_ptr(ptr: *mut u8) -> Self
Creates a new reference of this type without doing any checks.
Safety
This function is not ever guarranteed to be safe.
fn as_ptr(&self) -> *mut u8
Returns the value of the internal raw pointer in this array pointer
Dereferencing this raw pointer isn't safe unless the original array pointer pointed to valid memory.
fn is_null(&self) -> bool
Returns whether or not this pointer is null
fn lbl_ptr(&self) -> *mut L
Returns a raw pointer to the label associated with this array
Dereferencing this raw pointer isn't safe unless the original array pointer pointed to valid memory.
fn elem_ptr(&self, idx: usize) -> *mut E
Returns a raw pointer to the element at idx
Dereferencing this pointer is only safe if there actually is a properly initialized element at that location.
Provided methods
unsafe fn _init(&mut self)
Initializes fields at construction
Note that in BaseArray
this will be run before any other initialization
tasks; this means that the memory this method has access to is almost entirely
uninitialized.
Safety
Initializing memory that is accessible by dereferencing lbl_ptr
or elem_ptr
is safe, but may potentially result in a memory leak. However,
the memory accessed in this function is not initialized, so reading memory
in this function causes undefined behavior.
unsafe fn _drop(&mut self)
Runs destructors right before deallocating the buffer
In BaseArray
this will run after all other destructors; this means that
the memory this method has access to is almost entirely uninitialized.
Safety
Almost all accesses are unsafe. Tread with caution.
unsafe fn cast<T, Q, P>(&self) -> P where
P: BaseArrayPtr<T, Q>,
P: BaseArrayPtr<T, Q>,
Casts this pointer to another value, by transferring the internal pointer to its constructor. Super unsafe.