Browse Source

ctr-std: Migrate to the new liballoc API

pull/10/head
panicbit 8 years ago
parent
commit
d86fadb6c7
  1. 14
      ctr-std/src/collections/hash/table.rs
  2. 1
      ctr-std/src/lib.rs
  3. 20
      ctr-std/src/sys/unix/mod.rs

14
ctr-std/src/collections/hash/table.rs

@ -10,7 +10,8 @@
#![allow(deprecated)] #![allow(deprecated)]
use alloc::heap::{EMPTY, allocate, deallocate}; use sys::alloc::Heap;
use alloc::allocator::{Alloc,Layout};
use cmp; use cmp;
use hash::{BuildHasher, Hash, Hasher}; use hash::{BuildHasher, Hash, Hasher};
@ -622,7 +623,7 @@ impl<K, V> RawTable<K, V> {
return RawTable { return RawTable {
size: 0, size: 0,
capacity: 0, capacity: 0,
hashes: Unique::new(EMPTY as *mut HashUint), hashes: Unique::empty(),
marker: marker::PhantomData, marker: marker::PhantomData,
}; };
} }
@ -653,10 +654,8 @@ impl<K, V> RawTable<K, V> {
.expect("capacity overflow"), .expect("capacity overflow"),
"capacity overflow"); "capacity overflow");
let buffer = allocate(size, alignment); let layout = Layout::from_size_align(size, alignment).expect("invalid alloc layout");
if buffer.is_null() { let buffer = Heap.alloc(layout).unwrap_or_else(|err| Heap.oom(err));
::alloc::oom()
}
let hashes = buffer.offset(hash_offset as isize) as *mut HashUint; let hashes = buffer.offset(hash_offset as isize) as *mut HashUint;
@ -1063,7 +1062,8 @@ impl<K, V> Drop for RawTable<K, V> {
debug_assert!(!oflo, "should be impossible"); debug_assert!(!oflo, "should be impossible");
unsafe { unsafe {
deallocate(self.hashes.as_ptr() as *mut u8, size, align); let layout = Layout::from_size_align(size, align).expect("invalid alloc layout");
Heap.dealloc(self.hashes.as_ptr() as *mut u8, layout);
// Remember how everything was allocated out of one buffer // Remember how everything was allocated out of one buffer
// during initialization? We only need one call to free here. // during initialization? We only need one call to free here.
} }

1
ctr-std/src/lib.rs

@ -1,4 +1,5 @@
#![feature(alloc)] #![feature(alloc)]
#![feature(allocator_api)]
#![feature(alloc_system)] #![feature(alloc_system)]
#![feature(allow_internal_unstable)] #![feature(allow_internal_unstable)]
#![feature(box_syntax)] #![feature(box_syntax)]

20
ctr-std/src/sys/unix/mod.rs

@ -33,8 +33,6 @@ pub mod alloc;
#[cfg(not(test))] #[cfg(not(test))]
pub fn init() { pub fn init() {
use alloc::oom;
// By default, some platforms will send a *signal* when an EPIPE error // By default, some platforms will send a *signal* when an EPIPE error
// would otherwise be delivered. This runtime doesn't install a SIGPIPE // would otherwise be delivered. This runtime doesn't install a SIGPIPE
// handler, causing it to kill the program, which isn't exactly what we // handler, causing it to kill the program, which isn't exactly what we
@ -46,24 +44,6 @@ pub fn init() {
reset_sigpipe(); reset_sigpipe();
} }
oom::set_oom_handler(oom_handler);
// 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.
fn oom_handler() -> ! {
use intrinsics;
let msg = "fatal runtime error: out of memory\n";
unsafe {
libc::write(libc::STDERR_FILENO,
msg.as_ptr() as *const libc::c_void,
msg.len());
intrinsics::abort();
}
}
// I don't think we have signal handling on the 3DS, so let's leave this // I don't think we have signal handling on the 3DS, so let's leave this
// blank for now // blank for now
unsafe fn reset_sigpipe() {} unsafe fn reset_sigpipe() {}

Loading…
Cancel
Save