1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#![no_std]
#![cfg_attr(not(feature = "stable-rust"), feature(rustc_attrs))]
extern crate loca;
use core::{ptr::NonNull, usize};
pub use loca::*;
extern "Rust" {
#[cfg_attr(not(feature = "stable-rust"), rustc_allocator_nounwind)]
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
#[cfg_attr(not(feature = "stable-rust"), rustc_allocator_nounwind)]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
#[cfg_attr(not(feature = "stable-rust"), rustc_allocator_nounwind)]
fn __rust_realloc(ptr: *mut u8,
old_size: usize,
align: usize,
new_size: usize) -> *mut u8;
#[cfg_attr(not(feature = "stable-rust"), rustc_allocator_nounwind)]
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
#[cfg_attr(not(feature = "stable-rust"), rustc_allocator_nounwind)]
fn __rust_grow_in_place(ptr: *mut u8,
old_size: usize,
old_align: usize,
new_size: usize) -> u8;
#[cfg_attr(not(feature = "stable-rust"), rustc_allocator_nounwind)]
fn __rust_shrink_in_place(ptr: *mut u8,
old_size: usize,
old_align: usize,
new_size: usize) -> u8;
}
#[derive(Copy, Clone, Default, Debug)]
pub struct Heap;
unsafe impl Alloc for Heap {
#[inline]
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(__rust_alloc(layout.size(), layout.align().get()))
.ok_or(AllocErr::Exhausted { request: layout })
}
#[inline]
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
__rust_dealloc(ptr.as_ptr(), layout.size(), layout.align().get())
}
#[inline]
unsafe fn realloc(&mut self, ptr: NonNull<u8>, layout: Layout, new_size: usize)
-> Result<NonNull<u8>, AllocErr> {
NonNull::new(__rust_realloc(ptr.as_ptr(), layout.size(), layout.align().get(), new_size))
.ok_or(AllocErr::Exhausted { request: layout })
}
#[inline]
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
NonNull::new(__rust_alloc_zeroed(layout.size(), layout.align().get()))
.ok_or(AllocErr::Exhausted { request: layout })
}
#[inline]
unsafe fn resize_in_place(&mut self, ptr: NonNull<u8>, layout: Layout, new_size: usize)
-> Result<(), CannotReallocInPlace> {
use ::core::cmp::Ord;
use ::core::cmp::Ordering::*;
if 0 == match Ord::cmp(&new_size, &layout.size()) {
Greater => __rust_grow_in_place(ptr.as_ptr(), layout.size(), layout.align().get(), new_size),
Less => __rust_shrink_in_place(ptr.as_ptr(), layout.size(), layout.align().get(), new_size),
Equal => 1,
} { Err(CannotReallocInPlace) } else { Ok(()) }
}
}
#[cfg(test)]
mod tests {
use ::{Heap, Alloc, Layout};
#[test]
fn allocate_zeroed() {
unsafe {
let layout = Layout::from_size_align(1024, 1).unwrap();
let ptr = Heap.alloc_zeroed(layout.clone()).unwrap();
let end = ptr.offset(layout.size() as isize);
let mut i = ptr;
while i < end {
assert_eq!(*i, 0);
i = i.offset(1);
}
Heap.dealloc(ptr, layout);
}
}
}