2013-07-24 11:41:39 +04:00
|
|
|
/* -*- 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
|
2013-07-10 03:33:29 +04:00
|
|
|
* 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
|
|
|
|
|
2013-08-14 11:00:52 +04:00
|
|
|
#include "mozilla/Alignment.h"
|
2013-07-10 03:33:29 +04:00
|
|
|
#include "mozilla/AllocPolicy.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h" // for PointerRangeSize
|
2013-07-10 03:33:29 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/MathAlgorithms.h"
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/Move.h"
|
|
|
|
#include "mozilla/NullPtr.h"
|
|
|
|
#include "mozilla/ReentrancyGuard.h"
|
|
|
|
#include "mozilla/TemplateLib.h"
|
|
|
|
#include "mozilla/TypeTraits.h"
|
|
|
|
|
2013-07-26 07:01:42 +04:00
|
|
|
#include <new> // for placement new
|
|
|
|
|
2013-07-10 03:33:29 +04:00
|
|
|
/* 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 ThisVector>
|
|
|
|
class VectorBase;
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check that the given capacity wastes the minimal amount of space if
|
2014-07-11 06:10:17 +04:00
|
|
|
* 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.
|
2013-07-10 03:33:29 +04:00
|
|
|
*/
|
|
|
|
template<typename T>
|
2014-07-11 06:10:17 +04:00
|
|
|
static bool CapacityHasExcessSpace(size_t aCapacity)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t size = aCapacity * sizeof(T);
|
2013-07-10 03:33:29 +04:00
|
|
|
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, class ThisVector, bool IsPod>
|
|
|
|
struct VectorImpl
|
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
/* 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();
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* 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) T();
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* 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) T(*p);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* 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) T(Move(*p));
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* 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) T(aU);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* 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(VectorBase<T, N, AP, ThisVector>& aV, size_t aNewCap)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aV.usingInlineStorage());
|
|
|
|
MOZ_ASSERT(!CapacityHasExcessSpace<T>(aNewCap));
|
2014-08-09 00:22:39 +04:00
|
|
|
T* newbuf = aV.template pod_malloc<T>(aNewCap);
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!newbuf) {
|
|
|
|
return false;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
T* dst = newbuf;
|
|
|
|
T* src = aV.beginNoCheck();
|
|
|
|
for (; src < aV.endNoCheck(); ++dst, ++src) {
|
|
|
|
new(dst) T(Move(*src));
|
|
|
|
}
|
|
|
|
VectorImpl::destroy(aV.beginNoCheck(), aV.endNoCheck());
|
|
|
|
aV.free_(aV.mBegin);
|
|
|
|
aV.mBegin = newbuf;
|
|
|
|
/* aV.mLength is unchanged. */
|
|
|
|
aV.mCapacity = aNewCap;
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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, class ThisVector>
|
|
|
|
struct VectorImpl<T, N, AP, ThisVector, true>
|
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
static inline void destroy(T*, T*) {}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
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) T();
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
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) {
|
|
|
|
*aDst = *p;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
static inline void moveConstruct(T* aDst,
|
|
|
|
const U* aSrcStart, const U* aSrcEnd)
|
|
|
|
{
|
|
|
|
copyConstruct(aDst, aSrcStart, aSrcEnd);
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
static inline void copyConstructN(T* aDst, size_t aN, const T& aT)
|
|
|
|
{
|
|
|
|
for (T* end = aDst + aN; aDst < end; ++aDst) {
|
|
|
|
*aDst = aT;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
static inline bool
|
|
|
|
growTo(VectorBase<T, N, AP, ThisVector>& aV, size_t aNewCap)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!aV.usingInlineStorage());
|
|
|
|
MOZ_ASSERT(!CapacityHasExcessSpace<T>(aNewCap));
|
2014-08-06 01:06:35 +04:00
|
|
|
T* newbuf = aV.template pod_realloc<T>(aV.mBegin, aV.mCapacity, aNewCap);
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!newbuf) {
|
|
|
|
return false;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-07-11 06:10:17 +04:00
|
|
|
aV.mBegin = newbuf;
|
|
|
|
/* aV.mLength is unchanged. */
|
|
|
|
aV.mCapacity = aNewCap;
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
};
|
|
|
|
|
2014-11-27 00:01:19 +03:00
|
|
|
// A struct for TestVector.cpp to access private internal fields.
|
|
|
|
// DO NOT DEFINE IN YOUR OWN CODE.
|
|
|
|
struct VectorTesting;
|
|
|
|
|
2013-07-10 03:33:29 +04:00
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A CRTP base class for vector-like classes. Unless you really really want
|
|
|
|
* your own vector class -- and you almost certainly don't -- you should use
|
|
|
|
* mozilla::Vector instead!
|
|
|
|
*
|
|
|
|
* See mozilla::Vector for interface requirements.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N, class AllocPolicy, class ThisVector>
|
|
|
|
class VectorBase : private AllocPolicy
|
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
/* utilities */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
static const bool kElemIsPod = IsPod<T>::value;
|
|
|
|
typedef detail::VectorImpl<T, N, AllocPolicy, ThisVector, kElemIsPod> Impl;
|
|
|
|
friend struct detail::VectorImpl<T, N, AllocPolicy, ThisVector, kElemIsPod>;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-11-27 00:01:19 +03:00
|
|
|
friend struct detail::VectorTesting;
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
bool growStorageBy(size_t aIncr);
|
|
|
|
bool convertToHeapStorage(size_t aNewCap);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* magic constants */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
static const int kMaxInlineBytes = 1024;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* compute constants */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* 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<N, kMaxInlineBytes / ElemSize<N, 0>::value>::value;
|
|
|
|
|
|
|
|
/* Calculate inline buffer size; avoid 0-sized array. */
|
|
|
|
static const size_t kInlineBytes =
|
|
|
|
tl::Max<1, kInlineCapacity * ElemSize<N, 0>::value>::value;
|
|
|
|
|
|
|
|
/* member data */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* 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;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Number of elements in the vector. */
|
|
|
|
size_t mLength;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Max number of elements storable in the vector without resizing. */
|
|
|
|
size_t mCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Max elements of reserved or used space in this vector. */
|
|
|
|
size_t mReserved;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Memory used for inline storage. */
|
|
|
|
AlignedStorage<kInlineBytes> mStorage;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
friend class ReentrancyGuard;
|
|
|
|
bool mEntered;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* private accessors */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
bool usingInlineStorage() const
|
|
|
|
{
|
|
|
|
return mBegin == const_cast<VectorBase*>(this)->inlineStorage();
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* inlineStorage()
|
|
|
|
{
|
|
|
|
return static_cast<T*>(mStorage.addr());
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* beginNoCheck() const
|
|
|
|
{
|
|
|
|
return mBegin;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* endNoCheck()
|
|
|
|
{
|
|
|
|
return mBegin + mLength;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T* endNoCheck() const
|
|
|
|
{
|
|
|
|
return mBegin + mLength;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-11-27 00:01:19 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t reserved() const
|
|
|
|
{
|
2014-12-02 05:25:53 +03:00
|
|
|
MOZ_ASSERT(mLength <= mReserved);
|
2014-11-27 00:01:19 +03:00
|
|
|
MOZ_ASSERT(mReserved <= mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
return mReserved;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* 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, class UV>
|
|
|
|
void internalAppendAll(const VectorBase<U, O, BP, UV>& aU);
|
|
|
|
void internalAppendN(const T& aT, size_t aN);
|
|
|
|
template<typename U> void internalAppend(const U* aBegin, size_t aLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
|
|
|
static const size_t sMaxInlineStorage = N;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
typedef T ElementType;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
explicit VectorBase(AllocPolicy = AllocPolicy());
|
|
|
|
explicit VectorBase(ThisVector&&); /* Move constructor. */
|
|
|
|
ThisVector& operator=(ThisVector&&); /* Move assignment. */
|
|
|
|
~VectorBase();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* accessors */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const AllocPolicy& allocPolicy() const { return *this; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
AllocPolicy& allocPolicy() { return *this; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
enum { InlineLength = N };
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t length() const { return mLength; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
bool empty() const { return mLength == 0; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t capacity() const { return mCapacity; }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* begin()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T* begin() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T* end()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin + mLength;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T* end() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
return mBegin + mLength;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T& operator[](size_t aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(aIndex < mLength);
|
|
|
|
return begin()[aIndex];
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T& operator[](size_t aIndex) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(aIndex < mLength);
|
|
|
|
return begin()[aIndex];
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T& back()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *(end() - 1);
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
const T& back() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mEntered);
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
return *(end() - 1);
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
class Range
|
|
|
|
{
|
|
|
|
friend class VectorBase;
|
|
|
|
T* mCur;
|
|
|
|
T* mEnd;
|
|
|
|
Range(T* aCur, T* aEnd)
|
|
|
|
: mCur(aCur)
|
|
|
|
, mEnd(aEnd)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(aCur <= aEnd);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
public:
|
|
|
|
Range() {}
|
|
|
|
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++; }
|
|
|
|
};
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
Range all() { return Range(begin(), end()); }
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* mutators */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Given that the vector is empty and has no inline storage, grow to
|
|
|
|
* |capacity|.
|
|
|
|
*/
|
|
|
|
bool initCapacity(size_t aRequest);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
2014-11-27 00:01:19 +03:00
|
|
|
* 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.
|
2014-07-11 06:10:17 +04:00
|
|
|
*/
|
|
|
|
bool reserve(size_t aRequest);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Destroy elements in the range [end() - aIncr, end()). Does not deallocate
|
|
|
|
* or unreserve storage for those elements.
|
|
|
|
*/
|
|
|
|
void shrinkBy(size_t aIncr);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Grow the vector by aIncr elements. */
|
|
|
|
bool growBy(size_t aIncr);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Call shrinkBy or growBy based on whether newSize > length(). */
|
|
|
|
bool resize(size_t aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Increase the length of the vector, but don't initialize the new elements
|
|
|
|
* -- leave them as uninitialized memory.
|
|
|
|
*/
|
|
|
|
bool growByUninitialized(size_t aIncr);
|
2014-10-09 02:04:12 +04:00
|
|
|
void infallibleGrowByUninitialized(size_t aIncr);
|
2014-07-11 06:10:17 +04:00
|
|
|
bool resizeUninitialized(size_t aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Shorthand for shrinkBy(length()). */
|
|
|
|
void clear();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Clears and releases any heap-allocated storage. */
|
|
|
|
void clearAndFree();
|
2013-11-19 21:05:36 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* 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;
|
2013-11-19 21:05:36 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/** Potentially fallible append operations. */
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* 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> bool append(U&& aU);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
template<typename U, size_t O, class BP, class UV>
|
|
|
|
bool appendAll(const VectorBase<U, O, BP, UV>& aU);
|
|
|
|
bool appendN(const T& aT, size_t aN);
|
|
|
|
template<typename U> bool append(const U* aBegin, const U* aEnd);
|
|
|
|
template<typename U> bool append(const U* aBegin, size_t aLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
void popBack();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
T popCopy();
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
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>
|
|
|
|
T* insert(T* aP, U&& aVal);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* 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);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* 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);
|
2014-05-21 00:36:40 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Measure the size of the vector's heap-allocated storage.
|
|
|
|
*/
|
|
|
|
size_t sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/**
|
|
|
|
* Like sizeOfExcludingThis, but also measures the size of the vector
|
|
|
|
* object (which must be heap-allocated) itself.
|
|
|
|
*/
|
|
|
|
size_t sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
void swap(ThisVector& aOther);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
private:
|
|
|
|
VectorBase(const VectorBase&) MOZ_DELETE;
|
|
|
|
void operator=(const VectorBase&) MOZ_DELETE;
|
2013-11-19 21:05:36 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Move-construct/assign only from our derived class, ThisVector. */
|
|
|
|
VectorBase(VectorBase&&) MOZ_DELETE;
|
|
|
|
void operator=(VectorBase&&) MOZ_DELETE;
|
2013-07-10 03:33:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* This does the re-entrancy check plus several other sanity checks. */
|
|
|
|
#define MOZ_REENTRANCY_GUARD_ET_AL \
|
|
|
|
ReentrancyGuard g(*this); \
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT_IF(usingInlineStorage(), mCapacity == kInlineCapacity); \
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(reserved() <= mCapacity); \
|
|
|
|
MOZ_ASSERT(mLength <= reserved()); \
|
|
|
|
MOZ_ASSERT(mLength <= mCapacity)
|
|
|
|
|
|
|
|
/* Vector Implementation */
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::VectorBase(AP aAP)
|
|
|
|
: AP(aAP)
|
|
|
|
, mLength(0)
|
|
|
|
, mCapacity(kInlineCapacity)
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-11-27 00:01:19 +03:00
|
|
|
, mReserved(0)
|
2014-07-11 06:10:17 +04:00
|
|
|
, mEntered(false)
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2013-10-18 01:29:46 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = static_cast<T*>(mStorage.addr());
|
2013-10-18 01:29:46 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/* Move constructor. */
|
|
|
|
template<typename T, size_t N, class AllocPolicy, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AllocPolicy, TV>::VectorBase(TV&& aRhs)
|
|
|
|
: AllocPolicy(Move(aRhs))
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
, mEntered(false)
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
mLength = aRhs.mLength;
|
|
|
|
mCapacity = aRhs.mCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
mReserved = aRhs.mReserved;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aRhs.usingInlineStorage()) {
|
2013-07-10 03:33:29 +04:00
|
|
|
/* We can't move the buffer over in this case, so copy elements. */
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = static_cast<T*>(mStorage.addr());
|
|
|
|
Impl::moveConstruct(mBegin, aRhs.beginNoCheck(), aRhs.endNoCheck());
|
2013-07-10 03:33:29 +04:00
|
|
|
/*
|
2014-07-11 06:10:17 +04:00
|
|
|
* Leave aRhs's mLength, mBegin, mCapacity, and mReserved as they are.
|
2013-07-10 03:33:29 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = aRhs.mBegin;
|
|
|
|
aRhs.mBegin = static_cast<T*>(aRhs.mStorage.addr());
|
|
|
|
aRhs.mCapacity = kInlineCapacity;
|
|
|
|
aRhs.mLength = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-11-27 00:01:19 +03:00
|
|
|
aRhs.mReserved = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Move assignment. */
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ALWAYS_INLINE TV&
|
|
|
|
VectorBase<T, N, AP, TV>::operator=(TV&& aRhs)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(this != &aRhs, "self-move assignment is prohibited");
|
2013-07-10 03:33:29 +04:00
|
|
|
TV* tv = static_cast<TV*>(this);
|
|
|
|
tv->~TV();
|
2014-07-11 06:10:17 +04:00
|
|
|
new(tv) TV(Move(aRhs));
|
2013-07-10 03:33:29 +04:00
|
|
|
return *tv;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE
|
|
|
|
VectorBase<T, N, AP, TV>::~VectorBase()
|
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!usingInlineStorage()) {
|
2013-07-10 03:33:29 +04:00
|
|
|
this->free_(beginNoCheck());
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2014-07-11 06:10:17 +04:00
|
|
|
* This function will create a new heap buffer with capacity aNewCap,
|
2013-07-10 03:33:29 +04:00
|
|
|
* move all elements in the inline buffer to this new buffer,
|
|
|
|
* and fail on OOM.
|
|
|
|
*/
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::convertToHeapStorage(size_t aNewCap)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(usingInlineStorage());
|
|
|
|
|
|
|
|
/* Allocate buffer. */
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(!detail::CapacityHasExcessSpace<T>(aNewCap));
|
2014-08-09 00:22:39 +04:00
|
|
|
T* newBuf = this->template pod_malloc<T>(aNewCap);
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!newBuf) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/* Copy inline elements into heap buffer. */
|
|
|
|
Impl::moveConstruct(newBuf, beginNoCheck(), endNoCheck());
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
|
|
|
|
/* Switch in heap buffer. */
|
|
|
|
mBegin = newBuf;
|
|
|
|
/* mLength is unchanged. */
|
2014-07-11 06:10:17 +04:00
|
|
|
mCapacity = aNewCap;
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_NEVER_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::growStorageBy(size_t aIncr)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(mLength + aIncr > mCapacity);
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aIncr == 1) {
|
2013-07-10 03:33:29 +04:00
|
|
|
if (usingInlineStorage()) {
|
|
|
|
/* This case occurs in ~70--80% of the calls to this function. */
|
|
|
|
size_t newSize =
|
2014-07-11 06:10:17 +04:00
|
|
|
tl::RoundUpPow2<(kInlineCapacity + 1) * sizeof(T)>::value;
|
2013-07-10 03:33:29 +04:00
|
|
|
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 (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;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (detail::CapacityHasExcessSpace<T>(newCap)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
newCap += 1;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
|
|
|
/* This case occurs in ~2% of the calls to this function. */
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t newMinCap = mLength + aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
/* Did mLength + aIncr overflow? Will newCap * sizeof(T) overflow? */
|
2013-07-10 03:33:29 +04:00
|
|
|
if (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()) {
|
2014-07-11 06:10:17 +04:00
|
|
|
convert:
|
2013-07-10 03:33:29 +04:00
|
|
|
return convertToHeapStorage(newCap);
|
|
|
|
}
|
|
|
|
|
|
|
|
grow:
|
|
|
|
return Impl::growTo(*this, newCap);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::initCapacity(size_t aRequest)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(empty());
|
|
|
|
MOZ_ASSERT(usingInlineStorage());
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aRequest == 0) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2014-08-09 00:22:39 +04:00
|
|
|
T* newbuf = this->template pod_malloc<T>(aRequest);
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!newbuf) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
mBegin = newbuf;
|
2014-07-11 06:10:17 +04:00
|
|
|
mCapacity = aRequest;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
mReserved = aRequest;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::reserve(size_t aRequest)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aRequest > mCapacity && !growStorageBy(aRequest - mLength)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aRequest > mReserved) {
|
|
|
|
mReserved = aRequest;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(mLength <= mReserved);
|
|
|
|
MOZ_ASSERT(mReserved <= mCapacity);
|
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::shrinkBy(size_t aIncr)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(aIncr <= mLength);
|
|
|
|
Impl::destroy(endNoCheck() - aIncr, endNoCheck());
|
|
|
|
mLength -= aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::growBy(size_t aIncr)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aIncr > mCapacity - mLength && !growStorageBy(aIncr)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
MOZ_ASSERT(mLength + aIncr <= mCapacity);
|
|
|
|
T* newend = endNoCheck() + aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
Impl::initialize(endNoCheck(), newend);
|
2014-07-11 06:10:17 +04:00
|
|
|
mLength += aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
if (mLength > mReserved) {
|
2013-07-10 03:33:29 +04:00
|
|
|
mReserved = mLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::growByUninitialized(size_t aIncr)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aIncr > mCapacity - mLength && !growStorageBy(aIncr)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2014-10-09 02:04:12 +04:00
|
|
|
infallibleGrowByUninitialized(aIncr);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE void
|
|
|
|
VectorBase<T, N, AP, TV>::infallibleGrowByUninitialized(size_t aIncr)
|
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(mLength + aIncr <= mCapacity);
|
|
|
|
mLength += aIncr;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
if (mLength > mReserved) {
|
2013-07-10 03:33:29 +04:00
|
|
|
mReserved = mLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::resize(size_t aNewLength)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
size_t curLength = mLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aNewLength > curLength) {
|
|
|
|
return growBy(aNewLength - curLength);
|
|
|
|
}
|
|
|
|
shrinkBy(curLength - aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::resizeUninitialized(size_t aNewLength)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
size_t curLength = mLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aNewLength > curLength) {
|
|
|
|
return growByUninitialized(aNewLength - curLength);
|
|
|
|
}
|
|
|
|
shrinkBy(curLength - aNewLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline void
|
|
|
|
VectorBase<T, N, AP, TV>::clear()
|
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
mLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline void
|
|
|
|
VectorBase<T, N, AP, TV>::clearAndFree()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
if (usingInlineStorage()) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
this->free_(beginNoCheck());
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = static_cast<T*>(mStorage.addr());
|
|
|
|
mCapacity = kInlineCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-11-27 00:01:19 +03:00
|
|
|
mReserved = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::canAppendWithoutRealloc(size_t aNeeded) const
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
return mLength + aNeeded <= mCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
template<typename U, size_t O, class BP, class UV>
|
|
|
|
MOZ_ALWAYS_INLINE void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::internalAppendAll(
|
|
|
|
const VectorBase<U, O, BP, UV>& aOther)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
internalAppend(aOther.begin(), aOther.length());
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
template<typename U>
|
|
|
|
MOZ_ALWAYS_INLINE void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::internalAppend(U&& aU)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mLength + 1 <= mReserved);
|
|
|
|
MOZ_ASSERT(mReserved <= mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
new(endNoCheck()) T(Forward<U>(aU));
|
2013-07-10 03:33:29 +04:00
|
|
|
++mLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::appendN(const T& aT, size_t aNeeded)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (mLength + aNeeded > mCapacity && !growStorageBy(aNeeded)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
if (mLength + aNeeded > mReserved) {
|
|
|
|
mReserved = mLength + aNeeded;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
internalAppendN(aT, aNeeded);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::internalAppendN(const T& aT, size_t aNeeded)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(mLength + aNeeded <= mReserved);
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(mReserved <= mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
Impl::copyConstructN(endNoCheck(), aNeeded, aT);
|
|
|
|
mLength += aNeeded;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
2013-11-19 21:05:36 +04:00
|
|
|
template<typename U>
|
2013-07-10 03:33:29 +04:00
|
|
|
inline T*
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::insert(T* aP, U&& aVal)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(begin() <= aP);
|
|
|
|
MOZ_ASSERT(aP <= end());
|
|
|
|
size_t pos = aP - begin();
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(pos <= mLength);
|
|
|
|
size_t oldLength = mLength;
|
|
|
|
if (pos == oldLength) {
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!append(Forward<U>(aVal))) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return nullptr;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
2013-11-19 21:05:36 +04:00
|
|
|
T oldBack = Move(back());
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!append(Move(oldBack))) { /* Dup the last element. */
|
2013-07-10 03:33:29 +04:00
|
|
|
return nullptr;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
for (size_t i = oldLength; i > pos; --i) {
|
2013-11-19 21:05:36 +04:00
|
|
|
(*this)[i] = Move((*this)[i - 1]);
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
|
|
|
(*this)[pos] = Forward<U>(aVal);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
return begin() + pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::erase(T* aIt)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(begin() <= aIt);
|
|
|
|
MOZ_ASSERT(aIt < end());
|
|
|
|
while (aIt + 1 < end()) {
|
|
|
|
*aIt = Move(*(aIt + 1));
|
|
|
|
++aIt;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
2014-05-21 00:36:40 +04:00
|
|
|
popBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::erase(T* aBegin, T* aEnd)
|
2014-05-21 00:36:40 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(begin() <= aBegin);
|
|
|
|
MOZ_ASSERT(aBegin <= aEnd);
|
|
|
|
MOZ_ASSERT(aEnd <= end());
|
|
|
|
while (aEnd < end()) {
|
|
|
|
*aBegin++ = Move(*aEnd++);
|
|
|
|
}
|
|
|
|
shrinkBy(aEnd - aBegin);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
template<typename U>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::append(const U* aInsBegin, const U* aInsEnd)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
size_t aNeeded = PointerRangeSize(aInsBegin, aInsEnd);
|
|
|
|
if (mLength + aNeeded > mCapacity && !growStorageBy(aNeeded)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
if (mLength + aNeeded > mReserved) {
|
|
|
|
mReserved = mLength + aNeeded;
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
internalAppend(aInsBegin, aNeeded);
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
template<typename U>
|
|
|
|
MOZ_ALWAYS_INLINE void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::internalAppend(const U* aInsBegin, size_t aInsLength)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
MOZ_ASSERT(mLength + aInsLength <= mReserved);
|
2013-07-10 03:33:29 +04:00
|
|
|
MOZ_ASSERT(mReserved <= mCapacity);
|
2014-07-11 06:10:17 +04:00
|
|
|
Impl::copyConstruct(endNoCheck(), aInsBegin, aInsBegin + aInsLength);
|
|
|
|
mLength += aInsLength;
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
template<typename U>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::append(U&& aU)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
2014-07-11 06:10:17 +04:00
|
|
|
if (mLength == mCapacity && !growStorageBy(1)) {
|
2013-07-10 03:33:29 +04:00
|
|
|
return false;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
if (mLength + 1 > mReserved) {
|
2013-07-10 03:33:29 +04:00
|
|
|
mReserved = mLength + 1;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
2014-07-11 06:10:17 +04:00
|
|
|
internalAppend(Forward<U>(aU));
|
2013-07-10 03:33:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
template<typename U, size_t O, class BP, class UV>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::appendAll(const VectorBase<U, O, BP, UV>& aOther)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
return append(aOther.begin(), aOther.length());
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
template<class U>
|
|
|
|
MOZ_ALWAYS_INLINE bool
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::append(const U* aInsBegin, size_t aInsLength)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
return append(aInsBegin, aInsBegin + aInsLength);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE void
|
|
|
|
VectorBase<T, N, AP, TV>::popBack()
|
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
MOZ_ASSERT(!empty());
|
|
|
|
--mLength;
|
|
|
|
endNoCheck()->~T();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
MOZ_ALWAYS_INLINE T
|
|
|
|
VectorBase<T, N, AP, TV>::popCopy()
|
|
|
|
{
|
|
|
|
T ret = back();
|
|
|
|
popBack();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline T*
|
|
|
|
VectorBase<T, N, AP, TV>::extractRawBuffer()
|
|
|
|
{
|
|
|
|
T* ret;
|
|
|
|
if (usingInlineStorage()) {
|
2014-08-09 00:22:39 +04:00
|
|
|
ret = this->template pod_malloc<T>(mLength);
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!ret) {
|
2013-07-26 03:31:48 +04:00
|
|
|
return nullptr;
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
Impl::copyConstruct(ret, beginNoCheck(), endNoCheck());
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
|
|
|
/* mBegin, mCapacity are unchanged. */
|
|
|
|
mLength = 0;
|
|
|
|
} else {
|
|
|
|
ret = mBegin;
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = static_cast<T*>(mStorage.addr());
|
2013-07-10 03:33:29 +04:00
|
|
|
mLength = 0;
|
2014-07-11 06:10:17 +04:00
|
|
|
mCapacity = kInlineCapacity;
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-11-27 00:01:19 +03:00
|
|
|
mReserved = 0;
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::replaceRawBuffer(T* aP, size_t aLength)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
|
|
|
MOZ_REENTRANCY_GUARD_ET_AL;
|
|
|
|
|
|
|
|
/* Destroy what we have. */
|
|
|
|
Impl::destroy(beginNoCheck(), endNoCheck());
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!usingInlineStorage()) {
|
2013-07-10 03:33:29 +04:00
|
|
|
this->free_(beginNoCheck());
|
2014-07-11 06:10:17 +04:00
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
/* Take in the new buffer. */
|
2014-07-11 06:10:17 +04:00
|
|
|
if (aLength <= kInlineCapacity) {
|
2013-07-10 03:33:29 +04:00
|
|
|
/*
|
2014-07-11 06:10:17 +04:00
|
|
|
* We convert to inline storage if possible, even though aP might
|
2013-07-10 03:33:29 +04:00
|
|
|
* otherwise be acceptable. Maybe this behaviour should be
|
|
|
|
* specifiable with an argument to this function.
|
|
|
|
*/
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = static_cast<T*>(mStorage.addr());
|
2013-07-10 03:33:29 +04:00
|
|
|
mLength = aLength;
|
2014-07-11 06:10:17 +04:00
|
|
|
mCapacity = kInlineCapacity;
|
|
|
|
Impl::moveConstruct(mBegin, aP, aP + aLength);
|
|
|
|
Impl::destroy(aP, aP + aLength);
|
|
|
|
this->free_(aP);
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
2014-07-11 06:10:17 +04:00
|
|
|
mBegin = aP;
|
2013-07-10 03:33:29 +04:00
|
|
|
mLength = aLength;
|
|
|
|
mCapacity = aLength;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
mReserved = aLength;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline size_t
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::sizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
return usingInlineStorage() ? 0 : aMallocSizeOf(beginNoCheck());
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline size_t
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::sizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
return aMallocSizeOf(this) + sizeOfExcludingThis(aMallocSizeOf);
|
2013-07-10 03:33:29 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, size_t N, class AP, class TV>
|
|
|
|
inline void
|
2014-07-11 06:10:17 +04:00
|
|
|
VectorBase<T, N, AP, TV>::swap(TV& aOther)
|
2013-07-10 03:33:29 +04:00
|
|
|
{
|
2013-07-18 21:59:53 +04:00
|
|
|
static_assert(N == 0,
|
|
|
|
"still need to implement this for N != 0");
|
2013-07-10 03:33:29 +04:00
|
|
|
|
|
|
|
// This only works when inline storage is always empty.
|
2014-07-11 06:10:17 +04:00
|
|
|
if (!usingInlineStorage() && aOther.usingInlineStorage()) {
|
|
|
|
aOther.mBegin = mBegin;
|
2013-07-10 03:33:29 +04:00
|
|
|
mBegin = inlineStorage();
|
2014-07-11 06:10:17 +04:00
|
|
|
} else if (usingInlineStorage() && !aOther.usingInlineStorage()) {
|
|
|
|
mBegin = aOther.mBegin;
|
|
|
|
aOther.mBegin = aOther.inlineStorage();
|
|
|
|
} else if (!usingInlineStorage() && !aOther.usingInlineStorage()) {
|
|
|
|
Swap(mBegin, aOther.mBegin);
|
2013-07-10 03:33:29 +04:00
|
|
|
} else {
|
|
|
|
// This case is a no-op, since we'd set both to use their inline storage.
|
|
|
|
}
|
|
|
|
|
2014-07-11 06:10:17 +04:00
|
|
|
Swap(mLength, aOther.mLength);
|
|
|
|
Swap(mCapacity, aOther.mCapacity);
|
2013-07-10 03:33:29 +04:00
|
|
|
#ifdef DEBUG
|
2014-07-11 06:10:17 +04:00
|
|
|
Swap(mReserved, aOther.mReserved);
|
2013-07-10 03:33:29 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
* N requirements:
|
|
|
|
* - any value, however, N 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
|
|
|
|
: public VectorBase<T,
|
|
|
|
MinInlineCapacity,
|
|
|
|
AllocPolicy,
|
|
|
|
Vector<T, MinInlineCapacity, AllocPolicy> >
|
|
|
|
{
|
2014-07-11 06:10:17 +04:00
|
|
|
typedef VectorBase<T, MinInlineCapacity, AllocPolicy, Vector> Base;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit Vector(AllocPolicy alloc = AllocPolicy()) : Base(alloc) {}
|
|
|
|
Vector(Vector&& vec) : Base(Move(vec)) {}
|
|
|
|
Vector& operator=(Vector&& aOther)
|
|
|
|
{
|
|
|
|
return Base::operator=(Move(aOther));
|
|
|
|
}
|
2013-07-10 03:33:29 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* mozilla_Vector_h */
|