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
use core::alloc::Layout;
use core::ptr::{NonNull, slice_from_raw_parts_mut};
use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use bern_units::memory_size::{Byte, ExtByte};
use crate::alloc::allocator::{Allocator, AllocError};
use crate::log;
pub struct Bump {
start: NonNull<u8>,
end: NonNull<u8>,
current: AtomicPtr<u8>,
wastage: AtomicUsize,
}
impl Bump {
pub const unsafe fn new(start: NonNull<u8>, end: NonNull<u8>) -> Self {
Bump {
start,
end,
current: AtomicPtr::new(start.as_ptr()),
wastage: AtomicUsize::new(0),
}
}
}
impl Allocator for Bump {
fn alloc(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
loop {
let old = self.current.load(Ordering::Acquire);
let padding = old.align_offset(layout.align());
log::trace!(
"Try to allocate {}B at 0x{:x}",
layout.size(),
old as usize + padding
);
if (self.capacity() - self.usage()) < ((layout.size() + padding) as u32).B() {
return Err(AllocError::OutOfMemory);
}
unsafe {
match self.current.compare_exchange(
old,
old.add(layout.size() + padding),
Ordering::SeqCst,
Ordering::Relaxed
) {
Ok(_) => {
let memory = slice_from_raw_parts_mut(old.add(padding), layout.size());
self.wastage.fetch_add(padding, Ordering::Relaxed);
return Ok(NonNull::new_unchecked(memory));
},
Err(_) => continue,
}
}
}
}
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout) {
log::warn!(
"BumpAllocator cannot deallocate memory (0x{:x}, {}B). Ignoring call from .",
ptr.as_ptr() as usize,
layout.size()
);
self.wastage.fetch_add(layout.size(), Ordering::Relaxed);
}
fn capacity(&self) -> Byte {
Byte((self.end.as_ptr() as usize - self.start.as_ptr() as usize) as u32)
}
fn usage(&self) -> Byte {
Byte((self.current.load(Ordering::Relaxed) as usize -
self.start.as_ptr() as usize) as u32)
}
}
unsafe impl Sync for Bump { }
#[cfg(all(test, not(target_os = "none")))]
mod tests {
use core::mem::{align_of, size_of};
use super::*;
struct MyStruct {
a: u32,
b: u8,
}
#[test]
fn alloc_and_dealloc() {
static mut BUFFER: [u32; 128/4] = [0; 128/4];
let bump = unsafe { Bump::new(
NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
)};
assert_eq!(bump.capacity().0, 128);
let layout = Layout::from_size_align(size_of::<MyStruct>(), align_of::<MyStruct>()).unwrap();
let raw = bump.alloc(layout).unwrap();
let memory = raw.cast::<MyStruct>();
let s = unsafe { &mut *memory.as_ptr() };
s.a = 42;
s.b = 10;
let usage = size_of::<MyStruct>() as u32;
assert_eq!(bump.usage().0, (usage + bump.wastage.load(Ordering::Relaxed) as u32));
let wastage = bump.wastage.load(Ordering::Relaxed);
unsafe {
bump.dealloc(raw.cast::<u8>(), layout);
}
assert_eq!(bump.usage().0, usage);
assert_eq!(bump.wastage.load(Ordering::Relaxed), wastage + size_of::<MyStruct>());
}
#[test]
fn overflow() {
static mut BUFFER: [u8; 128] = [0; 128];
let bump = unsafe { Bump::new(
NonNull::new_unchecked(BUFFER.as_ptr() as *mut _),
NonNull::new_unchecked(BUFFER.as_ptr().add(BUFFER.len()) as *mut _)
)};
assert_eq!(bump.capacity().0, 128);
let layout = Layout::from_size_align(16, 1).unwrap();
for _ in 0..8 {
let _dump = bump.alloc(layout).unwrap();
}
assert_eq!(bump.usage(), bump.capacity());
let err = bump.alloc(layout).err().unwrap();
assert_eq!(err, AllocError::OutOfMemory);
}
}