rendy_memory/allocator/
mod.rs

1//! This module provides `Allocator` trait and few allocators that implements the trait.
2
3mod dedicated;
4mod dynamic;
5mod linear;
6
7use crate::block::Block;
8
9pub use self::{
10    dedicated::{DedicatedAllocator, DedicatedBlock},
11    dynamic::{DynamicAllocator, DynamicBlock, DynamicConfig},
12    linear::{LinearAllocator, LinearBlock, LinearConfig},
13};
14
15/// Allocator kind.
16#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
17pub enum Kind {
18    /// Memory object per allocation.
19    Dedicated,
20
21    /// General purpose allocator.
22    Dynamic,
23
24    /// Allocates linearly.
25    /// Fast and low overhead.
26    /// Suitable for one-time-use allocations.
27    Linear,
28}
29
30/// Allocator trait implemented for various allocators.
31pub trait Allocator<B: gfx_hal::Backend> {
32    /// Block type returned by allocator.
33    type Block: Block<B>;
34
35    /// Get allocator kind.
36    fn kind() -> Kind;
37
38    /// Allocate block of memory.
39    /// On success returns allocated block and amount of memory consumed from device.
40    fn alloc(
41        &mut self,
42        device: &B::Device,
43        size: u64,
44        align: u64,
45    ) -> Result<(Self::Block, u64), gfx_hal::device::AllocationError>;
46
47    /// Free block of memory.
48    /// Returns amount of memory returned to the device.
49    fn free(&mut self, device: &B::Device, block: Self::Block) -> u64;
50}