2014-05-13 18:10:05 -05:00
|
|
|
// 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.
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
//! # The Rust core allocation library
|
2014-05-13 18:10:05 -05:00
|
|
|
//!
|
|
|
|
//! This is the lowest level library through which allocation in Rust can be
|
2014-10-28 16:06:06 -05:00
|
|
|
//! performed.
|
2014-05-13 18:10:05 -05:00
|
|
|
//!
|
|
|
|
//! 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.
|
|
|
|
//!
|
2014-07-10 16:19:17 -05:00
|
|
|
//! ## Boxed values
|
2014-05-13 18:10:05 -05:00
|
|
|
//!
|
2015-06-09 15:26:21 -05:00
|
|
|
//! The [`Box`](boxed/index.html) type is a smart pointer type. There can
|
|
|
|
//! only be one owner of a `Box`, and the owner can decide to mutate the
|
|
|
|
//! contents, which live on the heap.
|
2014-05-13 18:10:05 -05:00
|
|
|
//!
|
2015-05-08 10:12:29 -05:00
|
|
|
//! This type can be sent among threads efficiently as the size of a `Box` value
|
2014-08-04 05:48:39 -05:00
|
|
|
//! is the same as that of a pointer. Tree-like data structures are often built
|
|
|
|
//! with boxes because each node often has only one owner, the parent.
|
2014-05-13 18:10:05 -05:00
|
|
|
//!
|
|
|
|
//! ## Reference counted pointers
|
|
|
|
//!
|
|
|
|
//! The [`Rc`](rc/index.html) type is a non-threadsafe reference-counted pointer
|
2015-05-08 10:12:29 -05:00
|
|
|
//! type intended for sharing memory within a thread. An `Rc` pointer wraps a
|
2014-05-13 18:10:05 -05:00
|
|
|
//! type, `T`, and only allows access to `&T`, a shared reference.
|
|
|
|
//!
|
2014-08-04 05:48:39 -05:00
|
|
|
//! This type is useful when inherited mutability (such as using `Box`) is too
|
|
|
|
//! constraining for an application, and is often paired with the `Cell` or
|
2014-05-13 18:10:05 -05:00
|
|
|
//! `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
|
|
|
|
//!
|
2014-10-24 16:34:57 -05:00
|
|
|
//! The [`heap`](heap/index.html) module defines the low-level interface to the
|
|
|
|
//! default global allocator. It is not compatible with the libc allocator API.
|
2014-05-13 18:10:05 -05:00
|
|
|
|
2015-03-05 10:53:51 -06:00
|
|
|
// Do not remove on snapshot creation. Needed for bootstrap. (Issue #22364)
|
|
|
|
#![cfg_attr(stage0, feature(custom_attribute))]
|
2014-07-01 09:12:04 -05:00
|
|
|
#![crate_name = "alloc"]
|
2014-05-13 18:10:05 -05:00
|
|
|
#![crate_type = "rlib"]
|
2015-06-09 13:52:41 -05:00
|
|
|
#![staged_api]
|
|
|
|
#![unstable(feature = "alloc",
|
|
|
|
reason = "this library is unlikely to be stabilized in its current \
|
|
|
|
form or name")]
|
2014-05-13 18:10:05 -05:00
|
|
|
#![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
|
2015-05-15 18:04:01 -05:00
|
|
|
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
|
2015-06-09 13:18:03 -05:00
|
|
|
html_root_url = "http://doc.rust-lang.org/nightly/",
|
|
|
|
test(no_crate_inject))]
|
2014-05-13 18:10:05 -05:00
|
|
|
#![no_std]
|
2015-06-09 13:18:03 -05:00
|
|
|
|
2015-03-12 21:19:30 -05:00
|
|
|
#![feature(allocator)]
|
2015-06-09 13:18:03 -05:00
|
|
|
#![feature(box_syntax)]
|
|
|
|
#![feature(coerce_unsized)]
|
|
|
|
#![feature(core)]
|
|
|
|
#![feature(core_intrinsics)]
|
|
|
|
#![feature(core_prelude)]
|
2015-03-30 16:52:00 -05:00
|
|
|
#![feature(custom_attribute)]
|
|
|
|
#![feature(fundamental)]
|
2015-04-27 16:10:49 -05:00
|
|
|
#![feature(lang_items)]
|
2015-06-09 13:18:03 -05:00
|
|
|
#![feature(no_std)]
|
|
|
|
#![feature(nonzero)]
|
2015-01-11 04:09:53 -06:00
|
|
|
#![feature(optin_builtin_traits)]
|
2015-02-12 04:30:16 -06:00
|
|
|
#![feature(placement_in_syntax)]
|
2015-06-09 13:18:03 -05:00
|
|
|
#![feature(raw)]
|
|
|
|
#![feature(staged_api)]
|
2015-01-26 13:39:58 -06:00
|
|
|
#![feature(unboxed_closures)]
|
2015-02-26 23:00:43 -06:00
|
|
|
#![feature(unique)]
|
2015-06-09 13:18:03 -05:00
|
|
|
#![feature(unsafe_no_drop_flag, filling_drop)]
|
|
|
|
#![feature(unsize)]
|
2015-07-09 23:57:21 -05:00
|
|
|
#![feature(core_slice_ext)]
|
2015-06-09 13:52:41 -05:00
|
|
|
|
2015-06-10 15:33:52 -05:00
|
|
|
#![cfg_attr(test, feature(test, alloc, rustc_private, box_raw))]
|
2015-02-05 09:51:17 -06:00
|
|
|
#![cfg_attr(all(not(feature = "external_funcs"), not(feature = "external_crate")),
|
|
|
|
feature(libc))]
|
|
|
|
|
2014-12-31 22:43:46 -06:00
|
|
|
#[macro_use]
|
|
|
|
extern crate core;
|
2014-12-25 18:38:07 -06:00
|
|
|
|
2014-12-26 16:28:04 -06:00
|
|
|
#[cfg(all(not(feature = "external_funcs"), not(feature = "external_crate")))]
|
2014-05-13 18:10:05 -05:00
|
|
|
extern crate libc;
|
|
|
|
|
|
|
|
// Allow testing this library
|
|
|
|
|
2015-01-06 11:24:46 -06:00
|
|
|
#[cfg(test)] #[macro_use] extern crate std;
|
|
|
|
#[cfg(test)] #[macro_use] extern crate log;
|
2014-05-13 18:10:05 -05:00
|
|
|
|
|
|
|
// Heaps provided for low-level allocation strategies
|
|
|
|
|
|
|
|
pub mod heap;
|
|
|
|
|
|
|
|
// Primitive types using the heaps above
|
|
|
|
|
2015-02-17 14:41:32 -06:00
|
|
|
// Need to conditionally define the mod from `boxed.rs` to avoid
|
|
|
|
// duplicating the lang-items when building in test cfg; but also need
|
|
|
|
// to allow code to have `use boxed::HEAP;`
|
|
|
|
// and `use boxed::Box;` declarations.
|
2014-05-13 18:10:05 -05:00
|
|
|
#[cfg(not(test))]
|
2014-07-10 16:19:17 -05:00
|
|
|
pub mod boxed;
|
2015-01-20 14:57:56 -06:00
|
|
|
#[cfg(test)]
|
2015-02-17 14:41:32 -06:00
|
|
|
mod boxed { pub use std::boxed::{Box, HEAP}; }
|
|
|
|
#[cfg(test)]
|
2015-01-20 14:57:56 -06:00
|
|
|
mod boxed_test;
|
2014-05-13 18:10:05 -05:00
|
|
|
pub mod arc;
|
|
|
|
pub mod rc;
|
2015-07-09 23:57:21 -05:00
|
|
|
pub mod raw_vec;
|
2014-05-13 18:10:05 -05:00
|
|
|
|
2014-10-28 16:06:06 -05:00
|
|
|
/// Common out-of-memory routine
|
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
2015-06-09 13:52:41 -05:00
|
|
|
#[unstable(feature = "oom", reason = "not a scrutinized interface")]
|
2014-10-28 16:06:06 -05:00
|
|
|
pub fn oom() -> ! {
|
2014-06-14 01:35:54 -05:00
|
|
|
// FIXME(#14674): This really needs to do something other than just abort
|
|
|
|
// here, but any printing done must be *guaranteed* to not
|
|
|
|
// allocate.
|
|
|
|
unsafe { core::intrinsics::abort() }
|
|
|
|
}
|