From 7ca3bea5bfd15dd960e6346eebf72422b0020d6b Mon Sep 17 00:00:00 2001 From: Eduard Burtescu Date: Wed, 15 Jan 2014 11:45:12 +0200 Subject: [PATCH] libstd: Added more #[inline] annotations and replaced uses of `libc::abort` with the intrinsic. --- src/libgreen/macros.rs | 4 ++-- src/libstd/at_vec.rs | 11 +++++++++++ src/libstd/libc.rs | 3 +-- src/libstd/local_data.rs | 3 ++- src/libstd/rt/global_heap.rs | 9 ++++----- src/libstd/rt/local_heap.rs | 12 ++++++++++++ src/libstd/rt/util.rs | 3 ++- src/libstd/unstable/lang.rs | 2 ++ 8 files changed, 36 insertions(+), 11 deletions(-) diff --git a/src/libgreen/macros.rs b/src/libgreen/macros.rs index 56dc3204da8..12def918bc9 100644 --- a/src/libgreen/macros.rs +++ b/src/libgreen/macros.rs @@ -13,7 +13,6 @@ #[macro_escape]; use std::fmt; -use std::libc; // Indicates whether we should perform expensive sanity checks, including rtassert! // 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(); fn abort() -> ! { - unsafe { libc::abort() } + use std::unstable::intrinsics; + unsafe { intrinsics::abort() } } } diff --git a/src/libstd/at_vec.rs b/src/libstd/at_vec.rs index aedda59bbac..18cb470db4d 100644 --- a/src/libstd/at_vec.rs +++ b/src/libstd/at_vec.rs @@ -68,6 +68,7 @@ pub fn append(lhs: @[T], rhs: &[T]) -> @[T] { /// Apply a function to each element of a vector and return the results +#[inline] pub fn map(v: &[T], f: |x: &T| -> U) -> @[U] { build(Some(v.len()), |push| { for elem in v.iter() { @@ -82,6 +83,7 @@ pub fn map(v: &[T], f: |x: &T| -> U) -> @[U] { * Creates an immutable vector of size `n_elts` and initializes the elements * to the value returned by the function `op`. */ +#[inline] pub fn from_fn(n_elts: uint, op: |uint| -> T) -> @[T] { build(Some(n_elts), |push| { let mut i: uint = 0u; @@ -95,6 +97,7 @@ pub fn from_fn(n_elts: uint, op: |uint| -> T) -> @[T] { * Creates an immutable vector of size `n_elts` and initializes the elements * to the value `t`. */ +#[inline] pub fn from_elem(n_elts: uint, t: T) -> @[T] { build(Some(n_elts), |push| { let mut i: uint = 0u; @@ -109,6 +112,7 @@ pub fn from_elem(n_elts: uint, t: T) -> @[T] { * Creates and initializes an immutable managed vector by moving all the * elements from an owned vector. */ +#[inline] pub fn to_managed_move(v: ~[T]) -> @[T] { let mut av = @[]; unsafe { @@ -124,6 +128,7 @@ pub fn to_managed_move(v: ~[T]) -> @[T] { * Creates and initializes an immutable managed vector by copying all the * elements of a slice. */ +#[inline] pub fn to_managed(v: &[T]) -> @[T] { from_fn(v.len(), |i| v[i].clone()) } @@ -135,6 +140,7 @@ impl Clone for @[T] { } impl FromIterator for @[A] { + #[inline] fn from_iterator>(iterator: &mut T) -> @[A] { let (lower, _) = iterator.size_hint(); build(Some(lower), |push| { @@ -216,6 +222,7 @@ pub mod raw { move_val_init(&mut(*p), initval); } + #[inline] unsafe fn push_slow(v: &mut @[T], initval: T) { reserve_at_least(v, v.len() + 1u); push_fast(v, initval); @@ -232,6 +239,7 @@ pub mod raw { * * v - A vector * * n - The number of elements to reserve space for */ + #[inline] pub unsafe fn reserve(v: &mut @[T], n: uint) { // Only make the (slow) call into the runtime if we have to if capacity(*v) < n { @@ -243,6 +251,7 @@ pub mod raw { // Implementation detail. Shouldn't be public #[allow(missing_doc)] + #[inline] pub fn reserve_raw(ty: *TyDesc, ptr: *mut *mut Box>, n: uint) { // check for `uint` overflow unsafe { @@ -257,6 +266,7 @@ pub mod raw { } } + #[inline] fn local_realloc(ptr: *(), size: uint) -> *() { use rt::local::Local; use rt::task::Task; @@ -281,6 +291,7 @@ pub mod raw { * * v - A vector * * n - The number of elements to reserve space for */ + #[inline] pub unsafe fn reserve_at_least(v: &mut @[T], n: uint) { reserve(v, uint::next_power_of_two(n)); } diff --git a/src/libstd/libc.rs b/src/libstd/libc.rs index 6b54a176e89..9cf94e5a1b8 100644 --- a/src/libstd/libc.rs +++ b/src/libstd/libc.rs @@ -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::{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::{realloc, srand, strtod, strtol}; 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 realloc(p: *mut c_void, size: size_t) -> *mut c_void; pub fn free(p: *c_void); - pub fn abort() -> !; pub fn exit(status: c_int) -> !; // Omitted: atexit. pub fn system(s: *c_char) -> c_int; diff --git a/src/libstd/local_data.rs b/src/libstd/local_data.rs index 159337bf503..7ef7a256c16 100644 --- a/src/libstd/local_data.rs +++ b/src/libstd/local_data.rs @@ -280,7 +280,8 @@ fn get_with ! { - unsafe { libc::abort() } + use std::unstable::intrinsics; + unsafe { intrinsics::abort() } } /// Inserts a value into task local storage. If the key is already present in diff --git a/src/libstd/rt/global_heap.rs b/src/libstd/rt/global_heap.rs index 7a417990a4c..ce4072fb1ab 100644 --- a/src/libstd/rt/global_heap.rs +++ b/src/libstd/rt/global_heap.rs @@ -10,14 +10,10 @@ use libc::{c_void, c_char, size_t, uintptr_t, free, malloc, realloc}; use ptr::RawPtr; -use unstable::intrinsics::TyDesc; +use unstable::intrinsics::{TyDesc, abort}; use unstable::raw; use mem::size_of; -extern { - fn abort(); -} - #[inline] pub fn get_box_size(body_size: uint, body_align: uint) -> uint { let header_size = size_of::>(); @@ -34,6 +30,7 @@ fn align_to(size: uint, align: uint) -> uint { } /// A wrapper around libc::malloc, aborting on out-of-memory +#[inline] pub unsafe fn malloc_raw(size: uint) -> *c_void { let p = malloc(size as size_t); 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 +#[inline] pub unsafe fn realloc_raw(ptr: *mut c_void, size: uint) -> *mut c_void { let p = realloc(ptr, size as size_t); if p.is_null() { @@ -94,6 +92,7 @@ pub unsafe fn exchange_free_(ptr: *c_char) { exchange_free(ptr) } +#[inline] pub unsafe fn exchange_free(ptr: *c_char) { free(ptr as *c_void); } diff --git a/src/libstd/rt/local_heap.rs b/src/libstd/rt/local_heap.rs index 1be942b2db1..90179612272 100644 --- a/src/libstd/rt/local_heap.rs +++ b/src/libstd/rt/local_heap.rs @@ -48,6 +48,7 @@ pub struct LocalHeap { } impl LocalHeap { + #[inline] pub fn new() -> LocalHeap { let region = MemoryRegion { allocations: ~[], @@ -60,6 +61,7 @@ impl LocalHeap { } } + #[inline] pub fn alloc(&mut self, td: *TyDesc, size: uint) -> *mut Box { let total_size = global_heap::get_box_size(size, unsafe { (*td).align }); let alloc = self.memory_region.malloc(total_size); @@ -80,6 +82,7 @@ impl LocalHeap { return alloc; } + #[inline] pub fn realloc(&mut self, ptr: *mut Box, size: uint) -> *mut Box { // Make sure that we can't use `mybox` outside of this scope let total_size = size + mem::size_of::(); @@ -100,6 +103,7 @@ impl LocalHeap { return new_box; } + #[inline] pub fn free(&mut self, alloc: *mut Box) { { // Make sure that we can't use `mybox` outside of this scope @@ -196,6 +200,7 @@ impl AllocHeader { } impl MemoryRegion { + #[inline] fn malloc(&mut self, size: uint) -> *mut Box { let total_size = size + AllocHeader::size(); let alloc: *AllocHeader = unsafe { @@ -210,6 +215,7 @@ impl MemoryRegion { return alloc.as_box(); } + #[inline] fn realloc(&mut self, alloc: *mut Box, size: uint) -> *mut Box { rtassert!(!alloc.is_null()); let orig_alloc = AllocHeader::from(alloc); @@ -228,6 +234,7 @@ impl MemoryRegion { return alloc.as_box(); } + #[inline] fn free(&mut self, alloc: *mut Box) { rtassert!(!alloc.is_null()); let alloc = AllocHeader::from(alloc); @@ -249,6 +256,7 @@ impl MemoryRegion { } } #[cfg(not(rtdebug))] + #[inline] fn claim(&mut self, _alloc: &mut AllocHeader) {} #[cfg(rtdebug)] @@ -260,6 +268,7 @@ impl MemoryRegion { } } #[cfg(not(rtdebug))] + #[inline] fn release(&mut self, _alloc: &AllocHeader) {} #[cfg(rtdebug)] @@ -271,6 +280,7 @@ impl MemoryRegion { } } #[cfg(not(rtdebug))] + #[inline] 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 { // XXX: Unsafe borrow for speed. Lame. 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 +#[inline] pub unsafe fn local_free(ptr: *libc::c_char) { // XXX: Unsafe borrow for speed. Lame. let task_ptr: Option<*mut Task> = Local::try_unsafe_borrow(); diff --git a/src/libstd/rt/util.rs b/src/libstd/rt/util.rs index ee336d7d847..a5d5a4677f1 100644 --- a/src/libstd/rt/util.rs +++ b/src/libstd/rt/util.rs @@ -141,6 +141,7 @@ memory and partly incapable of presentation to others.", abort(); fn abort() -> ! { - unsafe { libc::abort() } + use std::unstable::intrinsics; + unsafe { intrinsics::abort() } } } diff --git a/src/libstd/unstable/lang.rs b/src/libstd/unstable/lang.rs index e7e8cec9d5f..38c713ad7b7 100644 --- a/src/libstd/unstable/lang.rs +++ b/src/libstd/unstable/lang.rs @@ -29,6 +29,7 @@ pub fn fail_bounds_check(file: *c_char, line: size_t, index: size_t, len: size_t } #[lang="malloc"] +#[inline] pub unsafe fn local_malloc(td: *c_char, size: uintptr_t) -> *c_char { ::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 // problem occurs, call exit instead. #[lang="free"] +#[inline] pub unsafe fn local_free(ptr: *c_char) { ::rt::local_heap::local_free(ptr); }