2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; c-file-offsets: ((substatement-open . 0)) -*- */
|
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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2013-11-12 16:22:38 -08:00
|
|
|
#include "mozilla/MathAlgorithms.h"
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "nsVoidArray.h"
|
|
|
|
#include "nsQuickSort.h"
|
|
|
|
#include "nsISupportsImpl.h" // for nsTraceRefcnt
|
2011-10-28 00:35:45 -07:00
|
|
|
#include "nsAlgorithm.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Grow the array by at least this many elements at a time.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t kMinGrowArrayBy = 8;
|
|
|
|
static const int32_t kMaxGrowArrayBy = 1024;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This is the threshold (in bytes) of the mImpl struct, past which
|
|
|
|
* we'll force the array to grow geometrically
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
static const int32_t kLinearThreshold = 24 * sizeof(void *);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the number of bytes requires for the mImpl struct that will
|
|
|
|
* hold |n| elements.
|
|
|
|
*/
|
|
|
|
#define SIZEOF_IMPL(n_) (sizeof(Impl) + sizeof(void *) * ((n_) - 1))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute the number of elements that an mImpl struct of |n| bytes
|
|
|
|
* will hold.
|
|
|
|
*/
|
|
|
|
#define CAPACITYOF_IMPL(n_) ((((n_) - sizeof(Impl)) / sizeof(void *)) + 1)
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
#define MAXVOID 10
|
|
|
|
|
|
|
|
class VoidStats {
|
|
|
|
public:
|
|
|
|
VoidStats();
|
|
|
|
~VoidStats();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
static int sizesUsed; // number of the elements of the arrays used
|
|
|
|
static int sizesAlloced[MAXVOID]; // sizes of the allocations. sorted
|
|
|
|
static int NumberOfSize[MAXVOID]; // number of this allocation size (1 per array)
|
|
|
|
static int AllocedOfSize[MAXVOID]; // number of this allocation size (each size for array used)
|
|
|
|
static int MaxAuto[MAXVOID]; // AutoArrays that maxed out at this size
|
|
|
|
static int GrowInPlace[MAXVOID]; // arrays this size that grew in-place via realloc
|
|
|
|
|
|
|
|
// these are per-allocation
|
|
|
|
static int MaxElements[2000]; // # of arrays that maxed out at each size.
|
|
|
|
|
|
|
|
// statistics macros
|
|
|
|
#define ADD_TO_STATS(x,size) do {int i; for (i = 0; i < sizesUsed; i++) \
|
|
|
|
{ \
|
|
|
|
if (sizesAlloced[i] == (int)(size)) \
|
|
|
|
{ ((x)[i])++; break; } \
|
|
|
|
} \
|
|
|
|
if (i >= sizesUsed && sizesUsed < MAXVOID) \
|
|
|
|
{ sizesAlloced[sizesUsed] = (size); \
|
|
|
|
((x)[sizesUsed++])++; break; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define SUB_FROM_STATS(x,size) do {int i; for (i = 0; i < sizesUsed; i++) \
|
|
|
|
{ \
|
|
|
|
if (sizesAlloced[i] == (int)(size)) \
|
|
|
|
{ ((x)[i])--; break; } \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
VoidStats::VoidStats()
|
|
|
|
{
|
|
|
|
sizesUsed = 1;
|
|
|
|
sizesAlloced[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VoidStats::~VoidStats()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < sizesUsed; i++)
|
|
|
|
{
|
|
|
|
printf("Size %d:\n",sizesAlloced[i]);
|
|
|
|
printf("\tNumber of VoidArrays this size (max): %d\n",NumberOfSize[i]-MaxAuto[i]);
|
|
|
|
printf("\tNumber of AutoVoidArrays this size (max): %d\n",MaxAuto[i]);
|
|
|
|
printf("\tNumber of allocations this size (total): %d\n",AllocedOfSize[i]);
|
|
|
|
printf("\tNumber of GrowsInPlace this size (total): %d\n",GrowInPlace[i]);
|
|
|
|
}
|
|
|
|
printf("Max Size of VoidArray:\n");
|
|
|
|
for (i = 0; i < (int)(sizeof(MaxElements)/sizeof(MaxElements[0])); i++)
|
|
|
|
{
|
|
|
|
if (MaxElements[i])
|
|
|
|
printf("\t%d: %d\n",i,MaxElements[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just so constructor/destructor's get called
|
|
|
|
VoidStats gVoidStats;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void
|
2012-12-22 01:00:15 -08:00
|
|
|
nsVoidArray::SetArray(Impl *newImpl, int32_t aSize, int32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// old mImpl has been realloced and so we don't free/delete it
|
|
|
|
NS_PRECONDITION(newImpl, "can't set size");
|
|
|
|
mImpl = newImpl;
|
|
|
|
mImpl->mCount = aCount;
|
2012-12-22 01:00:15 -08:00
|
|
|
mImpl->mSize = aSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// This does all allocation/reallocation of the array.
|
|
|
|
// It also will compact down to N - good for things that might grow a lot
|
|
|
|
// at times, but usually are smaller, like JS deferred GC releases.
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsVoidArray::SizeTo(int32_t aSize)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t oldsize = GetArraySize();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (aSize == (int32_t) oldsize)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true; // no change
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (aSize <= 0)
|
|
|
|
{
|
|
|
|
// free the array if allocated
|
|
|
|
if (mImpl)
|
|
|
|
{
|
2012-12-22 01:00:15 -08:00
|
|
|
free(reinterpret_cast<char *>(mImpl));
|
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-12-22 01:00:15 -08:00
|
|
|
if (mImpl)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// We currently own an array impl. Resize it appropriately.
|
|
|
|
if (aSize < mImpl->mCount)
|
|
|
|
{
|
|
|
|
// XXX Note: we could also just resize to mCount
|
2011-10-17 07:59:28 -07:00
|
|
|
return true; // can't make it that small, ignore request
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
char* bytes = (char *) realloc(mImpl,SIZEOF_IMPL(aSize));
|
2007-07-08 00:08:04 -07:00
|
|
|
Impl* newImpl = reinterpret_cast<Impl*>(bytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!newImpl)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl == newImpl)
|
|
|
|
ADD_TO_STATS(GrowInPlace,oldsize);
|
|
|
|
ADD_TO_STATS(AllocedOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (aSize > mMaxSize)
|
|
|
|
{
|
|
|
|
ADD_TO_STATS(NumberOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (oldsize)
|
|
|
|
SUB_FROM_STATS(NumberOfSize,oldsize);
|
|
|
|
mMaxSize = aSize;
|
|
|
|
if (mIsAuto)
|
|
|
|
{
|
|
|
|
ADD_TO_STATS(MaxAuto,SIZEOF_IMPL(aSize));
|
|
|
|
SUB_FROM_STATS(MaxAuto,oldsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2012-12-22 01:00:15 -08:00
|
|
|
SetArray(newImpl, aSize, newImpl->mCount);
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if ((uint32_t) aSize < oldsize) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// No point in allocating if it won't free the current Impl anyway.
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// just allocate an array
|
|
|
|
// allocate the exact size requested
|
|
|
|
char* bytes = (char *) malloc(SIZEOF_IMPL(aSize));
|
2007-07-08 00:08:04 -07:00
|
|
|
Impl* newImpl = reinterpret_cast<Impl*>(bytes);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!newImpl)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
ADD_TO_STATS(AllocedOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (aSize > mMaxSize)
|
|
|
|
{
|
|
|
|
ADD_TO_STATS(NumberOfSize,SIZEOF_IMPL(aSize));
|
|
|
|
if (oldsize && !mImpl)
|
|
|
|
SUB_FROM_STATS(NumberOfSize,oldsize);
|
|
|
|
mMaxSize = aSize;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
ADD_TO_STATS(MaxAuto,SIZEOF_IMPL(aSize));
|
|
|
|
SUB_FROM_STATS(MaxAuto,0);
|
|
|
|
SUB_FROM_STATS(NumberOfSize,0);
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAuto = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
// We must be growing an nsAutoVoidArray - copy since we didn't
|
|
|
|
// realloc.
|
|
|
|
memcpy(newImpl->mArray, mImpl->mArray,
|
|
|
|
mImpl->mCount * sizeof(mImpl->mArray[0]));
|
|
|
|
}
|
|
|
|
|
2012-12-22 01:00:15 -08:00
|
|
|
SetArray(newImpl, aSize, mImpl ? mImpl->mCount : 0);
|
2007-03-22 10:30:00 -07:00
|
|
|
// no memset; handled later in ReplaceElementAt if needed
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsVoidArray::GrowArrayBy(int32_t aGrowBy)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// We have to grow the array. Grow by kMinGrowArrayBy slots if we're
|
|
|
|
// smaller than kLinearThreshold bytes, or a power of two if we're
|
|
|
|
// larger. This is much more efficient with most memory allocators,
|
|
|
|
// especially if it's very large, or of the allocator is binned.
|
|
|
|
if (aGrowBy < kMinGrowArrayBy)
|
|
|
|
aGrowBy = kMinGrowArrayBy;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t newCapacity = GetArraySize() + aGrowBy; // Minimum increase
|
|
|
|
uint32_t newSize = SIZEOF_IMPL(newCapacity);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (newSize >= (uint32_t) kLinearThreshold)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// newCount includes enough space for at least kMinGrowArrayBy new
|
|
|
|
// slots. Select the next power-of-two size in bytes above or
|
|
|
|
// equal to that.
|
|
|
|
// Also, limit the increase in size to about a VM page or two.
|
|
|
|
if (GetArraySize() >= kMaxGrowArrayBy)
|
|
|
|
{
|
2013-01-15 04:22:03 -08:00
|
|
|
newCapacity = GetArraySize() + XPCOM_MAX(kMaxGrowArrayBy,aGrowBy);
|
2007-03-22 10:30:00 -07:00
|
|
|
newSize = SIZEOF_IMPL(newCapacity);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-11-12 16:22:38 -08:00
|
|
|
newSize = mozilla::CeilingLog2(newSize);
|
2012-10-19 19:01:43 -07:00
|
|
|
newCapacity = CAPACITYOF_IMPL(1u << newSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// frees old mImpl IF this succeeds
|
|
|
|
if (!SizeTo(newCapacity))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsVoidArray::nsVoidArray()
|
2012-07-30 07:20:58 -07:00
|
|
|
: mImpl(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsVoidArray);
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
mMaxCount = 0;
|
|
|
|
mMaxSize = 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAuto = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
ADD_TO_STATS(NumberOfSize,0);
|
|
|
|
MaxElements[0]++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsVoidArray::nsVoidArray(int32_t aCount)
|
2012-07-30 07:20:58 -07:00
|
|
|
: mImpl(nullptr)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsVoidArray);
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
mMaxCount = 0;
|
|
|
|
mMaxSize = 0;
|
2011-10-17 07:59:28 -07:00
|
|
|
mIsAuto = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
MaxElements[0]++;
|
|
|
|
#endif
|
|
|
|
SizeTo(aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsVoidArray& nsVoidArray::operator=(const nsVoidArray& other)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t otherCount = other.Count();
|
|
|
|
int32_t maxCount = GetArraySize();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (otherCount)
|
|
|
|
{
|
|
|
|
if (otherCount > maxCount)
|
|
|
|
{
|
|
|
|
// frees old mImpl IF this succeeds
|
|
|
|
if (!GrowArrayBy(otherCount-maxCount))
|
|
|
|
return *this; // XXX The allocation failed - don't do anything
|
|
|
|
|
|
|
|
memcpy(mImpl->mArray, other.mImpl->mArray, otherCount * sizeof(mImpl->mArray[0]));
|
|
|
|
mImpl->mCount = otherCount;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// the old array can hold the new array
|
|
|
|
memcpy(mImpl->mArray, other.mImpl->mArray, otherCount * sizeof(mImpl->mArray[0]));
|
|
|
|
mImpl->mCount = otherCount;
|
|
|
|
// if it shrank a lot, compact it anyways
|
|
|
|
if ((otherCount*2) < maxCount && maxCount > 100)
|
|
|
|
{
|
|
|
|
Compact(); // shrank by at least 50 entries
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
2012-08-22 08:56:38 -07:00
|
|
|
mImpl->mCount < (int32_t)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Why do we drop the buffer here when we don't in Clear()?
|
|
|
|
SizeTo(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsVoidArray::~nsVoidArray()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsVoidArray);
|
2012-12-22 01:00:15 -08:00
|
|
|
if (mImpl)
|
2007-07-08 00:08:04 -07:00
|
|
|
free(reinterpret_cast<char*>(mImpl));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsVoidArray::SetCount(int32_t aNewCount)
|
2010-03-15 04:38:50 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aNewCount >= 0,"SetCount(negative index)");
|
|
|
|
if (aNewCount < 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-15 04:38:50 -07:00
|
|
|
|
|
|
|
if (aNewCount == 0)
|
|
|
|
{
|
|
|
|
Clear();
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-03-15 04:38:50 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(aNewCount) > uint32_t(GetArraySize()))
|
2010-03-15 04:38:50 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t oldCount = Count();
|
|
|
|
int32_t growDelta = aNewCount - oldCount;
|
2010-03-15 04:38:50 -07:00
|
|
|
|
|
|
|
// frees old mImpl IF this succeeds
|
|
|
|
if (!GrowArrayBy(growDelta))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2010-03-15 04:38:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aNewCount > mImpl->mCount)
|
|
|
|
{
|
|
|
|
// Make sure that new entries added to the array by this
|
|
|
|
// SetCount are cleared to 0. Some users of this assume that.
|
|
|
|
// This code means we don't have to memset when we allocate an array.
|
|
|
|
memset(&mImpl->mArray[mImpl->mCount], 0,
|
|
|
|
(aNewCount - mImpl->mCount) * sizeof(mImpl->mArray[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mImpl->mCount = aNewCount;
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
2012-08-22 08:56:38 -07:00
|
|
|
mImpl->mCount < (int32_t)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
2010-03-15 04:38:50 -07:00
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2010-03-15 04:38:50 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nsVoidArray::IndexOf(void* aPossibleElement) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
void** ap = mImpl->mArray;
|
|
|
|
void** end = ap + mImpl->mCount;
|
|
|
|
while (ap < end)
|
|
|
|
{
|
|
|
|
if (*ap == aPossibleElement)
|
|
|
|
{
|
|
|
|
return ap - mImpl->mArray;
|
|
|
|
}
|
|
|
|
ap++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsVoidArray::InsertElementAt(void* aElement, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t oldCount = Count();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(aIndex >= 0,"InsertElementAt(negative index)");
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(aIndex) > uint32_t(oldCount))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// An invalid index causes the insertion to fail
|
|
|
|
// Invalid indexes are ones that add more than one entry to the
|
|
|
|
// array (i.e., they can append).
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oldCount >= GetArraySize())
|
|
|
|
{
|
|
|
|
if (!GrowArrayBy(1))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// else the array is already large enough
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t slide = oldCount - aIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (0 != slide)
|
|
|
|
{
|
|
|
|
// Slide data over to make room for the insertion
|
|
|
|
memmove(mImpl->mArray + aIndex + 1, mImpl->mArray + aIndex,
|
|
|
|
slide * sizeof(mImpl->mArray[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mImpl->mArray[aIndex] = aElement;
|
|
|
|
mImpl->mCount++;
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
2012-08-22 08:56:38 -07:00
|
|
|
mImpl->mCount < (int32_t)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsVoidArray::InsertElementsAt(const nsVoidArray& other, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t oldCount = Count();
|
|
|
|
int32_t otherCount = other.Count();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(aIndex >= 0,"InsertElementsAt(negative index)");
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(aIndex) > uint32_t(oldCount))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// An invalid index causes the insertion to fail
|
|
|
|
// Invalid indexes are ones that are more than one entry past the end of
|
|
|
|
// the array (i.e., they can append).
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (oldCount + otherCount > GetArraySize())
|
|
|
|
{
|
|
|
|
if (!GrowArrayBy(otherCount))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// else the array is already large enough
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t slide = oldCount - aIndex;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (0 != slide)
|
|
|
|
{
|
|
|
|
// Slide data over to make room for the insertion
|
|
|
|
memmove(mImpl->mArray + aIndex + otherCount, mImpl->mArray + aIndex,
|
|
|
|
slide * sizeof(mImpl->mArray[0]));
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < otherCount; i++)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// copy all the elements (destroys aIndex)
|
|
|
|
mImpl->mArray[aIndex++] = other.mImpl->mArray[i];
|
|
|
|
mImpl->mCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
2012-08-22 08:56:38 -07:00
|
|
|
mImpl->mCount < (int32_t)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsVoidArray::ReplaceElementAt(void* aElement, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(aIndex >= 0,"ReplaceElementAt(negative index)");
|
|
|
|
if (aIndex < 0)
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Unlike InsertElementAt, ReplaceElementAt can implicitly add more
|
|
|
|
// than just the one element to the array.
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(aIndex) >= uint32_t(GetArraySize()))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t oldCount = Count();
|
|
|
|
int32_t requestedCount = aIndex + 1;
|
|
|
|
int32_t growDelta = requestedCount - oldCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// frees old mImpl IF this succeeds
|
|
|
|
if (!GrowArrayBy(growDelta))
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
mImpl->mArray[aIndex] = aElement;
|
|
|
|
if (aIndex >= mImpl->mCount)
|
|
|
|
{
|
|
|
|
// Make sure that any entries implicitly added to the array by this
|
|
|
|
// ReplaceElementAt are cleared to 0. Some users of this assume that.
|
|
|
|
// This code means we don't have to memset when we allocate an array.
|
|
|
|
if (aIndex > mImpl->mCount) // note: not >=
|
|
|
|
{
|
|
|
|
// For example, if mCount is 2, and we do a ReplaceElementAt for
|
|
|
|
// element[5], then we need to set three entries ([2], [3], and [4])
|
|
|
|
// to 0.
|
|
|
|
memset(&mImpl->mArray[mImpl->mCount], 0,
|
|
|
|
(aIndex - mImpl->mCount) * sizeof(mImpl->mArray[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mImpl->mCount = aIndex + 1;
|
|
|
|
|
|
|
|
#if DEBUG_VOIDARRAY
|
|
|
|
if (mImpl->mCount > mMaxCount &&
|
2012-08-22 08:56:38 -07:00
|
|
|
mImpl->mCount < (int32_t)(sizeof(MaxElements)/sizeof(MaxElements[0])))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
MaxElements[mImpl->mCount]++;
|
|
|
|
MaxElements[mMaxCount]--;
|
|
|
|
mMaxCount = mImpl->mCount;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// useful for doing LRU arrays
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsVoidArray::MoveElement(int32_t aFrom, int32_t aTo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
void *tempElement;
|
|
|
|
|
|
|
|
if (aTo == aFrom)
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(aTo >= 0 && aFrom >= 0,"MoveElement(negative index)");
|
|
|
|
if (aTo >= Count() || aFrom >= Count())
|
|
|
|
{
|
|
|
|
// can't extend the array when moving an element. Also catches mImpl = null
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
tempElement = mImpl->mArray[aFrom];
|
|
|
|
|
|
|
|
if (aTo < aFrom)
|
|
|
|
{
|
|
|
|
// Moving one element closer to the head; the elements inbetween move down
|
|
|
|
memmove(mImpl->mArray + aTo + 1, mImpl->mArray + aTo,
|
|
|
|
(aFrom-aTo) * sizeof(mImpl->mArray[0]));
|
|
|
|
mImpl->mArray[aTo] = tempElement;
|
|
|
|
}
|
|
|
|
else // already handled aFrom == aTo
|
|
|
|
{
|
|
|
|
// Moving one element closer to the tail; the elements inbetween move up
|
|
|
|
memmove(mImpl->mArray + aFrom, mImpl->mArray + aFrom + 1,
|
|
|
|
(aTo-aFrom) * sizeof(mImpl->mArray[0]));
|
|
|
|
mImpl->mArray[aTo] = tempElement;
|
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-18 04:40:09 -07:00
|
|
|
void nsVoidArray::RemoveElementsAt(int32_t aIndex, int32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t oldCount = Count();
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ASSERTION(aIndex >= 0,"RemoveElementsAt(negative index)");
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(aIndex) >= uint32_t(oldCount))
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-18 04:40:09 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// Limit to available entries starting at aIndex
|
|
|
|
if (aCount + aIndex > oldCount)
|
|
|
|
aCount = oldCount - aIndex;
|
|
|
|
|
|
|
|
// We don't need to move any elements if we're removing the
|
|
|
|
// last element in the array
|
|
|
|
if (aIndex < (oldCount - aCount))
|
|
|
|
{
|
|
|
|
memmove(mImpl->mArray + aIndex, mImpl->mArray + aIndex + aCount,
|
|
|
|
(oldCount - (aIndex + aCount)) * sizeof(mImpl->mArray[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
mImpl->mCount -= aCount;
|
2013-04-18 04:40:09 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsVoidArray::RemoveElement(void* aElement)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t theIndex = IndexOf(aElement);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (theIndex != -1)
|
2013-04-18 04:40:09 -07:00
|
|
|
{
|
|
|
|
RemoveElementAt(theIndex);
|
|
|
|
return true;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Clear()
|
|
|
|
{
|
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
mImpl->mCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Compact()
|
|
|
|
{
|
|
|
|
if (mImpl)
|
|
|
|
{
|
|
|
|
// XXX NOTE: this is quite inefficient in many cases if we're only
|
|
|
|
// compacting by a little, but some callers care more about memory use.
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = Count();
|
2012-12-22 01:00:15 -08:00
|
|
|
if (GetArraySize() > count)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
SizeTo(Count());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Needed because we want to pass the pointer to the item in the array
|
|
|
|
// to the comparator function, not a pointer to the pointer in the array.
|
|
|
|
struct VoidArrayComparatorContext {
|
|
|
|
nsVoidArrayComparatorFunc mComparatorFunc;
|
|
|
|
void* mData;
|
|
|
|
};
|
|
|
|
|
2008-10-10 08:04:34 -07:00
|
|
|
static int
|
2007-03-22 10:30:00 -07:00
|
|
|
VoidArrayComparator(const void* aElement1, const void* aElement2, void* aData)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
VoidArrayComparatorContext* ctx = static_cast<VoidArrayComparatorContext*>(aData);
|
|
|
|
return (*ctx->mComparatorFunc)(*static_cast<void* const*>(aElement1),
|
|
|
|
*static_cast<void* const*>(aElement2),
|
2007-03-22 10:30:00 -07:00
|
|
|
ctx->mData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsVoidArray::Sort(nsVoidArrayComparatorFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
if (mImpl && mImpl->mCount > 1)
|
|
|
|
{
|
|
|
|
VoidArrayComparatorContext ctx = {aFunc, aData};
|
|
|
|
NS_QuickSort(mImpl->mArray, mImpl->mCount, sizeof(mImpl->mArray[0]),
|
|
|
|
VoidArrayComparator, &ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsVoidArray::EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = -1;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool running = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-02 18:18:26 -08:00
|
|
|
if (mImpl) {
|
|
|
|
while (running && (++index < mImpl->mCount)) {
|
|
|
|
running = (*aFunc)(mImpl->mArray[index], aData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsVoidArray::EnumerateForwards(nsVoidArrayEnumFuncConst aFunc,
|
|
|
|
void* aData) const
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = -1;
|
2012-01-02 18:18:26 -08:00
|
|
|
bool running = true;
|
|
|
|
|
|
|
|
if (mImpl) {
|
|
|
|
while (running && (++index < mImpl->mCount)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
running = (*aFunc)(mImpl->mArray[index], aData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsVoidArray::EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-09-28 23:19:26 -07:00
|
|
|
bool running = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (mImpl)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t index = Count();
|
2007-03-22 10:30:00 -07:00
|
|
|
while (running && (0 <= --index))
|
|
|
|
{
|
|
|
|
running = (*aFunc)(mImpl->mArray[index], aData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return running;
|
|
|
|
}
|
|
|
|
|
2012-01-02 18:18:26 -08:00
|
|
|
struct SizeOfElementIncludingThisData
|
|
|
|
{
|
|
|
|
size_t mSize;
|
|
|
|
nsVoidArraySizeOfElementIncludingThisFunc mSizeOfElementIncludingThis;
|
2013-06-23 05:03:39 -07:00
|
|
|
mozilla::MallocSizeOf mMallocSizeOf;
|
2012-01-02 18:18:26 -08:00
|
|
|
void *mData; // the arg passed by the user
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
SizeOfElementIncludingThisEnumerator(const void *aElement, void *aData)
|
|
|
|
{
|
|
|
|
SizeOfElementIncludingThisData *d = (SizeOfElementIncludingThisData *)aData;
|
|
|
|
d->mSize += d->mSizeOfElementIncludingThis(aElement, d->mMallocSizeOf, d->mData);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nsVoidArray::SizeOfExcludingThis(
|
|
|
|
nsVoidArraySizeOfElementIncludingThisFunc aSizeOfElementIncludingThis,
|
2013-06-23 05:03:39 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf, void* aData) const
|
2012-01-02 18:18:26 -08:00
|
|
|
{
|
|
|
|
size_t n = 0;
|
|
|
|
// Measure the element storage.
|
|
|
|
if (mImpl) {
|
|
|
|
n += aMallocSizeOf(mImpl);
|
|
|
|
}
|
|
|
|
// Measure things pointed to by the elements.
|
|
|
|
if (aSizeOfElementIncludingThis) {
|
|
|
|
SizeOfElementIncludingThisData data2 =
|
|
|
|
{ 0, aSizeOfElementIncludingThis, aMallocSizeOf, aData };
|
|
|
|
EnumerateForwards(SizeOfElementIncludingThisEnumerator, &data2);
|
|
|
|
n += data2.mSize;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// NOTE: nsSmallVoidArray elements MUST all have the low bit as 0.
|
|
|
|
// This means that normally it's only used for pointers, and in particular
|
|
|
|
// structures or objects.
|
|
|
|
nsSmallVoidArray::~nsSmallVoidArray()
|
|
|
|
{
|
|
|
|
if (HasSingle())
|
|
|
|
{
|
|
|
|
// Have to null out mImpl before the nsVoidArray dtor runs.
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsSmallVoidArray&
|
|
|
|
nsSmallVoidArray::operator=(nsSmallVoidArray& other)
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t count = other.Count();
|
2007-03-22 10:30:00 -07:00
|
|
|
switch (count) {
|
|
|
|
case 0:
|
|
|
|
Clear();
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
Clear();
|
|
|
|
AppendElement(other.ElementAt(0));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (GetArraySize() >= count || SizeTo(count)) {
|
|
|
|
*AsArray() = *other.AsArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::GetArraySize() const
|
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->GetArraySize();
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::Count() const
|
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->Count();
|
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSmallVoidArray::FastElementAt(int32_t aIndex) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(0 <= aIndex && aIndex < Count(), "nsSmallVoidArray::FastElementAt: index out of range");
|
|
|
|
|
|
|
|
if (HasSingle()) {
|
|
|
|
return GetSingle();
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->FastElementAt(aIndex);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::IndexOf(void* aPossibleElement) const
|
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
return aPossibleElement == GetSingle() ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->IndexOf(aPossibleElement);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSmallVoidArray::InsertElementAt(void* aElement, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aElement) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
|
|
|
|
|
|
|
if (aIndex == 0 && IsEmpty()) {
|
|
|
|
SetSingle(aElement);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureArray()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->InsertElementAt(aElement, aIndex);
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool nsSmallVoidArray::InsertElementsAt(const nsVoidArray &aOther, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (int i = 0; i < aOther.Count(); i++) {
|
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aOther.ElementAt(i)) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (aIndex == 0 && IsEmpty() && aOther.Count() == 1) {
|
|
|
|
SetSingle(aOther.FastElementAt(0));
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureArray()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->InsertElementsAt(aOther, aIndex);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSmallVoidArray::ReplaceElementAt(void* aElement, int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aElement) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
|
|
|
|
|
|
|
if (aIndex == 0 && (IsEmpty() || HasSingle())) {
|
|
|
|
SetSingle(aElement);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureArray()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->ReplaceElementAt(aElement, aIndex);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::AppendElement(void* aElement)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!(NS_PTR_TO_INT32(aElement) & 0x1),
|
|
|
|
"Attempt to add element with 0x1 bit set to nsSmallVoidArray");
|
|
|
|
|
|
|
|
if (IsEmpty()) {
|
|
|
|
SetSingle(aElement);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!EnsureArray()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->AppendElement(aElement);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::RemoveElement(void* aElement)
|
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
if (aElement == GetSingle()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return AsArray()->RemoveElement(aElement);
|
|
|
|
}
|
|
|
|
|
2013-04-18 04:40:09 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSmallVoidArray::RemoveElementAt(int32_t aIndex)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
if (aIndex == 0) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-18 04:40:09 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-18 04:40:09 -07:00
|
|
|
AsArray()->RemoveElementAt(aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-18 04:40:09 -07:00
|
|
|
void
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSmallVoidArray::RemoveElementsAt(int32_t aIndex, int32_t aCount)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
if (aIndex == 0) {
|
|
|
|
if (aCount > 0) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-18 04:40:09 -07:00
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2013-04-18 04:40:09 -07:00
|
|
|
AsArray()->RemoveElementsAt(aIndex, aCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSmallVoidArray::Clear()
|
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
AsArray()->Clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsSmallVoidArray::SizeTo(int32_t aMin)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!HasSingle()) {
|
|
|
|
return AsArray()->SizeTo(aMin);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aMin <= 0) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aMin == 1) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void* single = GetSingle();
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!AsArray()->SizeTo(aMin)) {
|
|
|
|
SetSingle(single);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
AsArray()->AppendElement(single);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSmallVoidArray::Compact()
|
|
|
|
{
|
|
|
|
if (!HasSingle()) {
|
|
|
|
AsArray()->Compact();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsSmallVoidArray::Sort(nsVoidArrayComparatorFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
if (!HasSingle()) {
|
|
|
|
AsArray()->Sort(aFunc,aData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
return (*aFunc)(GetSingle(), aData);
|
|
|
|
}
|
|
|
|
return AsArray()->EnumerateForwards(aFunc,aData);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData)
|
|
|
|
{
|
|
|
|
if (HasSingle()) {
|
|
|
|
return (*aFunc)(GetSingle(), aData);
|
|
|
|
}
|
|
|
|
return AsArray()->EnumerateBackwards(aFunc,aData);
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsSmallVoidArray::EnsureArray()
|
|
|
|
{
|
|
|
|
if (!HasSingle()) {
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void* single = GetSingle();
|
2012-07-30 07:20:58 -07:00
|
|
|
mImpl = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!AsArray()->AppendElement(single)) {
|
|
|
|
SetSingle(single);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return false;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|