Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
redox-os
uefi_alloc
Commits
bec53e05
Commit
bec53e05
authored
Jul 12, 2017
by
Jeremy Soller
Browse files
Update to new allocator API
parent
5c299c2f
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/lib.rs
View file @
bec53e05
#![feature(allocator)]
#![feature(alloc)]
#![feature(allocator_api)]
#![feature(const_fn)]
#![feature(try_trait)]
#![allocator]
#![no_std]
extern
crate
alloc
;
extern
crate
uefi
;
use
alloc
::
heap
::{
Alloc
,
AllocErr
,
Layout
};
use
core
::
ops
::
Try
;
use
uefi
::
memory
::
MemoryType
;
use
uefi
::
system
::
SystemTable
;
...
...
@@ -27,60 +28,29 @@ fn get_uefi() -> Option<&'static mut SystemTable> {
}
}
#[no_mangle]
pub
extern
fn
__rust_allocate
(
size
:
usize
,
_align
:
usize
)
->
*
mut
u8
{
if
let
Some
(
ref
mut
uefi
)
=
get_uefi
()
{
let
mut
ptr
=
0
;
if
let
Err
(
err
)
=
(
uefi
.BootServices.AllocatePool
)(
MemoryType
::
EfiLoaderData
,
size
,
&
mut
ptr
)
.into_result
()
{
panic!
(
"__rust_allocate: uefi returned {:?}"
,
err
);
pub
struct
Allocator
;
unsafe
impl
<
'a
>
Alloc
for
&
'a
Allocator
{
unsafe
fn
alloc
(
&
mut
self
,
layout
:
Layout
)
->
Result
<*
mut
u8
,
AllocErr
>
{
if
let
Some
(
ref
mut
uefi
)
=
get_uefi
()
{
let
mut
ptr
=
0
;
if
let
Err
(
_
)
=
(
uefi
.BootServices.AllocatePool
)(
MemoryType
::
EfiLoaderData
,
layout
.size
(),
&
mut
ptr
)
.into_result
()
{
Err
(
AllocErr
::
Exhausted
{
request
:
layout
})
}
else
{
Ok
(
ptr
as
*
mut
u8
)
}
}
else
{
panic!
(
"__rust_allocate: uefi not initialized"
);
}
ptr
as
*
mut
u8
}
else
{
panic!
(
"__rust_allocate: uefi not initialized"
);
}
}
#[no_mangle]
pub
extern
fn
__rust_allocate_zeroed
(
size
:
usize
,
align
:
usize
)
->
*
mut
u8
{
use
core
::
ptr
;
let
ptr
=
__rust_allocate
(
size
,
align
);
unsafe
{
ptr
::
write_bytes
(
ptr
,
0
,
size
)
};
ptr
}
#[no_mangle]
pub
extern
fn
__rust_deallocate
(
ptr
:
*
mut
u8
,
_size
:
usize
,
_align
:
usize
)
{
if
let
Some
(
ref
mut
uefi
)
=
get_uefi
()
{
let
_
=
(
uefi
.BootServices.FreePool
)(
ptr
as
usize
);
}
else
{
panic!
(
"__rust_deallocate: uefi not initialized"
);
unsafe
fn
dealloc
(
&
mut
self
,
ptr
:
*
mut
u8
,
_layout
:
Layout
)
{
if
let
Some
(
ref
mut
uefi
)
=
get_uefi
()
{
let
_
=
(
uefi
.BootServices.FreePool
)(
ptr
as
usize
);
}
else
{
panic!
(
"__rust_deallocate: uefi not initialized"
);
}
}
}
#[no_mangle]
pub
extern
fn
__rust_usable_size
(
size
:
usize
,
_align
:
usize
)
->
usize
{
size
}
#[no_mangle]
pub
extern
fn
__rust_reallocate_inplace
(
_ptr
:
*
mut
u8
,
size
:
usize
,
_new_size
:
usize
,
_align
:
usize
)
->
usize
{
size
}
#[no_mangle]
pub
extern
fn
__rust_reallocate
(
ptr
:
*
mut
u8
,
size
:
usize
,
new_size
:
usize
,
align
:
usize
)
->
*
mut
u8
{
use
core
::{
ptr
,
cmp
};
// from: https://github.com/rust-lang/rust/blob/
// c66d2380a810c9a2b3dbb4f93a830b101ee49cc2/
// src/liballoc_system/lib.rs#L98-L101
let
new_ptr
=
__rust_allocate
(
new_size
,
align
);
unsafe
{
ptr
::
copy
(
ptr
,
new_ptr
,
cmp
::
min
(
size
,
new_size
))
};
__rust_deallocate
(
ptr
,
size
,
align
);
new_ptr
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment