Skip to content

Commit

Permalink
Update AllocRef implementation for latest API changes (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalmiel authored Aug 13, 2020
1 parent 0526f9c commit 4e49060
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![feature(const_fn)]
#![cfg_attr(feature = "alloc_ref", feature(allocator_api, alloc_layout_extra))]
#![cfg_attr(
feature = "alloc_ref",
feature(allocator_api, alloc_layout_extra, nonnull_slice_from_raw_parts)
)]
#![no_std]

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

use alloc::alloc::Layout;
#[cfg(feature = "alloc_ref")]
use alloc::alloc::{AllocErr, AllocInit, AllocRef, MemoryBlock};
use alloc::alloc::{AllocErr, AllocRef};
#[cfg(feature = "use_spin")]
use core::alloc::GlobalAlloc;
use core::mem;
Expand Down Expand Up @@ -158,22 +161,12 @@ impl Heap {

#[cfg(feature = "alloc_ref")]
unsafe impl AllocRef for Heap {
fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
fn alloc(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocErr> {
if layout.size() == 0 {
return Ok(MemoryBlock {
ptr: layout.dangling(),
size: 0,
});
return Ok(NonNull::slice_from_raw_parts(layout.dangling(), 0));
}
match self.allocate_first_fit(layout) {
Ok(ptr) => {
let block = MemoryBlock {
ptr,
size: layout.size(),
};
unsafe { init.init(block) };
Ok(block)
}
Ok(ptr) => Ok(NonNull::slice_from_raw_parts(ptr, layout.size())),
Err(()) => Err(AllocErr),
}
}
Expand Down

0 comments on commit 4e49060

Please sign in to comment.