aboutsummaryrefslogtreecommitdiff
path: root/ctr-std/src/sys
diff options
context:
space:
mode:
authorpanicbit <[email protected]>2017-07-09 21:32:29 +0200
committerpanicbit <[email protected]>2017-07-10 00:01:48 +0200
commit78ca2a83a7d1c576453ea8eceb897f19a2d401ee (patch)
treefa0f040ffb9cb39c8b26faff930962878edd1405 /ctr-std/src/sys
parentctr-std: Use liballoc from rust-src (diff)
downloadctru-rs-78ca2a83a7d1c576453ea8eceb897f19a2d401ee.tar.xz
ctru-rs-78ca2a83a7d1c576453ea8eceb897f19a2d401ee.zip
ctr-std: Add heap allocator shim
Diffstat (limited to 'ctr-std/src/sys')
-rw-r--r--ctr-std/src/sys/unix/alloc.rs100
-rw-r--r--ctr-std/src/sys/unix/mod.rs1
2 files changed, 101 insertions, 0 deletions
diff --git a/ctr-std/src/sys/unix/alloc.rs b/ctr-std/src/sys/unix/alloc.rs
new file mode 100644
index 0000000..d7eb2f2
--- /dev/null
+++ b/ctr-std/src/sys/unix/alloc.rs
@@ -0,0 +1,100 @@
+// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+use alloc::allocator::{Alloc,Layout,AllocErr,Excess,CannotReallocInPlace};
+use alloc::heap;
+
+/// Heap allocator that delegates to the default liballoc heap allocator.
+/// Its purpose is to override methods while still using the standard alloc api.
+#[derive(Copy, Clone, Default, Debug)]
+pub struct Heap;
+
+unsafe impl Alloc for Heap {
+ #[inline]
+ unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
+ heap::Heap.alloc(layout)
+ }
+
+ // A nicer handler for out-of-memory situations than the default one. This
+ // one prints a message to stderr before aborting. It is critical that this
+ // code does not allocate any memory since we are in an OOM situation. Any
+ // errors are ignored while printing since there's nothing we can do about
+ // them and we are about to exit anyways.
+ #[inline]
+ fn oom(&mut self, err: AllocErr) -> ! {
+ use intrinsics;
+ use libc::{self, STDERR_FILENO, c_void};
+
+ let msg = err.description();
+ unsafe {
+ libc::write(STDERR_FILENO, msg.as_ptr() as *const c_void, msg.len());
+ libc::write(STDERR_FILENO, "\n".as_ptr() as *const c_void, 1);
+ intrinsics::abort();
+ }
+ }
+
+ #[inline]
+ unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
+ heap::Heap.dealloc(ptr, layout)
+ }
+
+ #[inline]
+ fn usable_size(&self, layout: &Layout) -> (usize, usize) {
+ heap::Heap.usable_size(layout)
+ }
+
+ #[inline]
+ unsafe fn realloc(&mut self,
+ ptr: *mut u8,
+ layout: Layout,
+ new_layout: Layout)
+ -> Result<*mut u8, AllocErr>
+ {
+ heap::Heap.realloc(ptr, layout, new_layout)
+ }
+
+ #[inline]
+ unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
+ heap::Heap.alloc_zeroed(layout)
+ }
+
+ #[inline]
+ unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
+ heap::Heap.alloc_excess(layout)
+ }
+
+ #[inline]
+ unsafe fn realloc_excess(&mut self,
+ ptr: *mut u8,
+ layout: Layout,
+ new_layout: Layout) -> Result<Excess, AllocErr>
+ {
+ heap::Heap.realloc_excess(ptr, layout, new_layout)
+ }
+
+ #[inline]
+ unsafe fn grow_in_place(&mut self,
+ ptr: *mut u8,
+ layout: Layout,
+ new_layout: Layout)
+ -> Result<(), CannotReallocInPlace>
+ {
+ heap::Heap.grow_in_place(ptr, layout, new_layout)
+ }
+
+ #[inline]
+ unsafe fn shrink_in_place(&mut self,
+ ptr: *mut u8,
+ layout: Layout,
+ new_layout: Layout) -> Result<(), CannotReallocInPlace>
+ {
+ heap::Heap.shrink_in_place(ptr, layout, new_layout)
+ }
+}
diff --git a/ctr-std/src/sys/unix/mod.rs b/ctr-std/src/sys/unix/mod.rs
index 4ac7a22..df2be06 100644
--- a/ctr-std/src/sys/unix/mod.rs
+++ b/ctr-std/src/sys/unix/mod.rs
@@ -29,6 +29,7 @@ pub mod thread;
pub mod rand;
pub mod thread_local;
pub mod time;
+pub mod alloc;
#[cfg(not(test))]
pub fn init() {