nsSupportsArray.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1998
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Scott Collins <scc@mozilla.org>: |do_QueryElementAt|
* Pierre Phaneuf <pp@ludusdesign.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include <string.h>
#include "prbit.h"
#include "nsSupportsArray.h"
#include "nsSupportsArrayEnumerator.h"
#include "nsAString.h"
#include "nsIObjectInputStream.h"
#include "nsIObjectOutputStream.h"
#define MAXSUPPORTS 20
class SupportsStats {
public:
~SupportsStats();
};
static int sizesUsed; // number of the elements of the arrays used
static int GrowInPlace[MAXSUPPORTS];
// these are per-allocation
static int MaxElements[3000];
// very evil
{ \
if (sizesAlloced[i] == (int)(size)) \
{ ((x)[i])++; break; } \
} \
((x)[sizesUsed++])++; break; \
} \
} while (0);
{ \
if (sizesAlloced[i] == (int)(size)) \
{ ((x)[i])--; break; } \
} \
} while (0);
{
sizesUsed = 1;
sizesAlloced[0] = 0;
}
{
int i;
for (i = 0; i < sizesUsed; i++)
{
}
printf("Max Size of SupportsArray:\n");
for (i = 0; i < (int)(sizeof(MaxElements)/sizeof(MaxElements[0])); i++)
{
if (MaxElements[i])
}
}
// Just so constructor/destructor get called
#endif
{
if ( mErrorPtr )
return status;
}
{
mArray = mAutoArray;
mCount = 0;
mMaxCount = 0;
mMaxSize = 0;
MaxElements[0]++;
#endif
}
{
DeleteArray();
}
{
// 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)
{
// 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.
}
// XXX This would be far more efficient in many allocators if we used
// XXX PR_Realloc(), etc
if (!mArray) { // ran out of memory
return PR_FALSE;
}
if (mArraySize > mMaxSize)
{
if (oldArray != &(mAutoArray[0]))
}
#endif
if (oldArray) { // need to move old data
if (0 < mCount) {
}
if (oldArray != &(mAutoArray[0])) {
delete[] oldArray;
}
}
return PR_TRUE;
}
{
if (aOuter)
return NS_ERROR_NO_AGGREGATION;
if (!it)
return NS_ERROR_OUT_OF_MEMORY;
}
{
if (newArraySize <= kAutoArraySize) {
if (mArray != mAutoArray) {
delete[] mArray;
mArray = mAutoArray;
}
}
else {
if (newArraySize <= mArraySize) {
// Keep non-default-size mArray, it's more than big enough.
}
else {
if (!array)
return NS_ERROR_OUT_OF_MEMORY;
if (mArray != mAutoArray)
delete[] mArray;
}
}
if (mCount > mArraySize)
mCount = mArraySize;
}
return NS_OK;
}
{
}
return NS_OK;
}
void nsSupportsArray::DeleteArray(void)
{
Clear();
if (mArray != &(mAutoArray[0])) {
delete[] mArray;
mArray = mAutoArray;
}
}
{
if (aOther) {
return PR_FALSE;
if (mCount == countOther) {
while (index--) {
return PR_FALSE;
return PR_FALSE;
}
return PR_TRUE;
}
}
return PR_FALSE;
}
{
return element;
}
return 0;
}
{
return IndexOfStartingAt(aPossibleElement, 0);
}
{
if (aStartIndex < mCount) {
if (aPossibleElement == *ep) {
}
ep++;
}
}
return -1;
}
{
if (0 < mCount) {
if (aPossibleElement == *ep) {
}
}
}
return -1;
}
{
// need to grow the array
if (!GrowArrayBy(1))
return PR_FALSE;
}
// Could be slightly more efficient if GrowArrayBy knew about the
// split, but the difference is trivial.
if (0 < slide) {
}
mCount++;
{
MaxElements[mCount]++;
MaxElements[mMaxCount]--;
}
#endif
return PR_TRUE;
}
return PR_FALSE;
}
{
if (!aElements) {
return PR_FALSE;
}
return PR_FALSE;
// need to grow the array
if (!GrowArrayBy(countElements))
return PR_FALSE;
}
// Could be slightly more efficient if GrowArrayBy knew about the
// split, but the difference is trivial.
if (0 < slide) {
slide * sizeof(nsISupports*));
}
// use GetElementAt to copy and do AddRef for us
return PR_FALSE;
}
{
MaxElements[mCount]++;
MaxElements[mMaxCount]--;
}
#endif
return PR_TRUE;
}
return PR_FALSE;
}
{
return PR_TRUE;
}
return PR_FALSE;
}
{
if (0 < slide) {
slide * sizeof(nsISupports*));
}
return PR_TRUE;
}
return PR_FALSE;
}
{
if (theIndex >= 0)
return RemoveElementAt(theIndex);
return PR_FALSE;
}
{
if (theIndex >= 0)
return RemoveElementAt(theIndex);
return PR_FALSE;
}
{
return PR_TRUE;
{
// can't extend the array when moving an element. Also catches mImpl = null
return PR_FALSE;
}
{
// Moving one element closer to the head; the elements inbetween move down
}
else // already handled aFrom == aTo
{
// Moving one element closer to the tail; the elements inbetween move up
}
return PR_TRUE;
}
nsSupportsArray::Clear(void)
{
if (0 < mCount) {
do {
--mCount;
} while (0 != mCount);
}
return NS_OK;
}
nsSupportsArray::Compact(void)
{
#endif
if (mCount <= kAutoArraySize) {
mArray = mAutoArray;
}
else {
if (!mArray) {
return NS_OK;
}
mArraySize = mCount;
}
if (oldArray != &(mAutoArray[0]))
#endif
delete[] oldArray;
}
return NS_OK;
}
{
#endif
// XXX for aSize < mCount we could resize to mCount
return PR_TRUE; // nothing to do
// switch back to autoarray if possible
mArray = mAutoArray;
}
else {
if (!mArray) {
return PR_FALSE;
}
mArraySize = aSize;
}
if (oldArray != &(mAutoArray[0]))
#endif
if (oldArray != mAutoArray)
delete[] oldArray;
return PR_TRUE;
}
{
}
return running;
}
{
}
return running;
}
{
nsSupportsArrayEnumerator* e = new nsSupportsArrayEnumerator(this);
if (!e)
return NS_ERROR_OUT_OF_MEMORY;
*result = e;
NS_ADDREF(e);
return NS_OK;
}
static PRBool
{
return NS_SUCCEEDED(rv);
}
{
if (!ok) return NS_ERROR_OUT_OF_MEMORY;
return NS_OK;
}
{
(void**)aInstancePtrResult);
return rv;
}