pub unsafe trait Allocator {
// Required methods
unsafe fn allocate(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
// Provided method
fn by_ref(&self) -> &Self
where Self: Sized { ... }
}Expand description
An implementation of Allocator can allocate, grow, shrink, and deallocate arbitrary blocks of
data described via Layout
§Safety
-
Memory blocks returned from an allocator that are [currently allocated] must point to valid memory and retain their validity while they are [currently allocated] and the shorter of:
- the borrow-checker lifetime of the allocator type itself.
- as long as at least one of the instance and all of its clones has not been dropped.
-
copying, cloning, or moving the allocator must not invalidate memory blocks returned from this allocator. A copied or cloned allocator must behave like the same allocator, and
-
any pointer to a memory block which is [currently allocated] may be passed to any other method of the allocator.
Required Methods§
Sourceunsafe fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
unsafe fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Attempts to allocate a block of memory.
On success, returns a NonNull<[u8]> meeting the size and alignment guarantees
of layout.
The returned block may have a larger size than specified by layout.size(), and may or may
not have its contents initialized.
The returned block of memory remains valid as long as it is [currently allocated] and the shorter of:
- the borrow-checker lifetime of the allocator type itself.
- as long as at the allocator and all its clones has not been dropped.
§Errors
Returning Err indicates that either memory is exhausted or layout does not meet
allocator’s size or alignment constraints.
§Safety
The memory is not necessarily available upon return from this function. The caller must ensure that the proper synchronization is performed before using the memory, if necessary.