Skip to content

Commit 4e49060

Browse files
authored
Update AllocRef implementation for latest API changes (#33)
1 parent 0526f9c commit 4e49060

File tree

1 file changed

+8
-15
lines changed

1 file changed

+8
-15
lines changed

src/lib.rs

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#![feature(const_fn)]
2-
#![cfg_attr(feature = "alloc_ref", feature(allocator_api, alloc_layout_extra))]
2+
#![cfg_attr(
3+
feature = "alloc_ref",
4+
feature(allocator_api, alloc_layout_extra, nonnull_slice_from_raw_parts)
5+
)]
36
#![no_std]
47

58
#[cfg(test)]
@@ -13,7 +16,7 @@ extern crate alloc;
1316

1417
use alloc::alloc::Layout;
1518
#[cfg(feature = "alloc_ref")]
16-
use alloc::alloc::{AllocErr, AllocInit, AllocRef, MemoryBlock};
19+
use alloc::alloc::{AllocErr, AllocRef};
1720
#[cfg(feature = "use_spin")]
1821
use core::alloc::GlobalAlloc;
1922
use core::mem;
@@ -158,22 +161,12 @@ impl Heap {
158161

159162
#[cfg(feature = "alloc_ref")]
160163
unsafe impl AllocRef for Heap {
161-
fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
164+
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
162165
if layout.size() == 0 {
163-
return Ok(MemoryBlock {
164-
ptr: layout.dangling(),
165-
size: 0,
166-
});
166+
return Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0));
167167
}
168168
match self.allocate_first_fit(layout) {
169-
Ok(ptr) => {
170-
let block = MemoryBlock {
171-
ptr,
172-
size: layout.size(),
173-
};
174-
unsafe { init.init(block) };
175-
Ok(block)
176-
}
169+
Ok(ptr) => Ok(NonNull::slice_from_raw_parts(ptr, layout.size())),
177170
Err(()) => Err(AllocErr),
178171
}
179172
}

0 commit comments

Comments
 (0)