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
use crate::allocators::allocator::Allocator;
use crate::memory_address::MemoryAddress;
use crate::memory_sources::mmap::memory_map_source::MemoryMapSource;
use std::alloc::AllocErr;
use std::fmt::Debug;
use std::num::NonZeroUsize;
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
pub struct MemoryMapAllocator(pub MemoryMapSource);
impl Allocator for MemoryMapAllocator {
#[inline(always)]
fn allocate(
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
) -> Result<MemoryAddress, AllocErr> {
const ASSUMED_PAGE_SIZE: usize = 4096;
if unlikely!(non_zero_power_of_two_alignment.get() > ASSUMED_PAGE_SIZE) {
return Err(AllocErr);
}
self.0.mmap_memory(non_zero_size.get())
}
#[inline(always)]
fn deallocate(
&self,
non_zero_size: NonZeroUsize,
_non_zero_power_of_two_alignment: NonZeroUsize,
current_memory: MemoryAddress,
) {
MemoryMapSource::munmap_memory(current_memory, non_zero_size.get())
}
#[inline(always)]
fn growing_reallocate(
&self,
non_zero_new_size: NonZeroUsize,
_non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
self.0.mremap_memory(
current_memory,
non_zero_current_size.get(),
non_zero_new_size.get(),
)
}
#[inline(always)]
fn shrinking_reallocate(
&self,
non_zero_new_size: NonZeroUsize,
_non_zero_power_of_two_alignment: NonZeroUsize,
non_zero_current_size: NonZeroUsize,
current_memory: MemoryAddress,
) -> Result<MemoryAddress, AllocErr> {
self.0.mremap_memory(
current_memory,
non_zero_current_size.get(),
non_zero_new_size.get(),
)
}
}