xref: /openbmc/linux/rust/alloc/lib.rs (revision 695c312ec5a68e4373d063ee649c7b925ffb5da7)
1057b8d25SMiguel Ojeda // SPDX-License-Identifier: Apache-2.0 OR MIT
2057b8d25SMiguel Ojeda 
3753dece8SMiguel Ojeda //! # The Rust core allocation and collections library
4753dece8SMiguel Ojeda //!
5753dece8SMiguel Ojeda //! This library provides smart pointers and collections for managing
6753dece8SMiguel Ojeda //! heap-allocated values.
7753dece8SMiguel Ojeda //!
83ed03f4dSMiguel Ojeda //! This library, like core, normally doesn’t need to be used directly
9753dece8SMiguel Ojeda //! since its contents are re-exported in the [`std` crate](../std/index.html).
10753dece8SMiguel Ojeda //! Crates that use the `#![no_std]` attribute however will typically
11753dece8SMiguel Ojeda //! not depend on `std`, so they’d use this crate instead.
12753dece8SMiguel Ojeda //!
13753dece8SMiguel Ojeda //! ## Boxed values
14753dece8SMiguel Ojeda //!
15753dece8SMiguel Ojeda //! The [`Box`] type is a smart pointer type. There can only be one owner of a
16753dece8SMiguel Ojeda //! [`Box`], and the owner can decide to mutate the contents, which live on the
17753dece8SMiguel Ojeda //! heap.
18753dece8SMiguel Ojeda //!
19753dece8SMiguel Ojeda //! This type can be sent among threads efficiently as the size of a `Box` value
20753dece8SMiguel Ojeda //! is the same as that of a pointer. Tree-like data structures are often built
21753dece8SMiguel Ojeda //! with boxes because each node often has only one owner, the parent.
22753dece8SMiguel Ojeda //!
23753dece8SMiguel Ojeda //! ## Reference counted pointers
24753dece8SMiguel Ojeda //!
25753dece8SMiguel Ojeda //! The [`Rc`] type is a non-threadsafe reference-counted pointer type intended
26753dece8SMiguel Ojeda //! for sharing memory within a thread. An [`Rc`] pointer wraps a type, `T`, and
27753dece8SMiguel Ojeda //! only allows access to `&T`, a shared reference.
28753dece8SMiguel Ojeda //!
29753dece8SMiguel Ojeda //! This type is useful when inherited mutability (such as using [`Box`]) is too
30753dece8SMiguel Ojeda //! constraining for an application, and is often paired with the [`Cell`] or
31753dece8SMiguel Ojeda //! [`RefCell`] types in order to allow mutation.
32753dece8SMiguel Ojeda //!
33753dece8SMiguel Ojeda //! ## Atomically reference counted pointers
34753dece8SMiguel Ojeda //!
35753dece8SMiguel Ojeda //! The [`Arc`] type is the threadsafe equivalent of the [`Rc`] type. It
36753dece8SMiguel Ojeda //! provides all the same functionality of [`Rc`], except it requires that the
37753dece8SMiguel Ojeda //! contained type `T` is shareable. Additionally, [`Arc<T>`][`Arc`] is itself
38753dece8SMiguel Ojeda //! sendable while [`Rc<T>`][`Rc`] is not.
39753dece8SMiguel Ojeda //!
40753dece8SMiguel Ojeda //! This type allows for shared access to the contained data, and is often
41753dece8SMiguel Ojeda //! paired with synchronization primitives such as mutexes to allow mutation of
42753dece8SMiguel Ojeda //! shared resources.
43753dece8SMiguel Ojeda //!
44753dece8SMiguel Ojeda //! ## Collections
45753dece8SMiguel Ojeda //!
46753dece8SMiguel Ojeda //! Implementations of the most common general purpose data structures are
47753dece8SMiguel Ojeda //! defined in this library. They are re-exported through the
48753dece8SMiguel Ojeda //! [standard collections library](../std/collections/index.html).
49753dece8SMiguel Ojeda //!
50753dece8SMiguel Ojeda //! ## Heap interfaces
51753dece8SMiguel Ojeda //!
52753dece8SMiguel Ojeda //! The [`alloc`](alloc/index.html) module defines the low-level interface to the
53753dece8SMiguel Ojeda //! default global allocator. It is not compatible with the libc allocator API.
54753dece8SMiguel Ojeda //!
55753dece8SMiguel Ojeda //! [`Arc`]: sync
56753dece8SMiguel Ojeda //! [`Box`]: boxed
57753dece8SMiguel Ojeda //! [`Cell`]: core::cell
58753dece8SMiguel Ojeda //! [`Rc`]: rc
59753dece8SMiguel Ojeda //! [`RefCell`]: core::cell
60753dece8SMiguel Ojeda 
619b33bb25SMiguel Ojeda // To run alloc tests without x.py without ending up with two copies of alloc, Miri needs to be
629b33bb25SMiguel Ojeda // able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
63*73596f5aSMiguel Ojeda // rustc itself never sets the feature, so this line has no effect there.
649b33bb25SMiguel Ojeda #![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
659b33bb25SMiguel Ojeda //
66753dece8SMiguel Ojeda #![allow(unused_attributes)]
67753dece8SMiguel Ojeda #![stable(feature = "alloc", since = "1.36.0")]
68753dece8SMiguel Ojeda #![doc(
69753dece8SMiguel Ojeda     html_playground_url = "https://play.rust-lang.org/",
70753dece8SMiguel Ojeda     issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
71753dece8SMiguel Ojeda     test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
72753dece8SMiguel Ojeda )]
73753dece8SMiguel Ojeda #![doc(cfg_hide(
74753dece8SMiguel Ojeda     not(test),
75753dece8SMiguel Ojeda     not(any(test, bootstrap)),
76753dece8SMiguel Ojeda     any(not(feature = "miri-test-libstd"), test, doctest),
77753dece8SMiguel Ojeda     no_global_oom_handling,
78753dece8SMiguel Ojeda     not(no_global_oom_handling),
793ed03f4dSMiguel Ojeda     not(no_rc),
803ed03f4dSMiguel Ojeda     not(no_sync),
81753dece8SMiguel Ojeda     target_has_atomic = "ptr"
82753dece8SMiguel Ojeda ))]
83753dece8SMiguel Ojeda #![no_std]
84753dece8SMiguel Ojeda #![needs_allocator]
85753dece8SMiguel Ojeda // Lints:
86753dece8SMiguel Ojeda #![deny(unsafe_op_in_unsafe_fn)]
873ed03f4dSMiguel Ojeda #![deny(fuzzy_provenance_casts)]
88753dece8SMiguel Ojeda #![warn(deprecated_in_future)]
89753dece8SMiguel Ojeda #![warn(missing_debug_implementations)]
90753dece8SMiguel Ojeda #![warn(missing_docs)]
91753dece8SMiguel Ojeda #![allow(explicit_outlives_requirements)]
9289eed1abSMiguel Ojeda #![warn(multiple_supertrait_upcastable)]
93*73596f5aSMiguel Ojeda #![cfg_attr(not(bootstrap), allow(internal_features))]
94*73596f5aSMiguel Ojeda #![cfg_attr(not(bootstrap), allow(rustdoc::redundant_explicit_links))]
95753dece8SMiguel Ojeda //
96753dece8SMiguel Ojeda // Library features:
9789eed1abSMiguel Ojeda // tidy-alphabetical-start
9889eed1abSMiguel Ojeda #![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
9989eed1abSMiguel Ojeda #![cfg_attr(not(no_global_oom_handling), feature(const_btree_len))]
10089eed1abSMiguel Ojeda #![cfg_attr(test, feature(is_sorted))]
10189eed1abSMiguel Ojeda #![cfg_attr(test, feature(new_uninit))]
102753dece8SMiguel Ojeda #![feature(alloc_layout_extra)]
103753dece8SMiguel Ojeda #![feature(allocator_api)]
104753dece8SMiguel Ojeda #![feature(array_chunks)]
1053ed03f4dSMiguel Ojeda #![feature(array_into_iter_constructors)]
106753dece8SMiguel Ojeda #![feature(array_methods)]
107753dece8SMiguel Ojeda #![feature(array_windows)]
10889eed1abSMiguel Ojeda #![feature(ascii_char)]
109753dece8SMiguel Ojeda #![feature(assert_matches)]
110753dece8SMiguel Ojeda #![feature(async_iterator)]
111753dece8SMiguel Ojeda #![feature(coerce_unsized)]
112753dece8SMiguel Ojeda #![feature(const_align_of_val)]
11389eed1abSMiguel Ojeda #![feature(const_box)]
11489eed1abSMiguel Ojeda #![cfg_attr(not(no_borrow), feature(const_cow_is_borrowed))]
11589eed1abSMiguel Ojeda #![feature(const_eval_select)]
116753dece8SMiguel Ojeda #![feature(const_maybe_uninit_as_mut_ptr)]
11789eed1abSMiguel Ojeda #![feature(const_maybe_uninit_write)]
11889eed1abSMiguel Ojeda #![feature(const_maybe_uninit_zeroed)]
11989eed1abSMiguel Ojeda #![feature(const_pin)]
120753dece8SMiguel Ojeda #![feature(const_refs_to_cell)]
12189eed1abSMiguel Ojeda #![feature(const_size_of_val)]
12289eed1abSMiguel Ojeda #![feature(const_waker)]
123753dece8SMiguel Ojeda #![feature(core_intrinsics)]
1243ed03f4dSMiguel Ojeda #![feature(core_panic)]
125753dece8SMiguel Ojeda #![feature(dispatch_from_dyn)]
1263ed03f4dSMiguel Ojeda #![feature(error_generic_member_access)]
1273ed03f4dSMiguel Ojeda #![feature(error_in_core)]
128753dece8SMiguel Ojeda #![feature(exact_size_is_empty)]
129753dece8SMiguel Ojeda #![feature(extend_one)]
130753dece8SMiguel Ojeda #![feature(fmt_internals)]
131753dece8SMiguel Ojeda #![feature(fn_traits)]
132753dece8SMiguel Ojeda #![feature(hasher_prefixfree_extras)]
1333ed03f4dSMiguel Ojeda #![feature(inline_const)]
134753dece8SMiguel Ojeda #![feature(inplace_iteration)]
135753dece8SMiguel Ojeda #![feature(iter_advance_by)]
1363ed03f4dSMiguel Ojeda #![feature(iter_next_chunk)]
1373ed03f4dSMiguel Ojeda #![feature(iter_repeat_n)]
138753dece8SMiguel Ojeda #![feature(layout_for_ptr)]
139753dece8SMiguel Ojeda #![feature(maybe_uninit_slice)]
1403ed03f4dSMiguel Ojeda #![feature(maybe_uninit_uninit_array)]
1413ed03f4dSMiguel Ojeda #![feature(maybe_uninit_uninit_array_transpose)]
142753dece8SMiguel Ojeda #![feature(pattern)]
1433ed03f4dSMiguel Ojeda #![feature(pointer_byte_offsets)]
144753dece8SMiguel Ojeda #![feature(ptr_internals)]
145753dece8SMiguel Ojeda #![feature(ptr_metadata)]
146753dece8SMiguel Ojeda #![feature(ptr_sub_ptr)]
147753dece8SMiguel Ojeda #![feature(receiver_trait)]
1483ed03f4dSMiguel Ojeda #![feature(saturating_int_impl)]
149753dece8SMiguel Ojeda #![feature(set_ptr_value)]
1503ed03f4dSMiguel Ojeda #![feature(sized_type_properties)]
1513ed03f4dSMiguel Ojeda #![feature(slice_from_ptr_range)]
152753dece8SMiguel Ojeda #![feature(slice_group_by)]
153753dece8SMiguel Ojeda #![feature(slice_ptr_get)]
154753dece8SMiguel Ojeda #![feature(slice_ptr_len)]
155753dece8SMiguel Ojeda #![feature(slice_range)]
15689eed1abSMiguel Ojeda #![feature(std_internals)]
157753dece8SMiguel Ojeda #![feature(str_internals)]
158753dece8SMiguel Ojeda #![feature(strict_provenance)]
159753dece8SMiguel Ojeda #![feature(trusted_len)]
160753dece8SMiguel Ojeda #![feature(trusted_random_access)]
161753dece8SMiguel Ojeda #![feature(try_trait_v2)]
1623ed03f4dSMiguel Ojeda #![feature(tuple_trait)]
163753dece8SMiguel Ojeda #![feature(unchecked_math)]
164753dece8SMiguel Ojeda #![feature(unicode_internals)]
165753dece8SMiguel Ojeda #![feature(unsize)]
1663ed03f4dSMiguel Ojeda #![feature(utf8_chunks)]
16789eed1abSMiguel Ojeda // tidy-alphabetical-end
168753dece8SMiguel Ojeda //
169753dece8SMiguel Ojeda // Language features:
17089eed1abSMiguel Ojeda // tidy-alphabetical-start
17189eed1abSMiguel Ojeda #![cfg_attr(not(test), feature(generator_trait))]
17289eed1abSMiguel Ojeda #![cfg_attr(test, feature(panic_update_hook))]
17389eed1abSMiguel Ojeda #![cfg_attr(test, feature(test))]
174753dece8SMiguel Ojeda #![feature(allocator_internals)]
175753dece8SMiguel Ojeda #![feature(allow_internal_unstable)]
176753dece8SMiguel Ojeda #![feature(associated_type_bounds)]
17789eed1abSMiguel Ojeda #![feature(c_unwind)]
178753dece8SMiguel Ojeda #![feature(cfg_sanitize)]
179753dece8SMiguel Ojeda #![feature(const_mut_refs)]
180753dece8SMiguel Ojeda #![feature(const_precise_live_drops)]
18189eed1abSMiguel Ojeda #![feature(const_ptr_write)]
182753dece8SMiguel Ojeda #![feature(const_trait_impl)]
183753dece8SMiguel Ojeda #![feature(const_try)]
184753dece8SMiguel Ojeda #![feature(dropck_eyepatch)]
185753dece8SMiguel Ojeda #![feature(exclusive_range_pattern)]
186753dece8SMiguel Ojeda #![feature(fundamental)]
187753dece8SMiguel Ojeda #![feature(hashmap_internals)]
188753dece8SMiguel Ojeda #![feature(lang_items)]
189753dece8SMiguel Ojeda #![feature(min_specialization)]
19089eed1abSMiguel Ojeda #![feature(multiple_supertrait_upcastable)]
191753dece8SMiguel Ojeda #![feature(negative_impls)]
192753dece8SMiguel Ojeda #![feature(never_type)]
19389eed1abSMiguel Ojeda #![feature(pointer_is_aligned)]
194753dece8SMiguel Ojeda #![feature(rustc_allow_const_fn_unstable)]
195753dece8SMiguel Ojeda #![feature(rustc_attrs)]
196753dece8SMiguel Ojeda #![feature(slice_internals)]
197753dece8SMiguel Ojeda #![feature(staged_api)]
1983ed03f4dSMiguel Ojeda #![feature(stmt_expr_attributes)]
199753dece8SMiguel Ojeda #![feature(unboxed_closures)]
200753dece8SMiguel Ojeda #![feature(unsized_fn_params)]
2013ed03f4dSMiguel Ojeda #![feature(with_negative_coherence)]
20289eed1abSMiguel Ojeda // tidy-alphabetical-end
203753dece8SMiguel Ojeda //
204753dece8SMiguel Ojeda // Rustdoc features:
205753dece8SMiguel Ojeda #![feature(doc_cfg)]
206753dece8SMiguel Ojeda #![feature(doc_cfg_hide)]
207753dece8SMiguel Ojeda // Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
208753dece8SMiguel Ojeda // blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
209753dece8SMiguel Ojeda // that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs
210753dece8SMiguel Ojeda // from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
211753dece8SMiguel Ojeda #![feature(intra_doc_pointers)]
212753dece8SMiguel Ojeda 
213753dece8SMiguel Ojeda // Allow testing this library
214753dece8SMiguel Ojeda #[cfg(test)]
215753dece8SMiguel Ojeda #[macro_use]
216753dece8SMiguel Ojeda extern crate std;
217753dece8SMiguel Ojeda #[cfg(test)]
218753dece8SMiguel Ojeda extern crate test;
2193ed03f4dSMiguel Ojeda #[cfg(test)]
2203ed03f4dSMiguel Ojeda mod testing;
221753dece8SMiguel Ojeda 
222753dece8SMiguel Ojeda // Module with internal macros used by other modules (needs to be included before other modules).
223057b8d25SMiguel Ojeda #[cfg(not(no_macros))]
224753dece8SMiguel Ojeda #[macro_use]
225753dece8SMiguel Ojeda mod macros;
226753dece8SMiguel Ojeda 
227753dece8SMiguel Ojeda mod raw_vec;
228753dece8SMiguel Ojeda 
229753dece8SMiguel Ojeda // Heaps provided for low-level allocation strategies
230753dece8SMiguel Ojeda 
231753dece8SMiguel Ojeda pub mod alloc;
232753dece8SMiguel Ojeda 
233753dece8SMiguel Ojeda // Primitive types using the heaps above
234753dece8SMiguel Ojeda 
235753dece8SMiguel Ojeda // Need to conditionally define the mod from `boxed.rs` to avoid
236753dece8SMiguel Ojeda // duplicating the lang-items when building in test cfg; but also need
237753dece8SMiguel Ojeda // to allow code to have `use boxed::Box;` declarations.
238753dece8SMiguel Ojeda #[cfg(not(test))]
239753dece8SMiguel Ojeda pub mod boxed;
240753dece8SMiguel Ojeda #[cfg(test)]
241753dece8SMiguel Ojeda mod boxed {
242753dece8SMiguel Ojeda     pub use std::boxed::Box;
243753dece8SMiguel Ojeda }
2448909a80eSMiguel Ojeda #[cfg(not(no_borrow))]
245753dece8SMiguel Ojeda pub mod borrow;
246753dece8SMiguel Ojeda pub mod collections;
2473ed03f4dSMiguel Ojeda #[cfg(all(not(no_rc), not(no_sync), not(no_global_oom_handling)))]
248753dece8SMiguel Ojeda pub mod ffi;
249057b8d25SMiguel Ojeda #[cfg(not(no_fmt))]
250753dece8SMiguel Ojeda pub mod fmt;
251057b8d25SMiguel Ojeda #[cfg(not(no_rc))]
252753dece8SMiguel Ojeda pub mod rc;
253753dece8SMiguel Ojeda pub mod slice;
254057b8d25SMiguel Ojeda #[cfg(not(no_str))]
255753dece8SMiguel Ojeda pub mod str;
256057b8d25SMiguel Ojeda #[cfg(not(no_string))]
257753dece8SMiguel Ojeda pub mod string;
2583ed03f4dSMiguel Ojeda #[cfg(all(not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
259753dece8SMiguel Ojeda pub mod sync;
2603ed03f4dSMiguel Ojeda #[cfg(all(not(no_global_oom_handling), not(no_rc), not(no_sync), target_has_atomic = "ptr"))]
261753dece8SMiguel Ojeda pub mod task;
262753dece8SMiguel Ojeda #[cfg(test)]
263753dece8SMiguel Ojeda mod tests;
264753dece8SMiguel Ojeda pub mod vec;
265753dece8SMiguel Ojeda 
266753dece8SMiguel Ojeda #[doc(hidden)]
267753dece8SMiguel Ojeda #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
268753dece8SMiguel Ojeda pub mod __export {
269753dece8SMiguel Ojeda     pub use core::format_args;
270753dece8SMiguel Ojeda }
2713ed03f4dSMiguel Ojeda 
2723ed03f4dSMiguel Ojeda #[cfg(test)]
2733ed03f4dSMiguel Ojeda #[allow(dead_code)] // Not used in all configurations
2743ed03f4dSMiguel Ojeda pub(crate) mod test_helpers {
2753ed03f4dSMiguel Ojeda     /// Copied from `std::test_helpers::test_rng`, since these tests rely on the
2763ed03f4dSMiguel Ojeda     /// seed not being the same for every RNG invocation too.
test_rng() -> rand_xorshift::XorShiftRng2773ed03f4dSMiguel Ojeda     pub(crate) fn test_rng() -> rand_xorshift::XorShiftRng {
2783ed03f4dSMiguel Ojeda         use std::hash::{BuildHasher, Hash, Hasher};
2793ed03f4dSMiguel Ojeda         let mut hasher = std::collections::hash_map::RandomState::new().build_hasher();
2803ed03f4dSMiguel Ojeda         std::panic::Location::caller().hash(&mut hasher);
2813ed03f4dSMiguel Ojeda         let hc64 = hasher.finish();
2823ed03f4dSMiguel Ojeda         let seed_vec =
2833ed03f4dSMiguel Ojeda             hc64.to_le_bytes().into_iter().chain(0u8..8).collect::<crate::vec::Vec<u8>>();
2843ed03f4dSMiguel Ojeda         let seed: [u8; 16] = seed_vec.as_slice().try_into().unwrap();
2853ed03f4dSMiguel Ojeda         rand::SeedableRng::from_seed(seed)
2863ed03f4dSMiguel Ojeda     }
2873ed03f4dSMiguel Ojeda }
288