From 72dc0b4c6fcbef1358b9cf99b577ee0c80d2ccbb Mon Sep 17 00:00:00 2001 From: James Forcier Date: Wed, 21 Jun 2017 11:04:06 -0700 Subject: [PATCH] allocator: add implementation of new Alloc trait The Alloc trait for allocators was added in Rust nightly 2017/06/21 (from rust-lang/rust#42313). With the addition of allowing a global allocator to be specified (pending in rust-lang/rust#42727) this will obviate the need for the alloc_uefi crate. --- src/allocator.rs | 36 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 5 +++++ 2 files changed, 41 insertions(+) create mode 100644 src/allocator.rs diff --git a/src/allocator.rs b/src/allocator.rs new file mode 100644 index 0000000..a8901cd --- /dev/null +++ b/src/allocator.rs @@ -0,0 +1,36 @@ +// Copyright 2017 CoreOS, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use alloc::allocator::*; + +use base::Status; + +/// Rust Allocator utilizing the EFI {allocate, free}_pool functions. +struct EfiAllocator {} + +unsafe impl<'a> Alloc for &'a EfiAllocator { + unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> { + ::get_system_table() + .boot_services() + .allocate_pool(layout.size()) + .map_err(|e| match e { + Status::OutOfResources => AllocErr::Exhausted { request: layout }, + x => AllocErr::Unsupported { details: x.str() }, + }) + } + + unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) { + ::get_system_table().boot_services().free_pool(ptr); + } +} diff --git a/src/lib.rs b/src/lib.rs index 955a6ed..b42a75f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,10 @@ #![allow(dead_code)] +#![feature(alloc)] +#![feature(allocator_api)] #![no_std] +extern crate alloc; + pub mod protocol; mod void; mod base; @@ -13,6 +17,7 @@ mod console; mod task; mod event; pub mod util; +mod allocator; pub use base::{Handle, Handles, Event, MemoryType, Status, Time};