Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std: Refactor liballoc out of lib{std,sync} #14230

Merged
merged 2 commits into from
May 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions mk/crates.mk
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@

TARGET_CRATES := libc std green rustuv native flate arena glob term semver \
uuid serialize sync getopts collections num test time rand \
workcache url log regex graphviz core rlibc
workcache url log regex graphviz core rlibc alloc
HOST_CRATES := syntax rustc rustdoc fourcc hexfloat regex_macros fmt_macros
CRATES := $(TARGET_CRATES) $(HOST_CRATES)
TOOLS := compiletest rustdoc rustc

DEPS_core :=
DEPS_rlibc :=
DEPS_std := core libc native:rustrt native:compiler-rt native:backtrace native:jemalloc
DEPS_alloc := core libc native:jemalloc
DEPS_std := core libc alloc native:rustrt native:backtrace
DEPS_graphviz := std
DEPS_green := std rand native:context_switch
DEPS_rustuv := std native:uv native:uv_support
Expand All @@ -76,7 +77,7 @@ DEPS_serialize := std collections log
DEPS_term := std collections log
DEPS_semver := std
DEPS_uuid := std serialize rand
DEPS_sync := std
DEPS_sync := std alloc
DEPS_getopts := std
DEPS_collections := std rand
DEPS_fourcc := syntax std
Expand All @@ -101,6 +102,7 @@ TOOL_SOURCE_rustc := $(S)src/driver/driver.rs

ONLY_RLIB_core := 1
ONLY_RLIB_rlibc := 1
ONLY_RLIB_alloc := 1

################################################################################
# You should not need to edit below this line
Expand Down
27 changes: 20 additions & 7 deletions src/libsync/arc.rs → src/liballoc/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@
* between tasks.
*/

use std::mem;
use std::ptr;
use std::rt::heap::deallocate;
use std::sync::atomics;
use std::mem::{min_align_of, size_of};
use core::atomics;
use core::clone::Clone;
use core::kinds::{Share, Send};
use core::mem::{min_align_of, size_of, drop};
use core::mem;
use core::ops::{Drop, Deref};
use core::option::{Some, None, Option};
use core::ptr;
use core::ptr::RawPtr;
use heap::deallocate;

/// An atomically reference counted wrapper for shared state.
///
Expand All @@ -28,6 +33,8 @@ use std::mem::{min_align_of, size_of};
/// task.
///
/// ```rust
/// extern crate sync;
///
/// use sync::Arc;
///
/// fn main() {
Expand Down Expand Up @@ -251,10 +258,16 @@ impl<T: Share + Send> Drop for Weak<T> {
#[cfg(test)]
#[allow(experimental)]
mod tests {
use super::{Arc, Weak};
use std::clone::Clone;
use std::comm::channel;
use std::mem::drop;
use std::ops::{Drop, Deref, DerefMut};
use std::option::{Option, Some, None};
use std::sync::atomics;
use std::task;
use Mutex;
use std::vec::Vec;
use super::{Arc, Weak};
use sync::Mutex;

struct Canary(*mut atomics::AtomicUint);

Expand Down
14 changes: 9 additions & 5 deletions src/libstd/rt/heap.rs → src/liballoc/heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
// FIXME: #13994: port to the sized deallocation API when available
// FIXME: #13996: need a way to mark the `allocate` and `reallocate` return values as `noalias`

use intrinsics::{abort, cttz32};
use core::intrinsics::{abort, cttz32};
use core::option::{None, Option};
use core::ptr::{RawPtr, mut_null, null};
use libc::{c_char, c_int, c_void, size_t};
use ptr::{RawPtr, mut_null, null};
use option::{None, Option};

#[cfg(not(test))] use core::raw;
#[cfg(not(test))] use util;

#[link(name = "jemalloc", kind = "static")]
extern {
Expand Down Expand Up @@ -148,11 +151,12 @@ unsafe fn exchange_free(ptr: *mut u8) {
#[cfg(not(test))]
#[lang="closure_exchange_malloc"]
#[inline]
#[allow(deprecated)]
unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint, align: uint) -> *mut u8 {
let total_size = ::rt::util::get_box_size(size, align);
let total_size = util::get_box_size(size, align);
let p = allocate(total_size, 8);

let alloc = p as *mut ::raw::Box<()>;
let alloc = p as *mut raw::Box<()>;
(*alloc).drop_glue = drop_glue;

alloc as *mut u8
Expand Down
101 changes: 101 additions & 0 deletions src/liballoc/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright 2014 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.

//! Rust's core allocation library
//!
//! This is the lowest level library through which allocation in Rust can be
//! performed where the allocation is assumed to succeed. This library will
//! trigger a task failure when allocation fails.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm... shouldn't the lowest level library not be triggering task failure? Are we planning to have any lower-level libraries returning Option or something?

(Not a blocker for landing this at all.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found that it was quite common to want to trigger task failure, much more so than I originally realized. I also found that all contexts have some form or notion of failure, although it's not always task failure.

For example, any of these operations can fail:

  • Option::unwrap
  • Result::unwrap
  • integer division
  • indexing a vector
  • indexing a string

There's a bunch of others throughout the methods in libcore. Consumers of libcore also really want to fail such as liballoc, libcollections, etc. It ended up being common enough that I found it a core enough concept to declare in libcore, but not define in libcore.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking from the perspective of task failure not being recoverable at the call site, i.e. a higher level library is free to fail, but the absolute lowest building blocks shouldn't, so that people can handle problems as they wish (even if it's just manually triggering task failure).

If liballoc isn't designed to be the lowest level allocation library, failing is fine.

(BTW, I think you may've misinterpreted my comment, because I wasn't talking about libcore, just liballoc.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry! I believe that the core allocator interface (located in liballoc) will be specced to not fail!(), just the primitives on top of them (for example, the box operator).

Perhaps we could extend the box syntax to allow returning Option<T> one day to accommodate this use case, because I'd definitely like to be able to re-use this code!

//!
//! This library, like libcore, is not intended for general usage, but rather as
//! a building block of other libraries. The types and interfaces in this
//! library are reexported through the [standard library](../std/index.html),
//! and should not be used through this library.
//!
//! Currently, there are four major definitions in this library.
//!
//! ## Owned pointers
//!
//! The [`Box`](owned/index.html) type is the core owned pointer type in rust.
//! There can only be one owner of a `Box`, and the owner can decide to mutate
//! the contents.
//!
//! This type can be sent among tasks efficiently as the size of a `Box` value
//! is just a pointer. Tree-like data structures are often built on owned
//! pointers because each node often has only one owner, the parent.
//!
//! ## Reference counted pointers
//!
//! The [`Rc`](rc/index.html) type is a non-threadsafe reference-counted pointer
//! type intended for sharing memory within a task. An `Rc` pointer wraps a
//! type, `T`, and only allows access to `&T`, a shared reference.
//!
//! This type is useful when inherited mutability is too constraining for an
//! application (such as using `Box`), and is often paired with the `Cell` or
//! `RefCell` types in order to allow mutation.
//!
//! ## Atomically reference counted pointers
//!
//! The [`Arc`](arc/index.html) type is the threadsafe equivalent of the `Rc`
//! type. It provides all the same functionality of `Rc`, except it requires
//! that the contained type `T` is shareable. Additionally, `Arc<T>` is itself
//! sendable while `Rc<T>` is not.
//!
//! This types allows for shared access to the contained data, and is often
//! paired with synchronization primitives such as mutexes to allow mutation of
//! shared resources.
//!
//! ## Heap interfaces
//!
//! The [`heap`](heap/index.html) and [`libc_heap`](libc_heap/index.html)
//! modules are the unsafe interfaces to the underlying allocation systems. The
//! `heap` module is considered the default heap, and is not necessarily backed
//! by libc malloc/free. The `libc_heap` module is defined to be wired up to
//! the system malloc/free.

#![crate_id = "alloc#0.11.0-pre"]
#![license = "MIT/ASL2"]
#![crate_type = "rlib"]
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
html_root_url = "http://static.rust-lang.org/doc/master")]

#![no_std]
#![feature(phase)]

#[phase(syntax, link)]
extern crate core;
extern crate libc;

// Allow testing this library

#[cfg(test)] extern crate sync;
#[cfg(test)] extern crate native;
#[cfg(test)] #[phase(syntax, link)] extern crate std;
#[cfg(test)] #[phase(syntax, link)] extern crate log;

// Heaps provided for low-level allocation strategies

pub mod heap;
pub mod libc_heap;
pub mod util;

// Primitive types using the heaps above

#[cfg(not(test))]
pub mod owned;
pub mod arc;
pub mod rc;

#[cfg(not(test))]
mod std {
pub use core::fmt;
pub use core::option;
}
4 changes: 2 additions & 2 deletions src/libstd/rt/libc_heap.rs → src/liballoc/libc_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
//! The global (exchange) heap.

use libc::{c_void, size_t, free, malloc, realloc};
use ptr::{RawPtr, mut_null};
use intrinsics::abort;
use core::ptr::{RawPtr, mut_null};
use core::intrinsics::abort;

/// A wrapper around libc::malloc, aborting on out-of-memory
#[inline]
Expand Down
21 changes: 10 additions & 11 deletions src/libstd/owned.rs → src/liballoc/owned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Operations on unique pointer types
//! A unique pointer type

use any::{Any, AnyRefExt};
use clone::Clone;
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
use default::Default;
use fmt;
use intrinsics;
use mem;
use raw::TraitObject;
use result::{Ok, Err, Result};
use core::any::{Any, AnyRefExt};
use core::clone::Clone;
use core::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
use core::default::Default;
use core::fmt;
use core::intrinsics;
use core::mem;
use core::raw::TraitObject;
use core::result::{Ok, Err, Result};

/// A value that represents the global exchange heap. This is the default
/// place that the `box` keyword allocates into when no place is supplied.
Expand Down Expand Up @@ -107,7 +107,6 @@ impl<T: fmt::Show> fmt::Show for Box<T> {
}
}

#[cfg(not(stage0))]
impl fmt::Show for Box<Any> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("Box<Any>")
Expand Down
33 changes: 18 additions & 15 deletions src/libstd/rc.rs → src/liballoc/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,18 @@ pointers, and then storing the parent pointers as `Weak` pointers.

*/

use mem::transmute;
use cell::Cell;
use clone::Clone;
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
use kinds::marker;
use ops::{Deref, Drop};
use option::{Option, Some, None};
use ptr;
use ptr::RawPtr;
use mem::{min_align_of, size_of};
use rt::heap::deallocate;
use core::mem::transmute;
use core::cell::Cell;
use core::clone::Clone;
use core::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
use core::kinds::marker;
use core::ops::{Deref, Drop};
use core::option::{Option, Some, None};
use core::ptr;
use core::ptr::RawPtr;
use core::mem::{min_align_of, size_of};

use heap::deallocate;

struct RcBox<T> {
value: T,
Expand Down Expand Up @@ -230,9 +231,11 @@ impl<T> RcBoxPtr<T> for Weak<T> {

#[cfg(test)]
mod tests {
use prelude::*;
use super::*;
use cell::RefCell;
use super::{Rc, Weak};
use std::cell::RefCell;
use std::option::{Option, Some, None};
use std::mem::drop;
use std::clone::Clone;

#[test]
fn test_clone() {
Expand Down Expand Up @@ -280,7 +283,7 @@ mod tests {
#[test]
fn gc_inside() {
// see issue #11532
use gc::Gc;
use std::gc::Gc;
let a = Rc::new(RefCell::new(Gc::new(1)));
assert!(a.try_borrow_mut().is_some());
}
Expand Down
30 changes: 30 additions & 0 deletions src/liballoc/util.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only needed for @ right? Could these functions just move to local_heap?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, no, sorry, ignore that. My ctrl-F-ing mislead me. (I didn't notice the closure use too.)

// 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.

#![doc(hidden)]

use core::mem;
use core::raw;

#[inline]
#[deprecated]
pub fn get_box_size(body_size: uint, body_align: uint) -> uint {
let header_size = mem::size_of::<raw::Box<()>>();
let total_size = align_to(header_size, body_align) + body_size;
total_size
}

// Rounds size to the next alignment. Alignment is required to be a power of
// two.
#[inline]
fn align_to(size: uint, align: uint) -> uint {
assert!(align != 0);
(size + align - 1) & !(align - 1)
}
4 changes: 0 additions & 4 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ pub struct Formatter<'a> {
/// Optionally specified precision for numeric types
pub precision: Option<uint>,

#[allow(missing_doc)]
#[cfg(stage0)]
pub buf: &'a mut FormatWriter,
#[cfg(not(stage0))]
buf: &'a mut FormatWriter,
curarg: slice::Items<'a, Argument<'a>>,
args: &'a [Argument<'a>],
Expand Down
Loading