2010-11-08 18:48:59 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* 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/. */
|
2010-11-08 18:48:59 -08:00
|
|
|
|
|
|
|
#ifndef nsTArray_h__
|
|
|
|
# error "Don't include this file directly"
|
|
|
|
#endif
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
|
|
|
nsTArray_base<Alloc, Copy>::nsTArray_base()
|
2010-11-08 18:48:59 -08:00
|
|
|
: mHdr(EmptyHdr()) {
|
|
|
|
MOZ_COUNT_CTOR(nsTArray_base);
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
|
|
|
nsTArray_base<Alloc, Copy>::~nsTArray_base() {
|
2010-11-08 18:48:59 -08:00
|
|
|
if (mHdr != EmptyHdr() && !UsesAutoArrayBuffer()) {
|
|
|
|
Alloc::Free(mHdr);
|
|
|
|
}
|
|
|
|
MOZ_COUNT_DTOR(nsTArray_base);
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
|
|
|
const nsTArrayHeader* nsTArray_base<Alloc, Copy>::GetAutoArrayBufferUnsafe(size_t elemAlign) const {
|
2011-10-05 06:11:17 -07:00
|
|
|
// Assuming |this| points to an nsAutoArray, we want to get a pointer to
|
|
|
|
// mAutoBuf. So just cast |this| to nsAutoArray* and read &mAutoBuf!
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const void* autoBuf = &reinterpret_cast<const nsAutoArrayBase<nsTArray<uint32_t>, 1>*>(this)->mAutoBuf;
|
2011-10-05 06:11:17 -07:00
|
|
|
|
|
|
|
// If we're on a 32-bit system and elemAlign is 8, we need to adjust our
|
|
|
|
// pointer to take into account the extra alignment in the auto array.
|
|
|
|
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(sizeof(void*) != 4 ||
|
|
|
|
(MOZ_ALIGNOF(mozilla::AlignedElem<8>) == 8 &&
|
|
|
|
sizeof(nsAutoTArray<mozilla::AlignedElem<8>, 1>) ==
|
|
|
|
sizeof(void*) + sizeof(nsTArrayHeader) +
|
|
|
|
4 + sizeof(mozilla::AlignedElem<8>)),
|
|
|
|
"auto array padding wasn't what we expected");
|
2011-10-05 06:11:17 -07:00
|
|
|
|
|
|
|
// We don't support alignments greater than 8 bytes.
|
|
|
|
NS_ABORT_IF_FALSE(elemAlign <= 4 || elemAlign == 8, "unsupported alignment.");
|
|
|
|
if (sizeof(void*) == 4 && elemAlign == 8) {
|
2011-10-14 00:06:35 -07:00
|
|
|
autoBuf = reinterpret_cast<const char*>(autoBuf) + 4;
|
2011-10-05 06:11:17 -07:00
|
|
|
}
|
|
|
|
|
2011-10-14 00:06:35 -07:00
|
|
|
return reinterpret_cast<const Header*>(autoBuf);
|
2011-10-05 06:11:17 -07:00
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
|
|
|
bool nsTArray_base<Alloc, Copy>::UsesAutoArrayBuffer() const {
|
2011-10-05 06:11:17 -07:00
|
|
|
if (!mHdr->mIsAutoArray) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2011-10-05 06:11:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is nuts. If we were sane, we'd pass elemAlign as a parameter to
|
|
|
|
// this function. Unfortunately this function is called in nsTArray_base's
|
|
|
|
// destructor, at which point we don't know elem_type's alignment.
|
|
|
|
//
|
|
|
|
// We'll fall on our face and return true when we should say false if
|
|
|
|
//
|
|
|
|
// * we're not using our auto buffer,
|
|
|
|
// * elemAlign == 4, and
|
|
|
|
// * mHdr == GetAutoArrayBuffer(8).
|
|
|
|
//
|
|
|
|
// This could happen if |*this| lives on the heap and malloc allocated our
|
|
|
|
// buffer on the heap adjacent to |*this|.
|
|
|
|
//
|
|
|
|
// However, we can show that this can't happen. If |this| is an auto array
|
|
|
|
// (as we ensured at the beginning of the method), GetAutoArrayBuffer(8)
|
|
|
|
// always points to memory owned by |*this|, because (as we assert below)
|
|
|
|
//
|
|
|
|
// * GetAutoArrayBuffer(8) is at most 4 bytes past GetAutoArrayBuffer(4), and
|
|
|
|
// * sizeof(nsTArrayHeader) > 4.
|
|
|
|
//
|
|
|
|
// Since nsAutoTArray always contains an nsTArrayHeader,
|
|
|
|
// GetAutoArrayBuffer(8) will always point inside the auto array object,
|
|
|
|
// even if it doesn't point at the beginning of the header.
|
|
|
|
//
|
|
|
|
// Note that this means that we can't store elements with alignment 16 in an
|
|
|
|
// nsTArray, because GetAutoArrayBuffer(16) could lie outside the memory
|
|
|
|
// owned by this nsAutoTArray. We statically assert that elem_type's
|
|
|
|
// alignment is 8 bytes or less in nsAutoArrayBase.
|
|
|
|
|
2013-07-18 10:59:53 -07:00
|
|
|
static_assert(sizeof(nsTArrayHeader) > 4,
|
|
|
|
"see comment above");
|
2011-10-05 06:11:17 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-08-09 00:09:31 -07:00
|
|
|
ptrdiff_t diff = reinterpret_cast<const char*>(GetAutoArrayBuffer(8)) -
|
2011-10-14 00:06:35 -07:00
|
|
|
reinterpret_cast<const char*>(GetAutoArrayBuffer(4));
|
2011-10-05 06:11:17 -07:00
|
|
|
NS_ABORT_IF_FALSE(diff >= 0 && diff <= 4, "GetAutoArrayBuffer doesn't do what we expect.");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return mHdr == GetAutoArrayBuffer(4) || mHdr == GetAutoArrayBuffer(8);
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:03:35 -07:00
|
|
|
// defined in nsTArray.cpp
|
|
|
|
bool
|
|
|
|
IsTwiceTheRequiredBytesRepresentableAsUint32(size_t capacity, size_t elemSize);
|
2011-10-05 06:11:17 -07:00
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
2013-03-21 12:02:20 -07:00
|
|
|
typename Alloc::ResultTypeProxy
|
2013-06-18 03:00:37 -07:00
|
|
|
nsTArray_base<Alloc, Copy>::EnsureCapacity(size_type capacity, size_type elemSize) {
|
2010-11-08 18:48:59 -08:00
|
|
|
// This should be the most common case so test this first
|
|
|
|
if (capacity <= mHdr->mCapacity)
|
2013-03-21 12:02:20 -07:00
|
|
|
return Alloc::SuccessResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
|
|
|
|
// If the requested memory allocation exceeds size_type(-1)/2, then
|
|
|
|
// our doubling algorithm may not be able to allocate it.
|
2014-05-08 18:03:35 -07:00
|
|
|
// Additionally, if it exceeds uint32_t(-1) then we couldn't fit in the
|
|
|
|
// Header::mCapacity member. Just bail out in cases like that. We don't want
|
|
|
|
// to be allocating 2 GB+ arrays anyway.
|
|
|
|
if (!IsTwiceTheRequiredBytesRepresentableAsUint32(capacity, elemSize)) {
|
2013-11-25 12:06:17 -08:00
|
|
|
Alloc::SizeTooBig((size_t)capacity * elemSize);
|
2013-03-21 12:02:20 -07:00
|
|
|
return Alloc::FailureResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mHdr == EmptyHdr()) {
|
|
|
|
// Malloc() new data
|
2014-06-02 02:26:12 -07:00
|
|
|
Header *header = static_cast<Header*>
|
|
|
|
(Alloc::Malloc(sizeof(Header) + capacity * elemSize));
|
|
|
|
if (!header)
|
2013-03-21 12:02:20 -07:00
|
|
|
return Alloc::FailureResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
header->mLength = 0;
|
2014-06-02 02:26:12 -07:00
|
|
|
header->mCapacity = capacity;
|
2010-11-08 18:48:59 -08:00
|
|
|
header->mIsAutoArray = 0;
|
2014-06-02 02:26:12 -07:00
|
|
|
mHdr = header;
|
2010-11-08 18:48:59 -08:00
|
|
|
|
2014-06-02 02:26:12 -07:00
|
|
|
return Alloc::SuccessResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We increase our capacity so |capacity * elemSize + sizeof(Header)| is the
|
|
|
|
// next power of two, if this value is less than pageSize bytes, or otherwise
|
|
|
|
// so it's the next multiple of pageSize.
|
|
|
|
const size_t pageSizeBytes = 12;
|
|
|
|
const size_t pageSize = 1 << pageSizeBytes;
|
|
|
|
|
|
|
|
size_t minBytes = capacity * elemSize + sizeof(Header);
|
|
|
|
size_t bytesToAlloc;
|
|
|
|
if (minBytes >= pageSize) {
|
|
|
|
// Round up to the next multiple of pageSize.
|
|
|
|
bytesToAlloc = pageSize * ((minBytes + pageSize - 1) / pageSize);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Round up to the next power of two. See
|
|
|
|
// http://graphics.stanford.edu/~seander/bithacks.html
|
|
|
|
bytesToAlloc = minBytes - 1;
|
|
|
|
bytesToAlloc |= bytesToAlloc >> 1;
|
|
|
|
bytesToAlloc |= bytesToAlloc >> 2;
|
|
|
|
bytesToAlloc |= bytesToAlloc >> 4;
|
|
|
|
bytesToAlloc |= bytesToAlloc >> 8;
|
|
|
|
bytesToAlloc |= bytesToAlloc >> 16;
|
|
|
|
bytesToAlloc++;
|
|
|
|
|
|
|
|
MOZ_ASSERT((bytesToAlloc & (bytesToAlloc - 1)) == 0,
|
|
|
|
"nsTArray's allocation size should be a power of two!");
|
|
|
|
}
|
|
|
|
|
|
|
|
Header *header;
|
|
|
|
if (UsesAutoArrayBuffer() || !Copy::allowRealloc) {
|
2010-11-08 18:48:59 -08:00
|
|
|
// Malloc() and copy
|
2014-06-02 02:26:12 -07:00
|
|
|
header = static_cast<Header*>(Alloc::Malloc(bytesToAlloc));
|
|
|
|
if (!header)
|
2013-03-21 12:02:20 -07:00
|
|
|
return Alloc::FailureResult();
|
2014-06-02 02:26:12 -07:00
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
Copy::CopyHeaderAndElements(header, mHdr, Length(), elemSize);
|
|
|
|
|
2014-06-02 02:26:12 -07:00
|
|
|
if (!UsesAutoArrayBuffer())
|
2013-06-18 03:00:37 -07:00
|
|
|
Alloc::Free(mHdr);
|
2010-11-08 18:48:59 -08:00
|
|
|
} else {
|
|
|
|
// Realloc() existing data
|
2014-06-02 02:26:12 -07:00
|
|
|
header = static_cast<Header*>(Alloc::Realloc(mHdr, bytesToAlloc));
|
|
|
|
if (!header)
|
2013-03-21 12:02:20 -07:00
|
|
|
return Alloc::FailureResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
2014-06-02 02:26:12 -07:00
|
|
|
// How many elements can we fit in bytesToAlloc?
|
|
|
|
size_t newCapacity = (bytesToAlloc - sizeof(Header)) / elemSize;
|
|
|
|
MOZ_ASSERT(newCapacity >= capacity, "Didn't enlarge the array enough!");
|
2011-09-20 21:46:56 -07:00
|
|
|
header->mCapacity = newCapacity;
|
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
mHdr = header;
|
|
|
|
|
2013-03-21 12:02:20 -07:00
|
|
|
return Alloc::SuccessResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
2010-11-08 18:48:59 -08:00
|
|
|
void
|
2013-06-18 03:00:37 -07:00
|
|
|
nsTArray_base<Alloc, Copy>::ShrinkCapacity(size_type elemSize, size_t elemAlign) {
|
2010-11-08 18:48:59 -08:00
|
|
|
if (mHdr == EmptyHdr() || UsesAutoArrayBuffer())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (mHdr->mLength >= mHdr->mCapacity) // should never be greater than...
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_type length = Length();
|
|
|
|
|
2011-10-05 06:11:17 -07:00
|
|
|
if (IsAutoArray() && GetAutoArrayBuffer(elemAlign)->mCapacity >= length) {
|
|
|
|
Header* header = GetAutoArrayBuffer(elemAlign);
|
2010-11-08 18:48:59 -08:00
|
|
|
|
|
|
|
// Copy data, but don't copy the header to avoid overwriting mCapacity
|
|
|
|
header->mLength = length;
|
2013-06-18 03:00:37 -07:00
|
|
|
Copy::CopyElements(header + 1, mHdr + 1, length, elemSize);
|
2010-11-08 18:48:59 -08:00
|
|
|
|
|
|
|
Alloc::Free(mHdr);
|
|
|
|
mHdr = header;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (length == 0) {
|
2012-03-03 13:16:13 -08:00
|
|
|
MOZ_ASSERT(!IsAutoArray(), "autoarray should have fit 0 elements");
|
2010-11-08 18:48:59 -08:00
|
|
|
Alloc::Free(mHdr);
|
|
|
|
mHdr = EmptyHdr();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-06-02 02:26:12 -07:00
|
|
|
size_type size = sizeof(Header) + length * elemSize;
|
|
|
|
void *ptr = Alloc::Realloc(mHdr, size);
|
|
|
|
if (!ptr)
|
|
|
|
return;
|
|
|
|
mHdr = static_cast<Header*>(ptr);
|
|
|
|
mHdr->mCapacity = length;
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
2010-11-08 18:48:59 -08:00
|
|
|
void
|
2013-06-18 03:00:37 -07:00
|
|
|
nsTArray_base<Alloc, Copy>::ShiftData(index_type start,
|
2010-11-08 18:48:59 -08:00
|
|
|
size_type oldLen, size_type newLen,
|
2011-10-05 06:11:17 -07:00
|
|
|
size_type elemSize, size_t elemAlign) {
|
2010-11-08 18:48:59 -08:00
|
|
|
if (oldLen == newLen)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Determine how many elements need to be shifted
|
|
|
|
size_type num = mHdr->mLength - (start + oldLen);
|
|
|
|
|
|
|
|
// Compute the resulting length of the array
|
|
|
|
mHdr->mLength += newLen - oldLen;
|
|
|
|
if (mHdr->mLength == 0) {
|
2011-10-05 06:11:17 -07:00
|
|
|
ShrinkCapacity(elemSize, elemAlign);
|
2010-11-08 18:48:59 -08:00
|
|
|
} else {
|
|
|
|
// Maybe nothing needs to be shifted
|
|
|
|
if (num == 0)
|
|
|
|
return;
|
|
|
|
// Perform shift (change units to bytes first)
|
|
|
|
start *= elemSize;
|
|
|
|
newLen *= elemSize;
|
|
|
|
oldLen *= elemSize;
|
|
|
|
char *base = reinterpret_cast<char*>(mHdr + 1) + start;
|
2013-06-18 03:00:37 -07:00
|
|
|
Copy::MoveElements(base + newLen, base + oldLen, num, elemSize);
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-06-18 03:00:37 -07:00
|
|
|
nsTArray_base<Alloc, Copy>::InsertSlotsAt(index_type index, size_type count,
|
2011-10-05 06:11:17 -07:00
|
|
|
size_type elementSize, size_t elemAlign) {
|
2012-03-03 13:16:13 -08:00
|
|
|
MOZ_ASSERT(index <= Length(), "Bogus insertion index");
|
2010-11-08 18:48:59 -08:00
|
|
|
size_type newLen = Length() + count;
|
|
|
|
|
|
|
|
EnsureCapacity(newLen, elementSize);
|
|
|
|
|
|
|
|
// Check for out of memory conditions
|
|
|
|
if (Capacity() < newLen)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-11-08 18:48:59 -08:00
|
|
|
|
|
|
|
// Move the existing elements as needed. Note that this will
|
|
|
|
// change our mLength, so no need to call IncrementLength.
|
2011-10-05 06:11:17 -07:00
|
|
|
ShiftData(index, 0, count, elementSize, elemAlign);
|
2013-06-18 03:00:37 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
2011-09-22 08:22:20 -07:00
|
|
|
// nsTArray_base::IsAutoArrayRestorer is an RAII class which takes
|
|
|
|
// |nsTArray_base &array| in its constructor. When it's destructed, it ensures
|
|
|
|
// that
|
|
|
|
//
|
|
|
|
// * array.mIsAutoArray has the same value as it did when we started, and
|
|
|
|
// * if array has an auto buffer and mHdr would otherwise point to sEmptyHdr,
|
|
|
|
// array.mHdr points to array's auto buffer.
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
|
|
|
nsTArray_base<Alloc, Copy>::IsAutoArrayRestorer::IsAutoArrayRestorer(
|
|
|
|
nsTArray_base<Alloc, Copy> &array,
|
|
|
|
size_t elemAlign)
|
2011-09-22 08:22:20 -07:00
|
|
|
: mArray(array),
|
2011-10-05 06:11:17 -07:00
|
|
|
mElemAlign(elemAlign),
|
2011-09-22 08:22:20 -07:00
|
|
|
mIsAuto(array.IsAutoArray())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
|
|
|
nsTArray_base<Alloc, Copy>::IsAutoArrayRestorer::~IsAutoArrayRestorer() {
|
2011-09-22 08:22:20 -07:00
|
|
|
// Careful: We don't want to set mIsAutoArray = 1 on sEmptyHdr.
|
|
|
|
if (mIsAuto && mArray.mHdr == mArray.EmptyHdr()) {
|
|
|
|
// Call GetAutoArrayBufferUnsafe() because GetAutoArrayBuffer() asserts
|
|
|
|
// that mHdr->mIsAutoArray is true, which surely isn't the case here.
|
2011-10-05 06:11:17 -07:00
|
|
|
mArray.mHdr = mArray.GetAutoArrayBufferUnsafe(mElemAlign);
|
2011-09-22 08:22:20 -07:00
|
|
|
mArray.mHdr->mLength = 0;
|
|
|
|
}
|
2013-02-26 08:24:41 -08:00
|
|
|
else if (mArray.mHdr != mArray.EmptyHdr()) {
|
2011-09-22 08:22:20 -07:00
|
|
|
mArray.mHdr->mIsAutoArray = mIsAuto;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
2010-11-08 18:48:59 -08:00
|
|
|
template<class Allocator>
|
2013-04-01 10:43:34 -07:00
|
|
|
typename Alloc::ResultTypeProxy
|
2013-06-18 03:00:37 -07:00
|
|
|
nsTArray_base<Alloc, Copy>::SwapArrayElements(nsTArray_base<Allocator, Copy>& other,
|
|
|
|
size_type elemSize, size_t elemAlign) {
|
2010-11-08 18:48:59 -08:00
|
|
|
|
2011-09-22 08:22:20 -07:00
|
|
|
// EnsureNotUsingAutoArrayBuffer will set mHdr = sEmptyHdr even if we have an
|
|
|
|
// auto buffer. We need to point mHdr back to our auto buffer before we
|
|
|
|
// return, otherwise we'll forget that we have an auto buffer at all!
|
|
|
|
// IsAutoArrayRestorer takes care of this for us.
|
|
|
|
|
2011-10-05 06:11:17 -07:00
|
|
|
IsAutoArrayRestorer ourAutoRestorer(*this, elemAlign);
|
2013-06-18 03:00:37 -07:00
|
|
|
typename nsTArray_base<Allocator, Copy>::IsAutoArrayRestorer otherAutoRestorer(other, elemAlign);
|
2011-09-22 08:22:20 -07:00
|
|
|
|
|
|
|
// If neither array uses an auto buffer which is big enough to store the
|
|
|
|
// other array's elements, then ensure that both arrays use malloc'ed storage
|
|
|
|
// and swap their mHdr pointers.
|
|
|
|
if ((!UsesAutoArrayBuffer() || Capacity() < other.Length()) &&
|
|
|
|
(!other.UsesAutoArrayBuffer() || other.Capacity() < Length())) {
|
|
|
|
|
|
|
|
if (!EnsureNotUsingAutoArrayBuffer(elemSize) ||
|
|
|
|
!other.EnsureNotUsingAutoArrayBuffer(elemSize)) {
|
2013-04-01 10:43:34 -07:00
|
|
|
return Alloc::FailureResult();
|
2011-09-22 08:22:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Header *temp = mHdr;
|
|
|
|
mHdr = other.mHdr;
|
|
|
|
other.mHdr = temp;
|
|
|
|
|
2013-04-01 10:43:34 -07:00
|
|
|
return Alloc::SuccessResult();
|
2011-09-22 08:22:20 -07:00
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
// Swap the two arrays by copying, since at least one is using an auto
|
2011-09-22 08:22:20 -07:00
|
|
|
// buffer which is large enough to hold all of the other's elements. We'll
|
|
|
|
// copy the shorter array into temporary storage.
|
|
|
|
//
|
|
|
|
// (We could do better than this in some circumstances. Suppose we're
|
|
|
|
// swapping arrays X and Y. X has space for 2 elements in its auto buffer,
|
|
|
|
// but currently has length 4, so it's using malloc'ed storage. Y has length
|
|
|
|
// 2. When we swap X and Y, we don't need to use a temporary buffer; we can
|
|
|
|
// write Y straight into X's auto buffer, write X's malloc'ed buffer on top
|
|
|
|
// of Y, and then switch X to using its auto buffer.)
|
|
|
|
|
2013-03-21 12:02:20 -07:00
|
|
|
if (!Alloc::Successful(EnsureCapacity(other.Length(), elemSize)) ||
|
|
|
|
!Allocator::Successful(other.EnsureCapacity(Length(), elemSize))) {
|
2013-04-01 10:43:34 -07:00
|
|
|
return Alloc::FailureResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
2011-09-22 08:22:20 -07:00
|
|
|
// The EnsureCapacity calls above shouldn't have caused *both* arrays to
|
|
|
|
// switch from their auto buffers to malloc'ed space.
|
2011-10-05 06:11:17 -07:00
|
|
|
NS_ABORT_IF_FALSE(UsesAutoArrayBuffer() ||
|
|
|
|
other.UsesAutoArrayBuffer(),
|
2011-09-22 08:22:20 -07:00
|
|
|
"One of the arrays should be using its auto buffer.");
|
|
|
|
|
2013-01-15 04:22:03 -08:00
|
|
|
size_type smallerLength = XPCOM_MIN(Length(), other.Length());
|
|
|
|
size_type largerLength = XPCOM_MAX(Length(), other.Length());
|
2011-09-22 08:22:20 -07:00
|
|
|
void *smallerElements, *largerElements;
|
|
|
|
if (Length() <= other.Length()) {
|
|
|
|
smallerElements = Hdr() + 1;
|
|
|
|
largerElements = other.Hdr() + 1;
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
2011-09-22 08:22:20 -07:00
|
|
|
else {
|
|
|
|
smallerElements = other.Hdr() + 1;
|
|
|
|
largerElements = Hdr() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate temporary storage for the smaller of the two arrays. We want to
|
2012-02-22 03:26:21 -08:00
|
|
|
// allocate this space on the stack, if it's not too large. Sounds like a
|
2011-09-22 08:22:20 -07:00
|
|
|
// job for AutoTArray! (One of the two arrays we're swapping is using an
|
|
|
|
// auto buffer, so we're likely not allocating a lot of space here. But one
|
|
|
|
// could, in theory, allocate a huge AutoTArray on the heap.)
|
2012-12-18 17:16:06 -08:00
|
|
|
nsAutoArrayBase<nsTArray_Impl<uint8_t, Alloc>, 64> temp;
|
2013-03-21 12:02:20 -07:00
|
|
|
if (!Alloc::Successful(temp.EnsureCapacity(smallerLength, elemSize))) {
|
2013-04-01 10:43:34 -07:00
|
|
|
return Alloc::FailureResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
Copy::CopyElements(temp.Elements(), smallerElements, smallerLength, elemSize);
|
|
|
|
Copy::CopyElements(smallerElements, largerElements, largerLength, elemSize);
|
|
|
|
Copy::CopyElements(largerElements, temp.Elements(), smallerLength, elemSize);
|
2010-11-08 18:48:59 -08:00
|
|
|
|
2011-09-22 08:22:20 -07:00
|
|
|
// Swap the arrays' lengths.
|
|
|
|
NS_ABORT_IF_FALSE((other.Length() == 0 || mHdr != EmptyHdr()) &&
|
|
|
|
(Length() == 0 || other.mHdr != EmptyHdr()),
|
|
|
|
"Don't set sEmptyHdr's length.");
|
|
|
|
size_type tempLength = Length();
|
|
|
|
mHdr->mLength = other.Length();
|
|
|
|
other.mHdr->mLength = tempLength;
|
2010-11-08 18:48:59 -08:00
|
|
|
|
2013-04-01 10:43:34 -07:00
|
|
|
return Alloc::SuccessResult();
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
template<class Alloc, class Copy>
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2013-06-18 03:00:37 -07:00
|
|
|
nsTArray_base<Alloc, Copy>::EnsureNotUsingAutoArrayBuffer(size_type elemSize) {
|
2010-11-08 18:48:59 -08:00
|
|
|
if (UsesAutoArrayBuffer()) {
|
2011-09-22 08:22:20 -07:00
|
|
|
|
|
|
|
// If you call this on a 0-length array, we'll set that array's mHdr to
|
|
|
|
// sEmptyHdr, in flagrant violation of the nsAutoTArray invariants. It's
|
|
|
|
// up to you to set it back! (If you don't, the nsAutoTArray will forget
|
|
|
|
// that it has an auto buffer.)
|
|
|
|
if (Length() == 0) {
|
|
|
|
mHdr = EmptyHdr();
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2011-09-22 08:22:20 -07:00
|
|
|
}
|
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
size_type size = sizeof(Header) + Length() * elemSize;
|
|
|
|
|
|
|
|
Header* header = static_cast<Header*>(Alloc::Malloc(size));
|
|
|
|
if (!header)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-11-08 18:48:59 -08:00
|
|
|
|
2013-06-18 03:00:37 -07:00
|
|
|
Copy::CopyHeaderAndElements(header, mHdr, Length(), elemSize);
|
2010-11-08 18:48:59 -08:00
|
|
|
header->mCapacity = Length();
|
|
|
|
mHdr = header;
|
|
|
|
}
|
2013-06-18 03:00:37 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-11-08 18:48:59 -08:00
|
|
|
}
|