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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
//! This module provides the raw allocator and its support types.
//!
//! A "raw allocator" is one, that simply gets request for a specific memory
//! size but does not need to worry about alignment.
mod buffer;
mod entry;
use buffer::HEADER_SIZE;
use entry::{Entry, State};
use core::mem::MaybeUninit;
/// An error occurred when calling `free()`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FreeError {
/// There is a double-free detected. An already freed-up-block is freed up
/// again.
DoubleFreeDetected,
/// An invalid pointer was freed up (either a pointer outside of the heap
/// memory or a pointer to a header).
AllocationNotFound,
}
/// A raw memory allocator for contiguous slices of bytes without any alignment.
///
/// This allocator is an intermediate one, which does not need to handle the
/// alignment of a [`Layout`](core::alloc::Layout). This abstracts the parts
/// "allocating of memory" and "getting a pointer with proper alignment".
///
/// Note, that the allocated memory is always aligned to `4`.
pub struct RawAllocator<const N: usize> {
/// The internal buffer abstracting over the raw bytes of the heap.
buffer: buffer::Buffer<N>,
}
impl<const N: usize> RawAllocator<N> {
/// Create a new [`RawAllocator`] with a given heap size.
///
/// # Panics
/// This function panics if the buffer size is less than `8` (the minimum
/// useful allocation heap) or if it is not divisible by 4.
pub const fn new() -> Self {
assert!(N >= 8, "too small heap memory: minimum size is 8");
assert!(N % 4 == 0, "memory size has to be divisible by 4");
let buffer = buffer::Buffer::new();
Self { buffer }
}
/// Allocate a new memory block of size `n`.
///
/// This method is used for general allocation of multiple contiguous bytes.
/// It searches for the smallest possible free entry and mark it as "used".
/// As usual with [`RawAllocator`], this does not take alignment in account.
///
/// If the allocation fails, `None` will be returned.
pub fn alloc(&mut self, n: usize) -> Option<&mut [MaybeUninit<u8>]> {
self.buffer.ensure_initialization();
// round up `n` to next multiple of `size_of::<Entry>()`
let n = (n + HEADER_SIZE - 1) / HEADER_SIZE * HEADER_SIZE;
let (offset, _) = self
.buffer
.entries()
.map(|offset| (offset, self.buffer[offset]))
.filter(|(_offset, entry)| entry.state() == State::Free)
.filter(|(_offset, entry)| entry.size() >= n)
.min_by_key(|(_offset, entry)| entry.size())?;
// if the found block is large enough, split it into a used and a free
self.buffer.mark_as_used(offset, n);
Some(self.buffer.memory_of_mut(offset))
}
/// Free a pointer inside a used memory block.
///
/// This method is used to release a memory block allocated with this raw
/// allocator. If a entry to the given pointer is found, the corresponding
/// memory block is marked as free. If no entry is found, than an error is
/// reported (as allocators are not allowed to unwind).
///
/// # Algorithm
/// Freeing a pointer is done in the following way: all the entries are
/// scanned linearly. The pointer is compared against each block. If the
/// pointer points to the memory of an entry, than that entry is selected.
/// If no such entry is found, than the user tried to free an allocation,
/// that was not allocated with this allocator (or the allocator messed up
/// internally). [`FreeError::AllocationNotFound`] is reported.
///
/// The selected block is tested for its state. If it is marked as "used",
/// than everything is fine. If it is already marked as "free", than
/// [`FreeError::DoubleFreeDetected`] is returned. If the block following
/// the just freed up one is also free, the two blocks are concatenated to a
/// single one (to prevent fragmentation).
pub fn free(&mut self, ptr: *mut u8) -> Result<(), FreeError> {
self.buffer.ensure_initialization();
// find the offset of the entry, which the `ptr` points into
let offset = self
.buffer
.entries()
.find(|offset| {
let size = self.buffer[*offset].size();
let memory = self.buffer.memory_of(*offset);
let ptr = ptr as *const _;
let start = memory.as_ptr();
let end = start.wrapping_add(size);
start <= ptr && ptr < end
})
.ok_or(FreeError::AllocationNotFound)?;
// check, if the entry is occupied. If it is free, a double free (or a
// really wrong pointer) was detected, so report an error in that case
let entry = self.buffer[offset];
if entry.state() == State::Free {
return Err(FreeError::DoubleFreeDetected);
}
// query the following free memory or `0` if the following entry is used
let additional_memory = self
.buffer
.following_free_entry(offset)
.map_or(0, |entry| entry.size() + HEADER_SIZE);
// write the header (entry) to the buffer. If the additional memory is
// non-zero, then the following entry is simply "ignored" by enlarging
// the current one
self.buffer[offset] = Entry::free(entry.size() + additional_memory);
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::{Entry, FreeError, RawAllocator};
/// Test, that the given allocator has exactly the given entries.
macro_rules! assert_allocations {
($allocator:expr, $($entry:expr),*$(,)?) => {{
let mut iter = $allocator
.buffer
.entries()
.map(|offset| $allocator.buffer[offset]);
$(assert_eq!(iter.next(), Some($entry));)*
assert_eq!(iter.next(), None);
}};
}
#[test]
fn successful_single_allocation() {
let mut allocator = RawAllocator::<32>::new();
allocator.alloc(4).unwrap();
assert_allocations!(allocator, Entry::used(4), Entry::free(20));
}
#[test]
fn unsuccessful_single_allocation() {
// the allocation is larger than the buffer itself
let mut allocator = RawAllocator::<32>::new();
assert!(allocator.alloc(36).is_none());
assert_allocations!(allocator, Entry::free(28));
}
#[test]
fn successful_multiple_allocation() {
let mut allocator = RawAllocator::<32>::new();
allocator.alloc(12).unwrap();
allocator.alloc(12).unwrap();
// allocator is now full
assert_allocations!(allocator, Entry::used(12), Entry::used(12));
}
#[test]
fn unsuccessful_multiple_allocation() {
let mut allocator = RawAllocator::<32>::new();
allocator.alloc(12).unwrap();
// the second allocation is larger than the remaining space
assert!(allocator.alloc(13).is_none());
assert_allocations!(allocator, Entry::used(12), Entry::free(12));
}
macro_rules! address {
($memory:expr) => {
$memory.as_mut_ptr().cast::<u8>()
};
}
#[test]
fn unsuccessful_allocation_due_to_fragmentation() {
// this test case shows, that the allocator is susceptible to memory
// fragmentation, which makes larger allocations impossible, if the
// heap is in a bad state.
let mut allocator = RawAllocator::<60>::new();
// build a fragmented heap
let ptr1 = address!(allocator.alloc(8).unwrap());
let _ptr2 = address!(allocator.alloc(8).unwrap());
let ptr3 = address!(allocator.alloc(8).unwrap());
let _ptr4 = address!(allocator.alloc(8).unwrap());
let ptr5 = address!(allocator.alloc(8).unwrap());
allocator.free(ptr1).unwrap();
allocator.free(ptr3).unwrap();
allocator.free(ptr5).unwrap();
assert_allocations!(
allocator,
Entry::free(8),
Entry::used(8),
Entry::free(8),
Entry::used(8),
Entry::free(8)
);
// now, there are 24 free bytes (3x 8 bytes) and the headers, but the
// allocation of 10 bytes must fail, since there is no contiguous memory
// of that size
assert!(allocator.alloc(10).is_none());
}
#[test]
fn simple_free() {
let mut allocator = RawAllocator::<16>::new();
let ptr = address!(allocator.alloc(4).unwrap());
allocator.alloc(4).unwrap();
assert_allocations!(allocator, Entry::used(4), Entry::used(4));
// now, that the heap is properly built up, there are two used entries.
// when the first one is freed up, there is no possibility for merging
// the free memory with the following one (as that one is used)
allocator.free(ptr).unwrap();
assert_allocations!(allocator, Entry::free(4), Entry::used(4));
}
#[test]
fn double_free() {
let mut allocator = RawAllocator::<16>::new();
let ptr = address!(allocator.alloc(4).unwrap());
allocator.alloc(4).unwrap();
// try to free up the pointer twice. The first time has to succeed, but
// the second time has to result in a double-free-error.
allocator.free(ptr).unwrap();
assert_eq!(allocator.free(ptr), Err(FreeError::DoubleFreeDetected));
assert_allocations!(allocator, Entry::free(4), Entry::used(4));
}
#[test]
fn invalid_free() {
use core::ptr;
let mut allocator = RawAllocator::<32>::new();
allocator.alloc(4).unwrap();
// try to free up a pointer, that was not allocated by this allocator.
// This invalid usage has to be detected.
let mut x = 0_u32;
let ptr = ptr::addr_of_mut!(x).cast();
assert_eq!(allocator.free(ptr), Err(FreeError::AllocationNotFound));
}
#[test]
fn free_of_modified_pointer() {
let mut allocator = RawAllocator::<16>::new();
let ptr = address!(allocator.alloc(4).unwrap());
allocator.alloc(4).unwrap();
assert_allocations!(allocator, Entry::used(4), Entry::used(4));
let ptr = ptr.wrapping_add(3);
// now there is a valid pointer, but this pointer was modified, e.g. to
// be aligned properly. As the `free()`-call should support any pointer
// into the memory block, this should succeed.
allocator.free(ptr).unwrap();
assert_allocations!(allocator, Entry::free(4), Entry::used(4));
}
#[test]
fn free_with_concatenation() {
let mut allocator = RawAllocator::<32>::new();
let ptr = address!(allocator.alloc(4).unwrap());
assert_allocations!(allocator, Entry::used(4), Entry::free(20));
// now there is a used block followed by a free block. When the used
// block is freed up as well, this should lead to a single free block.
allocator.free(ptr).unwrap();
assert_allocations!(allocator, Entry::free(28));
}
#[test]
fn free_at_end() {
let mut allocator = RawAllocator::<16>::new();
allocator.alloc(4).unwrap();
let ptr = address!(allocator.alloc(4).unwrap());
assert_allocations!(allocator, Entry::used(4), Entry::used(4));
// now, that the heap is properly built up, there are two used entries.
// when the second one is freed up, there is no possibility for merging
// the free memory with the following one (as there is no following one)
allocator.free(ptr).unwrap();
assert_allocations!(allocator, Entry::used(4), Entry::free(4));
}
#[test]
fn free_impossible_defrag() {
let mut allocator = RawAllocator::<16>::new();
let ptr1 = address!(allocator.alloc(4).unwrap());
let ptr2 = address!(allocator.alloc(4).unwrap());
allocator.free(ptr1).unwrap();
// now we have a free block, followed by a used block which in turn gets
// freed up. Therefore there are two contiguous free blocks, but those
// aren't concatenated, since the old free block is to the left (instead
// of to the right).
allocator.free(ptr2).unwrap();
// therefore there must be two free blocks
assert_allocations!(allocator, Entry::free(4), Entry::free(4));
}
#[test]
fn alloc_impossible_splitting() {
let mut allocator = RawAllocator::<32>::new();
let _ptr1 = address!(allocator.alloc(4).unwrap());
let ptr2 = address!(allocator.alloc(12).unwrap());
let _ptr3 = address!(allocator.alloc(4).unwrap());
allocator.free(ptr2).unwrap();
assert_allocations!(allocator, Entry::used(4), Entry::free(12), Entry::used(4));
// new we've set up the heap such there is a free block of 12 in the
// middle (and no free data at the end). If one acquires a block of size
// 4 everything should work fine and the free block should be split up.;
let ptr4 = address!(allocator.alloc(4).unwrap());
assert_allocations!(
allocator,
Entry::used(4),
Entry::used(4),
Entry::free(4),
Entry::used(4)
);
allocator.free(ptr4).unwrap();
assert_allocations!(allocator, Entry::used(4), Entry::free(12), Entry::used(4));
// now the previous state is restored. If there is an allocation for a
// size of 12, no splitting must be happening, since the block is only
// 12 bytes of size, so splitting would tamper the following block.
let _ptr5 = address!(allocator.alloc(12).unwrap());
assert_allocations!(allocator, Entry::used(4), Entry::used(12), Entry::used(4));
}
#[test]
fn free_error_properties() {
// pointless and rather dumb test case: check, that the derived traits
// work as expected.
use super::FreeError::{AllocationNotFound, DoubleFreeDetected};
assert_eq!(AllocationNotFound, AllocationNotFound);
assert_eq!(DoubleFreeDetected, DoubleFreeDetected);
assert_ne!(AllocationNotFound, DoubleFreeDetected);
assert_eq!(AllocationNotFound.clone(), AllocationNotFound);
assert_eq!(DoubleFreeDetected.clone(), DoubleFreeDetected);
assert_ne!(AllocationNotFound.clone(), DoubleFreeDetected);
assert_eq!(format!("{:?}", AllocationNotFound), "AllocationNotFound");
assert_eq!(format!("{:?}", DoubleFreeDetected), "DoubleFreeDetected");
}
}