Skip to content

Commit

Permalink
libstd: Added more #[inline] annotations and replaced uses of `libc::…
Browse files Browse the repository at this point in the history
…abort` with the intrinsic.
  • Loading branch information
eddyb committed Jan 15, 2014
1 parent e063e96 commit 7ca3bea
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/libgreen/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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() }
}
}
11 changes: 11 additions & 0 deletions src/libstd/at_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
#[inline]
pub fn map<T, U>(v: &[T], f: |x: &T| -> U) -> @[U] {
build(Some(v.len()), |push| {
for elem in v.iter() {
Expand All @@ -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
* to the value returned by the function `op`.
*/
#[inline]
pub fn from_fn<T>(n_elts: uint, op: |uint| -> T) -> @[T] {
build(Some(n_elts), |push| {
let mut i: uint = 0u;
Expand All @@ -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
* to the value `t`.
*/
#[inline]
pub fn from_elem<T:Clone>(n_elts: uint, t: T) -> @[T] {
build(Some(n_elts), |push| {
let mut i: uint = 0u;
Expand All @@ -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
* elements from an owned vector.
*/
#[inline]
pub fn to_managed_move<T>(v: ~[T]) -> @[T] {
let mut av = @[];
unsafe {
Expand All @@ -124,6 +128,7 @@ pub fn to_managed_move<T>(v: ~[T]) -> @[T] {
* Creates and initializes an immutable managed vector by copying all the
* elements of a slice.
*/
#[inline]
pub fn to_managed<T:Clone>(v: &[T]) -> @[T] {
from_fn(v.len(), |i| v[i].clone())
}
Expand All @@ -135,6 +140,7 @@ impl<T> Clone for @[T] {
}

impl<A> FromIterator<A> for @[A] {
#[inline]
fn from_iterator<T: Iterator<A>>(iterator: &mut T) -> @[A] {
let (lower, _) = iterator.size_hint();
build(Some(lower), |push| {
Expand Down Expand Up @@ -216,6 +222,7 @@ pub mod raw {
move_val_init(&mut(*p), initval);
}

#[inline]
unsafe fn push_slow<T>(v: &mut @[T], initval: T) {
reserve_at_least(v, v.len() + 1u);
push_fast(v, initval);
Expand All @@ -232,6 +239,7 @@ pub mod raw {
* * v - A vector
* * n - The number of elements to reserve space for
*/
#[inline]
pub unsafe fn reserve<T>(v: &mut @[T], n: uint) {
// Only make the (slow) call into the runtime if we have to
if capacity(*v) < n {
Expand All @@ -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<Vec<()>>, n: uint) {
// check for `uint` overflow
unsafe {
Expand All @@ -257,6 +266,7 @@ pub mod raw {
}
}

#[inline]
fn local_realloc(ptr: *(), size: uint) -> *() {
use rt::local::Local;
use rt::task::Task;
Expand All @@ -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<T>(v: &mut @[T], n: uint) {
reserve(v, uint::next_power_of_two(n));
}
Expand Down
3 changes: 1 addition & 2 deletions src/libstd/libc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/local_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ fn get_with<T:'static,
}

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
Expand Down
9 changes: 4 additions & 5 deletions src/libstd/rt/global_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<raw::Box<()>>();
Expand All @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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);
}
Expand Down
12 changes: 12 additions & 0 deletions src/libstd/rt/local_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub struct LocalHeap {
}

impl LocalHeap {
#[inline]
pub fn new() -> LocalHeap {
let region = MemoryRegion {
allocations: ~[],
Expand All @@ -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);
Expand All @@ -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::<Box>();
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -249,6 +256,7 @@ impl MemoryRegion {
}
}
#[cfg(not(rtdebug))]
#[inline]
fn claim(&mut self, _alloc: &mut AllocHeader) {}

#[cfg(rtdebug)]
Expand All @@ -260,6 +268,7 @@ impl MemoryRegion {
}
}
#[cfg(not(rtdebug))]
#[inline]
fn release(&mut self, _alloc: &AllocHeader) {}

#[cfg(rtdebug)]
Expand All @@ -271,6 +280,7 @@ impl MemoryRegion {
}
}
#[cfg(not(rtdebug))]
#[inline]
fn update(&mut self, _alloc: &mut AllocHeader, _orig: *AllocHeader) {}
}

Expand All @@ -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();
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion src/libstd/rt/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
}
}
2 changes: 2 additions & 0 deletions src/libstd/unstable/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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);
}
Expand Down

0 comments on commit 7ca3bea

Please sign in to comment.