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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use crate::{os, page, util, Error, Protection, Result};
#[allow(clippy::len_without_is_empty)]
pub struct Allocation {
base: *const (),
size: usize,
}
impl Allocation {
#[inline(always)]
pub fn as_ptr<T>(&self) -> *const T {
self.base.cast()
}
#[inline(always)]
pub fn as_mut_ptr<T>(&mut self) -> *mut T {
self.base as *mut T
}
#[inline(always)]
pub fn as_ptr_range<T>(&self) -> std::ops::Range<*const T> {
let range = self.as_range();
(range.start as *const T)..(range.end as *const T)
}
#[inline(always)]
pub fn as_mut_ptr_range<T>(&mut self) -> std::ops::Range<*mut T> {
let range = self.as_range();
(range.start as *mut T)..(range.end as *mut T)
}
#[inline(always)]
pub fn as_range(&self) -> std::ops::Range<usize> {
(self.base as usize)..(self.base as usize).saturating_add(self.size)
}
#[inline(always)]
pub fn len(&self) -> usize {
self.size
}
}
impl Drop for Allocation {
#[inline]
fn drop(&mut self) {
let result = unsafe { os::free(self.base, self.size) };
debug_assert!(result.is_ok(), "freeing region: {:?}", result);
}
}
#[inline]
pub fn alloc(size: usize, protection: Protection) -> Result<Allocation> {
if size == 0 {
return Err(Error::InvalidParameter("size"));
}
let size = page::ceil(size as *const ()) as usize;
unsafe {
let base = os::alloc(std::ptr::null::<()>(), size, protection)?;
Ok(Allocation { base, size })
}
}
#[inline]
pub fn alloc_at<T>(address: *const T, size: usize, protection: Protection) -> Result<Allocation> {
let (address, size) = util::round_to_page_boundaries(address, size)?;
unsafe {
let base = os::alloc(address.cast(), size, protection)?;
Ok(Allocation { base, size })
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn alloc_size_is_aligned_to_page_size() -> Result<()> {
let memory = alloc(1, Protection::NONE)?;
assert_eq!(memory.len(), page::size());
Ok(())
}
#[test]
fn alloc_rejects_empty_allocation() {
assert!(matches!(
alloc(0, Protection::NONE),
Err(Error::InvalidParameter(_))
));
}
#[test]
fn alloc_obtains_correct_properties() -> Result<()> {
let memory = alloc(1, Protection::READ_WRITE)?;
let region = crate::query(memory.as_ptr::<()>())?;
assert_eq!(region.protection(), Protection::READ_WRITE);
assert!(region.len() >= memory.len());
assert!(!region.is_guarded());
assert!(!region.is_shared());
assert!(region.is_committed());
Ok(())
}
#[test]
fn alloc_frees_memory_when_dropped() -> Result<()> {
let base = alloc(1, Protection::READ_WRITE)?.as_ptr::<()>();
let query = crate::query(base);
assert!(matches!(query, Err(Error::UnmappedRegion)));
Ok(())
}
#[test]
fn alloc_can_allocate_unused_region() -> Result<()> {
let base = alloc(1, Protection::NONE)?.as_ptr::<()>();
let memory = alloc_at(base, 1, Protection::READ_WRITE)?;
assert_eq!(memory.as_ptr(), base);
Ok(())
}
}