auto merge of #11561 : eddyb/rust/moar-inlines, r=pcwalton

This commit is contained in:
bors 2014-01-15 06:26:38 -08:00
commit 7bebdbd968
8 changed files with 36 additions and 11 deletions

View file

@ -13,7 +13,6 @@
#[macro_escape]; #[macro_escape];
use std::fmt; use std::fmt;
use std::libc;
// Indicates whether we should perform expensive sanity checks, including rtassert! // Indicates whether we should perform expensive sanity checks, including rtassert!
// XXX: Once the runtime matures remove the `true` below to turn off rtassert, etc. // XXX: Once the runtime matures remove the `true` below to turn off rtassert, etc.
@ -124,6 +123,7 @@ memory and partly incapable of presentation to others.",
abort(); abort();
fn abort() -> ! { fn abort() -> ! {
unsafe { libc::abort() } use std::unstable::intrinsics;
unsafe { intrinsics::abort() }
} }
} }

View file

@ -68,6 +68,7 @@ pub fn append<T:Clone>(lhs: @[T], rhs: &[T]) -> @[T] {
/// Apply a function to each element of a vector and return the results /// Apply a function to each element of a vector and return the results
#[inline]
pub fn map<T, U>(v: &[T], f: |x: &T| -> U) -> @[U] { pub fn map<T, U>(v: &[T], f: |x: &T| -> U) -> @[U] {
build(Some(v.len()), |push| { build(Some(v.len()), |push| {
for elem in v.iter() { for elem in v.iter() {
@ -82,6 +83,7 @@ pub fn map<T, U>(v: &[T], f: |x: &T| -> U) -> @[U] {
* Creates an immutable vector of size `n_elts` and initializes the elements * Creates an immutable vector of size `n_elts` and initializes the elements
* to the value returned by the function `op`. * to the value returned by the function `op`.
*/ */
#[inline]
pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> @[T] { pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> @[T] {
build(Some(n_elts), |push| { build(Some(n_elts), |push| {
let mut i: uint = 0u; let mut i: uint = 0u;
@ -95,6 +97,7 @@ pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> @[T] {
* Creates an immutable vector of size `n_elts` and initializes the elements * Creates an immutable vector of size `n_elts` and initializes the elements
* to the value `t`. * to the value `t`.
*/ */
#[inline]
pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] { pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] {
build(Some(n_elts), |push| { build(Some(n_elts), |push| {
let mut i: uint = 0u; let mut i: uint = 0u;
@ -109,6 +112,7 @@ pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] {
* Creates and initializes an immutable managed vector by moving all the * Creates and initializes an immutable managed vector by moving all the
* elements from an owned vector. * elements from an owned vector.
*/ */
#[inline]
pub fn to_managed_move<T>(v: ~[T]) -> @[T] { pub fn to_managed_move<T>(v: ~[T]) -> @[T] {
let mut av = @[]; let mut av = @[];
unsafe { unsafe {
@ -124,6 +128,7 @@ pub fn to_managed_move<T>(v: ~[T]) -> @[T] {
* Creates and initializes an immutable managed vector by copying all the * Creates and initializes an immutable managed vector by copying all the
* elements of a slice. * elements of a slice.
*/ */
#[inline]
pub fn to_managed<T:Clone>(v: &[T]) -> @[T] { pub fn to_managed<T:Clone>(v: &[T]) -> @[T] {
from_fn(v.len(), |i| v[i].clone()) from_fn(v.len(), |i| v[i].clone())
} }
@ -135,6 +140,7 @@ impl<T> Clone for @[T] {
} }
impl<A> FromIterator<A> for @[A] { impl<A> FromIterator<A> for @[A] {
#[inline]
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> @[A] { fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> @[A] {
let (lower, _) = iterator.size_hint(); let (lower, _) = iterator.size_hint();
build(Some(lower), |push| { build(Some(lower), |push| {
@ -216,6 +222,7 @@ pub mod raw {
move_val_init(&mut(*p), initval); move_val_init(&mut(*p), initval);
} }
#[inline]
unsafe fn push_slow<T>(v: &mut @[T], initval: T) { unsafe fn push_slow<T>(v: &mut @[T], initval: T) {
reserve_at_least(v, v.len() + 1u); reserve_at_least(v, v.len() + 1u);
push_fast(v, initval); push_fast(v, initval);
@ -232,6 +239,7 @@ pub mod raw {
* * v - A vector * * v - A vector
* * n - The number of elements to reserve space for * * n - The number of elements to reserve space for
*/ */
#[inline]
pub unsafe fn reserve<T>(v: &mut @[T], n: uint) { pub unsafe fn reserve<T>(v: &mut @[T], n: uint) {
// Only make the (slow) call into the runtime if we have to // Only make the (slow) call into the runtime if we have to
if capacity(*v) < n { if capacity(*v) < n {
@ -243,6 +251,7 @@ pub mod raw {
// Implementation detail. Shouldn't be public // Implementation detail. Shouldn't be public
#[allow(missing_doc)] #[allow(missing_doc)]
#[inline]
pub fn reserve_raw(ty: *TyDesc, ptr: *mut *mut Box<Vec<()>>, n: uint) { pub fn reserve_raw(ty: *TyDesc, ptr: *mut *mut Box<Vec<()>>, n: uint) {
// check for `uint` overflow // check for `uint` overflow
unsafe { unsafe {
@ -257,6 +266,7 @@ pub mod raw {
} }
} }
#[inline]
fn local_realloc(ptr: *(), size: uint) -> *() { fn local_realloc(ptr: *(), size: uint) -> *() {
use rt::local::Local; use rt::local::Local;
use rt::task::Task; use rt::task::Task;
@ -281,6 +291,7 @@ pub mod raw {
* * v - A vector * * v - A vector
* * n - The number of elements to reserve space for * * n - The number of elements to reserve space for
*/ */
#[inline]
pub unsafe fn reserve_at_least<T>(v: &mut @[T], n: uint) { pub unsafe fn reserve_at_least<T>(v: &mut @[T], n: uint) {
reserve(v, uint::next_power_of_two(n)); reserve(v, uint::next_power_of_two(n));
} }

View file

@ -159,7 +159,7 @@ pub use libc::funcs::c95::stdio::{fread, freopen, fseek, fsetpos, ftell};
pub use libc::funcs::c95::stdio::{fwrite, perror, puts, remove, rewind}; pub use libc::funcs::c95::stdio::{fwrite, perror, puts, remove, rewind};
pub use libc::funcs::c95::stdio::{setbuf, setvbuf, tmpfile, ungetc}; pub use libc::funcs::c95::stdio::{setbuf, setvbuf, tmpfile, ungetc};
pub use libc::funcs::c95::stdlib::{abort, abs, atof, atoi, calloc, exit}; pub use libc::funcs::c95::stdlib::{abs, atof, atoi, calloc, exit};
pub use libc::funcs::c95::stdlib::{free, getenv, labs, malloc, rand}; pub use libc::funcs::c95::stdlib::{free, getenv, labs, malloc, rand};
pub use libc::funcs::c95::stdlib::{realloc, srand, strtod, strtol}; pub use libc::funcs::c95::stdlib::{realloc, srand, strtod, strtol};
pub use libc::funcs::c95::stdlib::{strtoul, system}; pub use libc::funcs::c95::stdlib::{strtoul, system};
@ -3226,7 +3226,6 @@ pub mod funcs {
pub fn malloc(size: size_t) -> *c_void; pub fn malloc(size: size_t) -> *c_void;
pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void; pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
pub fn free(p: *c_void); pub fn free(p: *c_void);
pub fn abort() -> !;
pub fn exit(status: c_int) -> !; pub fn exit(status: c_int) -> !;
// Omitted: atexit. // Omitted: atexit.
pub fn system(s: *c_char) -> c_int; pub fn system(s: *c_char) -> c_int;

View file

@ -280,7 +280,8 @@ fn get_with<T:'static,
} }
fn abort() -> ! { fn abort() -> ! {
unsafe { libc::abort() } use std::unstable::intrinsics;
unsafe { intrinsics::abort() }
} }
/// Inserts a value into task local storage. If the key is already present in /// Inserts a value into task local storage. If the key is already present in

View file

@ -10,14 +10,10 @@
use libc::{c_void, c_char, size_t, uintptr_t, free, malloc, realloc}; use libc::{c_void, c_char, size_t, uintptr_t, free, malloc, realloc};
use ptr::RawPtr; use ptr::RawPtr;
use unstable::intrinsics::TyDesc; use unstable::intrinsics::{TyDesc, abort};
use unstable::raw; use unstable::raw;
use mem::size_of; use mem::size_of;
extern {
fn abort();
}
#[inline] #[inline]
pub fn get_box_size(body_size: uint, body_align: uint) -> uint { pub fn get_box_size(body_size: uint, body_align: uint) -> uint {
let header_size = size_of::<raw::Box<()>>(); let header_size = size_of::<raw::Box<()>>();
@ -34,6 +30,7 @@ fn align_to(size: uint, align: uint) -> uint {
} }
/// A wrapper around libc::malloc, aborting on out-of-memory /// A wrapper around libc::malloc, aborting on out-of-memory
#[inline]
pub unsafe fn malloc_raw(size: uint) -> *c_void { pub unsafe fn malloc_raw(size: uint) -> *c_void {
let p = malloc(size as size_t); let p = malloc(size as size_t);
if p.is_null() { if p.is_null() {
@ -44,6 +41,7 @@ pub unsafe fn malloc_raw(size: uint) -> *c_void {
} }
/// A wrapper around libc::realloc, aborting on out-of-memory /// A wrapper around libc::realloc, aborting on out-of-memory
#[inline]
pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void { pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void {
let p = realloc(ptr, size as size_t); let p = realloc(ptr, size as size_t);
if p.is_null() { if p.is_null() {
@ -94,6 +92,7 @@ pub unsafe fn exchange_free_(ptr: *c_char) {
exchange_free(ptr) exchange_free(ptr)
} }
#[inline]
pub unsafe fn exchange_free(ptr: *c_char) { pub unsafe fn exchange_free(ptr: *c_char) {
free(ptr as *c_void); free(ptr as *c_void);
} }

View file

@ -48,6 +48,7 @@ pub struct LocalHeap {
} }
impl LocalHeap { impl LocalHeap {
#[inline]
pub fn new() -> LocalHeap { pub fn new() -> LocalHeap {
let region = MemoryRegion { let region = MemoryRegion {
allocations: ~[], allocations: ~[],
@ -60,6 +61,7 @@ impl LocalHeap {
} }
} }
#[inline]
pub fn alloc(&mut self, td: *TyDesc, size: uint) -> *mut Box { pub fn alloc(&mut self, td: *TyDesc, size: uint) -> *mut Box {
let total_size = global_heap::get_box_size(size, unsafe { (*td).align }); let total_size = global_heap::get_box_size(size, unsafe { (*td).align });
let alloc = self.memory_region.malloc(total_size); let alloc = self.memory_region.malloc(total_size);
@ -80,6 +82,7 @@ impl LocalHeap {
return alloc; return alloc;
} }
#[inline]
pub fn realloc(&mut self, ptr: *mut Box, size: uint) -> *mut Box { pub fn realloc(&mut self, ptr: *mut Box, size: uint) -> *mut Box {
// Make sure that we can't use `mybox` outside of this scope // Make sure that we can't use `mybox` outside of this scope
let total_size = size + mem::size_of::<Box>(); let total_size = size + mem::size_of::<Box>();
@ -100,6 +103,7 @@ impl LocalHeap {
return new_box; return new_box;
} }
#[inline]
pub fn free(&mut self, alloc: *mut Box) { pub fn free(&mut self, alloc: *mut Box) {
{ {
// Make sure that we can't use `mybox` outside of this scope // Make sure that we can't use `mybox` outside of this scope
@ -196,6 +200,7 @@ impl AllocHeader {
} }
impl MemoryRegion { impl MemoryRegion {
#[inline]
fn malloc(&mut self, size: uint) -> *mut Box { fn malloc(&mut self, size: uint) -> *mut Box {
let total_size = size + AllocHeader::size(); let total_size = size + AllocHeader::size();
let alloc: *AllocHeader = unsafe { let alloc: *AllocHeader = unsafe {
@ -210,6 +215,7 @@ impl MemoryRegion {
return alloc.as_box(); return alloc.as_box();
} }
#[inline]
fn realloc(&mut self, alloc: *mut Box, size: uint) -> *mut Box { fn realloc(&mut self, alloc: *mut Box, size: uint) -> *mut Box {
rtassert!(!alloc.is_null()); rtassert!(!alloc.is_null());
let orig_alloc = AllocHeader::from(alloc); let orig_alloc = AllocHeader::from(alloc);
@ -228,6 +234,7 @@ impl MemoryRegion {
return alloc.as_box(); return alloc.as_box();
} }
#[inline]
fn free(&mut self, alloc: *mut Box) { fn free(&mut self, alloc: *mut Box) {
rtassert!(!alloc.is_null()); rtassert!(!alloc.is_null());
let alloc = AllocHeader::from(alloc); let alloc = AllocHeader::from(alloc);
@ -249,6 +256,7 @@ impl MemoryRegion {
} }
} }
#[cfg(not(rtdebug))] #[cfg(not(rtdebug))]
#[inline]
fn claim(&mut self, _alloc: &mut AllocHeader) {} fn claim(&mut self, _alloc: &mut AllocHeader) {}
#[cfg(rtdebug)] #[cfg(rtdebug)]
@ -260,6 +268,7 @@ impl MemoryRegion {
} }
} }
#[cfg(not(rtdebug))] #[cfg(not(rtdebug))]
#[inline]
fn release(&mut self, _alloc: &AllocHeader) {} fn release(&mut self, _alloc: &AllocHeader) {}
#[cfg(rtdebug)] #[cfg(rtdebug)]
@ -271,6 +280,7 @@ impl MemoryRegion {
} }
} }
#[cfg(not(rtdebug))] #[cfg(not(rtdebug))]
#[inline]
fn update(&mut self, _alloc: &mut AllocHeader, _orig: *AllocHeader) {} fn update(&mut self, _alloc: &mut AllocHeader, _orig: *AllocHeader) {}
} }
@ -283,6 +293,7 @@ impl Drop for MemoryRegion {
} }
} }
#[inline]
pub unsafe fn local_malloc(td: *libc::c_char, size: libc::uintptr_t) -> *libc::c_char { pub unsafe fn local_malloc(td: *libc::c_char, size: libc::uintptr_t) -> *libc::c_char {
// XXX: Unsafe borrow for speed. Lame. // XXX: Unsafe borrow for speed. Lame.
let task: Option<*mut Task> = Local::try_unsafe_borrow(); let task: Option<*mut Task> = Local::try_unsafe_borrow();
@ -295,6 +306,7 @@ pub unsafe fn local_malloc(td: *libc::c_char, size: libc::uintptr_t) -> *libc::c
} }
// A little compatibility function // A little compatibility function
#[inline]
pub unsafe fn local_free(ptr: *libc::c_char) { pub unsafe fn local_free(ptr: *libc::c_char) {
// XXX: Unsafe borrow for speed. Lame. // XXX: Unsafe borrow for speed. Lame.
let task_ptr: Option<*mut Task> = Local::try_unsafe_borrow(); let task_ptr: Option<*mut Task> = Local::try_unsafe_borrow();

View file

@ -141,6 +141,7 @@ memory and partly incapable of presentation to others.",
abort(); abort();
fn abort() -> ! { fn abort() -> ! {
unsafe { libc::abort() } use std::unstable::intrinsics;
unsafe { intrinsics::abort() }
} }
} }

View file

@ -29,6 +29,7 @@ pub fn fail_bounds_check(file: *c_char, line: size_t, index: size_t, len: size_t
} }
#[lang="malloc"] #[lang="malloc"]
#[inline]
pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
::rt::local_heap::local_malloc(td, size) ::rt::local_heap::local_malloc(td, size)
} }
@ -37,6 +38,7 @@ pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char {
// inside a landing pad may corrupt the state of the exception handler. If a // inside a landing pad may corrupt the state of the exception handler. If a
// problem occurs, call exit instead. // problem occurs, call exit instead.
#[lang="free"] #[lang="free"]
#[inline]
pub unsafe fn local_free(ptr: *c_char) { pub unsafe fn local_free(ptr: *c_char) {
::rt::local_heap::local_free(ptr); ::rt::local_heap::local_free(ptr);
} }