Skip to content

Commit 134f797

Browse files
committed
auto merge of #14230 : alexcrichton/rust/liballoc, r=cmr,huonw
This commit is part of the libstd facade RFC, issue #13851. This creates a new library, liballoc, which is intended to be the core allocation library for all of Rust. It is pinned on the basic assumption that an allocation failure is an abort or failure. This module has inherited the heap/libc_heap modules from std::rt, the owned/rc modules from std, and the arc module from libsync. These three pointers are currently the three most core pointer implementations in Rust. The UnsafeArc type in std::sync should be considered deprecated and replaced by `Arc<Unsafe<T>>`. This commit does not currently migrate to this type, but future commits will continue this refactoring.
2 parents 3da5a5c + 4a1d21a commit 134f797

File tree

19 files changed

+226
-97
lines changed

19 files changed

+226
-97
lines changed

mk/crates.mk

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151

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

5959
DEPS_core :=
6060
DEPS_rlibc :=
61-
DEPS_std := core libc native:rustrt native:compiler-rt native:backtrace native:jemalloc
61+
DEPS_alloc := core libc native:jemalloc
62+
DEPS_std := core libc alloc native:rustrt native:backtrace
6263
DEPS_graphviz := std
6364
DEPS_green := std rand native:context_switch
6465
DEPS_rustuv := std native:uv native:uv_support
@@ -76,7 +77,7 @@ DEPS_serialize := std collections log
7677
DEPS_term := std collections log
7778
DEPS_semver := std
7879
DEPS_uuid := std serialize rand
79-
DEPS_sync := std
80+
DEPS_sync := std alloc
8081
DEPS_getopts := std
8182
DEPS_collections := std rand
8283
DEPS_fourcc := syntax std
@@ -101,6 +102,7 @@ TOOL_SOURCE_rustc := $(S)src/driver/driver.rs
101102

102103
ONLY_RLIB_core := 1
103104
ONLY_RLIB_rlibc := 1
105+
ONLY_RLIB_alloc := 1
104106

105107
################################################################################
106108
# You should not need to edit below this line

src/libsync/arc.rs renamed to src/liballoc/arc.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@
1313
* between tasks.
1414
*/
1515

16-
use std::mem;
17-
use std::ptr;
18-
use std::rt::heap::deallocate;
19-
use std::sync::atomics;
20-
use std::mem::{min_align_of, size_of};
16+
use core::atomics;
17+
use core::clone::Clone;
18+
use core::kinds::{Share, Send};
19+
use core::mem::{min_align_of, size_of, drop};
20+
use core::mem;
21+
use core::ops::{Drop, Deref};
22+
use core::option::{Some, None, Option};
23+
use core::ptr;
24+
use core::ptr::RawPtr;
25+
use heap::deallocate;
2126

2227
/// An atomically reference counted wrapper for shared state.
2328
///
@@ -28,6 +33,8 @@ use std::mem::{min_align_of, size_of};
2833
/// task.
2934
///
3035
/// ```rust
36+
/// extern crate sync;
37+
///
3138
/// use sync::Arc;
3239
///
3340
/// fn main() {
@@ -251,10 +258,16 @@ impl<T: Share + Send> Drop for Weak<T> {
251258
#[cfg(test)]
252259
#[allow(experimental)]
253260
mod tests {
254-
use super::{Arc, Weak};
261+
use std::clone::Clone;
262+
use std::comm::channel;
263+
use std::mem::drop;
264+
use std::ops::{Drop, Deref, DerefMut};
265+
use std::option::{Option, Some, None};
255266
use std::sync::atomics;
256267
use std::task;
257-
use Mutex;
268+
use std::vec::Vec;
269+
use super::{Arc, Weak};
270+
use sync::Mutex;
258271

259272
struct Canary(*mut atomics::AtomicUint);
260273

src/libstd/rt/heap.rs renamed to src/liballoc/heap.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111
// FIXME: #13994: port to the sized deallocation API when available
1212
// FIXME: #13996: need a way to mark the `allocate` and `reallocate` return values as `noalias`
1313

14-
use intrinsics::{abort, cttz32};
14+
use core::intrinsics::{abort, cttz32};
15+
use core::option::{None, Option};
16+
use core::ptr::{RawPtr, mut_null, null};
1517
use libc::{c_char, c_int, c_void, size_t};
16-
use ptr::{RawPtr, mut_null, null};
17-
use option::{None, Option};
18+
19+
#[cfg(not(test))] use core::raw;
20+
#[cfg(not(test))] use util;
1821

1922
#[link(name = "jemalloc", kind = "static")]
2023
extern {
@@ -148,11 +151,12 @@ unsafe fn exchange_free(ptr: *mut u8) {
148151
#[cfg(not(test))]
149152
#[lang="closure_exchange_malloc"]
150153
#[inline]
154+
#[allow(deprecated)]
151155
unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint, align: uint) -> *mut u8 {
152-
let total_size = ::rt::util::get_box_size(size, align);
156+
let total_size = util::get_box_size(size, align);
153157
let p = allocate(total_size, 8);
154158

155-
let alloc = p as *mut ::raw::Box<()>;
159+
let alloc = p as *mut raw::Box<()>;
156160
(*alloc).drop_glue = drop_glue;
157161

158162
alloc as *mut u8

src/liballoc/lib.rs

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Rust's core allocation library
12+
//!
13+
//! This is the lowest level library through which allocation in Rust can be
14+
//! performed where the allocation is assumed to succeed. This library will
15+
//! trigger a task failure when allocation fails.
16+
//!
17+
//! This library, like libcore, is not intended for general usage, but rather as
18+
//! a building block of other libraries. The types and interfaces in this
19+
//! library are reexported through the [standard library](../std/index.html),
20+
//! and should not be used through this library.
21+
//!
22+
//! Currently, there are four major definitions in this library.
23+
//!
24+
//! ## Owned pointers
25+
//!
26+
//! The [`Box`](owned/index.html) type is the core owned pointer type in rust.
27+
//! There can only be one owner of a `Box`, and the owner can decide to mutate
28+
//! the contents.
29+
//!
30+
//! This type can be sent among tasks efficiently as the size of a `Box` value
31+
//! is just a pointer. Tree-like data structures are often built on owned
32+
//! pointers because each node often has only one owner, the parent.
33+
//!
34+
//! ## Reference counted pointers
35+
//!
36+
//! The [`Rc`](rc/index.html) type is a non-threadsafe reference-counted pointer
37+
//! type intended for sharing memory within a task. An `Rc` pointer wraps a
38+
//! type, `T`, and only allows access to `&T`, a shared reference.
39+
//!
40+
//! This type is useful when inherited mutability is too constraining for an
41+
//! application (such as using `Box`), and is often paired with the `Cell` or
42+
//! `RefCell` types in order to allow mutation.
43+
//!
44+
//! ## Atomically reference counted pointers
45+
//!
46+
//! The [`Arc`](arc/index.html) type is the threadsafe equivalent of the `Rc`
47+
//! type. It provides all the same functionality of `Rc`, except it requires
48+
//! that the contained type `T` is shareable. Additionally, `Arc<T>` is itself
49+
//! sendable while `Rc<T>` is not.
50+
//!
51+
//! This types allows for shared access to the contained data, and is often
52+
//! paired with synchronization primitives such as mutexes to allow mutation of
53+
//! shared resources.
54+
//!
55+
//! ## Heap interfaces
56+
//!
57+
//! The [`heap`](heap/index.html) and [`libc_heap`](libc_heap/index.html)
58+
//! modules are the unsafe interfaces to the underlying allocation systems. The
59+
//! `heap` module is considered the default heap, and is not necessarily backed
60+
//! by libc malloc/free. The `libc_heap` module is defined to be wired up to
61+
//! the system malloc/free.
62+
63+
#![crate_id = "alloc#0.11.0-pre"]
64+
#![license = "MIT/ASL2"]
65+
#![crate_type = "rlib"]
66+
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
67+
html_favicon_url = "http://www.rust-lang.org/favicon.ico",
68+
html_root_url = "http://static.rust-lang.org/doc/master")]
69+
70+
#![no_std]
71+
#![feature(phase)]
72+
73+
#[phase(syntax, link)]
74+
extern crate core;
75+
extern crate libc;
76+
77+
// Allow testing this library
78+
79+
#[cfg(test)] extern crate sync;
80+
#[cfg(test)] extern crate native;
81+
#[cfg(test)] #[phase(syntax, link)] extern crate std;
82+
#[cfg(test)] #[phase(syntax, link)] extern crate log;
83+
84+
// Heaps provided for low-level allocation strategies
85+
86+
pub mod heap;
87+
pub mod libc_heap;
88+
pub mod util;
89+
90+
// Primitive types using the heaps above
91+
92+
#[cfg(not(test))]
93+
pub mod owned;
94+
pub mod arc;
95+
pub mod rc;
96+
97+
#[cfg(not(test))]
98+
mod std {
99+
pub use core::fmt;
100+
pub use core::option;
101+
}

src/libstd/rt/libc_heap.rs renamed to src/liballoc/libc_heap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//! The global (exchange) heap.
1313
1414
use libc::{c_void, size_t, free, malloc, realloc};
15-
use ptr::{RawPtr, mut_null};
16-
use intrinsics::abort;
15+
use core::ptr::{RawPtr, mut_null};
16+
use core::intrinsics::abort;
1717

1818
/// A wrapper around libc::malloc, aborting on out-of-memory
1919
#[inline]

src/libstd/owned.rs renamed to src/liballoc/owned.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
//! Operations on unique pointer types
11+
//! A unique pointer type
1212
13-
use any::{Any, AnyRefExt};
14-
use clone::Clone;
15-
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
16-
use default::Default;
17-
use fmt;
18-
use intrinsics;
19-
use mem;
20-
use raw::TraitObject;
21-
use result::{Ok, Err, Result};
13+
use core::any::{Any, AnyRefExt};
14+
use core::clone::Clone;
15+
use core::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
16+
use core::default::Default;
17+
use core::fmt;
18+
use core::intrinsics;
19+
use core::mem;
20+
use core::raw::TraitObject;
21+
use core::result::{Ok, Err, Result};
2222

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

110-
#[cfg(not(stage0))]
111110
impl fmt::Show for Box<Any> {
112111
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
113112
f.pad("Box<Any>")

src/libstd/rc.rs renamed to src/liballoc/rc.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ pointers, and then storing the parent pointers as `Weak` pointers.
2323
2424
*/
2525

26-
use mem::transmute;
27-
use cell::Cell;
28-
use clone::Clone;
29-
use cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
30-
use kinds::marker;
31-
use ops::{Deref, Drop};
32-
use option::{Option, Some, None};
33-
use ptr;
34-
use ptr::RawPtr;
35-
use mem::{min_align_of, size_of};
36-
use rt::heap::deallocate;
26+
use core::mem::transmute;
27+
use core::cell::Cell;
28+
use core::clone::Clone;
29+
use core::cmp::{Eq, Ord, TotalEq, TotalOrd, Ordering};
30+
use core::kinds::marker;
31+
use core::ops::{Deref, Drop};
32+
use core::option::{Option, Some, None};
33+
use core::ptr;
34+
use core::ptr::RawPtr;
35+
use core::mem::{min_align_of, size_of};
36+
37+
use heap::deallocate;
3738

3839
struct RcBox<T> {
3940
value: T,
@@ -230,9 +231,11 @@ impl<T> RcBoxPtr<T> for Weak<T> {
230231

231232
#[cfg(test)]
232233
mod tests {
233-
use prelude::*;
234-
use super::*;
235-
use cell::RefCell;
234+
use super::{Rc, Weak};
235+
use std::cell::RefCell;
236+
use std::option::{Option, Some, None};
237+
use std::mem::drop;
238+
use std::clone::Clone;
236239

237240
#[test]
238241
fn test_clone() {
@@ -280,7 +283,7 @@ mod tests {
280283
#[test]
281284
fn gc_inside() {
282285
// see issue #11532
283-
use gc::Gc;
286+
use std::gc::Gc;
284287
let a = Rc::new(RefCell::new(Gc::new(1)));
285288
assert!(a.try_borrow_mut().is_some());
286289
}

src/liballoc/util.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![doc(hidden)]
12+
13+
use core::mem;
14+
use core::raw;
15+
16+
#[inline]
17+
#[deprecated]
18+
pub fn get_box_size(body_size: uint, body_align: uint) -> uint {
19+
let header_size = mem::size_of::<raw::Box<()>>();
20+
let total_size = align_to(header_size, body_align) + body_size;
21+
total_size
22+
}
23+
24+
// Rounds size to the next alignment. Alignment is required to be a power of
25+
// two.
26+
#[inline]
27+
fn align_to(size: uint, align: uint) -> uint {
28+
assert!(align != 0);
29+
(size + align - 1) & !(align - 1)
30+
}

src/libcore/fmt/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,6 @@ pub struct Formatter<'a> {
7070
/// Optionally specified precision for numeric types
7171
pub precision: Option<uint>,
7272

73-
#[allow(missing_doc)]
74-
#[cfg(stage0)]
75-
pub buf: &'a mut FormatWriter,
76-
#[cfg(not(stage0))]
7773
buf: &'a mut FormatWriter,
7874
curarg: slice::Items<'a, Argument<'a>>,
7975
args: &'a [Argument<'a>],

0 commit comments

Comments
 (0)