-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuddy.rs
58 lines (47 loc) · 1.49 KB
/
buddy.rs
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
//! Buddy memory allocation.
//!
//! TODO: more efficient
use buddy_system_allocator::Heap;
use core::alloc::Layout;
use core::ptr::NonNull;
use crate::{AllocError, AllocResult, BaseAllocator, ByteAllocator};
/// A byte-granularity memory allocator based on the [buddy_system_allocator].
///
/// [buddy_system_allocator]: https://docs.rs/buddy_system_allocator/latest/buddy_system_allocator/
pub struct BuddyByteAllocator {
inner: Heap<32>,
}
impl BuddyByteAllocator {
/// Creates a new empty `BuddyByteAllocator`.
pub const fn new() -> Self {
Self {
inner: Heap::<32>::new(),
}
}
}
impl BaseAllocator for BuddyByteAllocator {
fn init(&mut self, start: usize, size: usize) {
unsafe { self.inner.init(start, size) };
}
fn add_memory(&mut self, start: usize, size: usize) -> AllocResult {
unsafe { self.inner.add_to_heap(start, start + size) };
Ok(())
}
}
impl ByteAllocator for BuddyByteAllocator {
fn alloc(&mut self, layout: Layout) -> AllocResult<NonNull<u8>> {
self.inner.alloc(layout).map_err(|_| AllocError::NoMemory)
}
fn dealloc(&mut self, pos: NonNull<u8>, layout: Layout) {
self.inner.dealloc(pos, layout)
}
fn total_bytes(&self) -> usize {
self.inner.stats_total_bytes()
}
fn used_bytes(&self) -> usize {
self.inner.stats_alloc_actual()
}
fn available_bytes(&self) -> usize {
self.inner.stats_total_bytes() - self.inner.stats_alloc_actual()
}
}