Skip to content

Commit 72dc0b4

Browse files
committed
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.
1 parent f2f0375 commit 72dc0b4

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/allocator.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2017 CoreOS, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use alloc::allocator::*;
16+
17+
use base::Status;
18+
19+
/// Rust Allocator utilizing the EFI {allocate, free}_pool functions.
20+
struct EfiAllocator {}
21+
22+
unsafe impl<'a> Alloc for &'a EfiAllocator {
23+
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
24+
::get_system_table()
25+
.boot_services()
26+
.allocate_pool(layout.size())
27+
.map_err(|e| match e {
28+
Status::OutOfResources => AllocErr::Exhausted { request: layout },
29+
x => AllocErr::Unsupported { details: x.str() },
30+
})
31+
}
32+
33+
unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) {
34+
::get_system_table().boot_services().free_pool(ptr);
35+
}
36+
}

src/lib.rs

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#![allow(dead_code)]
2+
#![feature(alloc)]
3+
#![feature(allocator_api)]
24
#![no_std]
35

6+
extern crate alloc;
7+
48
pub mod protocol;
59
mod void;
610
mod base;
@@ -13,6 +17,7 @@ mod console;
1317
mod task;
1418
mod event;
1519
pub mod util;
20+
mod allocator;
1621

1722

1823
pub use base::{Handle, Handles, Event, MemoryType, Status, Time};

0 commit comments

Comments
 (0)