Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AllocRef implementation for latest API changes #33

Merged
merged 1 commit into from
Aug 13, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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