Bug 1690340 - Part 4: Insert the "Page Source" before the "Extensions for Developers...
[gecko.git] / mfbt / Vector.h
blobf69e45fb4e9b8a342a2c49f41083fc3a65f11dde
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 <utility>
15 #include "mozilla/Alignment.h"
16 #include "mozilla/AllocPolicy.h"
17 #include "mozilla/ArrayUtils.h" // for PointerRangeSize
18 #include "mozilla/Assertions.h"
19 #include "mozilla/Attributes.h"
20 #include "mozilla/MathAlgorithms.h"
21 #include "mozilla/MemoryReporting.h"
22 #include "mozilla/OperatorNewExtensions.h"
23 #include "mozilla/ReentrancyGuard.h"
24 #include "mozilla/Span.h"
25 #include "mozilla/TemplateLib.h"
26 #include "mozilla/TypeTraits.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*sizeof(T) is as close to a
38 * power-of-two as possible. growStorageBy() is responsible for ensuring this.
40 template <typename T>
41 static bool CapacityHasExcessSpace(size_t aCapacity) {
42 size_t size = aCapacity * sizeof(T);
43 return RoundUpPow2(size) - size >= sizeof(T);
47 * This template class provides a default implementation for vector operations
48 * when the element type is not known to be a POD, as judged by IsPod.
50 template <typename T, size_t N, class AP, bool IsPod>
51 struct VectorImpl {
53 * Constructs an object in the uninitialized memory at *aDst with aArgs.
55 template <typename... Args>
56 MOZ_NONNULL(1)
57 static inline void new_(T* aDst, Args&&... aArgs) {
58 new (KnownNotNull, aDst) T(std::forward<Args>(aArgs)...);
61 /* Destroys constructed objects in the range [aBegin, aEnd). */
62 static inline void destroy(T* aBegin, T* aEnd) {
63 MOZ_ASSERT(aBegin <= aEnd);
64 for (T* p = aBegin; p < aEnd; ++p) {
65 p->~T();
69 /* Constructs objects in the uninitialized range [aBegin, aEnd). */
70 static inline void initialize(T* aBegin, T* aEnd) {
71 MOZ_ASSERT(aBegin <= aEnd);
72 for (T* p = aBegin; p < aEnd; ++p) {
73 new_(p);
78 * Copy-constructs objects in the uninitialized range
79 * [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
81 template <typename U>
82 static inline void copyConstruct(T* aDst, const U* aSrcStart,
83 const U* aSrcEnd) {
84 MOZ_ASSERT(aSrcStart <= aSrcEnd);
85 for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
86 new_(aDst, *p);
91 * Move-constructs objects in the uninitialized range
92 * [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
94 template <typename U>
95 static inline void moveConstruct(T* aDst, U* aSrcStart, U* aSrcEnd) {
96 MOZ_ASSERT(aSrcStart <= aSrcEnd);
97 for (U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
98 new_(aDst, std::move(*p));
103 * Copy-constructs objects in the uninitialized range [aDst, aDst+aN) from
104 * the same object aU.
106 template <typename U>
107 static inline void copyConstructN(T* aDst, size_t aN, const U& aU) {
108 for (T* end = aDst + aN; aDst < end; ++aDst) {
109 new_(aDst, aU);
114 * Grows the given buffer to have capacity aNewCap, preserving the objects
115 * constructed in the range [begin, end) and updating aV. Assumes that (1)
116 * aNewCap has not overflowed, and (2) multiplying aNewCap by sizeof(T) will
117 * not overflow.
119 static inline MOZ_MUST_USE bool growTo(Vector<T, N, AP>& aV, size_t aNewCap) {
120 MOZ_ASSERT(!aV.usingInlineStorage());
121 MOZ_ASSERT(!CapacityHasExcessSpace<T>(aNewCap));
122 T* newbuf = aV.template pod_malloc<T>(aNewCap);
123 if (MOZ_UNLIKELY(!newbuf)) {
124 return false;
126 T* dst = newbuf;
127 T* src = aV.beginNoCheck();
128 for (; src < aV.endNoCheck(); ++dst, ++src) {
129 new_(dst, std::move(*src));
131 VectorImpl::destroy(aV.beginNoCheck(), aV.endNoCheck());
132 aV.free_(aV.mBegin, aV.mTail.mCapacity);
133 aV.mBegin = newbuf;
134 /* aV.mLength is unchanged. */
135 aV.mTail.mCapacity = aNewCap;
136 return true;
141 * This partial template specialization provides a default implementation for
142 * vector operations when the element type is known to be a POD, as judged by
143 * IsPod.
145 template <typename T, size_t N, class AP>
146 struct VectorImpl<T, N, AP, true> {
147 template <typename... Args>
148 MOZ_NONNULL(1)
149 static inline void new_(T* aDst, Args&&... aArgs) {
150 // Explicitly construct a local object instead of using a temporary since
151 // T(args...) will be treated like a C-style cast in the unary case and
152 // allow unsafe conversions. Both forms should be equivalent to an
153 // optimizing compiler.
154 T temp(std::forward<Args>(aArgs)...);
155 *aDst = temp;
158 static inline void destroy(T*, T*) {}
160 static inline void initialize(T* aBegin, T* aEnd) {
162 * You would think that memset would be a big win (or even break even)
163 * when we know T is a POD. But currently it's not. This is probably
164 * because |append| tends to be given small ranges and memset requires
165 * a function call that doesn't get inlined.
167 * memset(aBegin, 0, sizeof(T) * (aEnd - aBegin));
169 MOZ_ASSERT(aBegin <= aEnd);
170 for (T* p = aBegin; p < aEnd; ++p) {
171 new_(p);
175 template <typename U>
176 static inline void copyConstruct(T* aDst, const U* aSrcStart,
177 const U* aSrcEnd) {
179 * See above memset comment. Also, notice that copyConstruct is
180 * currently templated (T != U), so memcpy won't work without
181 * requiring T == U.
183 * memcpy(aDst, aSrcStart, sizeof(T) * (aSrcEnd - aSrcStart));
185 MOZ_ASSERT(aSrcStart <= aSrcEnd);
186 for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
187 new_(aDst, *p);
191 template <typename U>
192 static inline void moveConstruct(T* aDst, const U* aSrcStart,
193 const U* aSrcEnd) {
194 copyConstruct(aDst, aSrcStart, aSrcEnd);
197 static inline void copyConstructN(T* aDst, size_t aN, const T& aT) {
198 for (T* end = aDst + aN; aDst < end; ++aDst) {
199 new_(aDst, aT);
203 static inline MOZ_MUST_USE bool growTo(Vector<T, N, AP>& aV, size_t aNewCap) {
204 MOZ_ASSERT(!aV.usingInlineStorage());
205 MOZ_ASSERT(!CapacityHasExcessSpace<T>(aNewCap));
206 T* newbuf =
207 aV.template pod_realloc<T>(aV.mBegin, aV.mTail.mCapacity, aNewCap);
208 if (MOZ_UNLIKELY(!newbuf)) {
209 return false;
211 aV.mBegin = newbuf;
212 /* aV.mLength is unchanged. */
213 aV.mTail.mCapacity = aNewCap;
214 return true;
218 // A struct for TestVector.cpp to access private internal fields.
219 // DO NOT DEFINE IN YOUR OWN CODE.
220 struct VectorTesting;
222 } // namespace detail
225 * STL-like container providing a short-lived, dynamic buffer. Vector calls the
226 * constructors/destructors of all elements stored in its internal buffer, so
227 * non-PODs may be safely used. Additionally, Vector will store the first N
228 * elements in-place before resorting to dynamic allocation.
230 * T requirements:
231 * - default and copy constructible, assignable, destructible
232 * - operations do not throw
233 * MinInlineCapacity requirements:
234 * - any value, however, MinInlineCapacity is clamped to min/max values
235 * AllocPolicy:
236 * - see "Allocation policies" in AllocPolicy.h (defaults to
237 * mozilla::MallocAllocPolicy)
239 * Vector is not reentrant: T member functions called during Vector member
240 * functions must not call back into the same object!
242 template <typename T, size_t MinInlineCapacity = 0,
243 class AllocPolicy = MallocAllocPolicy>
244 class MOZ_NON_PARAM Vector final : private AllocPolicy {
245 /* utilities */
247 static constexpr bool kElemIsPod = IsPod<T>::value;
248 typedef detail::VectorImpl<T, MinInlineCapacity, AllocPolicy, kElemIsPod>
249 Impl;
250 friend struct detail::VectorImpl<T, MinInlineCapacity, AllocPolicy,
251 kElemIsPod>;
253 friend struct detail::VectorTesting;
255 MOZ_MUST_USE bool growStorageBy(size_t aIncr);
256 MOZ_MUST_USE bool convertToHeapStorage(size_t aNewCap);
257 MOZ_MUST_USE bool maybeCheckSimulatedOOM(size_t aRequestedSize);
259 /* magic constants */
262 * The maximum space allocated for inline element storage.
264 * We reduce space by what the AllocPolicy base class and prior Vector member
265 * fields likely consume to attempt to play well with binary size classes.
267 static constexpr size_t kMaxInlineBytes =
268 1024 -
269 (sizeof(AllocPolicy) + sizeof(T*) + sizeof(size_t) + sizeof(size_t));
272 * The number of T elements of inline capacity built into this Vector. This
273 * is usually |MinInlineCapacity|, but it may be less (or zero!) for large T.
275 * We use a partially-specialized template (not explicit specialization, which
276 * is only allowed at namespace scope) to compute this value. The benefit is
277 * that |sizeof(T)| need not be computed, and |T| doesn't have to be fully
278 * defined at the time |Vector<T>| appears, if no inline storage is requested.
280 template <size_t MinimumInlineCapacity, size_t Dummy>
281 struct ComputeCapacity {
282 static constexpr size_t value =
283 tl::Min<MinimumInlineCapacity, kMaxInlineBytes / sizeof(T)>::value;
286 template <size_t Dummy>
287 struct ComputeCapacity<0, Dummy> {
288 static constexpr size_t value = 0;
291 /** The actual inline capacity in number of elements T. This may be zero! */
292 static constexpr size_t kInlineCapacity =
293 ComputeCapacity<MinInlineCapacity, 0>::value;
295 /* member data */
298 * Pointer to the buffer, be it inline or heap-allocated. Only [mBegin,
299 * mBegin + mLength) hold valid constructed T objects. The range [mBegin +
300 * mLength, mBegin + mCapacity) holds uninitialized memory. The range
301 * [mBegin + mLength, mBegin + mReserved) also holds uninitialized memory
302 * previously allocated by a call to reserve().
304 T* mBegin;
306 /* Number of elements in the vector. */
307 size_t mLength;
310 * Memory used to store capacity, reserved element count (debug builds only),
311 * and inline storage. The simple "answer" is:
313 * size_t mCapacity;
314 * #ifdef DEBUG
315 * size_t mReserved;
316 * #endif
317 * alignas(T) unsigned char mBytes[kInlineCapacity * sizeof(T)];
319 * but there are complications. First, C++ forbids zero-sized arrays that
320 * might result. Second, we don't want zero capacity to affect Vector's size
321 * (even empty classes take up a byte, unless they're base classes).
323 * Yet again, we eliminate the zero-sized array using partial specialization.
324 * And we eliminate potential size hit by putting capacity/reserved in one
325 * struct, then putting the array (if any) in a derived struct. If no array
326 * is needed, the derived struct won't consume extra space.
328 struct CapacityAndReserved {
329 explicit CapacityAndReserved(size_t aCapacity, size_t aReserved)
330 : mCapacity(aCapacity)
331 #ifdef DEBUG
333 mReserved(aReserved)
334 #endif
337 CapacityAndReserved() = default;
339 /* Max number of elements storable in the vector without resizing. */
340 size_t mCapacity;
342 #ifdef DEBUG
343 /* Max elements of reserved or used space in this vector. */
344 size_t mReserved;
345 #endif
348 // Silence warnings about this struct possibly being padded dued to the
349 // alignas() in it -- there's nothing we can do to avoid it.
350 #ifdef _MSC_VER
351 # pragma warning(push)
352 # pragma warning(disable : 4324)
353 #endif // _MSC_VER
355 template <size_t Capacity, size_t Dummy>
356 struct CRAndStorage : CapacityAndReserved {
357 explicit CRAndStorage(size_t aCapacity, size_t aReserved)
358 : CapacityAndReserved(aCapacity, aReserved) {}
359 CRAndStorage() = default;
361 alignas(T) unsigned char mBytes[Capacity * sizeof(T)];
363 // GCC fails due to -Werror=strict-aliasing if |mBytes| is directly cast to
364 // T*. Indirecting through this function addresses the problem.
365 void* data() { return mBytes; }
367 T* storage() { return static_cast<T*>(data()); }
370 template <size_t Dummy>
371 struct CRAndStorage<0, Dummy> : CapacityAndReserved {
372 explicit CRAndStorage(size_t aCapacity, size_t aReserved)
373 : CapacityAndReserved(aCapacity, aReserved) {}
374 CRAndStorage() = default;
376 T* storage() {
377 // If this returns |nullptr|, functions like |Vector::begin()| would too,
378 // breaking callers that pass a vector's elements as pointer/length to
379 // code that bounds its operation by length but (even just as a sanity
380 // check) always wants a non-null pointer. Fake up an aligned, non-null
381 // pointer to support these callers.
382 return reinterpret_cast<T*>(sizeof(T));
386 CRAndStorage<kInlineCapacity, 0> mTail;
388 #ifdef _MSC_VER
389 # pragma warning(pop)
390 #endif // _MSC_VER
392 #ifdef DEBUG
393 friend class ReentrancyGuard;
394 bool mEntered;
395 #endif
397 /* private accessors */
399 bool usingInlineStorage() const {
400 return mBegin == const_cast<Vector*>(this)->inlineStorage();
403 T* inlineStorage() { return mTail.storage(); }
405 T* beginNoCheck() const { return mBegin; }
407 T* endNoCheck() { return mBegin + mLength; }
409 const T* endNoCheck() const { return mBegin + mLength; }
411 #ifdef DEBUG
413 * The amount of explicitly allocated space in this vector that is immediately
414 * available to be filled by appending additional elements. This value is
415 * always greater than or equal to |length()| -- the vector's actual elements
416 * are implicitly reserved. This value is always less than or equal to
417 * |capacity()|. It may be explicitly increased using the |reserve()| method.
419 size_t reserved() const {
420 MOZ_ASSERT(mLength <= mTail.mReserved);
421 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
422 return mTail.mReserved;
424 #endif
426 bool internalEnsureCapacity(size_t aNeeded);
428 /* Append operations guaranteed to succeed due to pre-reserved space. */
429 template <typename U>
430 void internalAppend(U&& aU);
431 template <typename U, size_t O, class BP>
432 void internalAppendAll(const Vector<U, O, BP>& aU);
433 void internalAppendN(const T& aT, size_t aN);
434 template <typename U>
435 void internalAppend(const U* aBegin, size_t aLength);
436 template <typename U>
437 void internalMoveAppend(U* aBegin, size_t aLength);
439 public:
440 static const size_t sMaxInlineStorage = MinInlineCapacity;
442 typedef T ElementType;
444 explicit Vector(AllocPolicy = AllocPolicy());
445 Vector(Vector&&); /* Move constructor. */
446 Vector& operator=(Vector&&); /* Move assignment. */
447 ~Vector();
449 /* accessors */
451 const AllocPolicy& allocPolicy() const { return *this; }
453 AllocPolicy& allocPolicy() { return *this; }
455 enum { InlineLength = MinInlineCapacity };
457 size_t length() const { return mLength; }
459 bool empty() const { return mLength == 0; }
461 size_t capacity() const { return mTail.mCapacity; }
463 T* begin() {
464 MOZ_ASSERT(!mEntered);
465 return mBegin;
468 const T* begin() const {
469 MOZ_ASSERT(!mEntered);
470 return mBegin;
473 T* end() {
474 MOZ_ASSERT(!mEntered);
475 return mBegin + mLength;
478 const T* end() const {
479 MOZ_ASSERT(!mEntered);
480 return mBegin + mLength;
483 T& operator[](size_t aIndex) {
484 MOZ_ASSERT(!mEntered);
485 MOZ_ASSERT(aIndex < mLength);
486 return begin()[aIndex];
489 const T& operator[](size_t aIndex) const {
490 MOZ_ASSERT(!mEntered);
491 MOZ_ASSERT(aIndex < mLength);
492 return begin()[aIndex];
495 T& back() {
496 MOZ_ASSERT(!mEntered);
497 MOZ_ASSERT(!empty());
498 return *(end() - 1);
501 const T& back() const {
502 MOZ_ASSERT(!mEntered);
503 MOZ_ASSERT(!empty());
504 return *(end() - 1);
507 operator mozilla::Span<const T>() const {
508 // Explicitly specify template argument here to avoid instantiating Span<T>
509 // first and then implicitly converting to Span<const T>
510 return mozilla::Span<const T>{mBegin, mLength};
513 operator mozilla::Span<T>() { return mozilla::Span{mBegin, mLength}; }
515 class Range {
516 friend class Vector;
517 T* mCur;
518 T* mEnd;
519 Range(T* aCur, T* aEnd) : mCur(aCur), mEnd(aEnd) {
520 MOZ_ASSERT(aCur <= aEnd);
523 public:
524 bool empty() const { return mCur == mEnd; }
525 size_t remain() const { return PointerRangeSize(mCur, mEnd); }
526 T& front() const {
527 MOZ_ASSERT(!empty());
528 return *mCur;
530 void popFront() {
531 MOZ_ASSERT(!empty());
532 ++mCur;
534 T popCopyFront() {
535 MOZ_ASSERT(!empty());
536 return *mCur++;
540 class ConstRange {
541 friend class Vector;
542 const T* mCur;
543 const T* mEnd;
544 ConstRange(const T* aCur, const T* aEnd) : mCur(aCur), mEnd(aEnd) {
545 MOZ_ASSERT(aCur <= aEnd);
548 public:
549 bool empty() const { return mCur == mEnd; }
550 size_t remain() const { return PointerRangeSize(mCur, mEnd); }
551 const T& front() const {
552 MOZ_ASSERT(!empty());
553 return *mCur;
555 void popFront() {
556 MOZ_ASSERT(!empty());
557 ++mCur;
559 T popCopyFront() {
560 MOZ_ASSERT(!empty());
561 return *mCur++;
565 Range all() { return Range(begin(), end()); }
566 ConstRange all() const { return ConstRange(begin(), end()); }
568 /* mutators */
571 * Reverse the order of the elements in the vector in place.
573 void reverse();
576 * Given that the vector is empty, grow the internal capacity to |aRequest|,
577 * keeping the length 0.
579 MOZ_MUST_USE bool initCapacity(size_t aRequest);
582 * Given that the vector is empty, grow the internal capacity and length to
583 * |aRequest| leaving the elements' memory completely uninitialized (with all
584 * the associated hazards and caveats). This avoids the usual allocation-size
585 * rounding that happens in resize and overhead of initialization for elements
586 * that are about to be overwritten.
588 MOZ_MUST_USE bool initLengthUninitialized(size_t aRequest);
591 * If reserve(aRequest) succeeds and |aRequest >= length()|, then appending
592 * |aRequest - length()| elements, in any sequence of append/appendAll calls,
593 * is guaranteed to succeed.
595 * A request to reserve an amount less than the current length does not affect
596 * reserved space.
598 MOZ_MUST_USE bool reserve(size_t aRequest);
601 * Destroy elements in the range [end() - aIncr, end()). Does not deallocate
602 * or unreserve storage for those elements.
604 void shrinkBy(size_t aIncr);
607 * Destroy elements in the range [aNewLength, end()). Does not deallocate
608 * or unreserve storage for those elements.
610 void shrinkTo(size_t aNewLength);
612 /** Grow the vector by aIncr elements. */
613 MOZ_MUST_USE bool growBy(size_t aIncr);
615 /** Call shrinkBy or growBy based on whether newSize > length(). */
616 MOZ_MUST_USE bool resize(size_t aNewLength);
619 * Increase the length of the vector, but don't initialize the new elements
620 * -- leave them as uninitialized memory.
622 MOZ_MUST_USE bool growByUninitialized(size_t aIncr);
623 void infallibleGrowByUninitialized(size_t aIncr);
624 MOZ_MUST_USE bool resizeUninitialized(size_t aNewLength);
626 /** Shorthand for shrinkBy(length()). */
627 void clear();
629 /** Clears and releases any heap-allocated storage. */
630 void clearAndFree();
633 * Shrinks the storage to drop excess capacity if possible.
635 * The return value indicates whether the operation succeeded, otherwise, it
636 * represents an OOM. The bool can be safely ignored unless you want to
637 * provide the guarantee that `length() == capacity()`.
639 * For PODs, it calls the AllocPolicy's pod_realloc. For non-PODs, it moves
640 * the elements into the new storage.
642 bool shrinkStorageToFit();
645 * If true, appending |aNeeded| elements won't reallocate elements storage.
646 * This *doesn't* mean that infallibleAppend may be used! You still must
647 * reserve the extra space, even if this method indicates that appends won't
648 * need to reallocate elements storage.
650 bool canAppendWithoutRealloc(size_t aNeeded) const;
652 /** Potentially fallible append operations. */
655 * This can take either a T& or a T&&. Given a T&&, it moves |aU| into the
656 * vector, instead of copying it. If it fails, |aU| is left unmoved. ("We are
657 * not amused.")
659 template <typename U>
660 MOZ_MUST_USE bool append(U&& aU);
663 * Construct a T in-place as a new entry at the end of this vector.
665 template <typename... Args>
666 MOZ_MUST_USE bool emplaceBack(Args&&... aArgs) {
667 if (!growByUninitialized(1)) return false;
668 Impl::new_(&back(), std::forward<Args>(aArgs)...);
669 return true;
672 template <typename U, size_t O, class BP>
673 MOZ_MUST_USE bool appendAll(const Vector<U, O, BP>& aU);
674 template <typename U, size_t O, class BP>
675 MOZ_MUST_USE bool appendAll(Vector<U, O, BP>&& aU);
676 MOZ_MUST_USE bool appendN(const T& aT, size_t aN);
677 template <typename U>
678 MOZ_MUST_USE bool append(const U* aBegin, const U* aEnd);
679 template <typename U>
680 MOZ_MUST_USE bool append(const U* aBegin, size_t aLength);
681 template <typename U>
682 MOZ_MUST_USE bool moveAppend(U* aBegin, U* aEnd);
685 * Guaranteed-infallible append operations for use upon vectors whose
686 * memory has been pre-reserved. Don't use this if you haven't reserved the
687 * memory!
689 template <typename U>
690 void infallibleAppend(U&& aU) {
691 internalAppend(std::forward<U>(aU));
693 void infallibleAppendN(const T& aT, size_t aN) { internalAppendN(aT, aN); }
694 template <typename U>
695 void infallibleAppend(const U* aBegin, const U* aEnd) {
696 internalAppend(aBegin, PointerRangeSize(aBegin, aEnd));
698 template <typename U>
699 void infallibleAppend(const U* aBegin, size_t aLength) {
700 internalAppend(aBegin, aLength);
702 template <typename... Args>
703 void infallibleEmplaceBack(Args&&... aArgs) {
704 infallibleGrowByUninitialized(1);
705 Impl::new_(&back(), std::forward<Args>(aArgs)...);
708 void popBack();
710 T popCopy();
713 * If elements are stored in-place, return nullptr and leave this vector
714 * unmodified.
716 * Otherwise return this vector's elements buffer, and clear this vector as if
717 * by clearAndFree(). The caller now owns the buffer and is responsible for
718 * deallocating it consistent with this vector's AllocPolicy.
720 * N.B. Although a T*, only the range [0, length()) is constructed.
722 MOZ_MUST_USE T* extractRawBuffer();
725 * If elements are stored in-place, allocate a new buffer, move this vector's
726 * elements into it, and return that buffer.
728 * Otherwise return this vector's elements buffer. The caller now owns the
729 * buffer and is responsible for deallocating it consistent with this vector's
730 * AllocPolicy.
732 * This vector is cleared, as if by clearAndFree(), when this method
733 * succeeds. This method fails and returns nullptr only if new elements buffer
734 * allocation fails.
736 * N.B. Only the range [0, length()) of the returned buffer is constructed.
737 * If any of these elements are uninitialized (as growByUninitialized
738 * enables), behavior is undefined.
740 MOZ_MUST_USE T* extractOrCopyRawBuffer();
743 * Transfer ownership of an array of objects into the vector. The caller
744 * must have allocated the array in accordance with this vector's
745 * AllocPolicy.
747 * N.B. This call assumes that there are no uninitialized elements in the
748 * passed range [aP, aP + aLength). The range [aP + aLength, aP +
749 * aCapacity) must be allocated uninitialized memory.
751 void replaceRawBuffer(T* aP, size_t aLength, size_t aCapacity);
754 * Transfer ownership of an array of objects into the vector. The caller
755 * must have allocated the array in accordance with this vector's
756 * AllocPolicy.
758 * N.B. This call assumes that there are no uninitialized elements in the
759 * passed array.
761 void replaceRawBuffer(T* aP, size_t aLength);
764 * Places |aVal| at position |aP|, shifting existing elements from |aP| onward
765 * one position higher. On success, |aP| should not be reused because it'll
766 * be a dangling pointer if reallocation of the vector storage occurred; the
767 * return value should be used instead. On failure, nullptr is returned.
769 * Example usage:
771 * if (!(p = vec.insert(p, val))) {
772 * <handle failure>
774 * <keep working with p>
776 * This is inherently a linear-time operation. Be careful!
778 template <typename U>
779 MOZ_MUST_USE T* insert(T* aP, U&& aVal);
782 * Removes the element |aT|, which must fall in the bounds [begin, end),
783 * shifting existing elements from |aT + 1| onward one position lower.
785 void erase(T* aT);
788 * Removes the elements [|aBegin|, |aEnd|), which must fall in the bounds
789 * [begin, end), shifting existing elements from |aEnd| onward to aBegin's old
790 * position.
792 void erase(T* aBegin, T* aEnd);
795 * Removes all elements that satisfy the predicate, shifting existing elements
796 * lower to fill erased gaps.
798 template <typename Pred>
799 void eraseIf(Pred aPred);
802 * Removes all elements that compare equal to |aU|, shifting existing elements
803 * lower to fill erased gaps.
805 template <typename U>
806 void eraseIfEqual(const U& aU);
809 * Measure the size of the vector's heap-allocated storage.
811 size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
814 * Like sizeOfExcludingThis, but also measures the size of the vector
815 * object (which must be heap-allocated) itself.
817 size_t sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
819 void swap(Vector& aOther);
821 private:
822 Vector(const Vector&) = delete;
823 void operator=(const Vector&) = delete;
826 /* This does the re-entrancy check plus several other sanity checks. */
827 #define MOZ_REENTRANCY_GUARD_ET_AL \
828 ReentrancyGuard g(*this); \
829 MOZ_ASSERT_IF(usingInlineStorage(), mTail.mCapacity == kInlineCapacity); \
830 MOZ_ASSERT(reserved() <= mTail.mCapacity); \
831 MOZ_ASSERT(mLength <= reserved()); \
832 MOZ_ASSERT(mLength <= mTail.mCapacity)
834 /* Vector Implementation */
836 template <typename T, size_t N, class AP>
837 MOZ_ALWAYS_INLINE Vector<T, N, AP>::Vector(AP aAP)
838 : AP(std::move(aAP)),
839 mLength(0),
840 mTail(kInlineCapacity, 0)
841 #ifdef DEBUG
843 mEntered(false)
844 #endif
846 mBegin = inlineStorage();
849 /* Move constructor. */
850 template <typename T, size_t N, class AllocPolicy>
851 MOZ_ALWAYS_INLINE Vector<T, N, AllocPolicy>::Vector(Vector&& aRhs)
852 : AllocPolicy(std::move(aRhs))
853 #ifdef DEBUG
855 mEntered(false)
856 #endif
858 mLength = aRhs.mLength;
859 mTail.mCapacity = aRhs.mTail.mCapacity;
860 #ifdef DEBUG
861 mTail.mReserved = aRhs.mTail.mReserved;
862 #endif
864 if (aRhs.usingInlineStorage()) {
865 /* We can't move the buffer over in this case, so copy elements. */
866 mBegin = inlineStorage();
867 Impl::moveConstruct(mBegin, aRhs.beginNoCheck(), aRhs.endNoCheck());
869 * Leave aRhs's mLength, mBegin, mCapacity, and mReserved as they are.
870 * The elements in its in-line storage still need to be destroyed.
872 } else {
874 * Take src's buffer, and turn src into an empty vector using
875 * in-line storage.
877 mBegin = aRhs.mBegin;
878 aRhs.mBegin = aRhs.inlineStorage();
879 aRhs.mTail.mCapacity = kInlineCapacity;
880 aRhs.mLength = 0;
881 #ifdef DEBUG
882 aRhs.mTail.mReserved = 0;
883 #endif
887 /* Move assignment. */
888 template <typename T, size_t N, class AP>
889 MOZ_ALWAYS_INLINE Vector<T, N, AP>& Vector<T, N, AP>::operator=(Vector&& aRhs) {
890 MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
891 this->~Vector();
892 new (KnownNotNull, this) Vector(std::move(aRhs));
893 return *this;
896 template <typename T, size_t N, class AP>
897 MOZ_ALWAYS_INLINE Vector<T, N, AP>::~Vector() {
898 MOZ_REENTRANCY_GUARD_ET_AL;
899 Impl::destroy(beginNoCheck(), endNoCheck());
900 if (!usingInlineStorage()) {
901 this->free_(beginNoCheck(), mTail.mCapacity);
905 template <typename T, size_t N, class AP>
906 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::reverse() {
907 MOZ_REENTRANCY_GUARD_ET_AL;
908 T* elems = mBegin;
909 size_t len = mLength;
910 size_t mid = len / 2;
911 for (size_t i = 0; i < mid; i++) {
912 std::swap(elems[i], elems[len - i - 1]);
917 * This function will create a new heap buffer with capacity aNewCap,
918 * move all elements in the inline buffer to this new buffer,
919 * and fail on OOM.
921 template <typename T, size_t N, class AP>
922 inline bool Vector<T, N, AP>::convertToHeapStorage(size_t aNewCap) {
923 MOZ_ASSERT(usingInlineStorage());
925 /* Allocate buffer. */
926 MOZ_ASSERT(!detail::CapacityHasExcessSpace<T>(aNewCap));
927 T* newBuf = this->template pod_malloc<T>(aNewCap);
928 if (MOZ_UNLIKELY(!newBuf)) {
929 return false;
932 /* Copy inline elements into heap buffer. */
933 Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
934 Impl::destroy(beginNoCheck(), endNoCheck());
936 /* Switch in heap buffer. */
937 mBegin = newBuf;
938 /* mLength is unchanged. */
939 mTail.mCapacity = aNewCap;
940 return true;
943 template <typename T, size_t N, class AP>
944 MOZ_NEVER_INLINE bool Vector<T, N, AP>::growStorageBy(size_t aIncr) {
945 MOZ_ASSERT(mLength + aIncr > mTail.mCapacity);
948 * When choosing a new capacity, its size should is as close to 2**N bytes
949 * as possible. 2**N-sized requests are best because they are unlikely to
950 * be rounded up by the allocator. Asking for a 2**N number of elements
951 * isn't as good, because if sizeof(T) is not a power-of-two that would
952 * result in a non-2**N request size.
955 size_t newCap;
957 if (aIncr == 1) {
958 if (usingInlineStorage()) {
959 /* This case occurs in ~70--80% of the calls to this function. */
960 size_t newSize =
961 tl::RoundUpPow2<(kInlineCapacity + 1) * sizeof(T)>::value;
962 newCap = newSize / sizeof(T);
963 goto convert;
966 if (mLength == 0) {
967 /* This case occurs in ~0--10% of the calls to this function. */
968 newCap = 1;
969 goto grow;
972 /* This case occurs in ~15--20% of the calls to this function. */
975 * Will mLength * 4 *sizeof(T) overflow? This condition limits a vector
976 * to 1GB of memory on a 32-bit system, which is a reasonable limit. It
977 * also ensures that
979 * static_cast<char*>(end()) - static_cast<char*>(begin())
981 * doesn't overflow ptrdiff_t (see bug 510319).
983 if (MOZ_UNLIKELY(mLength & tl::MulOverflowMask<4 * sizeof(T)>::value)) {
984 this->reportAllocOverflow();
985 return false;
989 * If we reach here, the existing capacity will have a size that is already
990 * as close to 2^N as sizeof(T) will allow. Just double the capacity, and
991 * then there might be space for one more element.
993 newCap = mLength * 2;
994 if (detail::CapacityHasExcessSpace<T>(newCap)) {
995 newCap += 1;
997 } else {
998 /* This case occurs in ~2% of the calls to this function. */
999 size_t newMinCap = mLength + aIncr;
1001 /* Did mLength + aIncr overflow? Will newCap * sizeof(T) overflow? */
1002 if (MOZ_UNLIKELY(newMinCap < mLength ||
1003 newMinCap & tl::MulOverflowMask<2 * sizeof(T)>::value)) {
1004 this->reportAllocOverflow();
1005 return false;
1008 size_t newMinSize = newMinCap * sizeof(T);
1009 size_t newSize = RoundUpPow2(newMinSize);
1010 newCap = newSize / sizeof(T);
1013 if (usingInlineStorage()) {
1014 convert:
1015 return convertToHeapStorage(newCap);
1018 grow:
1019 return Impl::growTo(*this, newCap);
1022 template <typename T, size_t N, class AP>
1023 inline bool Vector<T, N, AP>::initCapacity(size_t aRequest) {
1024 MOZ_ASSERT(empty());
1025 MOZ_ASSERT(usingInlineStorage());
1026 if (aRequest == 0) {
1027 return true;
1029 T* newbuf = this->template pod_malloc<T>(aRequest);
1030 if (MOZ_UNLIKELY(!newbuf)) {
1031 return false;
1033 mBegin = newbuf;
1034 mTail.mCapacity = aRequest;
1035 #ifdef DEBUG
1036 mTail.mReserved = aRequest;
1037 #endif
1038 return true;
1041 template <typename T, size_t N, class AP>
1042 inline bool Vector<T, N, AP>::initLengthUninitialized(size_t aRequest) {
1043 if (!initCapacity(aRequest)) {
1044 return false;
1046 infallibleGrowByUninitialized(aRequest);
1047 return true;
1050 template <typename T, size_t N, class AP>
1051 inline bool Vector<T, N, AP>::maybeCheckSimulatedOOM(size_t aRequestedSize) {
1052 if (aRequestedSize <= N) {
1053 return true;
1056 #ifdef DEBUG
1057 if (aRequestedSize <= mTail.mReserved) {
1058 return true;
1060 #endif
1062 return allocPolicy().checkSimulatedOOM();
1065 template <typename T, size_t N, class AP>
1066 inline bool Vector<T, N, AP>::reserve(size_t aRequest) {
1067 MOZ_REENTRANCY_GUARD_ET_AL;
1068 if (aRequest > mTail.mCapacity) {
1069 if (MOZ_UNLIKELY(!growStorageBy(aRequest - mLength))) {
1070 return false;
1072 } else if (!maybeCheckSimulatedOOM(aRequest)) {
1073 return false;
1075 #ifdef DEBUG
1076 if (aRequest > mTail.mReserved) {
1077 mTail.mReserved = aRequest;
1079 MOZ_ASSERT(mLength <= mTail.mReserved);
1080 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1081 #endif
1082 return true;
1085 template <typename T, size_t N, class AP>
1086 inline void Vector<T, N, AP>::shrinkBy(size_t aIncr) {
1087 MOZ_REENTRANCY_GUARD_ET_AL;
1088 MOZ_ASSERT(aIncr <= mLength);
1089 Impl::destroy(endNoCheck() - aIncr, endNoCheck());
1090 mLength -= aIncr;
1093 template <typename T, size_t N, class AP>
1094 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::shrinkTo(size_t aNewLength) {
1095 MOZ_ASSERT(aNewLength <= mLength);
1096 shrinkBy(mLength - aNewLength);
1099 template <typename T, size_t N, class AP>
1100 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::growBy(size_t aIncr) {
1101 MOZ_REENTRANCY_GUARD_ET_AL;
1102 if (aIncr > mTail.mCapacity - mLength) {
1103 if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
1104 return false;
1106 } else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
1107 return false;
1109 MOZ_ASSERT(mLength + aIncr <= mTail.mCapacity);
1110 T* newend = endNoCheck() + aIncr;
1111 Impl::initialize(endNoCheck(), newend);
1112 mLength += aIncr;
1113 #ifdef DEBUG
1114 if (mLength > mTail.mReserved) {
1115 mTail.mReserved = mLength;
1117 #endif
1118 return true;
1121 template <typename T, size_t N, class AP>
1122 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::growByUninitialized(size_t aIncr) {
1123 MOZ_REENTRANCY_GUARD_ET_AL;
1124 if (aIncr > mTail.mCapacity - mLength) {
1125 if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
1126 return false;
1128 } else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
1129 return false;
1131 #ifdef DEBUG
1132 if (mLength + aIncr > mTail.mReserved) {
1133 mTail.mReserved = mLength + aIncr;
1135 #endif
1136 infallibleGrowByUninitialized(aIncr);
1137 return true;
1140 template <typename T, size_t N, class AP>
1141 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::infallibleGrowByUninitialized(
1142 size_t aIncr) {
1143 MOZ_ASSERT(mLength + aIncr <= reserved());
1144 mLength += aIncr;
1147 template <typename T, size_t N, class AP>
1148 inline bool Vector<T, N, AP>::resize(size_t aNewLength) {
1149 size_t curLength = mLength;
1150 if (aNewLength > curLength) {
1151 return growBy(aNewLength - curLength);
1153 shrinkBy(curLength - aNewLength);
1154 return true;
1157 template <typename T, size_t N, class AP>
1158 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::resizeUninitialized(
1159 size_t aNewLength) {
1160 size_t curLength = mLength;
1161 if (aNewLength > curLength) {
1162 return growByUninitialized(aNewLength - curLength);
1164 shrinkBy(curLength - aNewLength);
1165 return true;
1168 template <typename T, size_t N, class AP>
1169 inline void Vector<T, N, AP>::clear() {
1170 MOZ_REENTRANCY_GUARD_ET_AL;
1171 Impl::destroy(beginNoCheck(), endNoCheck());
1172 mLength = 0;
1175 template <typename T, size_t N, class AP>
1176 inline void Vector<T, N, AP>::clearAndFree() {
1177 clear();
1179 if (usingInlineStorage()) {
1180 return;
1182 this->free_(beginNoCheck(), mTail.mCapacity);
1183 mBegin = inlineStorage();
1184 mTail.mCapacity = kInlineCapacity;
1185 #ifdef DEBUG
1186 mTail.mReserved = 0;
1187 #endif
1190 template <typename T, size_t N, class AP>
1191 inline bool Vector<T, N, AP>::shrinkStorageToFit() {
1192 MOZ_REENTRANCY_GUARD_ET_AL;
1194 const auto length = this->length();
1195 if (usingInlineStorage() || length == capacity()) {
1196 return true;
1199 if (!length) {
1200 this->free_(beginNoCheck(), mTail.mCapacity);
1201 mBegin = inlineStorage();
1202 mTail.mCapacity = kInlineCapacity;
1203 #ifdef DEBUG
1204 mTail.mReserved = 0;
1205 #endif
1206 return true;
1209 T* newBuf;
1210 size_t newCap;
1211 if (length <= kInlineCapacity) {
1212 newBuf = inlineStorage();
1213 newCap = kInlineCapacity;
1214 } else {
1215 if (kElemIsPod) {
1216 newBuf = this->template pod_realloc<T>(beginNoCheck(), mTail.mCapacity,
1217 length);
1218 } else {
1219 newBuf = this->template pod_malloc<T>(length);
1221 if (MOZ_UNLIKELY(!newBuf)) {
1222 return false;
1224 newCap = length;
1226 if (!kElemIsPod || newBuf == inlineStorage()) {
1227 Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
1228 Impl::destroy(beginNoCheck(), endNoCheck());
1230 if (!kElemIsPod) {
1231 this->free_(beginNoCheck(), mTail.mCapacity);
1233 mBegin = newBuf;
1234 mTail.mCapacity = newCap;
1235 #ifdef DEBUG
1236 mTail.mReserved = length;
1237 #endif
1238 return true;
1241 template <typename T, size_t N, class AP>
1242 inline bool Vector<T, N, AP>::canAppendWithoutRealloc(size_t aNeeded) const {
1243 return mLength + aNeeded <= mTail.mCapacity;
1246 template <typename T, size_t N, class AP>
1247 template <typename U, size_t O, class BP>
1248 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppendAll(
1249 const Vector<U, O, BP>& aOther) {
1250 internalAppend(aOther.begin(), aOther.length());
1253 template <typename T, size_t N, class AP>
1254 template <typename U>
1255 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppend(U&& aU) {
1256 MOZ_ASSERT(mLength + 1 <= mTail.mReserved);
1257 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1258 Impl::new_(endNoCheck(), std::forward<U>(aU));
1259 ++mLength;
1262 template <typename T, size_t N, class AP>
1263 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendN(const T& aT, size_t aNeeded) {
1264 MOZ_REENTRANCY_GUARD_ET_AL;
1265 if (mLength + aNeeded > mTail.mCapacity) {
1266 if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
1267 return false;
1269 } else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
1270 return false;
1272 #ifdef DEBUG
1273 if (mLength + aNeeded > mTail.mReserved) {
1274 mTail.mReserved = mLength + aNeeded;
1276 #endif
1277 internalAppendN(aT, aNeeded);
1278 return true;
1281 template <typename T, size_t N, class AP>
1282 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppendN(const T& aT,
1283 size_t aNeeded) {
1284 MOZ_ASSERT(mLength + aNeeded <= mTail.mReserved);
1285 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1286 Impl::copyConstructN(endNoCheck(), aNeeded, aT);
1287 mLength += aNeeded;
1290 template <typename T, size_t N, class AP>
1291 template <typename U>
1292 inline T* Vector<T, N, AP>::insert(T* aP, U&& aVal) {
1293 MOZ_ASSERT(begin() <= aP);
1294 MOZ_ASSERT(aP <= end());
1295 size_t pos = aP - begin();
1296 MOZ_ASSERT(pos <= mLength);
1297 size_t oldLength = mLength;
1298 if (pos == oldLength) {
1299 if (!append(std::forward<U>(aVal))) {
1300 return nullptr;
1302 } else {
1303 T oldBack = std::move(back());
1304 if (!append(std::move(oldBack))) {
1305 return nullptr;
1307 for (size_t i = oldLength - 1; i > pos; --i) {
1308 (*this)[i] = std::move((*this)[i - 1]);
1310 (*this)[pos] = std::forward<U>(aVal);
1312 return begin() + pos;
1315 template <typename T, size_t N, class AP>
1316 inline void Vector<T, N, AP>::erase(T* aIt) {
1317 MOZ_ASSERT(begin() <= aIt);
1318 MOZ_ASSERT(aIt < end());
1319 while (aIt + 1 < end()) {
1320 *aIt = std::move(*(aIt + 1));
1321 ++aIt;
1323 popBack();
1326 template <typename T, size_t N, class AP>
1327 inline void Vector<T, N, AP>::erase(T* aBegin, T* aEnd) {
1328 MOZ_ASSERT(begin() <= aBegin);
1329 MOZ_ASSERT(aBegin <= aEnd);
1330 MOZ_ASSERT(aEnd <= end());
1331 while (aEnd < end()) {
1332 *aBegin++ = std::move(*aEnd++);
1334 shrinkBy(aEnd - aBegin);
1337 template <typename T, size_t N, class AP>
1338 template <typename Pred>
1339 void Vector<T, N, AP>::eraseIf(Pred aPred) {
1340 // remove_if finds the first element to be erased, and then efficiently move-
1341 // assigns elements to effectively overwrite elements that satisfy the
1342 // predicate. It returns the new end pointer, after which there are only
1343 // moved-from elements ready to be destroyed, so we just need to shrink the
1344 // vector accordingly.
1345 T* newEnd = std::remove_if(begin(), end(),
1346 [&aPred](const T& aT) { return aPred(aT); });
1347 MOZ_ASSERT(newEnd <= end());
1348 shrinkBy(end() - newEnd);
1351 template <typename T, size_t N, class AP>
1352 template <typename U>
1353 void Vector<T, N, AP>::eraseIfEqual(const U& aU) {
1354 return eraseIf([&aU](const T& aT) { return aT == aU; });
1357 template <typename T, size_t N, class AP>
1358 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::internalEnsureCapacity(
1359 size_t aNeeded) {
1360 if (mLength + aNeeded > mTail.mCapacity) {
1361 if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
1362 return false;
1364 } else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
1365 return false;
1367 #ifdef DEBUG
1368 if (mLength + aNeeded > mTail.mReserved) {
1369 mTail.mReserved = mLength + aNeeded;
1371 #endif
1372 return true;
1375 template <typename T, size_t N, class AP>
1376 template <typename U>
1377 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(const U* aInsBegin,
1378 const U* aInsEnd) {
1379 MOZ_REENTRANCY_GUARD_ET_AL;
1380 const size_t needed = PointerRangeSize(aInsBegin, aInsEnd);
1381 if (!internalEnsureCapacity(needed)) {
1382 return false;
1384 internalAppend(aInsBegin, needed);
1385 return true;
1388 template <typename T, size_t N, class AP>
1389 template <typename U>
1390 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalAppend(const U* aInsBegin,
1391 size_t aInsLength) {
1392 MOZ_ASSERT(mLength + aInsLength <= mTail.mReserved);
1393 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1394 Impl::copyConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
1395 mLength += aInsLength;
1398 template <typename T, size_t N, class AP>
1399 template <typename U>
1400 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::moveAppend(U* aInsBegin, U* aInsEnd) {
1401 MOZ_REENTRANCY_GUARD_ET_AL;
1402 const size_t needed = PointerRangeSize(aInsBegin, aInsEnd);
1403 if (!internalEnsureCapacity(needed)) {
1404 return false;
1406 internalMoveAppend(aInsBegin, needed);
1407 return true;
1410 template <typename T, size_t N, class AP>
1411 template <typename U>
1412 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::internalMoveAppend(U* aInsBegin,
1413 size_t aInsLength) {
1414 MOZ_ASSERT(mLength + aInsLength <= mTail.mReserved);
1415 MOZ_ASSERT(mTail.mReserved <= mTail.mCapacity);
1416 Impl::moveConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
1417 mLength += aInsLength;
1420 template <typename T, size_t N, class AP>
1421 template <typename U>
1422 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(U&& aU) {
1423 MOZ_REENTRANCY_GUARD_ET_AL;
1424 if (mLength == mTail.mCapacity) {
1425 if (MOZ_UNLIKELY(!growStorageBy(1))) {
1426 return false;
1428 } else if (!maybeCheckSimulatedOOM(mLength + 1)) {
1429 return false;
1431 #ifdef DEBUG
1432 if (mLength + 1 > mTail.mReserved) {
1433 mTail.mReserved = mLength + 1;
1435 #endif
1436 internalAppend(std::forward<U>(aU));
1437 return true;
1440 template <typename T, size_t N, class AP>
1441 template <typename U, size_t O, class BP>
1442 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendAll(
1443 const Vector<U, O, BP>& aOther) {
1444 return append(aOther.begin(), aOther.length());
1447 template <typename T, size_t N, class AP>
1448 template <typename U, size_t O, class BP>
1449 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::appendAll(Vector<U, O, BP>&& aOther) {
1450 if (empty() && capacity() < aOther.length()) {
1451 *this = std::move(aOther);
1452 return true;
1455 if (moveAppend(aOther.begin(), aOther.end())) {
1456 aOther.clearAndFree();
1457 return true;
1460 return false;
1463 template <typename T, size_t N, class AP>
1464 template <class U>
1465 MOZ_ALWAYS_INLINE bool Vector<T, N, AP>::append(const U* aInsBegin,
1466 size_t aInsLength) {
1467 return append(aInsBegin, aInsBegin + aInsLength);
1470 template <typename T, size_t N, class AP>
1471 MOZ_ALWAYS_INLINE void Vector<T, N, AP>::popBack() {
1472 MOZ_REENTRANCY_GUARD_ET_AL;
1473 MOZ_ASSERT(!empty());
1474 --mLength;
1475 endNoCheck()->~T();
1478 template <typename T, size_t N, class AP>
1479 MOZ_ALWAYS_INLINE T Vector<T, N, AP>::popCopy() {
1480 T ret = back();
1481 popBack();
1482 return ret;
1485 template <typename T, size_t N, class AP>
1486 inline T* Vector<T, N, AP>::extractRawBuffer() {
1487 MOZ_REENTRANCY_GUARD_ET_AL;
1489 if (usingInlineStorage()) {
1490 return nullptr;
1493 T* ret = mBegin;
1494 mBegin = inlineStorage();
1495 mLength = 0;
1496 mTail.mCapacity = kInlineCapacity;
1497 #ifdef DEBUG
1498 mTail.mReserved = 0;
1499 #endif
1500 return ret;
1503 template <typename T, size_t N, class AP>
1504 inline T* Vector<T, N, AP>::extractOrCopyRawBuffer() {
1505 if (T* ret = extractRawBuffer()) {
1506 return ret;
1509 MOZ_REENTRANCY_GUARD_ET_AL;
1511 T* copy = this->template pod_malloc<T>(mLength);
1512 if (!copy) {
1513 return nullptr;
1516 Impl::moveConstruct(copy, beginNoCheck(), endNoCheck());
1517 Impl::destroy(beginNoCheck(), endNoCheck());
1518 mBegin = inlineStorage();
1519 mLength = 0;
1520 mTail.mCapacity = kInlineCapacity;
1521 #ifdef DEBUG
1522 mTail.mReserved = 0;
1523 #endif
1524 return copy;
1527 template <typename T, size_t N, class AP>
1528 inline void Vector<T, N, AP>::replaceRawBuffer(T* aP, size_t aLength,
1529 size_t aCapacity) {
1530 MOZ_REENTRANCY_GUARD_ET_AL;
1532 /* Destroy what we have. */
1533 Impl::destroy(beginNoCheck(), endNoCheck());
1534 if (!usingInlineStorage()) {
1535 this->free_(beginNoCheck(), mTail.mCapacity);
1538 /* Take in the new buffer. */
1539 if (aCapacity <= kInlineCapacity) {
1541 * We convert to inline storage if possible, even though aP might
1542 * otherwise be acceptable. Maybe this behaviour should be
1543 * specifiable with an argument to this function.
1545 mBegin = inlineStorage();
1546 mLength = aLength;
1547 mTail.mCapacity = kInlineCapacity;
1548 Impl::moveConstruct(mBegin, aP, aP + aLength);
1549 Impl::destroy(aP, aP + aLength);
1550 this->free_(aP, aCapacity);
1551 } else {
1552 mBegin = aP;
1553 mLength = aLength;
1554 mTail.mCapacity = aCapacity;
1556 #ifdef DEBUG
1557 mTail.mReserved = aCapacity;
1558 #endif
1561 template <typename T, size_t N, class AP>
1562 inline void Vector<T, N, AP>::replaceRawBuffer(T* aP, size_t aLength) {
1563 replaceRawBuffer(aP, aLength, aLength);
1566 template <typename T, size_t N, class AP>
1567 inline size_t Vector<T, N, AP>::sizeOfExcludingThis(
1568 MallocSizeOf aMallocSizeOf) const {
1569 return usingInlineStorage() ? 0 : aMallocSizeOf(beginNoCheck());
1572 template <typename T, size_t N, class AP>
1573 inline size_t Vector<T, N, AP>::sizeOfIncludingThis(
1574 MallocSizeOf aMallocSizeOf) const {
1575 return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf);
1578 template <typename T, size_t N, class AP>
1579 inline void Vector<T, N, AP>::swap(Vector& aOther) {
1580 static_assert(N == 0, "still need to implement this for N != 0");
1582 // This only works when inline storage is always empty.
1583 if (!usingInlineStorage() && aOther.usingInlineStorage()) {
1584 aOther.mBegin = mBegin;
1585 mBegin = inlineStorage();
1586 } else if (usingInlineStorage() && !aOther.usingInlineStorage()) {
1587 mBegin = aOther.mBegin;
1588 aOther.mBegin = aOther.inlineStorage();
1589 } else if (!usingInlineStorage() && !aOther.usingInlineStorage()) {
1590 std::swap(mBegin, aOther.mBegin);
1591 } else {
1592 // This case is a no-op, since we'd set both to use their inline storage.
1595 std::swap(mLength, aOther.mLength);
1596 std::swap(mTail.mCapacity, aOther.mTail.mCapacity);
1597 #ifdef DEBUG
1598 std::swap(mTail.mReserved, aOther.mTail.mReserved);
1599 #endif
1602 } // namespace mozilla
1604 #endif /* mozilla_Vector_h */