pub trait GameAllocator {
// Required methods
fn allocate(layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn deallocate(ptr: NonNull<u8>, layout: Layout);
}Expand description
A trait for global allocators within a game that are able to deallocate any
memory on the game’s main heap. Generally corresponds to the drop logic for
DLUT::DLAutoDeletePtr in games from Bloodborne forward.
The API for this generally matches std::alloc::Allocator where possible.
However, it diverges in certain places that are necessary for compatibility
with the games’ memory management.
§Allocator compatibility
An allocator is compatible with this allocator if memory it allocates can
be passed to deallocate without causing undefined behavior. A given
[GameAllocator] must be compatible with itself, but other allocators may
also be compatible. For example, the GameAllocator may track global
allocation arenas and determine which global allocator to use to deallocate
a given pointer based on where it appears in those arenas.
§Currently allocated memory
Some of the methods require that a memory block is currently allocated by an allocator. This means that:
- the starting address for that memory block was previously returned by
allocateor by another allocator that’s known to be compatible with this one, and - the memory block has not subsequently been deallocated.
A memory block is deallocated by a call to deallocate.
§Memory fitting
Some of the methods require that a layout fit a memory block or vice
versa. This means that the following conditions must hold:
-
the memory block must be currently allocated with alignment of
layout.align(), and -
layout.size()must fall in the rangemin ..= max, where:minis the size of the layout used to allocate the block, andmaxis the actual size returned fromallocate.
Required Methods§
Sourcefn allocate(layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(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 until it’s passed to
deallocate.
Note: Unlike std::alloc::Allocator, this does not take a reference
to &self. This implies that any state involved in allocation must be
global, thread-safe, and must be valid for the duration of execution.
§Errors
Unlike std::alloc::Allocator, this may return an error for reasons
other than the memory being exhausted or layout not meeting the
allocator’s size or alignment constraints.
Clients wishing to abort computation in response to an allocation error
are encouraged to call the handle_alloc_error function, rather than
directly invoking panic! or similar.
Sourceunsafe fn deallocate(ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(ptr: NonNull<u8>, layout: Layout)
Deallocates the memory referenced by ptr.
§Safety
ptrmust denote a block of memory currently allocated via this allocator, andlayoutmust fit that block of memory.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".