Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / mfbt / Vector.h
blob380e27254867abdd41ced3fe7a5820d8858da79a
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* A type/length-parametrized vector class. */
9 #ifndef mozilla_Vector_h
10 #define mozilla_Vector_h
12 #include <new> // for placement new
13 #include <type_traits>
14 #include <utility>
16 #include "mozilla/Alignment.h"
17 #include "mozilla/AllocPolicy.h"
18 #include "mozilla/ArrayUtils.h" // for PointerRangeSize
19 #include "mozilla/Assertions.h"
20 #include "mozilla/Attributes.h"
21 #include "mozilla/MathAlgorithms.h"
22 #include "mozilla/MemoryReporting.h"
23 #include "mozilla/OperatorNewExtensions.h"
24 #include "mozilla/ReentrancyGuard.h"
25 #include "mozilla/Span.h"
26 #include "mozilla/TemplateLib.h"
28 namespace mozilla {
30 template <typename T, size_t N, class AllocPolicy>
31 class Vector;
33 namespace detail {
36 * Check that the given capacity wastes the minimal amount of space if
37 * allocated on the heap. This means that aCapacity*EltSize is as close to a
38 * power-of-two as possible. growStorageBy() is responsible for ensuring this.
40 template <size_t EltSize>
41 static bool CapacityHasExcessSpace(size_t aCapacity) {
42 size_t size = aCapacity * EltSize;
43 return RoundUpPow2(size) - size >= EltSize;
47 * AllocPolicy can optionally provide a `computeGrowth<T>(size_t aOldElts,
48 * size_t aIncr)` method that returns the new number of elements to allocate
49 * when the current capacity is `aOldElts` and `aIncr` more are being
50 * requested. If the AllocPolicy does not have such a method, a fallback
51 * will be used that mostly will just round the new requested capacity up to
52 * the next power of two, which results in doubling capacity for the most part.
54 * If the new size would overflow some limit, `computeGrowth` returns 0.
56 * A simpler way would be to make computeGrowth() part of the API for all
57 * AllocPolicy classes, but this turns out to be rather complex because
58 * mozalloc.h defines a very widely-used InfallibleAllocPolicy, and yet it
59 * can only be compiled in limited contexts, eg within `extern "C"` and with
60 * -std=c++11 rather than a later version. That makes the headers that are
61 * necessary for the computation unavailable (eg mfbt/MathAlgorithms.h).
64 // Fallback version.
65 template <size_t EltSize>
66 inline size_t GrowEltsByDoubling(size_t aOldElts, size_t aIncr) {
68 * When choosing a new capacity, its size in bytes should is as close to 2**N
69 * bytes as possible. 2**N-sized requests are best because they are unlikely
70 * to be rounded up by the allocator. Asking for a 2**N number of elements
71 * isn't as good, because if EltSize is not a power-of-two that would
72 * result in a non-2**N request size.
75 if (aIncr == 1) {
76 if (aOldElts == 0) {
77 return 1;
80 /* This case occurs in ~15--20% of the calls to Vector::growStorageBy. */
83 * Will aOldSize * 4 * sizeof(T) overflow? This condition limits a
84 * collection to 1GB of memory on a 32-bit system, which is a reasonable
85 * limit. It also ensures that
87 * static_cast<char*>(end()) - static_cast<char*>(begin())
89 * for a Vector doesn't overflow ptrdiff_t (see bug 510319).
91 if (MOZ_UNLIKELY(aOldElts &
92 mozilla::tl::MulOverflowMask<4 * EltSize>::value)) {
93 return 0;
97 * If we reach here, the existing capacity will have a size that is already
98 * as close to 2^N as sizeof(T) will allow. Just double the capacity, and
99 * then there might be space for one more element.
101 size_t newElts = aOldElts * 2;
102 if (CapacityHasExcessSpace<EltSize>(newElts)) {
103 newElts += 1;
105 return newElts;
108 /* This case occurs in ~2% of the calls to Vector::growStorageBy. */
109 size_t newMinCap = aOldElts + aIncr;
111 /* Did aOldElts + aIncr overflow? Will newMinCap * EltSize rounded up to the
112 * next power of two overflow PTRDIFF_MAX? */
113 if (MOZ_UNLIKELY(newMinCap < aOldElts ||
114 newMinCap & tl::MulOverflowMask<4 * EltSize>::value)) {
115 return 0;
118 size_t newMinSize = newMinCap * EltSize;
119 size_t newSize = RoundUpPow2(newMinSize);
120 return newSize / EltSize;
123 // Fallback version.
124 template <typename AP, size_t EltSize>
125 static size_t ComputeGrowth(size_t aOldElts, size_t aIncr, int) {
126 return GrowEltsByDoubling<EltSize>(aOldElts, aIncr);
129 // If the AllocPolicy provides its own computeGrowth<EltSize> implementation,
130 // use that.
131 template <typename AP, size_t EltSize>
132 static size_t ComputeGrowth(
133 size_t aOldElts, size_t aIncr,
134 decltype(std::declval<AP>().template computeGrowth<EltSize>(0, 0),
135 bool()) aOverloadSelector) {
136 size_t newElts = AP::template computeGrowth<EltSize>(aOldElts, aIncr);
137 MOZ_ASSERT(newElts <= PTRDIFF_MAX && newElts * EltSize <= PTRDIFF_MAX,
138 "invalid Vector size (see bug 510319)");
139 return newElts;
143 * This template class provides a default implementation for vector operations
144 * when the element type is not known to be a POD, as judged by IsPod.
146 template <typename T, size_t N, class AP, bool IsPod>
147 struct VectorImpl {
149 * Constructs an object in the uninitialized memory at *aDst with aArgs.
151 template <typename... Args>
152 MOZ_NONNULL(1)
153 static inline void new_(T* aDst, Args&&... aArgs) {
154 new (KnownNotNull, aDst) T(std::forward<Args>(aArgs)...);
157 /* Destroys constructed objects in the range [aBegin, aEnd). */
158 static inline void destroy(T* aBegin, T* aEnd) {
159 MOZ_ASSERT(aBegin <= aEnd);
160 for (T* p = aBegin; p < aEnd; ++p) {
161 p->~T();
165 /* Constructs objects in the uninitialized range [aBegin, aEnd). */
166 static inline void initialize(T* aBegin, T* aEnd) {
167 MOZ_ASSERT(aBegin <= aEnd);
168 for (T* p = aBegin; p < aEnd; ++p) {
169 new_(p);
174 * Copy-constructs objects in the uninitialized range
175 * [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
177 template <typename U>
178 static inline void copyConstruct(T* aDst, const U* aSrcStart,
179 const U* aSrcEnd) {
180 MOZ_ASSERT(aSrcStart <= aSrcEnd);
181 for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
182 new_(aDst, *p);
187 * Move-constructs objects in the uninitialized range
188 * [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
190 template <typename U>
191 static inline void moveConstruct(T* aDst, U* aSrcStart, U* aSrcEnd) {
192 MOZ_ASSERT(aSrcStart <= aSrcEnd);
193 for (U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
194 new_(aDst, std::move(*p));
199 * Copy-constructs objects in the uninitialized range [aDst, aDst+aN) from
200 * the same object aU.
202 template <typename U>
203 static inline void copyConstructN(T* aDst, size_t aN, const U& aU) {
204 for (T* end = aDst + aN; aDst < end; ++aDst) {
205 new_(aDst, aU);
210 * Grows the given buffer to have capacity aNewCap, preserving the objects
211 * constructed in the range [begin, end) and updating aV. Assumes that (1)
212 * aNewCap has not overflowed, and (2) multiplying aNewCap by sizeof(T) will
213 * not overflow.
215 [[nodiscard]] static inline bool growTo(Vector<T, N, AP>& aV,
216 size_t aNewCap) {
217 MOZ_ASSERT(!aV.usingInlineStorage());
218 MOZ_ASSERT(!CapacityHasExcessSpace<sizeof(T)>(aNewCap));
219 T* newbuf = aV.template pod_malloc<T>(aNewCap);
220 if (MOZ_UNLIKELY(!newbuf)) {
221 return false;
223 T* dst = newbuf;
224 T* src = aV.beginNoCheck();
225 for (; src < aV.endNoCheck(); ++dst, ++src) {
226 new_(dst, std::move(*src));
228 VectorImpl::destroy(aV.beginNoCheck(), aV.endNoCheck());
229 aV.free_(aV.mBegin, aV.mTail.mCapacity);
230 aV.mBegin = newbuf;
231 /* aV.mLength is unchanged. */
232 aV.mTail.mCapacity = aNewCap;
233 return true;
238 * This partial template specialization provides a default implementation for
239 * vector operations when the element type is known to be a POD, as judged by
240 * IsPod.
242 template <typename T, size_t N, class AP>
243 struct VectorImpl<T, N, AP, true> {
244 template <typename... Args>
245 MOZ_NONNULL(1)
246 static inline void new_(T* aDst, Args&&... aArgs) {
247 // Explicitly construct a local object instead of using a temporary since
248 // T(args...) will be treated like a C-style cast in the unary case and
249 // allow unsafe conversions. Both forms should be equivalent to an
250 // optimizing compiler.
251 T temp(std::forward<Args>(aArgs)...);
252 *aDst = temp;
255 static inline void destroy(T*, T*) {}
257 static inline void initialize(T* aBegin, T* aEnd) {
259 * You would think that memset would be a big win (or even break even)
260 * when we know T is a POD. But currently it's not. This is probably
261 * because |append| tends to be given small ranges and memset requires
262 * a function call that doesn't get inlined.
264 * memset(aBegin, 0, sizeof(T) * (aEnd - aBegin));
266 MOZ_ASSERT(aBegin <= aEnd);
267 for (T* p = aBegin; p < aEnd; ++p) {
268 new_(p);
272 template <typename U>
273 static inline void copyConstruct(T* aDst, const U* aSrcStart,
274 const U* aSrcEnd) {
276 * See above memset comment. Also, notice that copyConstruct is
277 * currently templated (T != U), so memcpy won't work without
278 * requiring T == U.
280 * memcpy(aDst, aSrcStart, sizeof(T) * (aSrcEnd - aSrcStart));
282 MOZ_ASSERT(aSrcStart <= aSrcEnd);
283 for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
284 new_(aDst, *p);
288 template <typename U>
289 static inline void moveConstruct(T* aDst, const U* aSrcStart,
290 const U* aSrcEnd) {
291 copyConstruct(aDst, aSrcStart, aSrcEnd);
294 static inline void copyConstructN(T* aDst, size_t aN, const T& aT) {
295 for (T* end = aDst + aN; aDst < end; ++aDst) {
296 new_(aDst, aT);
300 [[nodiscard]] static inline bool growTo(Vector<T, N, AP>& aV,
301 size_t aNewCap) {
302 MOZ_ASSERT(!aV.usingInlineStorage());
303 MOZ_ASSERT(!CapacityHasExcessSpace<sizeof(T)>(aNewCap));
304 T* newbuf =
305 aV.template pod_realloc<T>(aV.mBegin, aV.mTail.mCapacity, aNewCap);
306 if (MOZ_UNLIKELY(!newbuf)) {
307 return false;
309 aV.mBegin = newbuf;
310 /* aV.mLength is unchanged. */
311 aV.mTail.mCapacity = aNewCap;
312 return true;
316 // A struct for TestVector.cpp to access private internal fields.
317 // DO NOT DEFINE IN YOUR OWN CODE.
318 struct VectorTesting;
320 } // namespace detail
323 * STL-like container providing a short-lived, dynamic buffer. Vector calls the
324 * constructors/destructors of all elements stored in its internal buffer, so
325 * non-PODs may be safely used. Additionally, Vector will store the first N
326 * elements in-place before resorting to dynamic allocation.
328 * T requirements:
329 * - default and copy constructible, assignable, destructible
330 * - operations do not throw
331 * MinInlineCapacity requirements:
332 * - any value, however, MinInlineCapacity is clamped to min/max values
333 * AllocPolicy:
334 * - see "Allocation policies" in AllocPolicy.h (defaults to
335 * mozilla::MallocAllocPolicy)
337 * Vector is not reentrant: T member functions called during Vector member
338 * functions must not call back into the same object!
340 template <typename T, size_t MinInlineCapacity = 0,
341 class AllocPolicy = MallocAllocPolicy>
342 class MOZ_NON_PARAM Vector final : private AllocPolicy {
343 /* utilities */
344 static constexpr bool kElemIsPod =
345 std::is_trivial_v<T> && std::is_standard_layout_v<T>;
346 typedef detail::VectorImpl<T, MinInlineCapacity, AllocPolicy, kElemIsPod>
347 Impl;
348 friend struct detail::VectorImpl<T, MinInlineCapacity, AllocPolicy,
349 kElemIsPod>;
351 friend struct detail::VectorTesting;
353 [[nodiscard]] bool growStorageBy(size_t aIncr);
354 [[nodiscard]] bool convertToHeapStorage(size_t aNewCap);
355 [[nodiscard]] bool maybeCheckSimulatedOOM(size_t aRequestedSize);
357 /* magic constants */
360 * The maximum space allocated for inline element storage.
362 * We reduce space by what the AllocPolicy base class and prior Vector member
363 * fields likely consume to attempt to play well with binary size classes.
365 static constexpr size_t kMaxInlineBytes =
366 1024 -
367 (sizeof(AllocPolicy) + sizeof(T*) + sizeof(size_t) + sizeof(size_t));
370 * The number of T elements of inline capacity built into this Vector. This
371 * is usually |MinInlineCapacity|, but it may be less (or zero!) for large T.
373 * We use a partially-specialized template (not explicit specialization, which
374 * is only allowed at namespace scope) to compute this value. The benefit is
375 * that |sizeof(T)| need not be computed, and |T| doesn't have to be fully
376 * defined at the time |Vector<T>| appears, if no inline storage is requested.
378 template <size_t MinimumInlineCapacity, size_t Dummy>
379 struct ComputeCapacity {
380 static constexpr size_t value =
381 tl::Min<MinimumInlineCapacity, kMaxInlineBytes / sizeof(T)>::value;
384 template <size_t Dummy>
385 struct ComputeCapacity<0, Dummy> {
386 static constexpr size_t value = 0;
389 /** The actual inline capacity in number of elements T. This may be zero! */
390 static constexpr size_t kInlineCapacity =
391 ComputeCapacity<MinInlineCapacity, 0>::value;
393 /* member data */
396 * Pointer to the buffer, be it inline or heap-allocated. Only [mBegin,
397 * mBegin + mLength) hold valid constructed T objects. The range [mBegin +
398 * mLength, mBegin + mCapacity) holds uninitialized memory. The range
399 * [mBegin + mLength, mBegin + mReserved) also holds uninitialized memory
400 * previously allocated by a call to reserve().
402 T* mBegin;
404 /* Number of elements in the vector. */
405 size_t mLength;
408 * Memory used to store capacity, reserved element count (debug builds only),
409 * and inline storage. The simple "answer" is:
411 * size_t mCapacity;
412 * #ifdef DEBUG
413 * size_t mReserved;
414 * #endif
415 * alignas(T) unsigned char mBytes[kInlineCapacity * sizeof(T)];
417 * but there are complications. First, C++ forbids zero-sized arrays that
418 * might result. Second, we don't want zero capacity to affect Vector's size
419 * (even empty classes take up a byte, unless they're base classes).
421 * Yet again, we eliminate the zero-sized array using partial specialization.
422 * And we eliminate potential size hit by putting capacity/reserved in one
423 * struct, then putting the array (if any) in a derived struct. If no array
424 * is needed, the derived struct won't consume extra space.
426 struct CapacityAndReserved {
427 explicit CapacityAndReserved(size_t aCapacity, size_t aReserved)
428 : mCapacity(aCapacity)
429 #ifdef DEBUG
431 mReserved(aReserved)
432 #endif
435 CapacityAndReserved() = default;
437 /* Max number of elements storable in the vector without resizing. */
438 size_t mCapacity;
440 #ifdef DEBUG
441 /* Max elements of reserved or used space in this vector. */
442 size_t mReserved;
443 #endif
446 // Silence warnings about this struct possibly being padded dued to the
447 // alignas() in it -- there's nothing we can do to avoid it.
448 #ifdef _MSC_VER
449 # pragma warning(push)
450 # pragma warning(disable : 4324)
451 #endif // _MSC_VER
453 template <size_t Capacity, size_t Dummy>
454 struct CRAndStorage : CapacityAndReserved {
455 explicit CRAndStorage(size_t aCapacity, size_t aReserved)
456 : CapacityAndReserved(aCapacity, aReserved) {}
457 CRAndStorage() = default;
459 alignas(T) unsigned char mBytes[Capacity * sizeof(T)];
461 // GCC fails due to -Werror=strict-aliasing if |mBytes| is directly cast to
462 // T*. Indirecting through this function addresses the problem.
463 void* data() { return mBytes; }
465 T* storage() { return static_cast<T*>(data()); }
468 template <size_t Dummy>
469 struct CRAndStorage<0, Dummy> : CapacityAndReserved {
470 explicit CRAndStorage(size_t aCapacity, size_t aReserved)
471 : CapacityAndReserved(aCapacity, aReserved) {}
472 CRAndStorage() = default;
474 T* storage() {
475 // If this returns |nullptr|, functions like |Vector::begin()| would too,
476 // breaking callers that pass a vector's elements as pointer/length to
477 // code that bounds its operation by length but (even just as a sanity
478 // check) always wants a non-null pointer. Fake up an aligned, non-null
479 // pointer to support these callers.
480 return reinterpret_cast<T*>(sizeof(T));
484 CRAndStorage<kInlineCapacity, 0> mTail;
486 #ifdef _MSC_VER
487 # pragma warning(pop)
488 #endif // _MSC_VER
490 #ifdef DEBUG
491 friend class ReentrancyGuard;
492 bool mEntered;
493 #endif
495 /* private accessors */
497 bool usingInlineStorage() const {
498 return mBegin == const_cast<Vector*>(this)->inlineStorage();
501 T* inlineStorage() { return mTail.storage(); }
503 T* beginNoCheck() const { return mBegin; }
505 T* endNoCheck() { return mBegin + mLength; }
507 const T* endNoCheck() const { return mBegin + mLength; }
509 #ifdef DEBUG
511 * The amount of explicitly allocated space in this vector that is immediately
512 * available to be filled by appending additional elements. This value is
513 * always greater than or equal to |length()| -- the vector's actual elements
514 * are implicitly reserved. This value is always less than or equal to
515 * |capacity()|. It may be explicitly increased using the |reserve()| method.
517 size_t reserved() const {
518 MOZ_ASSERT(mLength <= mTail.mReserved);
519 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
520 return mTail.mReserved;
522 #endif
524 bool internalEnsureCapacity(size_t aNeeded);
526 /* Append operations guaranteed to succeed due to pre-reserved space. */
527 template <typename U>
528 void internalAppend(U&& aU);
529 template <typename U, size_t O, class BP>
530 void internalAppendAll(const Vector<U, O, BP>& aU);
531 void internalAppendN(const T& aT, size_t aN);
532 template <typename U>
533 void internalAppend(const U* aBegin, size_t aLength);
534 template <typename U>
535 void internalMoveAppend(U* aBegin, size_t aLength);
537 public:
538 static const size_t sMaxInlineStorage = MinInlineCapacity;
540 typedef T ElementType;
542 explicit Vector(AllocPolicy);
543 Vector() : Vector(AllocPolicy()) {}
545 Vector(Vector&&); /* Move constructor. */
546 Vector& operator=(Vector&&); /* Move assignment. */
547 ~Vector();
549 /* accessors */
551 const AllocPolicy& allocPolicy() const { return *this; }
553 AllocPolicy& allocPolicy() { return *this; }
555 enum { InlineLength = MinInlineCapacity };
557 size_t length() const { return mLength; }
559 bool empty() const { return mLength == 0; }
561 size_t capacity() const { return mTail.mCapacity; }
563 T* begin() {
564 MOZ_ASSERT(!mEntered);
565 return mBegin;
568 const T* begin() const {
569 MOZ_ASSERT(!mEntered);
570 return mBegin;
573 T* end() {
574 MOZ_ASSERT(!mEntered);
575 return mBegin + mLength;
578 const T* end() const {
579 MOZ_ASSERT(!mEntered);
580 return mBegin + mLength;
583 T& operator[](size_t aIndex) {
584 MOZ_ASSERT(!mEntered);
585 MOZ_ASSERT(aIndex < mLength);
586 return begin()[aIndex];
589 const T& operator[](size_t aIndex) const {
590 MOZ_ASSERT(!mEntered);
591 MOZ_ASSERT(aIndex < mLength);
592 return begin()[aIndex];
595 T& back() {
596 MOZ_ASSERT(!mEntered);
597 MOZ_ASSERT(!empty());
598 return *(end() - 1);
601 const T& back() const {
602 MOZ_ASSERT(!mEntered);
603 MOZ_ASSERT(!empty());
604 return *(end() - 1);
607 operator mozilla::Span<const T>() const {
608 // Explicitly specify template argument here to avoid instantiating Span<T>
609 // first and then implicitly converting to Span<const T>
610 return mozilla::Span<const T>{mBegin, mLength};
613 operator mozilla::Span<T>() { return mozilla::Span{mBegin, mLength}; }
615 class Range {
616 friend class Vector;
617 T* mCur;
618 T* mEnd;
619 Range(T* aCur, T* aEnd) : mCur(aCur), mEnd(aEnd) {
620 MOZ_ASSERT(aCur <= aEnd);
623 public:
624 bool empty() const { return mCur == mEnd; }
625 size_t remain() const { return PointerRangeSize(mCur, mEnd); }
626 T& front() const {
627 MOZ_ASSERT(!empty());
628 return *mCur;
630 void popFront() {
631 MOZ_ASSERT(!empty());
632 ++mCur;
634 T popCopyFront() {
635 MOZ_ASSERT(!empty());
636 return *mCur++;
640 class ConstRange {
641 friend class Vector;
642 const T* mCur;
643 const T* mEnd;
644 ConstRange(const T* aCur, const T* aEnd) : mCur(aCur), mEnd(aEnd) {
645 MOZ_ASSERT(aCur <= aEnd);
648 public:
649 bool empty() const { return mCur == mEnd; }
650 size_t remain() const { return PointerRangeSize(mCur, mEnd); }
651 const T& front() const {
652 MOZ_ASSERT(!empty());
653 return *mCur;
655 void popFront() {
656 MOZ_ASSERT(!empty());
657 ++mCur;
659 T popCopyFront() {
660 MOZ_ASSERT(!empty());
661 return *mCur++;
665 Range all() { return Range(begin(), end()); }
666 ConstRange all() const { return ConstRange(begin(), end()); }
668 /* mutators */
671 * Reverse the order of the elements in the vector in place.
673 void reverse();
676 * Given that the vector is empty, grow the internal capacity to |aRequest|,
677 * keeping the length 0.
679 [[nodiscard]] bool initCapacity(size_t aRequest);
682 * Given that the vector is empty, grow the internal capacity and length to
683 * |aRequest| leaving the elements' memory completely uninitialized (with all
684 * the associated hazards and caveats). This avoids the usual allocation-size
685 * rounding that happens in resize and overhead of initialization for elements
686 * that are about to be overwritten.
688 [[nodiscard]] bool initLengthUninitialized(size_t aRequest);
691 * If reserve(aRequest) succeeds and |aRequest >= length()|, then appending
692 * |aRequest - length()| elements, in any sequence of append/appendAll calls,
693 * is guaranteed to succeed.
695 * A request to reserve an amount less than the current length does not affect
696 * reserved space.
698 [[nodiscard]] bool reserve(size_t aRequest);
701 * Destroy elements in the range [end() - aIncr, end()). Does not deallocate
702 * or unreserve storage for those elements.
704 void shrinkBy(size_t aIncr);
707 * Destroy elements in the range [aNewLength, end()). Does not deallocate
708 * or unreserve storage for those elements.
710 void shrinkTo(size_t aNewLength);
712 /** Grow the vector by aIncr elements. */
713 [[nodiscard]] bool growBy(size_t aIncr);
715 /** Call shrinkBy or growBy based on whether newSize > length(). */
716 [[nodiscard]] bool resize(size_t aNewLength);
719 * Increase the length of the vector, but don't initialize the new elements
720 * -- leave them as uninitialized memory.
722 [[nodiscard]] bool growByUninitialized(size_t aIncr);
723 void infallibleGrowByUninitialized(size_t aIncr);
724 [[nodiscard]] bool resizeUninitialized(size_t aNewLength);
726 /** Shorthand for shrinkBy(length()). */
727 void clear();
729 /** Clears and releases any heap-allocated storage. */
730 void clearAndFree();
733 * Shrinks the storage to drop excess capacity if possible.
735 * The return value indicates whether the operation succeeded, otherwise, it
736 * represents an OOM. The bool can be safely ignored unless you want to
737 * provide the guarantee that `length() == capacity()`.
739 * For PODs, it calls the AllocPolicy's pod_realloc. For non-PODs, it moves
740 * the elements into the new storage.
742 bool shrinkStorageToFit();
745 * If true, appending |aNeeded| elements won't reallocate elements storage.
746 * This *doesn't* mean that infallibleAppend may be used! You still must
747 * reserve the extra space, even if this method indicates that appends won't
748 * need to reallocate elements storage.
750 bool canAppendWithoutRealloc(size_t aNeeded) const;
752 /** Potentially fallible append operations. */
755 * This can take either a T& or a T&&. Given a T&&, it moves |aU| into the
756 * vector, instead of copying it. If it fails, |aU| is left unmoved. ("We are
757 * not amused.")
759 template <typename U>
760 [[nodiscard]] bool append(U&& aU);
763 * Construct a T in-place as a new entry at the end of this vector.
765 template <typename... Args>
766 [[nodiscard]] bool emplaceBack(Args&&... aArgs) {
767 if (!growByUninitialized(1)) return false;
768 Impl::new_(&back(), std::forward<Args>(aArgs)...);
769 return true;
772 template <typename U, size_t O, class BP>
773 [[nodiscard]] bool appendAll(const Vector<U, O, BP>& aU);
774 template <typename U, size_t O, class BP>
775 [[nodiscard]] bool appendAll(Vector<U, O, BP>&& aU);
776 [[nodiscard]] bool appendN(const T& aT, size_t aN);
777 template <typename U>
778 [[nodiscard]] bool append(const U* aBegin, const U* aEnd);
779 template <typename U>
780 [[nodiscard]] bool append(const U* aBegin, size_t aLength);
781 template <typename U>
782 [[nodiscard]] bool moveAppend(U* aBegin, U* aEnd);
785 * Guaranteed-infallible append operations for use upon vectors whose
786 * memory has been pre-reserved. Don't use this if you haven't reserved the
787 * memory!
789 template <typename U>
790 void infallibleAppend(U&& aU) {
791 internalAppend(std::forward<U>(aU));
793 void infallibleAppendN(const T& aT, size_t aN) { internalAppendN(aT, aN); }
794 template <typename U>
795 void infallibleAppend(const U* aBegin, const U* aEnd) {
796 internalAppend(aBegin, PointerRangeSize(aBegin, aEnd));
798 template <typename U>
799 void infallibleAppend(const U* aBegin, size_t aLength) {
800 internalAppend(aBegin, aLength);
802 template <typename... Args>
803 void infallibleEmplaceBack(Args&&... aArgs) {
804 infallibleGrowByUninitialized(1);
805 Impl::new_(&back(), std::forward<Args>(aArgs)...);
808 void popBack();
810 T popCopy();
813 * If elements are stored in-place, return nullptr and leave this vector
814 * unmodified.
816 * Otherwise return this vector's elements buffer, and clear this vector as if
817 * by clearAndFree(). The caller now owns the buffer and is responsible for
818 * deallocating it consistent with this vector's AllocPolicy.
820 * N.B. Although a T*, only the range [0, length()) is constructed.
822 [[nodiscard]] T* extractRawBuffer();
825 * If elements are stored in-place, allocate a new buffer, move this vector's
826 * elements into it, and return that buffer.
828 * Otherwise return this vector's elements buffer. The caller now owns the
829 * buffer and is responsible for deallocating it consistent with this vector's
830 * AllocPolicy.
832 * This vector is cleared, as if by clearAndFree(), when this method
833 * succeeds. This method fails and returns nullptr only if new elements buffer
834 * allocation fails.
836 * N.B. Only the range [0, length()) of the returned buffer is constructed.
837 * If any of these elements are uninitialized (as growByUninitialized
838 * enables), behavior is undefined.
840 [[nodiscard]] T* extractOrCopyRawBuffer();
843 * Transfer ownership of an array of objects into the vector. The caller
844 * must have allocated the array in accordance with this vector's
845 * AllocPolicy.
847 * N.B. This call assumes that there are no uninitialized elements in the
848 * passed range [aP, aP + aLength). The range [aP + aLength, aP +
849 * aCapacity) must be allocated uninitialized memory.
851 void replaceRawBuffer(T* aP, size_t aLength, size_t aCapacity);
854 * Transfer ownership of an array of objects into the vector. The caller
855 * must have allocated the array in accordance with this vector's
856 * AllocPolicy.
858 * N.B. This call assumes that there are no uninitialized elements in the
859 * passed array.
861 void replaceRawBuffer(T* aP, size_t aLength);
864 * Places |aVal| at position |aP|, shifting existing elements from |aP| onward
865 * one position higher. On success, |aP| should not be reused because it'll
866 * be a dangling pointer if reallocation of the vector storage occurred; the
867 * return value should be used instead. On failure, nullptr is returned.
869 * Example usage:
871 * if (!(p = vec.insert(p, val))) {
872 * <handle failure>
874 * <keep working with p>
876 * This is inherently a linear-time operation. Be careful!
878 template <typename U>
879 [[nodiscard]] T* insert(T* aP, U&& aVal);
882 * Removes the element |aT|, which must fall in the bounds [begin, end),
883 * shifting existing elements from |aT + 1| onward one position lower.
885 void erase(T* aT);
888 * Removes the elements [|aBegin|, |aEnd|), which must fall in the bounds
889 * [begin, end), shifting existing elements from |aEnd| onward to aBegin's old
890 * position.
892 void erase(T* aBegin, T* aEnd);
895 * Removes all elements that satisfy the predicate, shifting existing elements
896 * lower to fill erased gaps.
898 template <typename Pred>
899 void eraseIf(Pred aPred);
902 * Removes all elements that compare equal to |aU|, shifting existing elements
903 * lower to fill erased gaps.
905 template <typename U>
906 void eraseIfEqual(const U& aU);
909 * Measure the size of the vector's heap-allocated storage.
911 size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
914 * Like sizeOfExcludingThis, but also measures the size of the vector
915 * object (which must be heap-allocated) itself.
917 size_t sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
919 void swap(Vector& aOther);
921 private:
922 Vector(const Vector&) = delete;
923 void operator=(const Vector&) = delete;
926 /* This does the re-entrancy check plus several other sanity checks. */
927 #define MOZ_REENTRANCY_GUARD_ET_AL \
928 ReentrancyGuard g(*this); \
929 MOZ_ASSERT_IF(usingInlineStorage(), mTail.mCapacity == kInlineCapacity); \
930 MOZ_ASSERT(reserved() <= mTail.mCapacity); \
931 MOZ_ASSERT(mLength <= reserved()); \
932 MOZ_ASSERT(mLength <= mTail.mCapacity)
934 /* Vector Implementation */
936 template <typename T, size_t N, class AP>
937 MOZ_ALWAYS_INLINE Vector<T, N, AP>::Vector(AP aAP)
938 : AP(std::move(aAP)),
939 mLength(0),
940 mTail(kInlineCapacity, 0)
941 #ifdef DEBUG
943 mEntered(false)
944 #endif
946 mBegin = inlineStorage();
949 /* Move constructor. */
950 template <typename T, size_t N, class AllocPolicy>
951 MOZ_ALWAYS_INLINE Vector<T, N, AllocPolicy>::Vector(Vector&& aRhs)
952 : AllocPolicy(std::move(aRhs))
953 #ifdef DEBUG
955 mEntered(false)
956 #endif
958 mLength = aRhs.mLength;
959 mTail.mCapacity = aRhs.mTail.mCapacity;
960 #ifdef DEBUG
961 mTail.mReserved = aRhs.mTail.mReserved;
962 #endif
964 if (aRhs.usingInlineStorage()) {
965 /* We can't move the buffer over in this case, so copy elements. */
966 mBegin = inlineStorage();
967 Impl::moveConstruct(mBegin, aRhs.beginNoCheck(), aRhs.endNoCheck());
969 * Leave aRhs's mLength, mBegin, mCapacity, and mReserved as they are.
970 * The elements in its in-line storage still need to be destroyed.
972 } else {
974 * Take src's buffer, and turn src into an empty vector using
975 * in-line storage.
977 mBegin = aRhs.mBegin;
978 aRhs.mBegin = aRhs.inlineStorage();
979 aRhs.mTail.mCapacity = kInlineCapacity;
980 aRhs.mLength = 0;
981 #ifdef DEBUG
982 aRhs.mTail.mReserved = 0;
983 #endif
987 /* Move assignment. */
988 template <typename T, size_t N, class AP>
989 MOZ_ALWAYS_INLINE Vector<T, N, AP>& Vector<T, N, AP>::operator=(Vector&& aRhs) {
990 MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
991 this->~Vector();
992 new (KnownNotNull, this) Vector(std::move(aRhs));
993 return *this;
996 template <typename T, size_t N, class AP>
997 MOZ_ALWAYS_INLINE Vector<T, N, AP>::~Vector() {
998 MOZ_REENTRANCY_GUARD_ET_AL;
999 Impl::destroy(beginNoCheck(), endNoCheck());
1000 if (!usingInlineStorage()) {
1001 this->free_(beginNoCheck(), mTail.mCapacity);
1005 template <typename T, size_t N, class AP>
1006 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::reverse() {
1007 MOZ_REENTRANCY_GUARD_ET_AL;
1008 T* elems = mBegin;
1009 size_t len = mLength;
1010 size_t mid = len / 2;
1011 for (size_t i = 0; i < mid; i++) {
1012 std::swap(elems[i], elems[len - i - 1]);
1017 * This function will create a new heap buffer with capacity aNewCap,
1018 * move all elements in the inline buffer to this new buffer,
1019 * and fail on OOM.
1021 template <typename T, size_t N, class AP>
1022 inline bool Vector<T, N, AP>::convertToHeapStorage(size_t aNewCap) {
1023 MOZ_ASSERT(usingInlineStorage());
1025 /* Allocate buffer. */
1026 MOZ_ASSERT(!detail::CapacityHasExcessSpace<sizeof(T)>(aNewCap));
1027 T* newBuf = this->template pod_malloc<T>(aNewCap);
1028 if (MOZ_UNLIKELY(!newBuf)) {
1029 return false;
1032 /* Copy inline elements into heap buffer. */
1033 Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
1034 Impl::destroy(beginNoCheck(), endNoCheck());
1036 /* Switch in heap buffer. */
1037 mBegin = newBuf;
1038 /* mLength is unchanged. */
1039 mTail.mCapacity = aNewCap;
1040 return true;
1043 template <typename T, size_t N, class AP>
1044 MOZ_NEVER_INLINE bool Vector<T, N, AP>::growStorageBy(size_t aIncr) {
1045 MOZ_ASSERT(mLength + aIncr > mTail.mCapacity);
1047 size_t newCap;
1049 if (aIncr == 1 && usingInlineStorage()) {
1050 /* This case occurs in ~70--80% of the calls to this function. */
1051 constexpr size_t newSize =
1052 tl::RoundUpPow2<(kInlineCapacity + 1) * sizeof(T)>::value;
1053 static_assert(newSize / sizeof(T) > 0,
1054 "overflow when exceeding inline Vector storage");
1055 newCap = newSize / sizeof(T);
1056 } else {
1057 newCap = detail::ComputeGrowth<AP, sizeof(T)>(mLength, aIncr, true);
1058 if (MOZ_UNLIKELY(newCap == 0)) {
1059 this->reportAllocOverflow();
1060 return false;
1064 if (usingInlineStorage()) {
1065 return convertToHeapStorage(newCap);
1068 return Impl::growTo(*this, newCap);
1071 template <typename T, size_t N, class AP>
1072 inline bool Vector<T, N, AP>::initCapacity(size_t aRequest) {
1073 MOZ_ASSERT(empty());
1074 MOZ_ASSERT(usingInlineStorage());
1075 if (aRequest == 0) {
1076 return true;
1078 T* newbuf = this->template pod_malloc<T>(aRequest);
1079 if (MOZ_UNLIKELY(!newbuf)) {
1080 return false;
1082 mBegin = newbuf;
1083 mTail.mCapacity = aRequest;
1084 #ifdef DEBUG
1085 mTail.mReserved = aRequest;
1086 #endif
1087 return true;
1090 template <typename T, size_t N, class AP>
1091 inline bool Vector<T, N, AP>::initLengthUninitialized(size_t aRequest) {
1092 if (!initCapacity(aRequest)) {
1093 return false;
1095 infallibleGrowByUninitialized(aRequest);
1096 return true;
1099 template <typename T, size_t N, class AP>
1100 inline bool Vector<T, N, AP>::maybeCheckSimulatedOOM(size_t aRequestedSize) {
1101 if (aRequestedSize <= N) {
1102 return true;
1105 #ifdef DEBUG
1106 if (aRequestedSize <= mTail.mReserved) {
1107 return true;
1109 #endif
1111 return allocPolicy().checkSimulatedOOM();
1114 template <typename T, size_t N, class AP>
1115 inline bool Vector<T, N, AP>::reserve(size_t aRequest) {
1116 MOZ_REENTRANCY_GUARD_ET_AL;
1117 if (aRequest > mTail.mCapacity) {
1118 if (MOZ_UNLIKELY(!growStorageBy(aRequest - mLength))) {
1119 return false;
1121 } else if (!maybeCheckSimulatedOOM(aRequest)) {
1122 return false;
1124 #ifdef DEBUG
1125 if (aRequest > mTail.mReserved) {
1126 mTail.mReserved = aRequest;
1128 MOZ_ASSERT(mLength <= mTail.mReserved);
1129 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1130 #endif
1131 return true;
1134 template <typename T, size_t N, class AP>
1135 inline void Vector<T, N, AP>::shrinkBy(size_t aIncr) {
1136 MOZ_REENTRANCY_GUARD_ET_AL;
1137 MOZ_ASSERT(aIncr <= mLength);
1138 Impl::destroy(endNoCheck() - aIncr, endNoCheck());
1139 mLength -= aIncr;
1142 template <typename T, size_t N, class AP>
1143 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::shrinkTo(size_t aNewLength) {
1144 MOZ_ASSERT(aNewLength <= mLength);
1145 shrinkBy(mLength - aNewLength);
1148 template <typename T, size_t N, class AP>
1149 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::growBy(size_t aIncr) {
1150 MOZ_REENTRANCY_GUARD_ET_AL;
1151 if (aIncr > mTail.mCapacity - mLength) {
1152 if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
1153 return false;
1155 } else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
1156 return false;
1158 MOZ_ASSERT(mLength + aIncr <= mTail.mCapacity);
1159 T* newend = endNoCheck() + aIncr;
1160 Impl::initialize(endNoCheck(), newend);
1161 mLength += aIncr;
1162 #ifdef DEBUG
1163 if (mLength > mTail.mReserved) {
1164 mTail.mReserved = mLength;
1166 #endif
1167 return true;
1170 template <typename T, size_t N, class AP>
1171 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::growByUninitialized(size_t aIncr) {
1172 MOZ_REENTRANCY_GUARD_ET_AL;
1173 if (aIncr > mTail.mCapacity - mLength) {
1174 if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
1175 return false;
1177 } else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
1178 return false;
1180 #ifdef DEBUG
1181 if (mLength + aIncr > mTail.mReserved) {
1182 mTail.mReserved = mLength + aIncr;
1184 #endif
1185 infallibleGrowByUninitialized(aIncr);
1186 return true;
1189 template <typename T, size_t N, class AP>
1190 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::infallibleGrowByUninitialized(
1191 size_t aIncr) {
1192 MOZ_ASSERT(mLength + aIncr <= reserved());
1193 mLength += aIncr;
1196 template <typename T, size_t N, class AP>
1197 inline bool Vector<T, N, AP>::resize(size_t aNewLength) {
1198 size_t curLength = mLength;
1199 if (aNewLength > curLength) {
1200 return growBy(aNewLength - curLength);
1202 shrinkBy(curLength - aNewLength);
1203 return true;
1206 template <typename T, size_t N, class AP>
1207 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::resizeUninitialized(
1208 size_t aNewLength) {
1209 size_t curLength = mLength;
1210 if (aNewLength > curLength) {
1211 return growByUninitialized(aNewLength - curLength);
1213 shrinkBy(curLength - aNewLength);
1214 return true;
1217 template <typename T, size_t N, class AP>
1218 inline void Vector<T, N, AP>::clear() {
1219 MOZ_REENTRANCY_GUARD_ET_AL;
1220 Impl::destroy(beginNoCheck(), endNoCheck());
1221 mLength = 0;
1224 template <typename T, size_t N, class AP>
1225 inline void Vector<T, N, AP>::clearAndFree() {
1226 clear();
1228 if (usingInlineStorage()) {
1229 return;
1231 this->free_(beginNoCheck(), mTail.mCapacity);
1232 mBegin = inlineStorage();
1233 mTail.mCapacity = kInlineCapacity;
1234 #ifdef DEBUG
1235 mTail.mReserved = 0;
1236 #endif
1239 template <typename T, size_t N, class AP>
1240 inline bool Vector<T, N, AP>::shrinkStorageToFit() {
1241 MOZ_REENTRANCY_GUARD_ET_AL;
1243 const auto length = this->length();
1244 if (usingInlineStorage() || length == capacity()) {
1245 return true;
1248 if (!length) {
1249 this->free_(beginNoCheck(), mTail.mCapacity);
1250 mBegin = inlineStorage();
1251 mTail.mCapacity = kInlineCapacity;
1252 #ifdef DEBUG
1253 mTail.mReserved = 0;
1254 #endif
1255 return true;
1258 T* newBuf;
1259 size_t newCap;
1260 if (length <= kInlineCapacity) {
1261 newBuf = inlineStorage();
1262 newCap = kInlineCapacity;
1263 } else {
1264 if (kElemIsPod) {
1265 newBuf = this->template pod_realloc<T>(beginNoCheck(), mTail.mCapacity,
1266 length);
1267 } else {
1268 newBuf = this->template pod_malloc<T>(length);
1270 if (MOZ_UNLIKELY(!newBuf)) {
1271 return false;
1273 newCap = length;
1275 if (!kElemIsPod || newBuf == inlineStorage()) {
1276 Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
1277 Impl::destroy(beginNoCheck(), endNoCheck());
1279 if (!kElemIsPod) {
1280 this->free_(beginNoCheck(), mTail.mCapacity);
1282 mBegin = newBuf;
1283 mTail.mCapacity = newCap;
1284 #ifdef DEBUG
1285 mTail.mReserved = length;
1286 #endif
1287 return true;
1290 template <typename T, size_t N, class AP>
1291 inline bool Vector<T, N, AP>::canAppendWithoutRealloc(size_t aNeeded) const {
1292 return mLength + aNeeded <= mTail.mCapacity;
1295 template <typename T, size_t N, class AP>
1296 template <typename U, size_t O, class BP>
1297 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppendAll(
1298 const Vector<U, O, BP>& aOther) {
1299 internalAppend(aOther.begin(), aOther.length());
1302 template <typename T, size_t N, class AP>
1303 template <typename U>
1304 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppend(U&& aU) {
1305 MOZ_ASSERT(mLength + 1 <= mTail.mReserved);
1306 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1307 Impl::new_(endNoCheck(), std::forward<U>(aU));
1308 ++mLength;
1311 template <typename T, size_t N, class AP>
1312 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendN(const T& aT, size_t aNeeded) {
1313 MOZ_REENTRANCY_GUARD_ET_AL;
1314 if (mLength + aNeeded > mTail.mCapacity) {
1315 if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
1316 return false;
1318 } else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
1319 return false;
1321 #ifdef DEBUG
1322 if (mLength + aNeeded > mTail.mReserved) {
1323 mTail.mReserved = mLength + aNeeded;
1325 #endif
1326 internalAppendN(aT, aNeeded);
1327 return true;
1330 template <typename T, size_t N, class AP>
1331 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppendN(const T& aT,
1332 size_t aNeeded) {
1333 MOZ_ASSERT(mLength + aNeeded <= mTail.mReserved);
1334 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1335 Impl::copyConstructN(endNoCheck(), aNeeded, aT);
1336 mLength += aNeeded;
1339 template <typename T, size_t N, class AP>
1340 template <typename U>
1341 inline T* Vector<T, N, AP>::insert(T* aP, U&& aVal) {
1342 MOZ_ASSERT(begin() <= aP);
1343 MOZ_ASSERT(aP <= end());
1344 size_t pos = aP - begin();
1345 MOZ_ASSERT(pos <= mLength);
1346 size_t oldLength = mLength;
1347 if (pos == oldLength) {
1348 if (!append(std::forward<U>(aVal))) {
1349 return nullptr;
1351 } else {
1352 T oldBack = std::move(back());
1353 if (!append(std::move(oldBack))) {
1354 return nullptr;
1356 for (size_t i = oldLength - 1; i > pos; --i) {
1357 (*this)[i] = std::move((*this)[i - 1]);
1359 (*this)[pos] = std::forward<U>(aVal);
1361 return begin() + pos;
1364 template <typename T, size_t N, class AP>
1365 inline void Vector<T, N, AP>::erase(T* aIt) {
1366 MOZ_ASSERT(begin() <= aIt);
1367 MOZ_ASSERT(aIt < end());
1368 while (aIt + 1 < end()) {
1369 *aIt = std::move(*(aIt + 1));
1370 ++aIt;
1372 popBack();
1375 template <typename T, size_t N, class AP>
1376 inline void Vector<T, N, AP>::erase(T* aBegin, T* aEnd) {
1377 MOZ_ASSERT(begin() <= aBegin);
1378 MOZ_ASSERT(aBegin <= aEnd);
1379 MOZ_ASSERT(aEnd <= end());
1380 while (aEnd < end()) {
1381 *aBegin++ = std::move(*aEnd++);
1383 shrinkBy(aEnd - aBegin);
1386 template <typename T, size_t N, class AP>
1387 template <typename Pred>
1388 void Vector<T, N, AP>::eraseIf(Pred aPred) {
1389 // remove_if finds the first element to be erased, and then efficiently move-
1390 // assigns elements to effectively overwrite elements that satisfy the
1391 // predicate. It returns the new end pointer, after which there are only
1392 // moved-from elements ready to be destroyed, so we just need to shrink the
1393 // vector accordingly.
1394 T* newEnd = std::remove_if(begin(), end(),
1395 [&aPred](const T& aT) { return aPred(aT); });
1396 MOZ_ASSERT(newEnd <= end());
1397 shrinkBy(end() - newEnd);
1400 template <typename T, size_t N, class AP>
1401 template <typename U>
1402 void Vector<T, N, AP>::eraseIfEqual(const U& aU) {
1403 return eraseIf([&aU](const T& aT) { return aT == aU; });
1406 template <typename T, size_t N, class AP>
1407 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::internalEnsureCapacity(
1408 size_t aNeeded) {
1409 if (mLength + aNeeded > mTail.mCapacity) {
1410 if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
1411 return false;
1413 } else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
1414 return false;
1416 #ifdef DEBUG
1417 if (mLength + aNeeded > mTail.mReserved) {
1418 mTail.mReserved = mLength + aNeeded;
1420 #endif
1421 return true;
1424 template <typename T, size_t N, class AP>
1425 template <typename U>
1426 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(const U* aInsBegin,
1427 const U* aInsEnd) {
1428 MOZ_REENTRANCY_GUARD_ET_AL;
1429 const size_t needed = PointerRangeSize(aInsBegin, aInsEnd);
1430 if (!internalEnsureCapacity(needed)) {
1431 return false;
1433 internalAppend(aInsBegin, needed);
1434 return true;
1437 template <typename T, size_t N, class AP>
1438 template <typename U>
1439 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppend(const U* aInsBegin,
1440 size_t aInsLength) {
1441 MOZ_ASSERT(mLength + aInsLength <= mTail.mReserved);
1442 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1443 Impl::copyConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
1444 mLength += aInsLength;
1447 template <typename T, size_t N, class AP>
1448 template <typename U>
1449 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::moveAppend(U* aInsBegin, U* aInsEnd) {
1450 MOZ_REENTRANCY_GUARD_ET_AL;
1451 const size_t needed = PointerRangeSize(aInsBegin, aInsEnd);
1452 if (!internalEnsureCapacity(needed)) {
1453 return false;
1455 internalMoveAppend(aInsBegin, needed);
1456 return true;
1459 template <typename T, size_t N, class AP>
1460 template <typename U>
1461 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalMoveAppend(U* aInsBegin,
1462 size_t aInsLength) {
1463 MOZ_ASSERT(mLength + aInsLength <= mTail.mReserved);
1464 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1465 Impl::moveConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
1466 mLength += aInsLength;
1469 template <typename T, size_t N, class AP>
1470 template <typename U>
1471 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(U&& aU) {
1472 MOZ_REENTRANCY_GUARD_ET_AL;
1473 if (mLength == mTail.mCapacity) {
1474 if (MOZ_UNLIKELY(!growStorageBy(1))) {
1475 return false;
1477 } else if (!maybeCheckSimulatedOOM(mLength + 1)) {
1478 return false;
1480 #ifdef DEBUG
1481 if (mLength + 1 > mTail.mReserved) {
1482 mTail.mReserved = mLength + 1;
1484 #endif
1485 internalAppend(std::forward<U>(aU));
1486 return true;
1489 template <typename T, size_t N, class AP>
1490 template <typename U, size_t O, class BP>
1491 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendAll(
1492 const Vector<U, O, BP>& aOther) {
1493 return append(aOther.begin(), aOther.length());
1496 template <typename T, size_t N, class AP>
1497 template <typename U, size_t O, class BP>
1498 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendAll(Vector<U, O, BP>&& aOther) {
1499 if (empty() && capacity() < aOther.length()) {
1500 *this = std::move(aOther);
1501 return true;
1504 if (moveAppend(aOther.begin(), aOther.end())) {
1505 aOther.clearAndFree();
1506 return true;
1509 return false;
1512 template <typename T, size_t N, class AP>
1513 template <class U>
1514 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(const U* aInsBegin,
1515 size_t aInsLength) {
1516 return append(aInsBegin, aInsBegin + aInsLength);
1519 template <typename T, size_t N, class AP>
1520 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::popBack() {
1521 MOZ_REENTRANCY_GUARD_ET_AL;
1522 MOZ_ASSERT(!empty());
1523 --mLength;
1524 endNoCheck()->~T();
1527 template <typename T, size_t N, class AP>
1528 MOZ_ALWAYS_INLINE T Vector<T, N, AP>::popCopy() {
1529 T ret = back();
1530 popBack();
1531 return ret;
1534 template <typename T, size_t N, class AP>
1535 inline T* Vector<T, N, AP>::extractRawBuffer() {
1536 MOZ_REENTRANCY_GUARD_ET_AL;
1538 if (usingInlineStorage()) {
1539 return nullptr;
1542 T* ret = mBegin;
1543 mBegin = inlineStorage();
1544 mLength = 0;
1545 mTail.mCapacity = kInlineCapacity;
1546 #ifdef DEBUG
1547 mTail.mReserved = 0;
1548 #endif
1549 return ret;
1552 template <typename T, size_t N, class AP>
1553 inline T* Vector<T, N, AP>::extractOrCopyRawBuffer() {
1554 if (T* ret = extractRawBuffer()) {
1555 return ret;
1558 MOZ_REENTRANCY_GUARD_ET_AL;
1560 T* copy = this->template pod_malloc<T>(mLength);
1561 if (!copy) {
1562 return nullptr;
1565 Impl::moveConstruct(copy, beginNoCheck(), endNoCheck());
1566 Impl::destroy(beginNoCheck(), endNoCheck());
1567 mBegin = inlineStorage();
1568 mLength = 0;
1569 mTail.mCapacity = kInlineCapacity;
1570 #ifdef DEBUG
1571 mTail.mReserved = 0;
1572 #endif
1573 return copy;
1576 template <typename T, size_t N, class AP>
1577 inline void Vector<T, N, AP>::replaceRawBuffer(T* aP, size_t aLength,
1578 size_t aCapacity) {
1579 MOZ_REENTRANCY_GUARD_ET_AL;
1581 /* Destroy what we have. */
1582 Impl::destroy(beginNoCheck(), endNoCheck());
1583 if (!usingInlineStorage()) {
1584 this->free_(beginNoCheck(), mTail.mCapacity);
1587 /* Take in the new buffer. */
1588 if (aCapacity <= kInlineCapacity) {
1590 * We convert to inline storage if possible, even though aP might
1591 * otherwise be acceptable. Maybe this behaviour should be
1592 * specifiable with an argument to this function.
1594 mBegin = inlineStorage();
1595 mLength = aLength;
1596 mTail.mCapacity = kInlineCapacity;
1597 Impl::moveConstruct(mBegin, aP, aP + aLength);
1598 Impl::destroy(aP, aP + aLength);
1599 this->free_(aP, aCapacity);
1600 } else {
1601 mBegin = aP;
1602 mLength = aLength;
1603 mTail.mCapacity = aCapacity;
1605 #ifdef DEBUG
1606 mTail.mReserved = aCapacity;
1607 #endif
1610 template <typename T, size_t N, class AP>
1611 inline void Vector<T, N, AP>::replaceRawBuffer(T* aP, size_t aLength) {
1612 replaceRawBuffer(aP, aLength, aLength);
1615 template <typename T, size_t N, class AP>
1616 inline size_t Vector<T, N, AP>::sizeOfExcludingThis(
1617 MallocSizeOf aMallocSizeOf) const {
1618 return usingInlineStorage() ? 0 : aMallocSizeOf(beginNoCheck());
1621 template <typename T, size_t N, class AP>
1622 inline size_t Vector<T, N, AP>::sizeOfIncludingThis(
1623 MallocSizeOf aMallocSizeOf) const {
1624 return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf);
1627 template <typename T, size_t N, class AP>
1628 inline void Vector<T, N, AP>::swap(Vector& aOther) {
1629 static_assert(N == 0, "still need to implement this for N != 0");
1631 // This only works when inline storage is always empty.
1632 if (!usingInlineStorage() && aOther.usingInlineStorage()) {
1633 aOther.mBegin = mBegin;
1634 mBegin = inlineStorage();
1635 } else if (usingInlineStorage() && !aOther.usingInlineStorage()) {
1636 mBegin = aOther.mBegin;
1637 aOther.mBegin = aOther.inlineStorage();
1638 } else if (!usingInlineStorage() && !aOther.usingInlineStorage()) {
1639 std::swap(mBegin, aOther.mBegin);
1640 } else {
1641 // This case is a no-op, since we'd set both to use their inline storage.
1644 std::swap(mLength, aOther.mLength);
1645 std::swap(mTail.mCapacity, aOther.mTail.mCapacity);
1646 #ifdef DEBUG
1647 std::swap(mTail.mReserved, aOther.mTail.mReserved);
1648 #endif
1651 } // namespace mozilla
1653 #endif /* mozilla_Vector_h */