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 "mozilla/Alignment.h"
13 #include "mozilla/AllocPolicy.h"
14 #include "mozilla/ArrayUtils.h" // for PointerRangeSize
15 #include "mozilla/Assertions.h"
16 #include "mozilla/Attributes.h"
17 #include "mozilla/MathAlgorithms.h"
18 #include "mozilla/MemoryReporting.h"
19 #include "mozilla/Move.h"
20 #include "mozilla/OperatorNewExtensions.h"
21 #include "mozilla/ReentrancyGuard.h"
22 #include "mozilla/TemplateLib.h"
23 #include "mozilla/TypeTraits.h"
24 #include "mozilla/Span.h"
26 #include <new> // for placement new
30 template <typename T
, size_t N
, class AllocPolicy
>
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.
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
>
53 * Constructs an object in the uninitialized memory at *aDst with aArgs.
55 template <typename
... Args
>
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
) {
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
) {
78 * Copy-constructs objects in the uninitialized range
79 * [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
82 static inline void copyConstruct(T
* aDst
, const U
* aSrcStart
,
84 MOZ_ASSERT(aSrcStart
<= aSrcEnd
);
85 for (const U
* p
= aSrcStart
; p
< aSrcEnd
; ++p
, ++aDst
) {
91 * Move-constructs objects in the uninitialized range
92 * [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
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
) {
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
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
)) {
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
);
134 /* aV.mLength is unchanged. */
135 aV
.mTail
.mCapacity
= aNewCap
;
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
145 template <typename T
, size_t N
, class AP
>
146 struct VectorImpl
<T
, N
, AP
, true> {
147 template <typename
... Args
>
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
)...);
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
) {
175 template <typename U
>
176 static inline void copyConstruct(T
* aDst
, const U
* aSrcStart
,
179 * See above memset comment. Also, notice that copyConstruct is
180 * currently templated (T != U), so memcpy won't work without
183 * memcpy(aDst, aSrcStart, sizeof(T) * (aSrcEnd - aSrcStart));
185 MOZ_ASSERT(aSrcStart
<= aSrcEnd
);
186 for (const U
* p
= aSrcStart
; p
< aSrcEnd
; ++p
, ++aDst
) {
191 template <typename U
>
192 static inline void moveConstruct(T
* aDst
, const U
* aSrcStart
,
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
) {
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
));
207 aV
.template pod_realloc
<T
>(aV
.mBegin
, aV
.mTail
.mCapacity
, aNewCap
);
208 if (MOZ_UNLIKELY(!newbuf
)) {
212 /* aV.mLength is unchanged. */
213 aV
.mTail
.mCapacity
= aNewCap
;
217 static inline void podResizeToFit(Vector
<T
, N
, AP
>& aV
) {
218 if (aV
.usingInlineStorage() || aV
.mLength
== aV
.mTail
.mCapacity
) {
222 aV
.free_(aV
.mBegin
, aV
.mTail
.mCapacity
);
223 aV
.mBegin
= aV
.inlineStorage();
224 aV
.mTail
.mCapacity
= aV
.kInlineCapacity
;
226 aV
.mTail
.mReserved
= 0;
231 aV
.template pod_realloc
<T
>(aV
.mBegin
, aV
.mTail
.mCapacity
, aV
.mLength
);
232 if (MOZ_UNLIKELY(!newbuf
)) {
236 aV
.mTail
.mCapacity
= aV
.mLength
;
238 aV
.mTail
.mReserved
= aV
.mLength
;
243 // A struct for TestVector.cpp to access private internal fields.
244 // DO NOT DEFINE IN YOUR OWN CODE.
245 struct VectorTesting
;
247 } // namespace detail
250 * STL-like container providing a short-lived, dynamic buffer. Vector calls the
251 * constructors/destructors of all elements stored in its internal buffer, so
252 * non-PODs may be safely used. Additionally, Vector will store the first N
253 * elements in-place before resorting to dynamic allocation.
256 * - default and copy constructible, assignable, destructible
257 * - operations do not throw
258 * MinInlineCapacity requirements:
259 * - any value, however, MinInlineCapacity is clamped to min/max values
261 * - see "Allocation policies" in AllocPolicy.h (defaults to
262 * mozilla::MallocAllocPolicy)
264 * Vector is not reentrant: T member functions called during Vector member
265 * functions must not call back into the same object!
267 template <typename T
, size_t MinInlineCapacity
= 0,
268 class AllocPolicy
= MallocAllocPolicy
>
269 class MOZ_NON_PARAM Vector final
: private AllocPolicy
{
272 static const bool kElemIsPod
= IsPod
<T
>::value
;
273 typedef detail::VectorImpl
<T
, MinInlineCapacity
, AllocPolicy
, kElemIsPod
>
275 friend struct detail::VectorImpl
<T
, MinInlineCapacity
, AllocPolicy
,
278 friend struct detail::VectorTesting
;
280 MOZ_MUST_USE
bool growStorageBy(size_t aIncr
);
281 MOZ_MUST_USE
bool convertToHeapStorage(size_t aNewCap
);
282 MOZ_MUST_USE
bool maybeCheckSimulatedOOM(size_t aRequestedSize
);
284 /* magic constants */
287 * The maximum space allocated for inline element storage.
289 * We reduce space by what the AllocPolicy base class and prior Vector member
290 * fields likely consume to attempt to play well with binary size classes.
292 static constexpr size_t kMaxInlineBytes
=
294 (sizeof(AllocPolicy
) + sizeof(T
*) + sizeof(size_t) + sizeof(size_t));
297 * The number of T elements of inline capacity built into this Vector. This
298 * is usually |MinInlineCapacity|, but it may be less (or zero!) for large T.
300 * We use a partially-specialized template (not explicit specialization, which
301 * is only allowed at namespace scope) to compute this value. The benefit is
302 * that |sizeof(T)| need not be computed, and |T| doesn't have to be fully
303 * defined at the time |Vector<T>| appears, if no inline storage is requested.
305 template <size_t MinimumInlineCapacity
, size_t Dummy
>
306 struct ComputeCapacity
{
307 static constexpr size_t value
=
308 tl::Min
<MinimumInlineCapacity
, kMaxInlineBytes
/ sizeof(T
)>::value
;
311 template <size_t Dummy
>
312 struct ComputeCapacity
<0, Dummy
> {
313 static constexpr size_t value
= 0;
316 /** The actual inline capacity in number of elements T. This may be zero! */
317 static constexpr size_t kInlineCapacity
=
318 ComputeCapacity
<MinInlineCapacity
, 0>::value
;
323 * Pointer to the buffer, be it inline or heap-allocated. Only [mBegin,
324 * mBegin + mLength) hold valid constructed T objects. The range [mBegin +
325 * mLength, mBegin + mCapacity) holds uninitialized memory. The range
326 * [mBegin + mLength, mBegin + mReserved) also holds uninitialized memory
327 * previously allocated by a call to reserve().
331 /* Number of elements in the vector. */
335 * Memory used to store capacity, reserved element count (debug builds only),
336 * and inline storage. The simple "answer" is:
342 * alignas(T) unsigned char mBytes[kInlineCapacity * sizeof(T)];
344 * but there are complications. First, C++ forbids zero-sized arrays that
345 * might result. Second, we don't want zero capacity to affect Vector's size
346 * (even empty classes take up a byte, unless they're base classes).
348 * Yet again, we eliminate the zero-sized array using partial specialization.
349 * And we eliminate potential size hit by putting capacity/reserved in one
350 * struct, then putting the array (if any) in a derived struct. If no array
351 * is needed, the derived struct won't consume extra space.
353 struct CapacityAndReserved
{
354 explicit CapacityAndReserved(size_t aCapacity
, size_t aReserved
)
355 : mCapacity(aCapacity
)
362 CapacityAndReserved() = default;
364 /* Max number of elements storable in the vector without resizing. */
368 /* Max elements of reserved or used space in this vector. */
373 // Silence warnings about this struct possibly being padded dued to the
374 // alignas() in it -- there's nothing we can do to avoid it.
376 # pragma warning(push)
377 # pragma warning(disable : 4324)
380 template <size_t Capacity
, size_t Dummy
>
381 struct CRAndStorage
: CapacityAndReserved
{
382 explicit CRAndStorage(size_t aCapacity
, size_t aReserved
)
383 : CapacityAndReserved(aCapacity
, aReserved
) {}
384 CRAndStorage() = default;
386 alignas(T
) unsigned char mBytes
[Capacity
* sizeof(T
)];
388 // GCC fails due to -Werror=strict-aliasing if |mBytes| is directly cast to
389 // T*. Indirecting through this function addresses the problem.
390 void* data() { return mBytes
; }
392 T
* storage() { return static_cast<T
*>(data()); }
395 template <size_t Dummy
>
396 struct CRAndStorage
<0, Dummy
> : CapacityAndReserved
{
397 explicit CRAndStorage(size_t aCapacity
, size_t aReserved
)
398 : CapacityAndReserved(aCapacity
, aReserved
) {}
399 CRAndStorage() = default;
402 // If this returns |nullptr|, functions like |Vector::begin()| would too,
403 // breaking callers that pass a vector's elements as pointer/length to
404 // code that bounds its operation by length but (even just as a sanity
405 // check) always wants a non-null pointer. Fake up an aligned, non-null
406 // pointer to support these callers.
407 return reinterpret_cast<T
*>(sizeof(T
));
411 CRAndStorage
<kInlineCapacity
, 0> mTail
;
414 # pragma warning(pop)
418 friend class ReentrancyGuard
;
422 /* private accessors */
424 bool usingInlineStorage() const {
425 return mBegin
== const_cast<Vector
*>(this)->inlineStorage();
428 T
* inlineStorage() { return mTail
.storage(); }
430 T
* beginNoCheck() const { return mBegin
; }
432 T
* endNoCheck() { return mBegin
+ mLength
; }
434 const T
* endNoCheck() const { return mBegin
+ mLength
; }
438 * The amount of explicitly allocated space in this vector that is immediately
439 * available to be filled by appending additional elements. This value is
440 * always greater than or equal to |length()| -- the vector's actual elements
441 * are implicitly reserved. This value is always less than or equal to
442 * |capacity()|. It may be explicitly increased using the |reserve()| method.
444 size_t reserved() const {
445 MOZ_ASSERT(mLength
<= mTail
.mReserved
);
446 MOZ_ASSERT(mTail
.mReserved
<= mTail
.mCapacity
);
447 return mTail
.mReserved
;
451 /* Append operations guaranteed to succeed due to pre-reserved space. */
452 template <typename U
>
453 void internalAppend(U
&& aU
);
454 template <typename U
, size_t O
, class BP
>
455 void internalAppendAll(const Vector
<U
, O
, BP
>& aU
);
456 void internalAppendN(const T
& aT
, size_t aN
);
457 template <typename U
>
458 void internalAppend(const U
* aBegin
, size_t aLength
);
461 static const size_t sMaxInlineStorage
= MinInlineCapacity
;
463 typedef T ElementType
;
465 explicit Vector(AllocPolicy
= AllocPolicy());
466 Vector(Vector
&&); /* Move constructor. */
467 Vector
& operator=(Vector
&&); /* Move assignment. */
472 const AllocPolicy
& allocPolicy() const { return *this; }
474 AllocPolicy
& allocPolicy() { return *this; }
476 enum { InlineLength
= MinInlineCapacity
};
478 size_t length() const { return mLength
; }
480 bool empty() const { return mLength
== 0; }
482 size_t capacity() const { return mTail
.mCapacity
; }
485 MOZ_ASSERT(!mEntered
);
489 const T
* begin() const {
490 MOZ_ASSERT(!mEntered
);
495 MOZ_ASSERT(!mEntered
);
496 return mBegin
+ mLength
;
499 const T
* end() const {
500 MOZ_ASSERT(!mEntered
);
501 return mBegin
+ mLength
;
504 T
& operator[](size_t aIndex
) {
505 MOZ_ASSERT(!mEntered
);
506 MOZ_ASSERT(aIndex
< mLength
);
507 return begin()[aIndex
];
510 const T
& operator[](size_t aIndex
) const {
511 MOZ_ASSERT(!mEntered
);
512 MOZ_ASSERT(aIndex
< mLength
);
513 return begin()[aIndex
];
517 MOZ_ASSERT(!mEntered
);
518 MOZ_ASSERT(!empty());
522 const T
& back() const {
523 MOZ_ASSERT(!mEntered
);
524 MOZ_ASSERT(!empty());
528 operator mozilla::Span
<const T
>() const {
529 return mozilla::MakeSpan(mBegin
, mLength
);
532 operator mozilla::Span
<T
>() { return mozilla::MakeSpan(mBegin
, mLength
); }
538 Range(T
* aCur
, T
* aEnd
) : mCur(aCur
), mEnd(aEnd
) {
539 MOZ_ASSERT(aCur
<= aEnd
);
543 bool empty() const { return mCur
== mEnd
; }
544 size_t remain() const { return PointerRangeSize(mCur
, mEnd
); }
546 MOZ_ASSERT(!empty());
550 MOZ_ASSERT(!empty());
554 MOZ_ASSERT(!empty());
563 ConstRange(const T
* aCur
, const T
* aEnd
) : mCur(aCur
), mEnd(aEnd
) {
564 MOZ_ASSERT(aCur
<= aEnd
);
568 bool empty() const { return mCur
== mEnd
; }
569 size_t remain() const { return PointerRangeSize(mCur
, mEnd
); }
570 const T
& front() const {
571 MOZ_ASSERT(!empty());
575 MOZ_ASSERT(!empty());
579 MOZ_ASSERT(!empty());
584 Range
all() { return Range(begin(), end()); }
585 ConstRange
all() const { return ConstRange(begin(), end()); }
590 * Reverse the order of the elements in the vector in place.
595 * Given that the vector is empty, grow the internal capacity to |aRequest|,
596 * keeping the length 0.
598 MOZ_MUST_USE
bool initCapacity(size_t aRequest
);
601 * Given that the vector is empty, grow the internal capacity and length to
602 * |aRequest| leaving the elements' memory completely uninitialized (with all
603 * the associated hazards and caveats). This avoids the usual allocation-size
604 * rounding that happens in resize and overhead of initialization for elements
605 * that are about to be overwritten.
607 MOZ_MUST_USE
bool initLengthUninitialized(size_t aRequest
);
610 * If reserve(aRequest) succeeds and |aRequest >= length()|, then appending
611 * |aRequest - length()| elements, in any sequence of append/appendAll calls,
612 * is guaranteed to succeed.
614 * A request to reserve an amount less than the current length does not affect
617 MOZ_MUST_USE
bool reserve(size_t aRequest
);
620 * Destroy elements in the range [end() - aIncr, end()). Does not deallocate
621 * or unreserve storage for those elements.
623 void shrinkBy(size_t aIncr
);
626 * Destroy elements in the range [aNewLength, end()). Does not deallocate
627 * or unreserve storage for those elements.
629 void shrinkTo(size_t aNewLength
);
631 /** Grow the vector by aIncr elements. */
632 MOZ_MUST_USE
bool growBy(size_t aIncr
);
634 /** Call shrinkBy or growBy based on whether newSize > length(). */
635 MOZ_MUST_USE
bool resize(size_t aNewLength
);
638 * Increase the length of the vector, but don't initialize the new elements
639 * -- leave them as uninitialized memory.
641 MOZ_MUST_USE
bool growByUninitialized(size_t aIncr
);
642 void infallibleGrowByUninitialized(size_t aIncr
);
643 MOZ_MUST_USE
bool resizeUninitialized(size_t aNewLength
);
645 /** Shorthand for shrinkBy(length()). */
648 /** Clears and releases any heap-allocated storage. */
652 * Calls the AllocPolicy's pod_realloc to release excess capacity. Since
653 * realloc is only safe on PODs, this method fails to compile if IsPod<T>
656 void podResizeToFit();
659 * If true, appending |aNeeded| elements won't reallocate elements storage.
660 * This *doesn't* mean that infallibleAppend may be used! You still must
661 * reserve the extra space, even if this method indicates that appends won't
662 * need to reallocate elements storage.
664 bool canAppendWithoutRealloc(size_t aNeeded
) const;
666 /** Potentially fallible append operations. */
669 * This can take either a T& or a T&&. Given a T&&, it moves |aU| into the
670 * vector, instead of copying it. If it fails, |aU| is left unmoved. ("We are
673 template <typename U
>
674 MOZ_MUST_USE
bool append(U
&& aU
);
677 * Construct a T in-place as a new entry at the end of this vector.
679 template <typename
... Args
>
680 MOZ_MUST_USE
bool emplaceBack(Args
&&... aArgs
) {
681 if (!growByUninitialized(1)) return false;
682 Impl::new_(&back(), std::forward
<Args
>(aArgs
)...);
686 template <typename U
, size_t O
, class BP
>
687 MOZ_MUST_USE
bool appendAll(const Vector
<U
, O
, BP
>& aU
);
688 MOZ_MUST_USE
bool appendN(const T
& aT
, size_t aN
);
689 template <typename U
>
690 MOZ_MUST_USE
bool append(const U
* aBegin
, const U
* aEnd
);
691 template <typename U
>
692 MOZ_MUST_USE
bool append(const U
* aBegin
, size_t aLength
);
695 * Guaranteed-infallible append operations for use upon vectors whose
696 * memory has been pre-reserved. Don't use this if you haven't reserved the
699 template <typename U
>
700 void infallibleAppend(U
&& aU
) {
701 internalAppend(std::forward
<U
>(aU
));
703 void infallibleAppendN(const T
& aT
, size_t aN
) { internalAppendN(aT
, aN
); }
704 template <typename U
>
705 void infallibleAppend(const U
* aBegin
, const U
* aEnd
) {
706 internalAppend(aBegin
, PointerRangeSize(aBegin
, aEnd
));
708 template <typename U
>
709 void infallibleAppend(const U
* aBegin
, size_t aLength
) {
710 internalAppend(aBegin
, aLength
);
712 template <typename
... Args
>
713 void infallibleEmplaceBack(Args
&&... aArgs
) {
714 infallibleGrowByUninitialized(1);
715 Impl::new_(&back(), std::forward
<Args
>(aArgs
)...);
723 * If elements are stored in-place, return nullptr and leave this vector
726 * Otherwise return this vector's elements buffer, and clear this vector as if
727 * by clearAndFree(). The caller now owns the buffer and is responsible for
728 * deallocating it consistent with this vector's AllocPolicy.
730 * N.B. Although a T*, only the range [0, length()) is constructed.
732 MOZ_MUST_USE T
* extractRawBuffer();
735 * If elements are stored in-place, allocate a new buffer, move this vector's
736 * elements into it, and return that buffer.
738 * Otherwise return this vector's elements buffer. The caller now owns the
739 * buffer and is responsible for deallocating it consistent with this vector's
742 * This vector is cleared, as if by clearAndFree(), when this method
743 * succeeds. This method fails and returns nullptr only if new elements buffer
746 * N.B. Only the range [0, length()) of the returned buffer is constructed.
747 * If any of these elements are uninitialized (as growByUninitialized
748 * enables), behavior is undefined.
750 MOZ_MUST_USE T
* extractOrCopyRawBuffer();
753 * Transfer ownership of an array of objects into the vector. The caller
754 * must have allocated the array in accordance with this vector's
757 * N.B. This call assumes that there are no uninitialized elements in the
758 * passed range [aP, aP + aLength). The range [aP + aLength, aP +
759 * aCapacity) must be allocated uninitialized memory.
761 void replaceRawBuffer(T
* aP
, size_t aLength
, size_t aCapacity
);
764 * Transfer ownership of an array of objects into the vector. The caller
765 * must have allocated the array in accordance with this vector's
768 * N.B. This call assumes that there are no uninitialized elements in the
771 void replaceRawBuffer(T
* aP
, size_t aLength
);
774 * Places |aVal| at position |aP|, shifting existing elements from |aP| onward
775 * one position higher. On success, |aP| should not be reused because it'll
776 * be a dangling pointer if reallocation of the vector storage occurred; the
777 * return value should be used instead. On failure, nullptr is returned.
781 * if (!(p = vec.insert(p, val))) {
784 * <keep working with p>
786 * This is inherently a linear-time operation. Be careful!
788 template <typename U
>
789 MOZ_MUST_USE T
* insert(T
* aP
, U
&& aVal
);
792 * Removes the element |aT|, which must fall in the bounds [begin, end),
793 * shifting existing elements from |aT + 1| onward one position lower.
798 * Removes the elements [|aBegin|, |aEnd|), which must fall in the bounds
799 * [begin, end), shifting existing elements from |aEnd + 1| onward to aBegin's
802 void erase(T
* aBegin
, T
* aEnd
);
805 * Removes all elements that satisfy the predicate, shifting existing elements
806 * lower to fill erased gaps.
808 template <typename Pred
>
809 void eraseIf(Pred aPred
);
812 * Removes all elements that compare equal to |aU|, shifting existing elements
813 * lower to fill erased gaps.
815 template <typename U
>
816 void eraseIfEqual(const U
& aU
);
819 * Measure the size of the vector's heap-allocated storage.
821 size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf
) const;
824 * Like sizeOfExcludingThis, but also measures the size of the vector
825 * object (which must be heap-allocated) itself.
827 size_t sizeOfIncludingThis(MallocSizeOf aMallocSizeOf
) const;
829 void swap(Vector
& aOther
);
832 Vector(const Vector
&) = delete;
833 void operator=(const Vector
&) = delete;
836 /* This does the re-entrancy check plus several other sanity checks. */
837 #define MOZ_REENTRANCY_GUARD_ET_AL \
838 ReentrancyGuard g(*this); \
839 MOZ_ASSERT_IF(usingInlineStorage(), mTail.mCapacity == kInlineCapacity); \
840 MOZ_ASSERT(reserved() <= mTail.mCapacity); \
841 MOZ_ASSERT(mLength <= reserved()); \
842 MOZ_ASSERT(mLength <= mTail.mCapacity)
844 /* Vector Implementation */
846 template <typename T
, size_t N
, class AP
>
847 MOZ_ALWAYS_INLINE Vector
<T
, N
, AP
>::Vector(AP aAP
)
848 : AP(std::move(aAP
)),
850 mTail(kInlineCapacity
, 0)
856 mBegin
= inlineStorage();
859 /* Move constructor. */
860 template <typename T
, size_t N
, class AllocPolicy
>
861 MOZ_ALWAYS_INLINE Vector
<T
, N
, AllocPolicy
>::Vector(Vector
&& aRhs
)
862 : AllocPolicy(std::move(aRhs
))
868 mLength
= aRhs
.mLength
;
869 mTail
.mCapacity
= aRhs
.mTail
.mCapacity
;
871 mTail
.mReserved
= aRhs
.mTail
.mReserved
;
874 if (aRhs
.usingInlineStorage()) {
875 /* We can't move the buffer over in this case, so copy elements. */
876 mBegin
= inlineStorage();
877 Impl::moveConstruct(mBegin
, aRhs
.beginNoCheck(), aRhs
.endNoCheck());
879 * Leave aRhs's mLength, mBegin, mCapacity, and mReserved as they are.
880 * The elements in its in-line storage still need to be destroyed.
884 * Take src's buffer, and turn src into an empty vector using
887 mBegin
= aRhs
.mBegin
;
888 aRhs
.mBegin
= aRhs
.inlineStorage();
889 aRhs
.mTail
.mCapacity
= kInlineCapacity
;
892 aRhs
.mTail
.mReserved
= 0;
897 /* Move assignment. */
898 template <typename T
, size_t N
, class AP
>
899 MOZ_ALWAYS_INLINE Vector
<T
, N
, AP
>& Vector
<T
, N
, AP
>::operator=(Vector
&& aRhs
) {
900 MOZ_ASSERT(this != &aRhs
, "self-move assignment is prohibited");
902 new (KnownNotNull
, this) Vector(std::move(aRhs
));
906 template <typename T
, size_t N
, class AP
>
907 MOZ_ALWAYS_INLINE Vector
<T
, N
, AP
>::~Vector() {
908 MOZ_REENTRANCY_GUARD_ET_AL
;
909 Impl::destroy(beginNoCheck(), endNoCheck());
910 if (!usingInlineStorage()) {
911 this->free_(beginNoCheck(), mTail
.mCapacity
);
915 template <typename T
, size_t N
, class AP
>
916 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::reverse() {
917 MOZ_REENTRANCY_GUARD_ET_AL
;
919 size_t len
= mLength
;
920 size_t mid
= len
/ 2;
921 for (size_t i
= 0; i
< mid
; i
++) {
922 Swap(elems
[i
], elems
[len
- i
- 1]);
927 * This function will create a new heap buffer with capacity aNewCap,
928 * move all elements in the inline buffer to this new buffer,
931 template <typename T
, size_t N
, class AP
>
932 inline bool Vector
<T
, N
, AP
>::convertToHeapStorage(size_t aNewCap
) {
933 MOZ_ASSERT(usingInlineStorage());
935 /* Allocate buffer. */
936 MOZ_ASSERT(!detail::CapacityHasExcessSpace
<T
>(aNewCap
));
937 T
* newBuf
= this->template pod_malloc
<T
>(aNewCap
);
938 if (MOZ_UNLIKELY(!newBuf
)) {
942 /* Copy inline elements into heap buffer. */
943 Impl::moveConstruct(newBuf
, beginNoCheck(), endNoCheck());
944 Impl::destroy(beginNoCheck(), endNoCheck());
946 /* Switch in heap buffer. */
948 /* mLength is unchanged. */
949 mTail
.mCapacity
= aNewCap
;
953 template <typename T
, size_t N
, class AP
>
954 MOZ_NEVER_INLINE
bool Vector
<T
, N
, AP
>::growStorageBy(size_t aIncr
) {
955 MOZ_ASSERT(mLength
+ aIncr
> mTail
.mCapacity
);
958 * When choosing a new capacity, its size should is as close to 2**N bytes
959 * as possible. 2**N-sized requests are best because they are unlikely to
960 * be rounded up by the allocator. Asking for a 2**N number of elements
961 * isn't as good, because if sizeof(T) is not a power-of-two that would
962 * result in a non-2**N request size.
968 if (usingInlineStorage()) {
969 /* This case occurs in ~70--80% of the calls to this function. */
971 tl::RoundUpPow2
<(kInlineCapacity
+ 1) * sizeof(T
)>::value
;
972 newCap
= newSize
/ sizeof(T
);
977 /* This case occurs in ~0--10% of the calls to this function. */
982 /* This case occurs in ~15--20% of the calls to this function. */
985 * Will mLength * 4 *sizeof(T) overflow? This condition limits a vector
986 * to 1GB of memory on a 32-bit system, which is a reasonable limit. It
989 * static_cast<char*>(end()) - static_cast<char*>(begin())
991 * doesn't overflow ptrdiff_t (see bug 510319).
993 if (MOZ_UNLIKELY(mLength
& tl::MulOverflowMask
<4 * sizeof(T
)>::value
)) {
994 this->reportAllocOverflow();
999 * If we reach here, the existing capacity will have a size that is already
1000 * as close to 2^N as sizeof(T) will allow. Just double the capacity, and
1001 * then there might be space for one more element.
1003 newCap
= mLength
* 2;
1004 if (detail::CapacityHasExcessSpace
<T
>(newCap
)) {
1008 /* This case occurs in ~2% of the calls to this function. */
1009 size_t newMinCap
= mLength
+ aIncr
;
1011 /* Did mLength + aIncr overflow? Will newCap * sizeof(T) overflow? */
1012 if (MOZ_UNLIKELY(newMinCap
< mLength
||
1013 newMinCap
& tl::MulOverflowMask
<2 * sizeof(T
)>::value
)) {
1014 this->reportAllocOverflow();
1018 size_t newMinSize
= newMinCap
* sizeof(T
);
1019 size_t newSize
= RoundUpPow2(newMinSize
);
1020 newCap
= newSize
/ sizeof(T
);
1023 if (usingInlineStorage()) {
1025 return convertToHeapStorage(newCap
);
1029 return Impl::growTo(*this, newCap
);
1032 template <typename T
, size_t N
, class AP
>
1033 inline bool Vector
<T
, N
, AP
>::initCapacity(size_t aRequest
) {
1034 MOZ_ASSERT(empty());
1035 MOZ_ASSERT(usingInlineStorage());
1036 if (aRequest
== 0) {
1039 T
* newbuf
= this->template pod_malloc
<T
>(aRequest
);
1040 if (MOZ_UNLIKELY(!newbuf
)) {
1044 mTail
.mCapacity
= aRequest
;
1046 mTail
.mReserved
= aRequest
;
1051 template <typename T
, size_t N
, class AP
>
1052 inline bool Vector
<T
, N
, AP
>::initLengthUninitialized(size_t aRequest
) {
1053 if (!initCapacity(aRequest
)) {
1056 infallibleGrowByUninitialized(aRequest
);
1060 template <typename T
, size_t N
, class AP
>
1061 inline bool Vector
<T
, N
, AP
>::maybeCheckSimulatedOOM(size_t aRequestedSize
) {
1062 if (aRequestedSize
<= N
) {
1067 if (aRequestedSize
<= mTail
.mReserved
) {
1072 return allocPolicy().checkSimulatedOOM();
1075 template <typename T
, size_t N
, class AP
>
1076 inline bool Vector
<T
, N
, AP
>::reserve(size_t aRequest
) {
1077 MOZ_REENTRANCY_GUARD_ET_AL
;
1078 if (aRequest
> mTail
.mCapacity
) {
1079 if (MOZ_UNLIKELY(!growStorageBy(aRequest
- mLength
))) {
1082 } else if (!maybeCheckSimulatedOOM(aRequest
)) {
1086 if (aRequest
> mTail
.mReserved
) {
1087 mTail
.mReserved
= aRequest
;
1089 MOZ_ASSERT(mLength
<= mTail
.mReserved
);
1090 MOZ_ASSERT(mTail
.mReserved
<= mTail
.mCapacity
);
1095 template <typename T
, size_t N
, class AP
>
1096 inline void Vector
<T
, N
, AP
>::shrinkBy(size_t aIncr
) {
1097 MOZ_REENTRANCY_GUARD_ET_AL
;
1098 MOZ_ASSERT(aIncr
<= mLength
);
1099 Impl::destroy(endNoCheck() - aIncr
, endNoCheck());
1103 template <typename T
, size_t N
, class AP
>
1104 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::shrinkTo(size_t aNewLength
) {
1105 MOZ_ASSERT(aNewLength
<= mLength
);
1106 shrinkBy(mLength
- aNewLength
);
1109 template <typename T
, size_t N
, class AP
>
1110 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::growBy(size_t aIncr
) {
1111 MOZ_REENTRANCY_GUARD_ET_AL
;
1112 if (aIncr
> mTail
.mCapacity
- mLength
) {
1113 if (MOZ_UNLIKELY(!growStorageBy(aIncr
))) {
1116 } else if (!maybeCheckSimulatedOOM(mLength
+ aIncr
)) {
1119 MOZ_ASSERT(mLength
+ aIncr
<= mTail
.mCapacity
);
1120 T
* newend
= endNoCheck() + aIncr
;
1121 Impl::initialize(endNoCheck(), newend
);
1124 if (mLength
> mTail
.mReserved
) {
1125 mTail
.mReserved
= mLength
;
1131 template <typename T
, size_t N
, class AP
>
1132 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::growByUninitialized(size_t aIncr
) {
1133 MOZ_REENTRANCY_GUARD_ET_AL
;
1134 if (aIncr
> mTail
.mCapacity
- mLength
) {
1135 if (MOZ_UNLIKELY(!growStorageBy(aIncr
))) {
1138 } else if (!maybeCheckSimulatedOOM(mLength
+ aIncr
)) {
1142 if (mLength
+ aIncr
> mTail
.mReserved
) {
1143 mTail
.mReserved
= mLength
+ aIncr
;
1146 infallibleGrowByUninitialized(aIncr
);
1150 template <typename T
, size_t N
, class AP
>
1151 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::infallibleGrowByUninitialized(
1153 MOZ_ASSERT(mLength
+ aIncr
<= reserved());
1157 template <typename T
, size_t N
, class AP
>
1158 inline bool Vector
<T
, N
, AP
>::resize(size_t aNewLength
) {
1159 size_t curLength
= mLength
;
1160 if (aNewLength
> curLength
) {
1161 return growBy(aNewLength
- curLength
);
1163 shrinkBy(curLength
- aNewLength
);
1167 template <typename T
, size_t N
, class AP
>
1168 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::resizeUninitialized(
1169 size_t aNewLength
) {
1170 size_t curLength
= mLength
;
1171 if (aNewLength
> curLength
) {
1172 return growByUninitialized(aNewLength
- curLength
);
1174 shrinkBy(curLength
- aNewLength
);
1178 template <typename T
, size_t N
, class AP
>
1179 inline void Vector
<T
, N
, AP
>::clear() {
1180 MOZ_REENTRANCY_GUARD_ET_AL
;
1181 Impl::destroy(beginNoCheck(), endNoCheck());
1185 template <typename T
, size_t N
, class AP
>
1186 inline void Vector
<T
, N
, AP
>::clearAndFree() {
1189 if (usingInlineStorage()) {
1192 this->free_(beginNoCheck(), mTail
.mCapacity
);
1193 mBegin
= inlineStorage();
1194 mTail
.mCapacity
= kInlineCapacity
;
1196 mTail
.mReserved
= 0;
1200 template <typename T
, size_t N
, class AP
>
1201 inline void Vector
<T
, N
, AP
>::podResizeToFit() {
1202 // This function is only defined if IsPod is true and will fail to compile
1204 Impl::podResizeToFit(*this);
1207 template <typename T
, size_t N
, class AP
>
1208 inline bool Vector
<T
, N
, AP
>::canAppendWithoutRealloc(size_t aNeeded
) const {
1209 return mLength
+ aNeeded
<= mTail
.mCapacity
;
1212 template <typename T
, size_t N
, class AP
>
1213 template <typename U
, size_t O
, class BP
>
1214 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::internalAppendAll(
1215 const Vector
<U
, O
, BP
>& aOther
) {
1216 internalAppend(aOther
.begin(), aOther
.length());
1219 template <typename T
, size_t N
, class AP
>
1220 template <typename U
>
1221 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::internalAppend(U
&& aU
) {
1222 MOZ_ASSERT(mLength
+ 1 <= mTail
.mReserved
);
1223 MOZ_ASSERT(mTail
.mReserved
<= mTail
.mCapacity
);
1224 Impl::new_(endNoCheck(), std::forward
<U
>(aU
));
1228 template <typename T
, size_t N
, class AP
>
1229 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::appendN(const T
& aT
, size_t aNeeded
) {
1230 MOZ_REENTRANCY_GUARD_ET_AL
;
1231 if (mLength
+ aNeeded
> mTail
.mCapacity
) {
1232 if (MOZ_UNLIKELY(!growStorageBy(aNeeded
))) {
1235 } else if (!maybeCheckSimulatedOOM(mLength
+ aNeeded
)) {
1239 if (mLength
+ aNeeded
> mTail
.mReserved
) {
1240 mTail
.mReserved
= mLength
+ aNeeded
;
1243 internalAppendN(aT
, aNeeded
);
1247 template <typename T
, size_t N
, class AP
>
1248 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::internalAppendN(const T
& aT
,
1250 MOZ_ASSERT(mLength
+ aNeeded
<= mTail
.mReserved
);
1251 MOZ_ASSERT(mTail
.mReserved
<= mTail
.mCapacity
);
1252 Impl::copyConstructN(endNoCheck(), aNeeded
, aT
);
1256 template <typename T
, size_t N
, class AP
>
1257 template <typename U
>
1258 inline T
* Vector
<T
, N
, AP
>::insert(T
* aP
, U
&& aVal
) {
1259 MOZ_ASSERT(begin() <= aP
);
1260 MOZ_ASSERT(aP
<= end());
1261 size_t pos
= aP
- begin();
1262 MOZ_ASSERT(pos
<= mLength
);
1263 size_t oldLength
= mLength
;
1264 if (pos
== oldLength
) {
1265 if (!append(std::forward
<U
>(aVal
))) {
1269 T oldBack
= std::move(back());
1270 if (!append(std::move(oldBack
))) {
1273 for (size_t i
= oldLength
- 1; i
> pos
; --i
) {
1274 (*this)[i
] = std::move((*this)[i
- 1]);
1276 (*this)[pos
] = std::forward
<U
>(aVal
);
1278 return begin() + pos
;
1281 template <typename T
, size_t N
, class AP
>
1282 inline void Vector
<T
, N
, AP
>::erase(T
* aIt
) {
1283 MOZ_ASSERT(begin() <= aIt
);
1284 MOZ_ASSERT(aIt
< end());
1285 while (aIt
+ 1 < end()) {
1286 *aIt
= std::move(*(aIt
+ 1));
1292 template <typename T
, size_t N
, class AP
>
1293 inline void Vector
<T
, N
, AP
>::erase(T
* aBegin
, T
* aEnd
) {
1294 MOZ_ASSERT(begin() <= aBegin
);
1295 MOZ_ASSERT(aBegin
<= aEnd
);
1296 MOZ_ASSERT(aEnd
<= end());
1297 while (aEnd
< end()) {
1298 *aBegin
++ = std::move(*aEnd
++);
1300 shrinkBy(aEnd
- aBegin
);
1303 template <typename T
, size_t N
, class AP
>
1304 template <typename Pred
>
1305 void Vector
<T
, N
, AP
>::eraseIf(Pred aPred
) {
1306 // remove_if finds the first element to be erased, and then efficiently move-
1307 // assigns elements to effectively overwrite elements that satisfy the
1308 // predicate. It returns the new end pointer, after which there are only
1309 // moved-from elements ready to be destroyed, so we just need to shrink the
1310 // vector accordingly.
1311 T
* newEnd
= std::remove_if(begin(), end(),
1312 [&aPred
](const T
& aT
) { return aPred(aT
); });
1313 MOZ_ASSERT(newEnd
<= end());
1314 shrinkBy(end() - newEnd
);
1317 template <typename T
, size_t N
, class AP
>
1318 template <typename U
>
1319 void Vector
<T
, N
, AP
>::eraseIfEqual(const U
& aU
) {
1320 return eraseIf([&aU
](const T
& aT
) { return aT
== aU
; });
1323 template <typename T
, size_t N
, class AP
>
1324 template <typename U
>
1325 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::append(const U
* aInsBegin
,
1327 MOZ_REENTRANCY_GUARD_ET_AL
;
1328 size_t aNeeded
= PointerRangeSize(aInsBegin
, aInsEnd
);
1329 if (mLength
+ aNeeded
> mTail
.mCapacity
) {
1330 if (MOZ_UNLIKELY(!growStorageBy(aNeeded
))) {
1333 } else if (!maybeCheckSimulatedOOM(mLength
+ aNeeded
)) {
1337 if (mLength
+ aNeeded
> mTail
.mReserved
) {
1338 mTail
.mReserved
= mLength
+ aNeeded
;
1341 internalAppend(aInsBegin
, aNeeded
);
1345 template <typename T
, size_t N
, class AP
>
1346 template <typename U
>
1347 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::internalAppend(const U
* aInsBegin
,
1348 size_t aInsLength
) {
1349 MOZ_ASSERT(mLength
+ aInsLength
<= mTail
.mReserved
);
1350 MOZ_ASSERT(mTail
.mReserved
<= mTail
.mCapacity
);
1351 Impl::copyConstruct(endNoCheck(), aInsBegin
, aInsBegin
+ aInsLength
);
1352 mLength
+= aInsLength
;
1355 template <typename T
, size_t N
, class AP
>
1356 template <typename U
>
1357 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::append(U
&& aU
) {
1358 MOZ_REENTRANCY_GUARD_ET_AL
;
1359 if (mLength
== mTail
.mCapacity
) {
1360 if (MOZ_UNLIKELY(!growStorageBy(1))) {
1363 } else if (!maybeCheckSimulatedOOM(mLength
+ 1)) {
1367 if (mLength
+ 1 > mTail
.mReserved
) {
1368 mTail
.mReserved
= mLength
+ 1;
1371 internalAppend(std::forward
<U
>(aU
));
1375 template <typename T
, size_t N
, class AP
>
1376 template <typename U
, size_t O
, class BP
>
1377 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::appendAll(
1378 const Vector
<U
, O
, BP
>& aOther
) {
1379 return append(aOther
.begin(), aOther
.length());
1382 template <typename T
, size_t N
, class AP
>
1384 MOZ_ALWAYS_INLINE
bool Vector
<T
, N
, AP
>::append(const U
* aInsBegin
,
1385 size_t aInsLength
) {
1386 return append(aInsBegin
, aInsBegin
+ aInsLength
);
1389 template <typename T
, size_t N
, class AP
>
1390 MOZ_ALWAYS_INLINE
void Vector
<T
, N
, AP
>::popBack() {
1391 MOZ_REENTRANCY_GUARD_ET_AL
;
1392 MOZ_ASSERT(!empty());
1397 template <typename T
, size_t N
, class AP
>
1398 MOZ_ALWAYS_INLINE T Vector
<T
, N
, AP
>::popCopy() {
1404 template <typename T
, size_t N
, class AP
>
1405 inline T
* Vector
<T
, N
, AP
>::extractRawBuffer() {
1406 MOZ_REENTRANCY_GUARD_ET_AL
;
1408 if (usingInlineStorage()) {
1413 mBegin
= inlineStorage();
1415 mTail
.mCapacity
= kInlineCapacity
;
1417 mTail
.mReserved
= 0;
1422 template <typename T
, size_t N
, class AP
>
1423 inline T
* Vector
<T
, N
, AP
>::extractOrCopyRawBuffer() {
1424 if (T
* ret
= extractRawBuffer()) {
1428 MOZ_REENTRANCY_GUARD_ET_AL
;
1430 T
* copy
= this->template pod_malloc
<T
>(mLength
);
1435 Impl::moveConstruct(copy
, beginNoCheck(), endNoCheck());
1436 Impl::destroy(beginNoCheck(), endNoCheck());
1437 mBegin
= inlineStorage();
1439 mTail
.mCapacity
= kInlineCapacity
;
1441 mTail
.mReserved
= 0;
1446 template <typename T
, size_t N
, class AP
>
1447 inline void Vector
<T
, N
, AP
>::replaceRawBuffer(T
* aP
, size_t aLength
,
1449 MOZ_REENTRANCY_GUARD_ET_AL
;
1451 /* Destroy what we have. */
1452 Impl::destroy(beginNoCheck(), endNoCheck());
1453 if (!usingInlineStorage()) {
1454 this->free_(beginNoCheck(), mTail
.mCapacity
);
1457 /* Take in the new buffer. */
1458 if (aCapacity
<= kInlineCapacity
) {
1460 * We convert to inline storage if possible, even though aP might
1461 * otherwise be acceptable. Maybe this behaviour should be
1462 * specifiable with an argument to this function.
1464 mBegin
= inlineStorage();
1466 mTail
.mCapacity
= kInlineCapacity
;
1467 Impl::moveConstruct(mBegin
, aP
, aP
+ aLength
);
1468 Impl::destroy(aP
, aP
+ aLength
);
1469 this->free_(aP
, aCapacity
);
1473 mTail
.mCapacity
= aCapacity
;
1476 mTail
.mReserved
= aCapacity
;
1480 template <typename T
, size_t N
, class AP
>
1481 inline void Vector
<T
, N
, AP
>::replaceRawBuffer(T
* aP
, size_t aLength
) {
1482 replaceRawBuffer(aP
, aLength
, aLength
);
1485 template <typename T
, size_t N
, class AP
>
1486 inline size_t Vector
<T
, N
, AP
>::sizeOfExcludingThis(
1487 MallocSizeOf aMallocSizeOf
) const {
1488 return usingInlineStorage() ? 0 : aMallocSizeOf(beginNoCheck());
1491 template <typename T
, size_t N
, class AP
>
1492 inline size_t Vector
<T
, N
, AP
>::sizeOfIncludingThis(
1493 MallocSizeOf aMallocSizeOf
) const {
1494 return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf
);
1497 template <typename T
, size_t N
, class AP
>
1498 inline void Vector
<T
, N
, AP
>::swap(Vector
& aOther
) {
1499 static_assert(N
== 0, "still need to implement this for N != 0");
1501 // This only works when inline storage is always empty.
1502 if (!usingInlineStorage() && aOther
.usingInlineStorage()) {
1503 aOther
.mBegin
= mBegin
;
1504 mBegin
= inlineStorage();
1505 } else if (usingInlineStorage() && !aOther
.usingInlineStorage()) {
1506 mBegin
= aOther
.mBegin
;
1507 aOther
.mBegin
= aOther
.inlineStorage();
1508 } else if (!usingInlineStorage() && !aOther
.usingInlineStorage()) {
1509 Swap(mBegin
, aOther
.mBegin
);
1511 // This case is a no-op, since we'd set both to use their inline storage.
1514 Swap(mLength
, aOther
.mLength
);
1515 Swap(mTail
.mCapacity
, aOther
.mTail
.mCapacity
);
1517 Swap(mTail
.mReserved
, aOther
.mTail
.mReserved
);
1521 } // namespace mozilla
1523 #endif /* mozilla_Vector_h */