1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use {
    crate::{
        device_memory::DeviceMemory, Allocation, AllocationRequirements,
        AllocatorError, ComposableAllocator,
    },
    ash::vk,
};

/// A fake implementation of a composable memory allocator which keeps track of
/// all requested memory allocations.
#[derive(Default)]
pub struct FakeAllocator {
    /// An ordered collection of every allocation made with this allocator.
    pub allocations: Vec<AllocationRequirements>,

    /// The number of allocations which have yet to be freed.
    pub active_allocations: u32,

    /// The total number of allocations made with this allocator.
    pub allocation_count: u64,

    offset: u64,
}

impl ComposableAllocator for FakeAllocator {
    unsafe fn allocate(
        &mut self,
        allocation_requirements: AllocationRequirements,
    ) -> Result<Allocation, AllocatorError> {
        self.active_allocations += 1;
        self.allocation_count += 1;
        self.allocations.push(allocation_requirements);

        let allocation = Allocation::new(
            DeviceMemory::new(vk::DeviceMemory::null()),
            allocation_requirements.memory_type_index,
            self.offset,
            allocation_requirements.size_in_bytes,
            allocation_requirements,
        );

        self.offset += allocation_requirements.size_in_bytes;

        Ok(allocation)
    }

    unsafe fn free(&mut self, _allocation: Allocation) {
        self.active_allocations -= 1;
    }
}