Skip to content

Commit ec2423f

Browse files
authored
fix(alloc_ref): Use new nightly Allocator trait (rust-osdev#42)
Due to nightly changes, `AllocRef` was renamed `Allocator` and the methods don't take a `&mut self` anymore. Therefore `Heap` cannot implement `Allocator` anymore. Implement it for `LockedHeap` instead, which requires the `use_spin` feature.
1 parent 905058a commit ec2423f

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

src/lib.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern crate alloc;
1616

1717
use alloc::alloc::Layout;
1818
#[cfg(feature = "alloc_ref")]
19-
use alloc::alloc::{AllocErr, AllocRef};
19+
use alloc::alloc::{AllocError, Allocator};
2020
#[cfg(feature = "use_spin")]
2121
use core::alloc::GlobalAlloc;
2222
use core::mem;
@@ -160,20 +160,21 @@ impl Heap {
160160
}
161161

162162
#[cfg(feature = "alloc_ref")]
163-
unsafe impl AllocRef for Heap {
164-
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
163+
#[cfg(feature = "use_spin")]
164+
unsafe impl Allocator for LockedHeap {
165+
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
165166
if layout.size() == 0 {
166167
return Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0));
167168
}
168-
match self.allocate_first_fit(layout) {
169+
match self.0.lock().allocate_first_fit(layout) {
169170
Ok(ptr) => Ok(NonNull::slice_from_raw_parts(ptr, layout.size())),
170-
Err(()) => Err(AllocErr),
171+
Err(()) => Err(AllocError),
171172
}
172173
}
173174

174-
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
175+
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
175176
if layout.size() != 0 {
176-
self.deallocate(ptr, layout);
177+
self.0.lock().deallocate(ptr, layout);
177178
}
178179
}
179180
}

0 commit comments

Comments
 (0)