Skip to main content

BufPoolAllocation

Struct BufPoolAllocation 

Source
pub struct BufPoolAllocation { /* private fields */ }
Expand description

A RAII safe allocation object containing allocated buffers

§Lifetime

The object can/may outlive the scope that created it, while also being able to transfer across threads. As, internally the BufPool tracks all the active allocations and delays the drop until every allocation and all there references are dropped from memory.

§Example

use frozen_core::{
    bufpool::{BufPool, BufPoolCfg, BufferPointer},
    utils::BufferSize,
};

const BUF_SIZE: BufferSize = BufferSize::S16;

let pool = BufPool::new(BufPoolCfg {
    buffer_size: BUF_SIZE,
    max_memory: BUF_SIZE as usize * 0x10,
});

let alloc = pool.allocate(0x0A);

assert_eq!(alloc.length(), 0x0A);
assert!(!alloc.first().is_null());
assert_eq!(alloc.allocated_bytes(), BUF_SIZE as usize * 0x0A);

let ptrs: Vec<BufferPointer> = alloc.iter().collect();
assert_eq!(ptrs.len(), 0x0A);

Implementations§

Source§

impl BufPoolAllocation

Source

pub const fn first(&self) -> BufferPointer

Returns a BufferPointer to the first buffer from the allocated list of buffers

NOTE: The returned BufferPointer can also be used as a base_pointer to operate on the entire allocated memory slice.

§Example
use frozen_core::{
    bufpool::{BufPool, BufPoolCfg},
    utils::BufferSize,
};

const BUF_SIZE: BufferSize = BufferSize::S32;

let pool = BufPool::new(BufPoolCfg {
    buffer_size: BUF_SIZE,
    max_memory: BUF_SIZE as usize * 0x0A,
});

let alloc = pool.allocate(0x0A);
assert!(!alloc.first().is_null());
Source

pub const fn length(&self) -> usize

Returns the total number of allocated buffers

IMPORTANT: The returned value is always equal to the required value using while calling BufPool::allocate.

§Example
use frozen_core::{
    bufpool::{BufPool, BufPoolCfg},
    utils::BufferSize,
};

const BUF_SIZE: BufferSize = BufferSize::S64;

let pool = BufPool::new(BufPoolCfg {
    buffer_size: BUF_SIZE,
    max_memory: BUF_SIZE as usize * 0x0A,
});

let alloc = pool.allocate(0x0A);
assert_eq!(alloc.length(), 0x0A);
Source

pub const fn allocated_bytes(&self) -> usize

Returns the total number of bytes of memory allocated

§Example
use frozen_core::{
    bufpool::{BufPool, BufPoolCfg},
    utils::BufferSize,
};

const BUF_SIZE: BufferSize = BufferSize::S16;

let pool = BufPool::new(BufPoolCfg {
    buffer_size: BUF_SIZE,
    max_memory: BUF_SIZE as usize * 0x0A,
});

let alloc = pool.allocate(0x0A);
assert_eq!(alloc.allocated_bytes(), BUF_SIZE as usize * 0x0A);
Source

pub fn iter(&self) -> BufPoolAllocationIter

A custom Iterator implementation to enable iteration over the list of allocated buffers from BufPoolAllocation

NOTE: Each yielded pointer refers to a unique individual buffer each of size BufPoolCfg::buffer_size.

§Example
use frozen_core::{
    bufpool::{BufPool, BufPoolCfg, BufferPointer},
    utils::BufferSize,
};

const BUF_SIZE: BufferSize = BufferSize::S16;

let pool = BufPool::new(BufPoolCfg {
    buffer_size: BUF_SIZE,
    max_memory: BUF_SIZE as usize * 0x14,
});

let alloc = pool.allocate(0x0A);
let ptrs: Vec<BufferPointer> = alloc.iter().collect();

assert_eq!(ptrs.len(), 0x0A);

Trait Implementations§

Source§

impl Debug for BufPoolAllocation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for BufPoolAllocation

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for BufPoolAllocation

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.