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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use super::{Allocation, DeviceAllocator};

use anyhow::Result;
use ash::vk;
use std::{cell::RefCell, rc::Rc};

/// A device allocator implementation which represents a shared reference to an
/// underlying allocator implementation.
///
/// This is useful because multiple other allocators often compose over the
/// passthrough. When this occurs, they often need to use the *same* instance
/// of the passthrough allocator.
pub struct SharedRefAllocator<Alloc: DeviceAllocator> {
    allocator: Rc<RefCell<Alloc>>,
}

impl<Alloc: DeviceAllocator> SharedRefAllocator<Alloc> {
    pub fn new(allocator: Alloc) -> Self {
        Self {
            allocator: Rc::new(RefCell::new(allocator)),
        }
    }
}

impl<Alloc: DeviceAllocator> Clone for SharedRefAllocator<Alloc> {
    fn clone(&self) -> Self {
        Self {
            allocator: self.allocator.clone(),
        }
    }
}

impl<Alloc: DeviceAllocator> DeviceAllocator for SharedRefAllocator<Alloc> {
    unsafe fn allocate(
        &mut self,
        allocate_info: vk::MemoryAllocateInfo,
    ) -> Result<Allocation> {
        self.allocator.borrow_mut().allocate(allocate_info)
    }

    unsafe fn free(
        &mut self,
        allocation: &super::Allocation,
    ) -> anyhow::Result<()> {
        self.allocator.borrow_mut().free(allocation)
    }
}

#[cfg(test)]
mod test {
    use crate::graphics::vulkan::device_allocator::{
        stub_allocator::StubAllocator, SharedRefAllocator,
    };

    #[test]
    pub fn can_clone() {
        let allocator = SharedRefAllocator::new(StubAllocator {});
        let _alloc2 = allocator.clone();
    }

    #[should_panic]
    #[test]
    pub fn the_stub_allocator_cannot_clone() {
        let stub = StubAllocator {};
        let _cloned = stub.clone();
    }

    impl Clone for StubAllocator {
        fn clone(&self) -> Self {
            panic!("The Stub Allocator does not support cloning")
        }
    }
}