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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use crate::memory_address::MemoryAddress;
use std::alloc::{Alloc, AllocErr, CannotReallocInPlace, Excess, GlobalAlloc, Layout};
use std::ops::Deref;
use crate::allocators::allocator::Allocator;
use std::num::NonZeroUsize;
#[repr(transparent)]
#[derive(Debug)]
pub struct AllocatorAdaptor<'a, A: 'a + Allocator>(pub(crate) &'a A);
impl<'a, A: 'a + Allocator> Deref for AllocatorAdaptor<'a, A> {
type Target = A;
#[inline(always)]
fn deref(&self) -> &Self::Target {
self.0
}
}
unsafe impl<'a, A: 'a + Allocator> GlobalAlloc for AllocatorAdaptor<'a, A> {
#[inline(always)]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.global_alloc_alloc(layout)
}
#[inline(always)]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
self.global_alloc_alloc_zeroed(layout)
}
#[inline(always)]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.global_alloc_dealloc(ptr, layout)
}
#[inline(always)]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
self.global_alloc_realloc(ptr, layout, new_size)
}
}
unsafe impl<'a, A: 'a + Allocator> Alloc for AllocatorAdaptor<'a, A> {
#[inline(always)]
unsafe fn alloc(&mut self, layout: Layout) -> Result<MemoryAddress, AllocErr> {
self.alloc_alloc(layout)
}
#[inline(always)]
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<MemoryAddress, AllocErr> {
self.alloc_alloc_zeroed(layout)
}
#[inline(always)]
unsafe fn dealloc(&mut self, ptr: MemoryAddress, layout: Layout) {
self.alloc_dealloc(ptr, layout)
}
#[inline(always)]
unsafe fn realloc(
&mut self,
ptr: MemoryAddress,
layout: Layout,
new_size: usize,
) -> Result<MemoryAddress, AllocErr> {
self.alloc_realloc(ptr, layout, new_size)
}
#[inline(always)]
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
self.alloc_alloc_excess(layout)
}
#[inline(always)]
unsafe fn realloc_excess(
&mut self,
ptr: MemoryAddress,
layout: Layout,
new_size: usize,
) -> Result<Excess, AllocErr> {
self.alloc_realloc_excess(ptr, layout, new_size)
}
#[inline(always)]
unsafe fn grow_in_place(
&mut self,
ptr: MemoryAddress,
layout: Layout,
new_size: usize,
) -> Result<(), CannotReallocInPlace> {
self.alloc_grow_in_place(ptr, layout, new_size)
}
#[inline(always)]
unsafe fn shrink_in_place(
&mut self,
ptr: MemoryAddress,
layout: Layout,
new_size: usize,
) -> Result<(), CannotReallocInPlace> {
self.alloc_shrink_in_place(ptr, layout, new_size)
}
}
impl<'a, A: 'a + Allocator> Allocator for AllocatorAdaptor<'a, A> {
#[inline(always)]
fn allocate(
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
) -> Result<MemoryAddress, AllocErr> {
self.0
.allocate(non_zero_size, non_zero_power_of_two_alignment)
}
#[inline(always)]
fn deallocate(
&self,
non_zero_size: NonZeroUsize,
non_zero_power_of_two_alignment: NonZeroUsize,
current_memory: MemoryAddress,
) {
self.0.deallocate(
non_zero_size,
non_zero_power_of_two_alignment,
current_memory,
)
}
#[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.growing_reallocate(
non_zero_new_size,
non_zero_power_of_two_alignment,
non_zero_current_size,
current_memory,
)
}
#[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.shrinking_reallocate(
non_zero_new_size,
non_zero_power_of_two_alignment,
non_zero_current_size,
current_memory,
)
}
}