gecko-dev/xpcom/ds/nsSupportsArray.cpp
2016-10-14 14:58:35 +02:00

387 lines
8.7 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <stdint.h>
#include <string.h>
#include "mozilla/CheckedInt.h"
#include "mozilla/MathAlgorithms.h"
#include "nsSupportsArray.h"
#include "nsSupportsArrayEnumerator.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
nsresult
nsQueryElementAt::operator()(const nsIID& aIID, void** aResult) const
{
nsresult status =
mCollection ? mCollection->QueryElementAt(mIndex, aIID, aResult) :
NS_ERROR_NULL_POINTER;
if (mErrorPtr) {
*mErrorPtr = status;
}
return status;
}
nsSupportsArray::nsSupportsArray()
{
mArray = mAutoArray;
mArraySize = kAutoArraySize;
mCount = 0;
}
nsSupportsArray::~nsSupportsArray()
{
DeleteArray();
}
bool
nsSupportsArray::GrowArrayBy(uint32_t aGrowBy)
{
const uint32_t kGrowArrayBy = 8;
const uint32_t kLinearThreshold = 16 * sizeof(nsISupports*);
// We have to grow the array. Grow by kGrowArrayBy 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 < kGrowArrayBy) {
aGrowBy = kGrowArrayBy;
}
CheckedUint32 newCount(mArraySize);
newCount += aGrowBy; // Minimum increase
CheckedUint32 newSize(sizeof(mArray[0]));
newSize *= newCount;
if (!newSize.isValid()) {
return false;
}
if (newSize.value() >= kLinearThreshold) {
// newCount includes enough space for at least kGrowArrayBy new slots.
// Select the next power-of-two size in bytes above that if newSize is
// not a power of two.
if (newSize.value() & (newSize.value() - 1)) {
newSize = UINT64_C(1) << mozilla::CeilingLog2(newSize.value());
if (!newSize.isValid()) {
return false;
}
}
newCount = newSize / sizeof(mArray[0]);
}
// XXX This would be far more efficient in many allocators if we used
// XXX PR_Realloc(), etc
nsISupports** oldArray = mArray;
mArray = new nsISupports*[newCount.value()];
mArraySize = newCount.value();
if (oldArray) { // need to move old data
if (0 < mCount) {
::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*));
}
if (oldArray != &(mAutoArray[0])) {
delete[] oldArray;
}
}
return true;
}
nsresult
nsSupportsArray::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
{
if (aOuter) {
return NS_ERROR_NO_AGGREGATION;
}
nsCOMPtr<nsISupportsArray> it = new nsSupportsArray();
return it->QueryInterface(aIID, aResult);
}
NS_IMPL_ISUPPORTS(nsSupportsArray, nsISupportsArray, nsICollection,
nsISerializable)
NS_IMETHODIMP
nsSupportsArray::Read(nsIObjectInputStream* aStream)
{
nsresult rv;
uint32_t newArraySize;
rv = aStream->Read32(&newArraySize);
if (NS_FAILED(rv)) {
return rv;
}
if (newArraySize <= kAutoArraySize) {
if (mArray != mAutoArray) {
delete[] mArray;
mArray = mAutoArray;
}
newArraySize = kAutoArraySize;
} else {
if (newArraySize <= mArraySize) {
// Keep non-default-size mArray, it's more than big enough.
newArraySize = mArraySize;
} else {
nsISupports** array = new nsISupports*[newArraySize];
if (mArray != mAutoArray) {
delete[] mArray;
}
mArray = array;
}
}
mArraySize = newArraySize;
rv = aStream->Read32(&mCount);
if (NS_FAILED(rv)) {
return rv;
}
NS_ASSERTION(mCount <= mArraySize, "overlarge mCount!");
if (mCount > mArraySize) {
mCount = mArraySize;
}
for (uint32_t i = 0; i < mCount; i++) {
rv = aStream->ReadObject(true, &mArray[i]);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}
NS_IMETHODIMP
nsSupportsArray::Write(nsIObjectOutputStream* aStream)
{
nsresult rv;
rv = aStream->Write32(mArraySize);
if (NS_FAILED(rv)) {
return rv;
}
rv = aStream->Write32(mCount);
if (NS_FAILED(rv)) {
return rv;
}
for (uint32_t i = 0; i < mCount; i++) {
rv = aStream->WriteObject(mArray[i], true);
if (NS_FAILED(rv)) {
return rv;
}
}
return NS_OK;
}
void
nsSupportsArray::DeleteArray(void)
{
Clear();
if (mArray != &(mAutoArray[0])) {
delete[] mArray;
mArray = mAutoArray;
mArraySize = kAutoArraySize;
}
}
NS_IMETHODIMP
nsSupportsArray::GetElementAt(uint32_t aIndex, nsISupports** aOutPtr)
{
*aOutPtr = nullptr;
if (aIndex < mCount) {
NS_IF_ADDREF(*aOutPtr = mArray[aIndex]);
}
return NS_OK;
}
NS_IMETHODIMP_(int32_t)
nsSupportsArray::IndexOf(const nsISupports* aPossibleElement)
{
const nsISupports** start = (const nsISupports**)mArray; // work around goofy compiler behavior
const nsISupports** ep = start;
const nsISupports** end = (start + mCount);
while (ep < end) {
if (aPossibleElement == *ep) {
return (ep - start);
}
ep++;
}
return -1;
}
NS_IMETHODIMP_(int32_t)
nsSupportsArray::LastIndexOf(const nsISupports* aPossibleElement)
{
if (0 < mCount) {
const nsISupports** start = (const nsISupports**)mArray; // work around goofy compiler behavior
const nsISupports** ep = (start + mCount);
while (start <= --ep) {
if (aPossibleElement == *ep) {
return (ep - start);
}
}
}
return -1;
}
NS_IMETHODIMP_(bool)
nsSupportsArray::InsertElementAt(nsISupports* aElement, uint32_t aIndex)
{
if (aIndex <= mCount) {
CheckedUint32 newCount(mCount);
newCount += 1;
if (!newCount.isValid()) {
return false;
}
if (mArraySize < newCount.value()) {
// need to grow the array
if (!GrowArrayBy(1)) {
return false;
}
}
// Could be slightly more efficient if GrowArrayBy knew about the
// split, but the difference is trivial.
uint32_t slide = (mCount - aIndex);
if (0 < slide) {
::memmove(mArray + aIndex + 1, mArray + aIndex,
slide * sizeof(nsISupports*));
}
mArray[aIndex] = aElement;
NS_IF_ADDREF(aElement);
mCount++;
return true;
}
return false;
}
NS_IMETHODIMP_(bool)
nsSupportsArray::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex)
{
if (aIndex < mCount) {
NS_IF_ADDREF(aElement); // addref first in case it's the same object!
NS_IF_RELEASE(mArray[aIndex]);
mArray[aIndex] = aElement;
return true;
}
return false;
}
NS_IMETHODIMP_(bool)
nsSupportsArray::RemoveElementsAt(uint32_t aIndex, uint32_t aCount)
{
if (aIndex + aCount <= mCount) {
for (uint32_t i = 0; i < aCount; i++) {
NS_IF_RELEASE(mArray[aIndex + i]);
}
mCount -= aCount;
int32_t slide = (mCount - aIndex);
if (0 < slide) {
::memmove(mArray + aIndex, mArray + aIndex + aCount,
slide * sizeof(nsISupports*));
}
return true;
}
return false;
}
NS_IMETHODIMP
nsSupportsArray::RemoveElement(nsISupports* aElement)
{
int32_t theIndex = IndexOf(aElement);
if (theIndex >= 0) {
return RemoveElementAt(theIndex) ? NS_OK : NS_ERROR_FAILURE;
}
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSupportsArray::Clear(void)
{
if (0 < mCount) {
do {
--mCount;
NS_IF_RELEASE(mArray[mCount]);
} while (0 != mCount);
}
return NS_OK;
}
NS_IMETHODIMP
nsSupportsArray::Compact(void)
{
if ((mArraySize != mCount) && (kAutoArraySize < mArraySize)) {
nsISupports** oldArray = mArray;
if (mCount <= kAutoArraySize) {
mArray = mAutoArray;
mArraySize = kAutoArraySize;
} else {
mArray = new nsISupports*[mCount];
if (!mArray) {
mArray = oldArray;
return NS_OK;
}
mArraySize = mCount;
}
::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*));
delete[] oldArray;
}
return NS_OK;
}
NS_IMETHODIMP
nsSupportsArray::Enumerate(nsIEnumerator** aResult)
{
nsSupportsArrayEnumerator* e = new nsSupportsArrayEnumerator(this);
*aResult = e;
NS_ADDREF(e);
return NS_OK;
}
NS_IMETHODIMP
nsSupportsArray::Clone(nsISupportsArray** aResult)
{
nsCOMPtr<nsISupportsArray> newArray;
nsresult rv = NS_NewISupportsArray(getter_AddRefs(newArray));
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
uint32_t count = 0;
Count(&count);
for (uint32_t i = 0; i < count; i++) {
if (!newArray->InsertElementAt(mArray[i], i)) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
newArray.forget(aResult);
return NS_OK;
}
nsresult
NS_NewISupportsArray(nsISupportsArray** aInstancePtrResult)
{
nsresult rv;
rv = nsSupportsArray::Create(nullptr, NS_GET_IID(nsISupportsArray),
(void**)aInstancePtrResult);
return rv;
}