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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
mod allocation;
mod forced_offset;
mod mem_unit;
mod metrics;
mod page_allocator;
mod passthrough;
mod pool;
mod shared_ref;
mod size_selector;
mod suballocator;
mod type_index;
#[cfg(test)]
mod stub_allocator;
use anyhow::Result;
use ash::vk;
pub use self::{
forced_offset::ForcedOffsetAllocator,
mem_unit::MemUnit,
metrics::{ConsoleMarkdownReport, MetricsAllocator},
page_allocator::PageAllocator,
passthrough::PassthroughAllocator,
pool::PoolAllocator,
shared_ref::SharedRefAllocator,
size_selector::SizeSelector,
suballocator::Suballocator,
type_index::TypeIndexAllocator,
};
#[derive(Clone, Debug)]
pub struct Allocation {
pub memory: vk::DeviceMemory,
pub offset: vk::DeviceSize,
pub byte_size: vk::DeviceSize,
memory_type_index: u32,
}
pub trait DeviceAllocator {
unsafe fn allocate(
&mut self,
allocate_info: vk::MemoryAllocateInfo,
) -> Result<Allocation>;
unsafe fn free(&mut self, allocation: &Allocation) -> Result<()>;
}
pub fn build_standard_allocator(
ash_instance: ash::Instance,
logical_device: ash::Device,
physical_device: ash::vk::PhysicalDevice,
) -> Box<impl DeviceAllocator> {
let device_allocator = SharedRefAllocator::new(MetricsAllocator::new(
"Device Allocator",
ConsoleMarkdownReport::new(ash_instance.clone(), physical_device),
PassthroughAllocator::create(logical_device),
));
let typed_allocator = PageAllocator::new(
TypeIndexAllocator::new(
&ash_instance,
physical_device,
|_memory_type_index, _memory_type| {
SizeSelector::new(
PoolAllocator::new(
device_allocator.clone(),
MemUnit::MiB(1),
),
MemUnit::KiB(512),
SizeSelector::new(
PoolAllocator::new(
device_allocator.clone(),
MemUnit::MiB(512),
),
MemUnit::MiB(256),
device_allocator.clone(),
),
)
},
),
MemUnit::KiB(1),
);
Box::new(typed_allocator)
}