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
|
|
|
#ifndef nsVoidArray_h___
|
|
|
|
#define nsVoidArray_h___
|
|
|
|
|
|
|
|
//#define DEBUG_VOIDARRAY 1
|
|
|
|
|
|
|
|
#include "nsDebug.h"
|
|
|
|
|
2013-06-23 05:03:39 -07:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2012-04-11 17:17:44 -07:00
|
|
|
#include "mozilla/StandardInteger.h"
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// Comparator callback function for sorting array values.
|
2008-10-10 08:04:34 -07:00
|
|
|
typedef int (* nsVoidArrayComparatorFunc)
|
2007-03-22 10:30:00 -07:00
|
|
|
(const void* aElement1, const void* aElement2, void* aData);
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
// Enumerator callback function. Return false to stop
|
2011-09-28 23:19:26 -07:00
|
|
|
typedef bool (* nsVoidArrayEnumFunc)(void* aElement, void *aData);
|
2012-01-02 18:18:26 -08:00
|
|
|
typedef bool (* nsVoidArrayEnumFuncConst)(const void* aElement, void *aData);
|
|
|
|
|
|
|
|
// SizeOfExcludingThis callback function.
|
|
|
|
typedef size_t (* nsVoidArraySizeOfElementIncludingThisFunc)(const void* aElement,
|
2013-06-23 05:03:39 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf,
|
2012-01-02 18:18:26 -08:00
|
|
|
void *aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/// A basic zero-based array of void*'s that manages its own memory
|
|
|
|
class NS_COM_GLUE nsVoidArray {
|
|
|
|
public:
|
|
|
|
nsVoidArray();
|
2012-08-22 08:56:38 -07:00
|
|
|
nsVoidArray(int32_t aCount); // initial count of aCount elements set to nullptr
|
2007-03-22 10:30:00 -07:00
|
|
|
~nsVoidArray();
|
|
|
|
|
|
|
|
nsVoidArray& operator=(const nsVoidArray& other);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
inline int32_t Count() const {
|
2007-03-22 10:30:00 -07:00
|
|
|
return mImpl ? mImpl->mCount : 0;
|
|
|
|
}
|
2010-03-15 04:38:50 -07:00
|
|
|
// If the array grows, the newly created entries will all be null
|
2012-08-22 08:56:38 -07:00
|
|
|
bool SetCount(int32_t aNewCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
// returns the max number that can be held without allocating
|
2012-08-22 08:56:38 -07:00
|
|
|
inline int32_t GetArraySize() const {
|
2012-12-22 01:00:15 -08:00
|
|
|
return mImpl ? mImpl->mSize : 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void* FastElementAt(int32_t aIndex) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(0 <= aIndex && aIndex < Count(), "nsVoidArray::FastElementAt: index out of range");
|
|
|
|
return mImpl->mArray[aIndex];
|
|
|
|
}
|
|
|
|
|
|
|
|
// This both asserts and bounds-checks, because (1) we don't want
|
|
|
|
// people to write bad code, but (2) we don't want to change it to
|
|
|
|
// crashing for backwards compatibility. See bug 96108.
|
2012-08-22 08:56:38 -07:00
|
|
|
void* ElementAt(int32_t aIndex) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(0 <= aIndex && aIndex < Count(), "nsVoidArray::ElementAt: index out of range");
|
|
|
|
return SafeElementAt(aIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
// bounds-checked version
|
2012-08-22 08:56:38 -07:00
|
|
|
void* SafeElementAt(int32_t aIndex) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(aIndex) >= uint32_t(Count())) // handles aIndex < 0 too
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// The bounds check ensures mImpl is non-null.
|
|
|
|
return mImpl->mArray[aIndex];
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
void* operator[](int32_t aIndex) const { return ElementAt(aIndex); }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t IndexOf(void* aPossibleElement) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool InsertElementAt(void* aElement, int32_t aIndex);
|
|
|
|
bool InsertElementsAt(const nsVoidArray &other, int32_t aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool ReplaceElementAt(void* aElement, int32_t aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// useful for doing LRU arrays, sorting, etc
|
2012-08-22 08:56:38 -07:00
|
|
|
bool MoveElement(int32_t aFrom, int32_t aTo);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool AppendElement(void* aElement) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return InsertElementAt(aElement, Count());
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool AppendElements(nsVoidArray& aElements) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return InsertElementsAt(aElements, Count());
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool RemoveElement(void* aElement);
|
2013-04-18 04:40:09 -07:00
|
|
|
void RemoveElementsAt(int32_t aIndex, int32_t aCount);
|
|
|
|
void RemoveElementAt(int32_t aIndex) { return RemoveElementsAt(aIndex,1); }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void Clear();
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
bool SizeTo(int32_t aMin);
|
2007-03-22 10:30:00 -07:00
|
|
|
// Subtly different - Compact() tries to be smart about whether we
|
|
|
|
// should reallocate the array; SizeTo() always reallocates.
|
|
|
|
void Compact();
|
|
|
|
|
|
|
|
void Sort(nsVoidArrayComparatorFunc aFunc, void* aData);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData);
|
2012-01-02 18:18:26 -08:00
|
|
|
bool EnumerateForwards(nsVoidArrayEnumFuncConst aFunc, void* aData) const;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-01-02 18:18:26 -08:00
|
|
|
// Measures the size of the array's element storage, and if
|
|
|
|
// |aSizeOfElementIncludingThis| is non-NULL, measures the size of things
|
|
|
|
// pointed to by elements.
|
|
|
|
size_t SizeOfExcludingThis(
|
|
|
|
nsVoidArraySizeOfElementIncludingThisFunc aSizeOfElementIncludingThis,
|
2013-06-23 05:03:39 -07:00
|
|
|
mozilla::MallocSizeOf aMallocSizeOf, void* aData = NULL) const;
|
2012-01-02 18:18:26 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
protected:
|
2012-08-22 08:56:38 -07:00
|
|
|
bool GrowArrayBy(int32_t aGrowBy);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
struct Impl {
|
|
|
|
/**
|
2012-12-22 01:00:15 -08:00
|
|
|
* The actual array size.
|
2007-03-22 10:30:00 -07:00
|
|
|
*/
|
2012-12-22 01:00:15 -08:00
|
|
|
int32_t mSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The number of elements in the array
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array data, padded out to the actual size of the array.
|
|
|
|
*/
|
|
|
|
void* mArray[1];
|
|
|
|
};
|
|
|
|
|
|
|
|
Impl* mImpl;
|
|
|
|
#if DEBUG_VOIDARRAY
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t mMaxCount;
|
|
|
|
int32_t mMaxSize;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mIsAuto;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// bit twiddlers
|
2012-12-22 01:00:15 -08:00
|
|
|
void SetArray(Impl *newImpl, int32_t aSize, int32_t aCount);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
/// Copy constructors are not allowed
|
|
|
|
nsVoidArray(const nsVoidArray& other);
|
|
|
|
};
|
|
|
|
|
|
|
|
//===================================================================
|
|
|
|
// nsSmallVoidArray is not a general-purpose replacement for
|
|
|
|
// ns(Auto)VoidArray because there is (some) extra CPU overhead for arrays
|
|
|
|
// larger than 1 element, though not a lot. It is appropriate for
|
|
|
|
// space-sensitive uses where sizes of 0 or 1 are moderately common or
|
|
|
|
// more, and where we're NOT storing arbitrary integers or arbitrary
|
|
|
|
// pointers.
|
|
|
|
|
|
|
|
// NOTE: nsSmallVoidArray can ONLY be used for holding items that always
|
|
|
|
// have the low bit as a 0 - i.e. element & 1 == 0. This happens to be
|
|
|
|
// true for allocated and object pointers for all the architectures we run
|
|
|
|
// on, but conceivably there might be some architectures/compilers for
|
|
|
|
// which it is NOT true. We know this works for all existing architectures
|
|
|
|
// because if it didn't then nsCheapVoidArray would have failed. Also note
|
|
|
|
// that we will ASSERT if this assumption is violated in DEBUG builds.
|
|
|
|
|
|
|
|
// XXX we're really re-implementing the whole nsVoidArray interface here -
|
|
|
|
// some form of abstract class would be useful
|
|
|
|
|
|
|
|
// I disagree on the abstraction here. If the point of this class is to be
|
|
|
|
// as small as possible, and no one will ever derive from it, as I found
|
|
|
|
// today, there should not be any virtualness to it to avoid the vtable
|
|
|
|
// ptr overhead.
|
|
|
|
|
|
|
|
class NS_COM_GLUE nsSmallVoidArray : private nsVoidArray
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~nsSmallVoidArray();
|
|
|
|
|
|
|
|
nsSmallVoidArray& operator=(nsSmallVoidArray& other);
|
2012-08-22 08:56:38 -07:00
|
|
|
void* operator[](int32_t aIndex) const { return ElementAt(aIndex); }
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t GetArraySize() const;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t Count() const;
|
|
|
|
void* FastElementAt(int32_t aIndex) const;
|
2007-03-22 10:30:00 -07:00
|
|
|
// This both asserts and bounds-checks, because (1) we don't want
|
|
|
|
// people to write bad code, but (2) we don't want to change it to
|
|
|
|
// crashing for backwards compatibility. See bug 96108.
|
2012-08-22 08:56:38 -07:00
|
|
|
void* ElementAt(int32_t aIndex) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ASSERTION(0 <= aIndex && aIndex < Count(), "nsSmallVoidArray::ElementAt: index out of range");
|
|
|
|
return SafeElementAt(aIndex);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
void* SafeElementAt(int32_t aIndex) const {
|
2007-03-22 10:30:00 -07:00
|
|
|
// let compiler inline; it may be able to remove these checks
|
2012-08-22 08:56:38 -07:00
|
|
|
if (uint32_t(aIndex) >= uint32_t(Count())) // handles aIndex < 0 too
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
return FastElementAt(aIndex);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t IndexOf(void* aPossibleElement) const;
|
|
|
|
bool InsertElementAt(void* aElement, int32_t aIndex);
|
|
|
|
bool InsertElementsAt(const nsVoidArray &other, int32_t aIndex);
|
|
|
|
bool ReplaceElementAt(void* aElement, int32_t aIndex);
|
|
|
|
bool MoveElement(int32_t aFrom, int32_t aTo);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool AppendElement(void* aElement);
|
|
|
|
bool AppendElements(nsVoidArray& aElements) {
|
2007-03-22 10:30:00 -07:00
|
|
|
return InsertElementsAt(aElements, Count());
|
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool RemoveElement(void* aElement);
|
2013-04-18 04:40:09 -07:00
|
|
|
void RemoveElementsAt(int32_t aIndex, int32_t aCount);
|
|
|
|
void RemoveElementAt(int32_t aIndex);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
void Clear();
|
2012-08-22 08:56:38 -07:00
|
|
|
bool SizeTo(int32_t aMin);
|
2007-03-22 10:30:00 -07:00
|
|
|
void Compact();
|
|
|
|
void Sort(nsVoidArrayComparatorFunc aFunc, void* aData);
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool EnumerateForwards(nsVoidArrayEnumFunc aFunc, void* aData);
|
|
|
|
bool EnumerateBackwards(nsVoidArrayEnumFunc aFunc, void* aData);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool HasSingle() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-04-11 17:17:44 -07:00
|
|
|
return !!(reinterpret_cast<intptr_t>(mImpl) & 0x1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
void* GetSingle() const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(HasSingle(), "wrong type");
|
2007-07-08 00:08:04 -07:00
|
|
|
return reinterpret_cast<void*>
|
2012-04-11 17:17:44 -07:00
|
|
|
(reinterpret_cast<intptr_t>(mImpl) & ~0x1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
void SetSingle(void *aChild)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(HasSingle() || !mImpl, "overwriting array");
|
2007-07-08 00:08:04 -07:00
|
|
|
mImpl = reinterpret_cast<Impl*>
|
2012-04-11 17:17:44 -07:00
|
|
|
(reinterpret_cast<intptr_t>(aChild) | 0x1);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool IsEmpty() const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
// Note that this isn't the same as Count()==0
|
|
|
|
return !mImpl;
|
|
|
|
}
|
|
|
|
const nsVoidArray* AsArray() const
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!HasSingle(), "This is a single");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
nsVoidArray* AsArray()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!HasSingle(), "This is a single");
|
|
|
|
return this;
|
|
|
|
}
|
2011-09-28 23:19:26 -07:00
|
|
|
bool EnsureArray();
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsVoidArray_h___ */
|