-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
//! | ||
//! 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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only needed for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} |
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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, thebox
operator).Perhaps we could extend the
box
syntax to allow returningOption<T>
one day to accommodate this use case, because I'd definitely like to be able to re-use this code!