Files
palemoon27/mfbt/Vector.h
T
roytam1 869fcffa1f import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1262453: IonMonkey - Don't add resume point for constants, r=nbp (b5f8021cf8)
- Bug 1240521 - IonBuilder processSwitchEnd returns ControlStatus_Error on allocation errors. r=h4writer (997738a608)
- Bug 1261361 - Make Vector::infallibleGrowByUninitialized check mReserved instead of mCapacity. r=Waldo (1326f9121f)
- Bug 1254203 - SnapshotIterator::maybeRead use AutoEnterOOMUnsafeRegion instead of MOZ_CRASH. r=jandem (54fa438206)
- Bug 1261324 - Fix bogus return address for star generators' .throw being observed by the profiler in Debugger's onExceptionUnwind in Baseline. (r=jandem) (6e998a7d64)
- Bug 1263899 - Fix readFrameArgsAndLocals to always use maybeRead since it can't recover instructions. (r=nbp) (59288f533b)
- Bug 1265748 - Enable non-Object path in IonBuilder::inlineIsCallable. r=shu (c0de95d84f)
- Bug 1186006 - Assert that all blocks have an entry resume point, excepts for unreachable one added by the Value Numbering. r=sunfish (32e9a56e59)
- Bug 1257089 - Weaken the assertions to account for unreachable annotation added by Range Analysis. r=sunfish (87caf2580a)
- Bug 1263888 - Push TypeBarrier after ArraySlice. r=jandem (28e089137b)
- Bug 1246552 - IonMonkey: Don't eliminate toString on objects and symbols, r=jandem (da7c99d316)
- Bug 1258632 - Distinguish signed/unsigned MDiv/MMod in GVN. r=bbouvier (22dbf55c27)
- Bug 1263532 - Do not make RegExpPrototypeOptimizable and RegExpInstanceOptimizable movable. r=h4writer (9b4c75397f)
- Bug 1248202 - Handle an OOM case in optimization tracking. r=jandem (045d4c002c)
- Bug 1257929 - Special case OSR block to avoid bloating safeInsertTop condition. r=h4writer (738c4abd63)
- Bug 1239075 - RangeAnalysis: Assume that all captured results are used in bailing branches. r=h4writer (a8d6b4b29d)
- Bug 1256702 - Skip the operand of MAssertRecoveredOnBailout encoded in the snapshot. r=h4writer (a4705253af)
- Bug 1263895 - IonMonkey: Check return value of all functions in GVN, r=jandem (351ba8d825)
- Bug 1262453 - IonMonkey: Remove Nops which try to decrease liveness of instructions, but fail, r=bhackett (5f7e221a7a)
- Bug 1124397 - More crash diagnostics. r=nbp (4abe9de4f3)
- Bug 1124397 - Temporary diagnostic patch to help figure out what's going on. r=efaust a=kwierso (d7416f77c4)
- Bug 1124397 - Add more crash diagnostics. r=efaust (130cd14bff)
- Bug 1252326 - Reorder MacroAssembler flush functions to follow the header order. r=bbouvier (c361f37707)
- Quick follow-up to bug 1251225 to fix a possibly overflowing left shift (found by Coverity, rs=h4writer on irc) (7b33f428a5)
- Bug 1256588 - IonMonkey: MIPS64: Don't skip nops on Loongson by runtime flag. r=huangwenjun06 (7aa8d6b5d9)
- Bug 1265601 - OdinMonkey: MIPS: Fix i64 testing infrastructure in Baldr. r=bbouvier (f5dfa86b4d)
- Bug 1263090 - OdinMonkey: MIPS: Implement AsmReinterpretFromI64/ToI64. r=bbouvier (3c0bed4a24)
- Bug 1263094 - IonMonkey: MIPS: Change members to non-virtual in CodeGenerator. r=arai (195bfaa03f)
- Bug 1258253 - IonMonkey: MIPS64: Fix MacroAssembler::branchTestMagic. r=arai (6420433ec0)
- Bug 1258253 - IonMonkey: MIPS64: Fix MacroAssembler::branchValueIsNurseryObject. r=arai (271089e08e)
- Bug 1249895 - Replace unsigned __int128_t with __uint128_t. r=hev (8d29fb5234)
- Bug 1258907 - IonMonkey: MIPS32: Fix MacroAssembler::branchValueIsNurseryObject. r=arai (18ae03452f)
- Bug 1256588 - IonMonkey: MIPS: Implement isLoongson. r=huangwenjun06 (d82314788d)
- Bug 1260694 - IonMonkey: MIPS: Inline get MIPS flags. r=huangwenjun06 (caf060f2ed)
- Bug 1258277 - OdinMonkey: MIPS: Leave ARCH_BITS for CPUID. r=huangwenjun06 (8d2ce72d21)
- Bug 1263090 - OdinMonkey: MIPS: Implement AsmReinterpret. r=bbouvier (d1a98954e1)
- Bug 1258293 - IonMonkey: MIPS: Refactor float-point Not by conditional move. r=arai (269c5d3be9)
- Bug 1254369 - IonMonkey: MIPS: Clean up broken assertions. r=arai (aedad3d82a)
- Bug 1254369 - IonMonkey: MIPS: Fix header files missing in MacroAssembler. r=arai (c8b291d450)
- Bug 1250370 - IonMonkey: MIPS: Fix stack alignment checking in EmitBaselineEnterStubFrame. r=nbp (77cd114731)
- Bug 1247312 - Don't generate invalid LDRD instructions on ARM. r=nbp (440e999cf7)
- Bug 1258999: Check for OOM after flushing pools when merging MacroAssemblers; r=efaust (ad22c913ff)
- Bug 1234408 - Fix ARM assembler to bind labels on OOM to silence ~Label assert. r=bbouvier (a4b3f716b1)
- Bug 1254106 - IonMonkey: Disable assertion of incorrect labels on OOM on ARM, like on x86 and x64, r=jandem (86b143ff42)
- cleanup (1a71c7a761)
- Bug 1263040 - Add Intl.getCanonicalLocales. r=jwalden (999911b0b3)
- Bug 1259540 - Fix missing header include that breaks FILES_PER_UNIFIED_FILE=1. r=till (247f599cd9)
- Bug 1263118 - Fix replace substitution without any capture. r=till (c6d500b683)
- Bug 1265449: Remove static assumption about SIMD objects; r=jonco (cfe92c4636)
- Bug 1259600 - Divert typed arrays passed to Array.sort to a typed array specific sort; r=till (f91e07bb1b)
- Bug 1260673 - Stop using _DefineDataProperty in MoveHoles; r=jorendorff (e476f59b01)
- Bug 1260673 - Ensure that Array.sort works with sealed objects; r=jorendorff (6aba58306d)
- Bug 1261813: Ensure the argument to wasmBinaryToText always has a buffer; r=luke (7c86782427)
- Bug 1204562 - GetMethod should not box the receiver argument. r=till (8977188519)
- Bug 552533 - Support thiscall Win32 ABI for js-ctypes. Original patch is dwitte. r=jorendorff (3179fe82fb)
- Bug 1252958 - Implement Debugger.adoptDebuggeeValue;r=jimb (bb1eed3603)
- Bug 1261904 - Refactor Debugger.Object.getErrorMessageName; r=jimb (414b03ce1a)
- Bug 1256560 - Implement AnimationEffectTiming WebIDL. r=birtles, r=smaug (123bc00be6)
- Bug 1244643 - Part 1: Implement AnimationEffectTiming easing. r=hiro (958cde39eb)
- Bug 1244640 - implement AnimationEffectTiming iterations r=hiro (30420e3e22)
- Bug 1255710 - Part 0: Rename test_animation_property_state.html to test_animation_performance_warning.html. r=birtles (7c463acc9d)
- Bug 1255710 - Part 1: Remove all unnecessary t.step_func. r=birtles (b8586a3f6c)
- Bug 1255710 - Part 2: Enable tests for 'transform-style: preserve-3d'. r=birtles (35038639a8)
- Bug 1254840 - Use longer animation duration to avoid intermittent failures on slow platforms. r=dholbert (a0574ebd59)
- Bug 1254881 - Fix animation duration in dom/animation/test/chrome/test_running_on_compositor.html. r=hiro (db038a67ec)
- Bug 1256503 - Part 1: Use MS_PER_SEC instead of human-misreadable digits in animation tests. r=dholbert (ad420d4760)
- Bug 1256503 - Part 2: Increase some test animation durations to be at least 100 seconds. r=dholbert (8ea14b30f0)
- Bug 1244633 - Part 1: implement AnimationEffectTiming delay. r=birtles (94d1be534a)
- Bug 1244633 - Part 2: append tests for delay. r=birtles (cd685f1fe4)
- Bug 1244643 - Part 2: Append tests for easing. r=hiro (9f563a4636)
- Bug 1263631 - Update web-platform-tests to revision 8d896c2015ab1e50ad00a0013700f87813d9364c, a=testonly (71b25211b1)
- Bug 1244643 - Part 3: Move common test cases to a new file. r=hiro (7ee5afd9db)
- Bug 1255718. Remove the unused JSContext argument of AudioBuffer::Create. r=ehsan (c7d2112794)
- Bug 684208 - make dispatchEvent()'s return value to follow the spec, r=masayuki (4a8372e2fb)
- Bug 1223980 - Add iterable<ArrayBuffer,MediaKeyStatus> to MediaKeyStatusMap. r=bz (bcc52aabde)
- Bug 1260417 - Part a: Add a comment about the compartment of CallbackObject::mCallback; r=bz (2abca927f9)
- Bug 1260417 - Part c: Stop mentioning requests around CallSetup::mRootedCallable; r=bz (bb507b8f40)
- Bug 1260417 - Part b: Depend on AutoEntryScript to find an appropriate JSContext CallSetup; r=bz (e1b8fd132b)
- remove specific PM check (d7fae861af)
- minor cleanup (a47c325ad1)
- Bug 1254858: P4. Allow to pass mimetype in constructor. r=kentuckyfriedtakahe (3627f6bb0a)
- Bug 1254858: P5. Add h264 decoding gtest. r=kentuckyfriedtakahe (0bb7873e4f)
- Bug 1254858: P6. Add VP9 decoding gtest. r=kentuckyfriedtakahe (6940369a73)
- Bug 1254858: P8. Change default preferences value if prefs don't exist. r=kentuckyfriedtakahe (1516f43986)
- Bug 1247866: Avoid modifying hashtable during iteration, when iterating over redirects for saved files, to avoid tripping fatal assertion. r=njn (94f77efaad)
- bug 1230065 consider arrow size in dropdown minimum widget size r=acomminos (cd67908374)
- bug 1261277 use GtkTextView to get an appropriate color for resizers r=acomminos (b8b2df0ebf)
- bug 1197165 apply tooltip style class when creating window instead of when drawing r=acomminos (c58078774f)
- Bug 1257811 - set min-height for input widgets. r=karlt (43c5507346)
- bug 1199602 give existing wrap_gtk_window_check_resize internal linkage r=acomminos (ef8fe19a89)
- bug 1199602 emit resume-events on GdkFrameClock if flush/resume not balanced at dispose r=acomminos (16d79ac0cd)
- Bug 1249604 - Don't use gtk_drag_set_icon_surface on GTK versions that use X SHAPE for dnd with cairo. r=karlt (3e7b1f3aaf)
- Bug 1262634 - Avoid undefined reference to moz_gtk_get_entry_min_height in builds specifying cairo-gtk2 toolkit. r=karlt (112c35a394)
2024-05-08 09:45:48 +08:00

1411 lines
36 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* A type/length-parametrized vector class. */
#ifndef mozilla_Vector_h
#define mozilla_Vector_h
#include "mozilla/Alignment.h"
#include "mozilla/AllocPolicy.h"
#include "mozilla/ArrayUtils.h" // for PointerRangeSize
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/MathAlgorithms.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/Move.h"
#include "mozilla/ReentrancyGuard.h"
#include "mozilla/TemplateLib.h"
#include "mozilla/TypeTraits.h"
#include <new> // for placement new
/* Silence dire "bugs in previous versions of MSVC have been fixed" warnings */
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable:4345)
#endif
namespace mozilla {
template<typename T, size_t N, class AllocPolicy>
class Vector;
namespace detail {
/*
* Check that the given capacity wastes the minimal amount of space if
* allocated on the heap. This means that aCapacity*sizeof(T) is as close to a
* power-of-two as possible. growStorageBy() is responsible for ensuring this.
*/
template<typename T>
static bool CapacityHasExcessSpace(size_t aCapacity)
{
size_t size = aCapacity * sizeof(T);
return RoundUpPow2(size) - size >= sizeof(T);
}
/*
* This template class provides a default implementation for vector operations
* when the element type is not known to be a POD, as judged by IsPod.
*/
template<typename T, size_t N, class AP, bool IsPod>
struct VectorImpl
{
/*
* Constructs a default object in the uninitialized memory at *aDst.
*/
MOZ_NONNULL(1)
static inline void new_(T* aDst)
{
new(aDst) T();
}
/*
* Constructs an object in the uninitialized memory at *aDst from aSrc.
*/
template<typename U>
MOZ_NONNULL(1)
static inline void new_(T* aDst, U&& aU)
{
new(aDst) T(Forward<U>(aU));
}
/* Destroys constructed objects in the range [aBegin, aEnd). */
static inline void destroy(T* aBegin, T* aEnd)
{
MOZ_ASSERT(aBegin <= aEnd);
for (T* p = aBegin; p < aEnd; ++p) {
p->~T();
}
}
/* Constructs objects in the uninitialized range [aBegin, aEnd). */
static inline void initialize(T* aBegin, T* aEnd)
{
MOZ_ASSERT(aBegin <= aEnd);
for (T* p = aBegin; p < aEnd; ++p) {
new_(p);
}
}
/*
* Copy-constructs objects in the uninitialized range
* [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
*/
template<typename U>
static inline void copyConstruct(T* aDst,
const U* aSrcStart, const U* aSrcEnd)
{
MOZ_ASSERT(aSrcStart <= aSrcEnd);
for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
new_(aDst, *p);
}
}
/*
* Move-constructs objects in the uninitialized range
* [aDst, aDst+(aSrcEnd-aSrcStart)) from the range [aSrcStart, aSrcEnd).
*/
template<typename U>
static inline void moveConstruct(T* aDst, U* aSrcStart, U* aSrcEnd)
{
MOZ_ASSERT(aSrcStart <= aSrcEnd);
for (U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
new_(aDst, Move(*p));
}
}
/*
* Copy-constructs objects in the uninitialized range [aDst, aDst+aN) from
* the same object aU.
*/
template<typename U>
static inline void copyConstructN(T* aDst, size_t aN, const U& aU)
{
for (T* end = aDst + aN; aDst < end; ++aDst) {
new_(aDst, aU);
}
}
/*
* Grows the given buffer to have capacity aNewCap, preserving the objects
* constructed in the range [begin, end) and updating aV. Assumes that (1)
* aNewCap has not overflowed, and (2) multiplying aNewCap by sizeof(T) will
* not overflow.
*/
static inline bool
growTo(Vector<T, N, AP>& aV, size_t aNewCap)
{
MOZ_ASSERT(!aV.usingInlineStorage());
MOZ_ASSERT(!CapacityHasExcessSpace<T>(aNewCap));
T* newbuf = aV.template pod_malloc<T>(aNewCap);
if (MOZ_UNLIKELY(!newbuf)) {
return false;
}
T* dst = newbuf;
T* src = aV.beginNoCheck();
for (; src < aV.endNoCheck(); ++dst, ++src) {
new_(dst, Move(*src));
}
VectorImpl::destroy(aV.beginNoCheck(), aV.endNoCheck());
aV.free_(aV.mBegin);
aV.mBegin = newbuf;
/* aV.mLength is unchanged. */
aV.mCapacity = aNewCap;
return true;
}
};
/*
* This partial template specialization provides a default implementation for
* vector operations when the element type is known to be a POD, as judged by
* IsPod.
*/
template<typename T, size_t N, class AP>
struct VectorImpl<T, N, AP, true>
{
static inline void new_(T* aDst)
{
*aDst = T();
}
template<typename U>
static inline void new_(T* aDst, U&& aU)
{
*aDst = Forward<U>(aU);
}
static inline void destroy(T*, T*) {}
static inline void initialize(T* aBegin, T* aEnd)
{
/*
* You would think that memset would be a big win (or even break even)
* when we know T is a POD. But currently it's not. This is probably
* because |append| tends to be given small ranges and memset requires
* a function call that doesn't get inlined.
*
* memset(aBegin, 0, sizeof(T) * (aEnd - aBegin));
*/
MOZ_ASSERT(aBegin <= aEnd);
for (T* p = aBegin; p < aEnd; ++p) {
new_(p);
}
}
template<typename U>
static inline void copyConstruct(T* aDst,
const U* aSrcStart, const U* aSrcEnd)
{
/*
* See above memset comment. Also, notice that copyConstruct is
* currently templated (T != U), so memcpy won't work without
* requiring T == U.
*
* memcpy(aDst, aSrcStart, sizeof(T) * (aSrcEnd - aSrcStart));
*/
MOZ_ASSERT(aSrcStart <= aSrcEnd);
for (const U* p = aSrcStart; p < aSrcEnd; ++p, ++aDst) {
new_(aDst, *p);
}
}
template<typename U>
static inline void moveConstruct(T* aDst,
const U* aSrcStart, const U* aSrcEnd)
{
copyConstruct(aDst, aSrcStart, aSrcEnd);
}
static inline void copyConstructN(T* aDst, size_t aN, const T& aT)
{
for (T* end = aDst + aN; aDst < end; ++aDst) {
new_(aDst, aT);
}
}
static inline bool
growTo(Vector<T, N, AP>& aV, size_t aNewCap)
{
MOZ_ASSERT(!aV.usingInlineStorage());
MOZ_ASSERT(!CapacityHasExcessSpace<T>(aNewCap));
T* newbuf = aV.template pod_realloc<T>(aV.mBegin, aV.mCapacity, aNewCap);
if (MOZ_UNLIKELY(!newbuf)) {
return false;
}
aV.mBegin = newbuf;
/* aV.mLength is unchanged. */
aV.mCapacity = aNewCap;
return true;
}
};
// A struct for TestVector.cpp to access private internal fields.
// DO NOT DEFINE IN YOUR OWN CODE.
struct VectorTesting;
} // namespace detail
/*
* STL-like container providing a short-lived, dynamic buffer. Vector calls the
* constructors/destructors of all elements stored in its internal buffer, so
* non-PODs may be safely used. Additionally, Vector will store the first N
* elements in-place before resorting to dynamic allocation.
*
* T requirements:
* - default and copy constructible, assignable, destructible
* - operations do not throw
* MinInlineCapacity requirements:
* - any value, however, MinInlineCapacity is clamped to min/max values
* AllocPolicy:
* - see "Allocation policies" in AllocPolicy.h (defaults to
* mozilla::MallocAllocPolicy)
*
* Vector is not reentrant: T member functions called during Vector member
* functions must not call back into the same object!
*/
template<typename T,
size_t MinInlineCapacity = 0,
class AllocPolicy = MallocAllocPolicy>
class Vector final : private AllocPolicy
{
/* utilities */
static const bool kElemIsPod = IsPod<T>::value;
typedef detail::VectorImpl<T, MinInlineCapacity, AllocPolicy, kElemIsPod> Impl;
friend struct detail::VectorImpl<T, MinInlineCapacity, AllocPolicy, kElemIsPod>;
friend struct detail::VectorTesting;
MOZ_WARN_UNUSED_RESULT bool growStorageBy(size_t aIncr);
MOZ_WARN_UNUSED_RESULT bool convertToHeapStorage(size_t aNewCap);
MOZ_WARN_UNUSED_RESULT bool maybeCheckSimulatedOOM(size_t aRequestedSize);
/* magic constants */
static const int kMaxInlineBytes = 1024;
/* compute constants */
/*
* Consider element size to be 1 for buffer sizing if there are 0 inline
* elements. This allows us to compile when the definition of the element
* type is not visible here.
*
* Explicit specialization is only allowed at namespace scope, so in order
* to keep everything here, we use a dummy template parameter with partial
* specialization.
*/
template<int M, int Dummy>
struct ElemSize
{
static const size_t value = sizeof(T);
};
template<int Dummy>
struct ElemSize<0, Dummy>
{
static const size_t value = 1;
};
static const size_t kInlineCapacity =
tl::Min<MinInlineCapacity, kMaxInlineBytes / ElemSize<MinInlineCapacity, 0>::value>::value;
/* Calculate inline buffer size; avoid 0-sized array. */
static const size_t kInlineBytes =
tl::Max<1, kInlineCapacity * ElemSize<MinInlineCapacity, 0>::value>::value;
/* member data */
/*
* Pointer to the buffer, be it inline or heap-allocated. Only [mBegin,
* mBegin + mLength) hold valid constructed T objects. The range [mBegin +
* mLength, mBegin + mCapacity) holds uninitialized memory. The range
* [mBegin + mLength, mBegin + mReserved) also holds uninitialized memory
* previously allocated by a call to reserve().
*/
T* mBegin;
/* Number of elements in the vector. */
size_t mLength;
/* Max number of elements storable in the vector without resizing. */
size_t mCapacity;
#ifdef DEBUG
/* Max elements of reserved or used space in this vector. */
size_t mReserved;
#endif
/* Memory used for inline storage. */
AlignedStorage<kInlineBytes> mStorage;
#ifdef DEBUG
friend class ReentrancyGuard;
bool mEntered;
#endif
/* private accessors */
bool usingInlineStorage() const
{
return mBegin == const_cast<Vector*>(this)->inlineStorage();
}
T* inlineStorage()
{
return static_cast<T*>(mStorage.addr());
}
T* beginNoCheck() const
{
return mBegin;
}
T* endNoCheck()
{
return mBegin + mLength;
}
const T* endNoCheck() const
{
return mBegin + mLength;
}
#ifdef DEBUG
/**
* The amount of explicitly allocated space in this vector that is immediately
* available to be filled by appending additional elements. This value is
* always greater than or equal to |length()| -- the vector's actual elements
* are implicitly reserved. This value is always less than or equal to
* |capacity()|. It may be explicitly increased using the |reserve()| method.
*/
size_t reserved() const
{
MOZ_ASSERT(mLength <= mReserved);
MOZ_ASSERT(mReserved <= mCapacity);
return mReserved;
}
#endif
/* Append operations guaranteed to succeed due to pre-reserved space. */
template<typename U> void internalAppend(U&& aU);
template<typename U, size_t O, class BP>
void internalAppendAll(const Vector<U, O, BP>& aU);
void internalAppendN(const T& aT, size_t aN);
template<typename U> void internalAppend(const U* aBegin, size_t aLength);
public:
static const size_t sMaxInlineStorage = MinInlineCapacity;
typedef T ElementType;
explicit Vector(AllocPolicy = AllocPolicy());
Vector(Vector&&); /* Move constructor. */
Vector& operator=(Vector&&); /* Move assignment. */
~Vector();
/* accessors */
const AllocPolicy& allocPolicy() const { return *this; }
AllocPolicy& allocPolicy() { return *this; }
enum { InlineLength = MinInlineCapacity };
size_t length() const { return mLength; }
bool empty() const { return mLength == 0; }
size_t capacity() const { return mCapacity; }
T* begin()
{
MOZ_ASSERT(!mEntered);
return mBegin;
}
const T* begin() const
{
MOZ_ASSERT(!mEntered);
return mBegin;
}
T* end()
{
MOZ_ASSERT(!mEntered);
return mBegin + mLength;
}
const T* end() const
{
MOZ_ASSERT(!mEntered);
return mBegin + mLength;
}
T& operator[](size_t aIndex)
{
MOZ_ASSERT(!mEntered);
MOZ_ASSERT(aIndex < mLength);
return begin()[aIndex];
}
const T& operator[](size_t aIndex) const
{
MOZ_ASSERT(!mEntered);
MOZ_ASSERT(aIndex < mLength);
return begin()[aIndex];
}
T& back()
{
MOZ_ASSERT(!mEntered);
MOZ_ASSERT(!empty());
return *(end() - 1);
}
const T& back() const
{
MOZ_ASSERT(!mEntered);
MOZ_ASSERT(!empty());
return *(end() - 1);
}
class Range
{
friend class Vector;
T* mCur;
T* mEnd;
Range(T* aCur, T* aEnd)
: mCur(aCur)
, mEnd(aEnd)
{
MOZ_ASSERT(aCur <= aEnd);
}
public:
bool empty() const { return mCur == mEnd; }
size_t remain() const { return PointerRangeSize(mCur, mEnd); }
T& front() const { MOZ_ASSERT(!empty()); return *mCur; }
void popFront() { MOZ_ASSERT(!empty()); ++mCur; }
T popCopyFront() { MOZ_ASSERT(!empty()); return *mCur++; }
};
class ConstRange
{
friend class Vector;
const T* mCur;
const T* mEnd;
ConstRange(const T* aCur, const T* aEnd)
: mCur(aCur)
, mEnd(aEnd)
{
MOZ_ASSERT(aCur <= aEnd);
}
public:
bool empty() const { return mCur == mEnd; }
size_t remain() const { return PointerRangeSize(mCur, mEnd); }
const T& front() const { MOZ_ASSERT(!empty()); return *mCur; }
void popFront() { MOZ_ASSERT(!empty()); ++mCur; }
T popCopyFront() { MOZ_ASSERT(!empty()); return *mCur++; }
};
Range all() { return Range(begin(), end()); }
ConstRange all() const { return ConstRange(begin(), end()); }
/* mutators */
/**
* Reverse the order of the elements in the vector in place.
*/
void reverse();
/**
* Given that the vector is empty and has no inline storage, grow to
* |capacity|.
*/
MOZ_WARN_UNUSED_RESULT bool initCapacity(size_t aRequest);
/**
* If reserve(aRequest) succeeds and |aRequest >= length()|, then appending
* |aRequest - length()| elements, in any sequence of append/appendAll calls,
* is guaranteed to succeed.
*
* A request to reserve an amount less than the current length does not affect
* reserved space.
*/
MOZ_WARN_UNUSED_RESULT bool reserve(size_t aRequest);
/**
* Destroy elements in the range [end() - aIncr, end()). Does not deallocate
* or unreserve storage for those elements.
*/
void shrinkBy(size_t aIncr);
/**
* Destroy elements in the range [aNewLength, end()). Does not deallocate
* or unreserve storage for those elements.
*/
void shrinkTo(size_t aNewLength);
/** Grow the vector by aIncr elements. */
MOZ_WARN_UNUSED_RESULT bool growBy(size_t aIncr);
/** Call shrinkBy or growBy based on whether newSize > length(). */
MOZ_WARN_UNUSED_RESULT bool resize(size_t aNewLength);
/**
* Increase the length of the vector, but don't initialize the new elements
* -- leave them as uninitialized memory.
*/
MOZ_WARN_UNUSED_RESULT bool growByUninitialized(size_t aIncr);
void infallibleGrowByUninitialized(size_t aIncr);
MOZ_WARN_UNUSED_RESULT bool resizeUninitialized(size_t aNewLength);
/** Shorthand for shrinkBy(length()). */
void clear();
/** Clears and releases any heap-allocated storage. */
void clearAndFree();
/**
* If true, appending |aNeeded| elements won't reallocate elements storage.
* This *doesn't* mean that infallibleAppend may be used! You still must
* reserve the extra space, even if this method indicates that appends won't
* need to reallocate elements storage.
*/
bool canAppendWithoutRealloc(size_t aNeeded) const;
/** Potentially fallible append operations. */
/**
* This can take either a T& or a T&&. Given a T&&, it moves |aU| into the
* vector, instead of copying it. If it fails, |aU| is left unmoved. ("We are
* not amused.")
*/
template<typename U> MOZ_WARN_UNUSED_RESULT bool append(U&& aU);
/**
* Construct a T in-place as a new entry at the end of this vector.
*/
template<typename... Args>
MOZ_WARN_UNUSED_RESULT bool emplaceBack(Args&&... aArgs)
{
if (!growByUninitialized(1))
return false;
new (&back()) T(Forward<Args>(aArgs)...);
return true;
}
template<typename U, size_t O, class BP>
MOZ_WARN_UNUSED_RESULT bool appendAll(const Vector<U, O, BP>& aU);
MOZ_WARN_UNUSED_RESULT bool appendN(const T& aT, size_t aN);
template<typename U> MOZ_WARN_UNUSED_RESULT bool append(const U* aBegin, const U* aEnd);
template<typename U> MOZ_WARN_UNUSED_RESULT bool append(const U* aBegin, size_t aLength);
/*
* Guaranteed-infallible append operations for use upon vectors whose
* memory has been pre-reserved. Don't use this if you haven't reserved the
* memory!
*/
template<typename U> void infallibleAppend(U&& aU)
{
internalAppend(Forward<U>(aU));
}
void infallibleAppendN(const T& aT, size_t aN)
{
internalAppendN(aT, aN);
}
template<typename U> void infallibleAppend(const U* aBegin, const U* aEnd)
{
internalAppend(aBegin, PointerRangeSize(aBegin, aEnd));
}
template<typename U> void infallibleAppend(const U* aBegin, size_t aLength)
{
internalAppend(aBegin, aLength);
}
template<typename... Args>
void infallibleEmplaceBack(Args&&... aArgs)
{
infallibleGrowByUninitialized(1);
new (&back()) T(Forward<Args>(aArgs)...);
}
void popBack();
T popCopy();
/**
* Transfers ownership of the internal buffer used by this vector to the
* caller. (It's the caller's responsibility to properly deallocate this
* buffer, in accordance with this vector's AllocPolicy.) After this call,
* the vector is empty. Since the returned buffer may need to be allocated
* (if the elements are currently stored in-place), the call can fail,
* returning nullptr.
*
* N.B. Although a T*, only the range [0, length()) is constructed.
*/
MOZ_WARN_UNUSED_RESULT T* extractRawBuffer();
/**
* Transfer ownership of an array of objects into the vector. The caller
* must have allocated the array in accordance with this vector's
* AllocPolicy.
*
* N.B. This call assumes that there are no uninitialized elements in the
* passed array.
*/
void replaceRawBuffer(T* aP, size_t aLength);
/**
* Places |aVal| at position |aP|, shifting existing elements from |aP| onward
* one position higher. On success, |aP| should not be reused because it'll
* be a dangling pointer if reallocation of the vector storage occurred; the
* return value should be used instead. On failure, nullptr is returned.
*
* Example usage:
*
* if (!(p = vec.insert(p, val))) {
* <handle failure>
* }
* <keep working with p>
*
* This is inherently a linear-time operation. Be careful!
*/
template<typename U>
MOZ_WARN_UNUSED_RESULT T* insert(T* aP, U&& aVal);
/**
* Removes the element |aT|, which must fall in the bounds [begin, end),
* shifting existing elements from |aT + 1| onward one position lower.
*/
void erase(T* aT);
/**
* Removes the elements [|aBegin|, |aEnd|), which must fall in the bounds
* [begin, end), shifting existing elements from |aEnd + 1| onward to aBegin's
* old position.
*/
void erase(T* aBegin, T* aEnd);
/**
* Measure the size of the vector's heap-allocated storage.
*/
size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
/**
* Like sizeOfExcludingThis, but also measures the size of the vector
* object (which must be heap-allocated) itself.
*/
size_t sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
void swap(Vector& aOther);
private:
Vector(const Vector&) = delete;
void operator=(const Vector&) = delete;
};
/* This does the re-entrancy check plus several other sanity checks. */
#define MOZ_REENTRANCY_GUARD_ET_AL \
ReentrancyGuard g(*this); \
MOZ_ASSERT_IF(usingInlineStorage(), mCapacity == kInlineCapacity); \
MOZ_ASSERT(reserved() <= mCapacity); \
MOZ_ASSERT(mLength <= reserved()); \
MOZ_ASSERT(mLength <= mCapacity)
/* Vector Implementation */
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE
Vector<T, N, AP>::Vector(AP aAP)
: AP(aAP)
, mLength(0)
, mCapacity(kInlineCapacity)
#ifdef DEBUG
, mReserved(0)
, mEntered(false)
#endif
{
mBegin = static_cast<T*>(mStorage.addr());
}
/* Move constructor. */
template<typename T, size_t N, class AllocPolicy>
MOZ_ALWAYS_INLINE
Vector<T, N, AllocPolicy>::Vector(Vector&& aRhs)
: AllocPolicy(Move(aRhs))
#ifdef DEBUG
, mEntered(false)
#endif
{
mLength = aRhs.mLength;
mCapacity = aRhs.mCapacity;
#ifdef DEBUG
mReserved = aRhs.mReserved;
#endif
if (aRhs.usingInlineStorage()) {
/* We can't move the buffer over in this case, so copy elements. */
mBegin = static_cast<T*>(mStorage.addr());
Impl::moveConstruct(mBegin, aRhs.beginNoCheck(), aRhs.endNoCheck());
/*
* Leave aRhs's mLength, mBegin, mCapacity, and mReserved as they are.
* The elements in its in-line storage still need to be destroyed.
*/
} else {
/*
* Take src's buffer, and turn src into an empty vector using
* in-line storage.
*/
mBegin = aRhs.mBegin;
aRhs.mBegin = static_cast<T*>(aRhs.mStorage.addr());
aRhs.mCapacity = kInlineCapacity;
aRhs.mLength = 0;
#ifdef DEBUG
aRhs.mReserved = 0;
#endif
}
}
/* Move assignment. */
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE Vector<T, N, AP>&
Vector<T, N, AP>::operator=(Vector&& aRhs)
{
MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
this->~Vector();
new(this) Vector(Move(aRhs));
return *this;
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE
Vector<T, N, AP>::~Vector()
{
MOZ_REENTRANCY_GUARD_ET_AL;
Impl::destroy(beginNoCheck(), endNoCheck());
if (!usingInlineStorage()) {
this->free_(beginNoCheck());
}
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::reverse() {
MOZ_REENTRANCY_GUARD_ET_AL;
T* elems = mBegin;
size_t len = mLength;
size_t mid = len / 2;
for (size_t i = 0; i < mid; i++) {
Swap(elems[i], elems[len - i - 1]);
}
}
/*
* This function will create a new heap buffer with capacity aNewCap,
* move all elements in the inline buffer to this new buffer,
* and fail on OOM.
*/
template<typename T, size_t N, class AP>
inline bool
Vector<T, N, AP>::convertToHeapStorage(size_t aNewCap)
{
MOZ_ASSERT(usingInlineStorage());
/* Allocate buffer. */
MOZ_ASSERT(!detail::CapacityHasExcessSpace<T>(aNewCap));
T* newBuf = this->template pod_malloc<T>(aNewCap);
if (MOZ_UNLIKELY(!newBuf)) {
return false;
}
/* Copy inline elements into heap buffer. */
Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
Impl::destroy(beginNoCheck(), endNoCheck());
/* Switch in heap buffer. */
mBegin = newBuf;
/* mLength is unchanged. */
mCapacity = aNewCap;
return true;
}
template<typename T, size_t N, class AP>
MOZ_NEVER_INLINE bool
Vector<T, N, AP>::growStorageBy(size_t aIncr)
{
MOZ_ASSERT(mLength + aIncr > mCapacity);
/*
* When choosing a new capacity, its size should is as close to 2**N bytes
* as possible. 2**N-sized requests are best because they are unlikely to
* be rounded up by the allocator. Asking for a 2**N number of elements
* isn't as good, because if sizeof(T) is not a power-of-two that would
* result in a non-2**N request size.
*/
size_t newCap;
if (aIncr == 1) {
if (usingInlineStorage()) {
/* This case occurs in ~70--80% of the calls to this function. */
size_t newSize =
tl::RoundUpPow2<(kInlineCapacity + 1) * sizeof(T)>::value;
newCap = newSize / sizeof(T);
goto convert;
}
if (mLength == 0) {
/* This case occurs in ~0--10% of the calls to this function. */
newCap = 1;
goto grow;
}
/* This case occurs in ~15--20% of the calls to this function. */
/*
* Will mLength * 4 *sizeof(T) overflow? This condition limits a vector
* to 1GB of memory on a 32-bit system, which is a reasonable limit. It
* also ensures that
*
* static_cast<char*>(end()) - static_cast<char*>(begin())
*
* doesn't overflow ptrdiff_t (see bug 510319).
*/
if (MOZ_UNLIKELY(mLength & tl::MulOverflowMask<4 * sizeof(T)>::value)) {
this->reportAllocOverflow();
return false;
}
/*
* If we reach here, the existing capacity will have a size that is already
* as close to 2^N as sizeof(T) will allow. Just double the capacity, and
* then there might be space for one more element.
*/
newCap = mLength * 2;
if (detail::CapacityHasExcessSpace<T>(newCap)) {
newCap += 1;
}
} else {
/* This case occurs in ~2% of the calls to this function. */
size_t newMinCap = mLength + aIncr;
/* Did mLength + aIncr overflow? Will newCap * sizeof(T) overflow? */
if (MOZ_UNLIKELY(newMinCap < mLength ||
newMinCap & tl::MulOverflowMask<2 * sizeof(T)>::value))
{
this->reportAllocOverflow();
return false;
}
size_t newMinSize = newMinCap * sizeof(T);
size_t newSize = RoundUpPow2(newMinSize);
newCap = newSize / sizeof(T);
}
if (usingInlineStorage()) {
convert:
return convertToHeapStorage(newCap);
}
grow:
return Impl::growTo(*this, newCap);
}
template<typename T, size_t N, class AP>
inline bool
Vector<T, N, AP>::initCapacity(size_t aRequest)
{
MOZ_ASSERT(empty());
MOZ_ASSERT(usingInlineStorage());
if (aRequest == 0) {
return true;
}
T* newbuf = this->template pod_malloc<T>(aRequest);
if (MOZ_UNLIKELY(!newbuf)) {
return false;
}
mBegin = newbuf;
mCapacity = aRequest;
#ifdef DEBUG
mReserved = aRequest;
#endif
return true;
}
template<typename T, size_t N, class AP>
inline bool
Vector<T, N, AP>::maybeCheckSimulatedOOM(size_t aRequestedSize)
{
if (aRequestedSize <= N) {
return true;
}
#ifdef DEBUG
if (aRequestedSize <= mReserved) {
return true;
}
#endif
return allocPolicy().checkSimulatedOOM();
}
template<typename T, size_t N, class AP>
inline bool
Vector<T, N, AP>::reserve(size_t aRequest)
{
MOZ_REENTRANCY_GUARD_ET_AL;
if (aRequest > mCapacity) {
if (MOZ_UNLIKELY(!growStorageBy(aRequest - mLength))) {
return false;
}
} else if (!maybeCheckSimulatedOOM(aRequest)) {
return false;
}
#ifdef DEBUG
if (aRequest > mReserved) {
mReserved = aRequest;
}
MOZ_ASSERT(mLength <= mReserved);
MOZ_ASSERT(mReserved <= mCapacity);
#endif
return true;
}
template<typename T, size_t N, class AP>
inline void
Vector<T, N, AP>::shrinkBy(size_t aIncr)
{
MOZ_REENTRANCY_GUARD_ET_AL;
MOZ_ASSERT(aIncr <= mLength);
Impl::destroy(endNoCheck() - aIncr, endNoCheck());
mLength -= aIncr;
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::shrinkTo(size_t aNewLength)
{
MOZ_ASSERT(aNewLength <= mLength);
shrinkBy(mLength - aNewLength);
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::growBy(size_t aIncr)
{
MOZ_REENTRANCY_GUARD_ET_AL;
if (aIncr > mCapacity - mLength) {
if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
return false;
}
} else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
return false;
}
MOZ_ASSERT(mLength + aIncr <= mCapacity);
T* newend = endNoCheck() + aIncr;
Impl::initialize(endNoCheck(), newend);
mLength += aIncr;
#ifdef DEBUG
if (mLength > mReserved) {
mReserved = mLength;
}
#endif
return true;
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::growByUninitialized(size_t aIncr)
{
MOZ_REENTRANCY_GUARD_ET_AL;
if (aIncr > mCapacity - mLength) {
if (MOZ_UNLIKELY(!growStorageBy(aIncr))) {
return false;
}
} else if (!maybeCheckSimulatedOOM(mLength + aIncr)) {
return false;
}
#ifdef DEBUG
if (mLength + aIncr > mReserved) {
mReserved = mLength + aIncr;
}
#endif
infallibleGrowByUninitialized(aIncr);
return true;
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::infallibleGrowByUninitialized(size_t aIncr)
{
MOZ_ASSERT(mLength + aIncr <= reserved());
mLength += aIncr;
}
template<typename T, size_t N, class AP>
inline bool
Vector<T, N, AP>::resize(size_t aNewLength)
{
size_t curLength = mLength;
if (aNewLength > curLength) {
return growBy(aNewLength - curLength);
}
shrinkBy(curLength - aNewLength);
return true;
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::resizeUninitialized(size_t aNewLength)
{
size_t curLength = mLength;
if (aNewLength > curLength) {
return growByUninitialized(aNewLength - curLength);
}
shrinkBy(curLength - aNewLength);
return true;
}
template<typename T, size_t N, class AP>
inline void
Vector<T, N, AP>::clear()
{
MOZ_REENTRANCY_GUARD_ET_AL;
Impl::destroy(beginNoCheck(), endNoCheck());
mLength = 0;
}
template<typename T, size_t N, class AP>
inline void
Vector<T, N, AP>::clearAndFree()
{
clear();
if (usingInlineStorage()) {
return;
}
this->free_(beginNoCheck());
mBegin = static_cast<T*>(mStorage.addr());
mCapacity = kInlineCapacity;
#ifdef DEBUG
mReserved = 0;
#endif
}
template<typename T, size_t N, class AP>
inline bool
Vector<T, N, AP>::canAppendWithoutRealloc(size_t aNeeded) const
{
return mLength + aNeeded <= mCapacity;
}
template<typename T, size_t N, class AP>
template<typename U, size_t O, class BP>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::internalAppendAll(const Vector<U, O, BP>& aOther)
{
internalAppend(aOther.begin(), aOther.length());
}
template<typename T, size_t N, class AP>
template<typename U>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::internalAppend(U&& aU)
{
MOZ_ASSERT(mLength + 1 <= mReserved);
MOZ_ASSERT(mReserved <= mCapacity);
Impl::new_(endNoCheck(), Forward<U>(aU));
++mLength;
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::appendN(const T& aT, size_t aNeeded)
{
MOZ_REENTRANCY_GUARD_ET_AL;
if (mLength + aNeeded > mCapacity) {
if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
return false;
}
} else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
return false;
}
#ifdef DEBUG
if (mLength + aNeeded > mReserved) {
mReserved = mLength + aNeeded;
}
#endif
internalAppendN(aT, aNeeded);
return true;
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::internalAppendN(const T& aT, size_t aNeeded)
{
MOZ_ASSERT(mLength + aNeeded <= mReserved);
MOZ_ASSERT(mReserved <= mCapacity);
Impl::copyConstructN(endNoCheck(), aNeeded, aT);
mLength += aNeeded;
}
template<typename T, size_t N, class AP>
template<typename U>
inline T*
Vector<T, N, AP>::insert(T* aP, U&& aVal)
{
MOZ_ASSERT(begin() <= aP);
MOZ_ASSERT(aP <= end());
size_t pos = aP - begin();
MOZ_ASSERT(pos <= mLength);
size_t oldLength = mLength;
if (pos == oldLength) {
if (!append(Forward<U>(aVal))) {
return nullptr;
}
} else {
T oldBack = Move(back());
if (!append(Move(oldBack))) { /* Dup the last element. */
return nullptr;
}
for (size_t i = oldLength; i > pos; --i) {
(*this)[i] = Move((*this)[i - 1]);
}
(*this)[pos] = Forward<U>(aVal);
}
return begin() + pos;
}
template<typename T, size_t N, class AP>
inline void
Vector<T, N, AP>::erase(T* aIt)
{
MOZ_ASSERT(begin() <= aIt);
MOZ_ASSERT(aIt < end());
while (aIt + 1 < end()) {
*aIt = Move(*(aIt + 1));
++aIt;
}
popBack();
}
template<typename T, size_t N, class AP>
inline void
Vector<T, N, AP>::erase(T* aBegin, T* aEnd)
{
MOZ_ASSERT(begin() <= aBegin);
MOZ_ASSERT(aBegin <= aEnd);
MOZ_ASSERT(aEnd <= end());
while (aEnd < end()) {
*aBegin++ = Move(*aEnd++);
}
shrinkBy(aEnd - aBegin);
}
template<typename T, size_t N, class AP>
template<typename U>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::append(const U* aInsBegin, const U* aInsEnd)
{
MOZ_REENTRANCY_GUARD_ET_AL;
size_t aNeeded = PointerRangeSize(aInsBegin, aInsEnd);
if (mLength + aNeeded > mCapacity) {
if (MOZ_UNLIKELY(!growStorageBy(aNeeded))) {
return false;
}
} else if (!maybeCheckSimulatedOOM(mLength + aNeeded)) {
return false;
}
#ifdef DEBUG
if (mLength + aNeeded > mReserved) {
mReserved = mLength + aNeeded;
}
#endif
internalAppend(aInsBegin, aNeeded);
return true;
}
template<typename T, size_t N, class AP>
template<typename U>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::internalAppend(const U* aInsBegin, size_t aInsLength)
{
MOZ_ASSERT(mLength + aInsLength <= mReserved);
MOZ_ASSERT(mReserved <= mCapacity);
Impl::copyConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
mLength += aInsLength;
}
template<typename T, size_t N, class AP>
template<typename U>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::append(U&& aU)
{
MOZ_REENTRANCY_GUARD_ET_AL;
if (mLength == mCapacity) {
if (MOZ_UNLIKELY(!growStorageBy(1))) {
return false;
}
} else if (!maybeCheckSimulatedOOM(mLength + 1)) {
return false;
}
#ifdef DEBUG
if (mLength + 1 > mReserved) {
mReserved = mLength + 1;
}
#endif
internalAppend(Forward<U>(aU));
return true;
}
template<typename T, size_t N, class AP>
template<typename U, size_t O, class BP>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::appendAll(const Vector<U, O, BP>& aOther)
{
return append(aOther.begin(), aOther.length());
}
template<typename T, size_t N, class AP>
template<class U>
MOZ_ALWAYS_INLINE bool
Vector<T, N, AP>::append(const U* aInsBegin, size_t aInsLength)
{
return append(aInsBegin, aInsBegin + aInsLength);
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE void
Vector<T, N, AP>::popBack()
{
MOZ_REENTRANCY_GUARD_ET_AL;
MOZ_ASSERT(!empty());
--mLength;
endNoCheck()->~T();
}
template<typename T, size_t N, class AP>
MOZ_ALWAYS_INLINE T
Vector<T, N, AP>::popCopy()
{
T ret = back();
popBack();
return ret;
}
template<typename T, size_t N, class AP>
inline T*
Vector<T, N, AP>::extractRawBuffer()
{
T* ret;
if (usingInlineStorage()) {
ret = this->template pod_malloc<T>(mLength);
if (!ret) {
return nullptr;
}
Impl::copyConstruct(ret, beginNoCheck(), endNoCheck());
Impl::destroy(beginNoCheck(), endNoCheck());
/* mBegin, mCapacity are unchanged. */
mLength = 0;
} else {
ret = mBegin;
mBegin = static_cast<T*>(mStorage.addr());
mLength = 0;
mCapacity = kInlineCapacity;
#ifdef DEBUG
mReserved = 0;
#endif
}
return ret;
}
template<typename T, size_t N, class AP>
inline void
Vector<T, N, AP>::replaceRawBuffer(T* aP, size_t aLength)
{
MOZ_REENTRANCY_GUARD_ET_AL;
/* Destroy what we have. */
Impl::destroy(beginNoCheck(), endNoCheck());
if (!usingInlineStorage()) {
this->free_(beginNoCheck());
}
/* Take in the new buffer. */
if (aLength <= kInlineCapacity) {
/*
* We convert to inline storage if possible, even though aP might
* otherwise be acceptable. Maybe this behaviour should be
* specifiable with an argument to this function.
*/
mBegin = static_cast<T*>(mStorage.addr());
mLength = aLength;
mCapacity = kInlineCapacity;
Impl::moveConstruct(mBegin, aP, aP + aLength);
Impl::destroy(aP, aP + aLength);
this->free_(aP);
} else {
mBegin = aP;
mLength = aLength;
mCapacity = aLength;
}
#ifdef DEBUG
mReserved = aLength;
#endif
}
template<typename T, size_t N, class AP>
inline size_t
Vector<T, N, AP>::sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
{
return usingInlineStorage() ? 0 : aMallocSizeOf(beginNoCheck());
}
template<typename T, size_t N, class AP>
inline size_t
Vector<T, N, AP>::sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
{
return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf);
}
template<typename T, size_t N, class AP>
inline void
Vector<T, N, AP>::swap(Vector& aOther)
{
static_assert(N == 0,
"still need to implement this for N != 0");
// This only works when inline storage is always empty.
if (!usingInlineStorage() && aOther.usingInlineStorage()) {
aOther.mBegin = mBegin;
mBegin = inlineStorage();
} else if (usingInlineStorage() && !aOther.usingInlineStorage()) {
mBegin = aOther.mBegin;
aOther.mBegin = aOther.inlineStorage();
} else if (!usingInlineStorage() && !aOther.usingInlineStorage()) {
Swap(mBegin, aOther.mBegin);
} else {
// This case is a no-op, since we'd set both to use their inline storage.
}
Swap(mLength, aOther.mLength);
Swap(mCapacity, aOther.mCapacity);
#ifdef DEBUG
Swap(mReserved, aOther.mReserved);
#endif
}
} // namespace mozilla
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#endif /* mozilla_Vector_h */