]> git.lizzy.rs Git - rust.git/blob - src/liballoc/lib.rs
rollup merge of #21438: taralx/kill-racycell
[rust.git] / src / liballoc / lib.rs
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 //! # The Rust core allocation library
12 //!
13 //! This is the lowest level library through which allocation in Rust can be
14 //! performed.
15 //!
16 //! This library, like libcore, is not intended for general usage, but rather as
17 //! a building block of other libraries. The types and interfaces in this
18 //! library are reexported through the [standard library](../std/index.html),
19 //! and should not be used through this library.
20 //!
21 //! Currently, there are four major definitions in this library.
22 //!
23 //! ## Boxed values
24 //!
25 //! The [`Box`](boxed/index.html) type is the core owned pointer type in Rust.
26 //! There can only be one owner of a `Box`, and the owner can decide to mutate
27 //! the contents, which live on the heap.
28 //!
29 //! This type can be sent among tasks efficiently as the size of a `Box` value
30 //! is the same as that of a pointer. Tree-like data structures are often built
31 //! with boxes because each node often has only one owner, the parent.
32 //!
33 //! ## Reference counted pointers
34 //!
35 //! The [`Rc`](rc/index.html) type is a non-threadsafe reference-counted pointer
36 //! type intended for sharing memory within a task. An `Rc` pointer wraps a
37 //! type, `T`, and only allows access to `&T`, a shared reference.
38 //!
39 //! This type is useful when inherited mutability (such as using `Box`) is too
40 //! constraining for an application, and is often paired with the `Cell` or
41 //! `RefCell` types in order to allow mutation.
42 //!
43 //! ## Atomically reference counted pointers
44 //!
45 //! The [`Arc`](arc/index.html) type is the threadsafe equivalent of the `Rc`
46 //! type. It provides all the same functionality of `Rc`, except it requires
47 //! that the contained type `T` is shareable. Additionally, `Arc<T>` is itself
48 //! sendable while `Rc<T>` is not.
49 //!
50 //! This types allows for shared access to the contained data, and is often
51 //! paired with synchronization primitives such as mutexes to allow mutation of
52 //! shared resources.
53 //!
54 //! ## Heap interfaces
55 //!
56 //! The [`heap`](heap/index.html) module defines the low-level interface to the
57 //! default global allocator. It is not compatible with the libc allocator API.
58
59 #![crate_name = "alloc"]
60 #![unstable]
61 #![staged_api]
62 #![crate_type = "rlib"]
63 #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
64        html_favicon_url = "http://www.rust-lang.org/favicon.ico",
65        html_root_url = "http://doc.rust-lang.org/nightly/")]
66
67 #![no_std]
68 #![allow(unknown_features)]
69 #![allow(unstable)]
70 #![feature(lang_items, unsafe_destructor)]
71 #![feature(box_syntax)]
72 #![feature(optin_builtin_traits)]
73 // FIXME(#21363) remove `old_impl_check` when bug is fixed
74 #![feature(old_impl_check)]
75 #![allow(unknown_features)] #![feature(int_uint)]
76
77 #[macro_use]
78 extern crate core;
79
80 #[cfg(all(not(feature = "external_funcs"), not(feature = "external_crate")))]
81 extern crate libc;
82
83 // Allow testing this library
84
85 #[cfg(test)] #[macro_use] extern crate std;
86 #[cfg(test)] #[macro_use] extern crate log;
87
88 // Heaps provided for low-level allocation strategies
89
90 pub mod heap;
91
92 // Primitive types using the heaps above
93
94 #[cfg(not(test))]
95 pub mod boxed;
96 pub mod arc;
97 pub mod rc;
98
99 /// Common out-of-memory routine
100 #[cold]
101 #[inline(never)]
102 pub fn oom() -> ! {
103     // FIXME(#14674): This really needs to do something other than just abort
104     //                here, but any printing done must be *guaranteed* to not
105     //                allocate.
106     unsafe { core::intrinsics::abort() }
107 }
108
109 // FIXME(#14344): When linking liballoc with libstd, this library will be linked
110 //                as an rlib (it only exists as an rlib). It turns out that an
111 //                optimized standard library doesn't actually use *any* symbols
112 //                from this library. Everything is inlined and optimized away.
113 //                This means that linkers will actually omit the object for this
114 //                file, even though it may be needed in the future.
115 //
116 //                To get around this for now, we define a dummy symbol which
117 //                will never get inlined so the stdlib can call it. The stdlib's
118 //                reference to this symbol will cause this library's object file
119 //                to get linked in to libstd successfully (the linker won't
120 //                optimize it out).
121 #[doc(hidden)]
122 pub fn fixme_14344_be_sure_to_link_to_collections() {}
123
124 #[cfg(not(test))]
125 #[doc(hidden)]
126 mod std {
127     pub use core::fmt;
128     pub use core::option;
129 }