about summary refs log tree commit diff
path: root/library/alloc/src/raw_vec.rs
AgeCommit message (Collapse)AuthorLines
2023-01-22simplify layout calculations in rawvecThe 8472-5/+12
2022-09-22Make ZST checks in core/alloc more readableScott McMurray-6/+6
There's a bunch of these checks because of special handing for ZSTs in various unsafe implementations of stuff. This lets them be `T::IS_ZST` instead of `mem::size_of::<T>() == 0` every time, making them both more readable and more terse. *Not* proposed for stabilization at this time. Would be `pub(crate)` except `alloc` wants to use it too. (And while it doesn't matter now, if we ever get something like 85836 making it a const can help codegen be simpler.)
2022-06-29alloc: fix `no_global_oom_handling` warningsMiguel Ojeda-0/+1
Rust 1.62.0 introduced a couple new `unused_imports` warnings in `no_global_oom_handling` builds, making a total of 5 warnings: ```txt warning: unused import: `Unsize` --> library/alloc/src/boxed/thin.rs:6:33 | 6 | use core::marker::{PhantomData, Unsize}; | ^^^^^^ | = note: `#[warn(unused_imports)]` on by default warning: unused import: `from_fn` --> library/alloc/src/string.rs:51:18 | 51 | use core::iter::{from_fn, FusedIterator}; | ^^^^^^^ warning: unused import: `core::ops::Deref` --> library/alloc/src/vec/into_iter.rs:12:5 | 12 | use core::ops::Deref; | ^^^^^^^^^^^^^^^^ warning: associated function `shrink` is never used --> library/alloc/src/raw_vec.rs:424:8 | 424 | fn shrink(&mut self, cap: usize) -> Result<(), TryReserveError> { | ^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: associated function `forget_remaining_elements` is never used --> library/alloc/src/vec/into_iter.rs:126:19 | 126 | pub(crate) fn forget_remaining_elements(&mut self) { | ^^^^^^^^^^^^^^^^^^^^^^^^^ ``` This patch cleans them so that projects compiling `alloc` without infallible allocations do not see the warnings. It also enables the use of `-Dwarnings`. The couple `dead_code` ones may be reverted when some fallible allocation support starts using them. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2022-05-13Remove some unnecessary `rustc_allow_const_fn_unstable` attributes.Nicholas Nethercote-1/+0
2022-04-06formattingSparkyPotato-1/+1
2022-04-06cleanupSparkyPotato-8/+2
2022-04-06fix Vec leak with 0 capacitySparkyPotato-0/+7
2022-02-12Fix typoAlphyr-1/+1
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-02-12Fix `shrink` and `capacity_from_bytes`Benoît du Garreau-2/+3
2022-02-12Fix a layout miscalculation in alloc::RawVecBenoît du Garreau-3/+1
2022-01-19Improve estimation of capacity in Vec::from_iterAngelicosPhosphoros-1/+1
Closes #48994
2021-12-22RawVec: don't recompute capacity after allocating.Nicholas Nethercote-17/+18
Currently it sets the capacity to `ptr.len() / mem::size_of::<T>()` after any buffer allocation/reallocation. This would be useful if allocators ever returned a `NonNull<[u8]>` with a size larger than requested. But this never happens, so it's not useful. Removing this slightly reduces the size of generated LLVM IR, and slightly speeds up the hot path of `RawVec` growth.
2021-11-30Introduce `RawVec::reserve_for_push`.Nicholas Nethercote-0/+8
If `Vec::push`'s capacity check fails it calls `RawVec::reserve`, which then also does a capacity check. This commit introduces `reserve_for_push` which skips the redundant capacity check, for some slight compile time speed-ups. I tried lots of minor variations on this, e.g. different inlining attributes. This was the best one I could find.
2021-11-03Make RawVec private to allocThe8472-57/+4
RawVec was previously exposed for compiler-internal use (libarena specifically) in 1acbb0a9350560d951359cc359361b87992a6f2b Since it is unstable, doc-hidden and has no associated tracking issue it was never meant for public use. And since it is no longer used outside alloc itself it can be made private again. Also remove some functions that are dead due to lack of internal users.
2021-10-10Add #[must_use] to alloc constructorsJohn Kugelman-0/+3
2021-08-28Fix a typo in raw_vecterrarier2111-1/+1
2021-07-24Hide allocator details from TryReserveErrorKornel-10/+10
2021-06-30alloc: `RawVec<T, A>::shrink` can be in `no_global_oom_handling`.Miguel Ojeda-1/+0
Found in https://github.com/Rust-for-Linux/linux/pull/402. Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
2021-05-05alloc: Add unstable Cfg feature `no-global_oom_handling`John Ericson-1/+15
For certain sorts of systems, programming, it's deemed essential that all allocation failures be explicitly handled where they occur. For example, see Linus Torvald's opinion in [1]. Merely not calling global panic handlers, or always `try_reserving` first (for vectors), is not deemed good enough, because the mere presence of the global OOM handlers is burdens static analysis. One option for these projects to use rust would just be to skip `alloc`, rolling their own allocation abstractions. But this would, in my opinion be a real shame. `alloc` has a few `try_*` methods already, and we could easily have more. Features like custom allocator support also demonstrate and existing to support diverse use-cases with the same abstractions. A natural way to add such a feature flag would a Cargo feature, but there are currently uncertainties around how std library crate's Cargo features may or not be stable, so to avoid any risk of stabilizing by mistake we are going with a more low-level "raw cfg" token, which cannot be interacted with via Cargo alone. Note also that since there is no notion of "default cfg tokens" outside of Cargo features, we have to invert the condition from `global_oom_handling` to to `not(no_global_oom_handling)`. This breaks the monotonicity that would be important for a Cargo feature (i.e. turning on more features should never break compatibility), but it doesn't matter for raw cfg tokens which are not intended to be "constraint solved" by Cargo or anything else. To support this use-case we create a new feature, "global-oom-handling", on by default, and put the global OOM handler infra and everything else it that depends on it behind it. By default, nothing is changed, but users concerned about global handling can make sure it is disabled, and be confident that all OOM handling is local and explicit. For this first iteration, non-flat collections are outright disabled. `Vec` and `String` don't yet have `try_*` allocation methods, but are kept anyways since they can be oom-safely created "from parts", and we hope to add those `try_` methods in the future. [1]: https://lore.kernel.org/lkml/CAHk-=wh_sNLoz84AUUzuqXEsYH35u=8HV3vK-jbRbJ_B-JjGrg@mail.gmail.com/
2021-04-25get rid of min_const_fn references in library/ and rustdocRalf Jung-10/+4
2021-03-21fmt, change to coldBen Kimock-2/+6
2021-03-21Mark RawVec::reserve as inline and outline the resizing logicBen Kimock-1/+13
2021-01-26Auto merge of #79113 - andjo403:raw_vec_ptr, r=m-ou-sebors-0/+1
mark raw_vec::ptr with inline when a lot of vectors is used in a enum as in the example in #66617 if this function is not inlined and multiple cgus is used this results in huge compile times. with this fix the compile time is 6s from minutes for the example in #66617. I did not have the patience to wait for it to compile for more then 3 min.
2021-01-21Enforce statically that `MIN_NON_ZERO_CAP` is calculated at compile timeJoshua Nelson-16/+14
Previously, it would usually get computed by LLVM, but this enforces it.
2020-12-15Auto merge of #78682 - glandium:issue78471, r=lcnrbors-0/+1
Do not inline finish_grow Fixes #78471. Looking at libgkrust.a in Firefox, the sizes for the `gkrust.*.o` file is: - 18584816 (text) 582418 (data) with unmodified master - 17937659 (text) 582554 (data) with #72227 reverted - 17968228 (text) 582858 (data) with `#[inline(never)]` on `grow_amortized` and `grow_exact`, but that has some performance consequences - 17927760 (text) 582322 (data) with this change So in terms of size, at least in the case of Firefox, this patch more than undoes the regression. I don't think it should affect performance, but we'll see.
2020-12-08Do not inline finish_growMike Hommey-0/+1
We also change the specialization of `SpecFromIterNested::from_iter` for `TrustedLen` to use `Vec::with_capacity` when the iterator has a proper size hint, instead of `Vec::new`, avoiding calls to `grow_*` and thus `finish_grow` in some fully inlinable cases, which would regress with this change. Fixes #78471.
2020-12-04 Rename `AllocRef` to `Allocator` and `(de)alloc` to `(de)allocate`Tim Diekmann-12/+12
2020-11-22Auto merge of #79219 - shepmaster:beta-bump, r=Mark-Simulacrumbors-2/+1
Bump bootstrap compiler version r? `@Mark-Simulacrum` /cc `@pietroalbini`
2020-11-19Bump bootstrap compiler versionJake Goulding-2/+1
2020-11-18Add support for custom allocators in `Vec`Tim Diekmann-6/+1
2020-11-16mark raw_vec::ptr with inlineAndreas Jonson-0/+1
2020-11-16Rollup merge of #77691 - exrook:rename-layouterr, r=KodrAusMara Bos-2/+2
Rename/Deprecate LayoutErr in favor of LayoutError Implements rust-lang/wg-allocators#73. This patch renames LayoutErr to LayoutError, and uses a type alias to support users using the old name. The new name will be instantly stable in release 1.49 (current nightly), the type alias will become deprecated in release 1.51 (so that when the current nightly is 1.51, 1.49 will be stable). This is the only error type in `std` that ends in `Err` rather than `Error`, if this PR lands all stdlib error types will end in `Error` :smiling_face_with_three_hearts:
2020-10-25Merge remote-tracking branch 'upstream/master' into box-allocTim Diekmann-2/+3
2020-10-25Rollup merge of #78208 - liketechnik:issue-69399, r=oli-obkYuki Okushi-1/+2
replace `#[allow_internal_unstable]` with `#[rustc_allow_const_fn_unstable]` for `const fn`s `#[allow_internal_unstable]` is currently used to side-step feature gate and stability checks. While it was originally only meant to be used only on macros, its use was expanded to `const fn`s. This pr adds stricter checks for the usage of `#[allow_internal_unstable]` (only on macros) and introduces the `#[rustc_allow_const_fn_unstable]` attribute for usage on `const fn`s. This pr does not change any of the functionality associated with the use of `#[allow_internal_unstable]` on macros or the usage of `#[rustc_allow_const_fn_unstable]` (instead of `#[allow_internal_unstable]`) on `const fn`s (see https://github.com/rust-lang/rust/issues/69399#issuecomment-712911540). Note: The check for `#[rustc_allow_const_fn_unstable]` currently only validates that the attribute is used on a function, because I don't know how I would check if the function is a `const fn` at the place of the check. I therefore openend this as a 'draft pull request'. Closes rust-lang/rust#69399 r? @oli-obk
2020-10-21switch allow_internal_unstable const fns to rustc_allow_const_fn_unstableFlorian Warzecha-1/+2
2020-10-15Following #74010 by converting some newer cases of backticked O notations to ↵Ryan Scott-1/+1
be italicized
2020-10-08Rename LayoutErr to LayoutError outside of coreJacob Hughes-2/+2
2020-10-07Support custom allocators in `Box`Tim Diekmann-30/+30
Remove `Box::leak_with_alloc` Add leak-test for box with allocator Rename `AllocErr` to `AllocError` in leak-test Add `Box::alloc` and adjust examples to use the new API
2020-09-23Rollup merge of #76993 - blitzerr:alloc-ref, r=AmanieuDylan DPC-1/+1
Changing the alloc() to accept &self instead of &mut self Fixes: [#55](https://github.com/rust-lang/wg-allocators/issues/55) This is the first cut. It only makes the change for `alloc` method.
2020-09-22Update library functions with stability attributesDylan MacKenzie-0/+1
This may not be strictly minimal, but all unstable functions also need a `rustc_const_unstable` attribute.
2020-09-22removing &mut self for other methods of AllocRefblitzerr-1/+1
2020-09-21Rollup merge of #72734 - pickfire:liballoc, r=KodrAusecstatic-morse-15/+13
Reduce duplicate in liballoc reserve error handling Not sure if it affects compilation time.
2020-09-19Use `T::BITS` instead of `size_of::<T> * 8`.Mara Bos-1/+1
2020-08-22Reduce duplicate in liballoc reserve error handlingIvan Tham-15/+13
2020-08-20Assume same alignment in `RawVec`Tim Diekmann-1/+6
2020-08-20Add debug assertion for equal alignment in RawVecTim Diekmann-0/+1
2020-08-19Allow reallocation to different alignmentTim Diekmann-7/+6
2020-08-09Rollup merge of #75151 - pickfire:patch-4, r=LukasKalbertodtYuki Okushi-4/+6
Consistent variable name alloc for raw_vec
2020-08-04Replace `Memoryblock` with `NonNull<[u8]>`Tim Diekmann-15/+15
2020-08-04Consistent variable name alloc for raw_vecIvan Tham-4/+6