pub trait Allocator: Send + Sync {
// Required methods
fn allocate(&self, size: usize) -> Result<*mut u8>;
unsafe fn deallocate(&self, ptr: *mut u8, size: usize);
}Expand description
Trait for memory allocation backends.
Implementations of this trait provide the low-level memory allocation and deallocation operations used by the buffer pool. The default implementation uses the standard library’s global allocator.
§Safety
Implementations must ensure:
allocatereturns a valid, properly aligned pointer for the requested sizedeallocateis only called with pointers previously returned byallocate- The allocated memory remains valid until
deallocateis called
§Example
use ruapc_bufpool::Allocator;
use std::io::Result;
struct MyAllocator;
impl Allocator for MyAllocator {
fn allocate(&self, size: usize) -> Result<*mut u8> {
// Custom allocation logic
}
unsafe fn deallocate(&self, ptr: *mut u8, size: usize) {
// Custom deallocation logic
}
}Required Methods§
Sourcefn allocate(&self, size: usize) -> Result<*mut u8>
fn allocate(&self, size: usize) -> Result<*mut u8>
Allocates memory of the specified size.
§Arguments
size- The number of bytes to allocate. Must be greater than 0.
§Returns
Returns a pointer to the allocated memory on success, or an error if allocation fails.
§Errors
Returns an error if the allocation fails due to memory exhaustion or invalid size.
Sourceunsafe fn deallocate(&self, ptr: *mut u8, size: usize)
unsafe fn deallocate(&self, ptr: *mut u8, size: usize)
Deallocates memory previously allocated by this allocator.
§Arguments
ptr- Pointer to the memory to deallocate. Must have been returned by a previous call toallocateon this allocator.size- The size that was passed to the originalallocatecall.
§Safety
The caller must ensure:
ptrwas returned by a previous call toallocateon this allocatorsizematches the size passed to the originalallocatecall- The memory has not already been deallocated