Allocator

Trait Allocator 

Source
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:

  • allocate returns a valid, properly aligned pointer for the requested size
  • deallocate is only called with pointers previously returned by allocate
  • The allocated memory remains valid until deallocate is 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§

Source

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.

Source

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 to allocate on this allocator.
  • size - The size that was passed to the original allocate call.
§Safety

The caller must ensure:

  • ptr was returned by a previous call to allocate on this allocator
  • size matches the size passed to the original allocate call
  • The memory has not already been deallocated

Implementors§