xpcom/glue/nsCOMArray.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsCOMArray.h"
michael@0 7
michael@0 8 #include "mozilla/MemoryReporting.h"
michael@0 9
michael@0 10 #include "nsCOMPtr.h"
michael@0 11
michael@0 12 // This specialization is private to nsCOMArray.
michael@0 13 // It exists solely to automatically zero-out newly created array elements.
michael@0 14 template<>
michael@0 15 class nsTArrayElementTraits<nsISupports*>
michael@0 16 {
michael@0 17 typedef nsISupports* E;
michael@0 18 public:
michael@0 19 // Zero out the value
michael@0 20 static inline void Construct(E *e) {
michael@0 21 new (static_cast<void *>(e)) E();
michael@0 22 }
michael@0 23 // Invoke the copy-constructor in place.
michael@0 24 template<class A>
michael@0 25 static inline void Construct(E *e, const A &arg) {
michael@0 26 new (static_cast<void *>(e)) E(arg);
michael@0 27 }
michael@0 28 // Invoke the destructor in place.
michael@0 29 static inline void Destruct(E *e) {
michael@0 30 e->~E();
michael@0 31 }
michael@0 32 };
michael@0 33
michael@0 34 static void ReleaseObjects(nsTArray<nsISupports*> &aArray);
michael@0 35
michael@0 36 // implementations of non-trivial methods in nsCOMArray_base
michael@0 37
michael@0 38 nsCOMArray_base::nsCOMArray_base(const nsCOMArray_base& aOther)
michael@0 39 {
michael@0 40 // make sure we do only one allocation
michael@0 41 mArray.SetCapacity(aOther.Count());
michael@0 42 AppendObjects(aOther);
michael@0 43 }
michael@0 44
michael@0 45 nsCOMArray_base::~nsCOMArray_base()
michael@0 46 {
michael@0 47 Clear();
michael@0 48 }
michael@0 49
michael@0 50 int32_t
michael@0 51 nsCOMArray_base::IndexOf(nsISupports* aObject, uint32_t aStartIndex) const
michael@0 52 {
michael@0 53 return mArray.IndexOf(aObject, aStartIndex);
michael@0 54 }
michael@0 55
michael@0 56 int32_t
michael@0 57 nsCOMArray_base::IndexOfObject(nsISupports* aObject) const
michael@0 58 {
michael@0 59 nsCOMPtr<nsISupports> supports = do_QueryInterface(aObject);
michael@0 60 if (NS_WARN_IF(!supports))
michael@0 61 return -1;
michael@0 62
michael@0 63 uint32_t i, count;
michael@0 64 int32_t retval = -1;
michael@0 65 count = mArray.Length();
michael@0 66 for (i = 0; i < count; ++i) {
michael@0 67 nsCOMPtr<nsISupports> arrayItem = do_QueryInterface(mArray[i]);
michael@0 68 if (arrayItem == supports) {
michael@0 69 retval = i;
michael@0 70 break;
michael@0 71 }
michael@0 72 }
michael@0 73 return retval;
michael@0 74 }
michael@0 75
michael@0 76 bool
michael@0 77 nsCOMArray_base::EnumerateForwards(nsBaseArrayEnumFunc aFunc, void* aData) const
michael@0 78 {
michael@0 79 for (uint32_t index = 0; index < mArray.Length(); index++)
michael@0 80 if (!(*aFunc)(mArray[index], aData))
michael@0 81 return false;
michael@0 82
michael@0 83 return true;
michael@0 84 }
michael@0 85
michael@0 86 bool
michael@0 87 nsCOMArray_base::EnumerateBackwards(nsBaseArrayEnumFunc aFunc, void* aData) const
michael@0 88 {
michael@0 89 for (uint32_t index = mArray.Length(); index--; )
michael@0 90 if (!(*aFunc)(mArray[index], aData))
michael@0 91 return false;
michael@0 92
michael@0 93 return true;
michael@0 94 }
michael@0 95
michael@0 96 int
michael@0 97 nsCOMArray_base::nsCOMArrayComparator(const void* aElement1, const void* aElement2, void* aData)
michael@0 98 {
michael@0 99 nsCOMArrayComparatorContext* ctx = static_cast<nsCOMArrayComparatorContext*>(aData);
michael@0 100 return (*ctx->mComparatorFunc)(*static_cast<nsISupports* const*>(aElement1),
michael@0 101 *static_cast<nsISupports* const*>(aElement2),
michael@0 102 ctx->mData);
michael@0 103 }
michael@0 104
michael@0 105 void
michael@0 106 nsCOMArray_base::Sort(nsBaseArrayComparatorFunc aFunc, void* aData)
michael@0 107 {
michael@0 108 if (mArray.Length() > 1) {
michael@0 109 nsCOMArrayComparatorContext ctx = {aFunc, aData};
michael@0 110 NS_QuickSort(mArray.Elements(), mArray.Length(), sizeof(nsISupports*),
michael@0 111 nsCOMArrayComparator, &ctx);
michael@0 112 }
michael@0 113 }
michael@0 114
michael@0 115 bool
michael@0 116 nsCOMArray_base::InsertObjectAt(nsISupports* aObject, int32_t aIndex)
michael@0 117 {
michael@0 118 if ((uint32_t)aIndex > mArray.Length())
michael@0 119 return false;
michael@0 120
michael@0 121 if (!mArray.InsertElementAt(aIndex, aObject))
michael@0 122 return false;
michael@0 123
michael@0 124 NS_IF_ADDREF(aObject);
michael@0 125 return true;
michael@0 126 }
michael@0 127
michael@0 128 void
michael@0 129 nsCOMArray_base::InsertElementAt(uint32_t aIndex, nsISupports* aElement)
michael@0 130 {
michael@0 131 mArray.InsertElementAt(aIndex, aElement);
michael@0 132 NS_IF_ADDREF(aElement);
michael@0 133 }
michael@0 134
michael@0 135 bool
michael@0 136 nsCOMArray_base::InsertObjectsAt(const nsCOMArray_base& aObjects, int32_t aIndex)
michael@0 137 {
michael@0 138 if ((uint32_t)aIndex > mArray.Length())
michael@0 139 return false;
michael@0 140
michael@0 141 if (!mArray.InsertElementsAt(aIndex, aObjects.mArray))
michael@0 142 return false;
michael@0 143
michael@0 144 // need to addref all these
michael@0 145 uint32_t count = aObjects.Length();
michael@0 146 for (uint32_t i = 0; i < count; ++i)
michael@0 147 NS_IF_ADDREF(aObjects[i]);
michael@0 148
michael@0 149 return true;
michael@0 150 }
michael@0 151
michael@0 152 void
michael@0 153 nsCOMArray_base::InsertElementsAt(uint32_t aIndex, const nsCOMArray_base& aElements)
michael@0 154 {
michael@0 155 mArray.InsertElementsAt(aIndex, aElements.mArray);
michael@0 156
michael@0 157 // need to addref all these
michael@0 158 uint32_t count = aElements.Length();
michael@0 159 for (uint32_t i = 0; i < count; ++i)
michael@0 160 NS_IF_ADDREF(aElements[i]);
michael@0 161 }
michael@0 162
michael@0 163 void
michael@0 164 nsCOMArray_base::InsertElementsAt(uint32_t aIndex, nsISupports* const* aElements, uint32_t aCount)
michael@0 165 {
michael@0 166 mArray.InsertElementsAt(aIndex, aElements, aCount);
michael@0 167
michael@0 168 // need to addref all these
michael@0 169 for (uint32_t i = 0; i < aCount; ++i)
michael@0 170 NS_IF_ADDREF(aElements[i]);
michael@0 171 }
michael@0 172
michael@0 173 bool
michael@0 174 nsCOMArray_base::ReplaceObjectAt(nsISupports* aObject, int32_t aIndex)
michael@0 175 {
michael@0 176 mArray.EnsureLengthAtLeast(aIndex + 1);
michael@0 177 nsISupports *oldObject = mArray[aIndex];
michael@0 178 // Make sure to addref first, in case aObject == oldObject
michael@0 179 NS_IF_ADDREF(mArray[aIndex] = aObject);
michael@0 180 NS_IF_RELEASE(oldObject);
michael@0 181 // XXX make this return void
michael@0 182 return true;
michael@0 183 }
michael@0 184
michael@0 185 bool
michael@0 186 nsCOMArray_base::RemoveObject(nsISupports *aObject)
michael@0 187 {
michael@0 188 bool result = mArray.RemoveElement(aObject);
michael@0 189 if (result)
michael@0 190 NS_IF_RELEASE(aObject);
michael@0 191 return result;
michael@0 192 }
michael@0 193
michael@0 194 bool
michael@0 195 nsCOMArray_base::RemoveObjectAt(int32_t aIndex)
michael@0 196 {
michael@0 197 if (uint32_t(aIndex) < mArray.Length()) {
michael@0 198 nsISupports* element = mArray[aIndex];
michael@0 199
michael@0 200 mArray.RemoveElementAt(aIndex);
michael@0 201 NS_IF_RELEASE(element);
michael@0 202 return true;
michael@0 203 }
michael@0 204
michael@0 205 return false;
michael@0 206 }
michael@0 207
michael@0 208 void
michael@0 209 nsCOMArray_base::RemoveElementAt(uint32_t aIndex)
michael@0 210 {
michael@0 211 nsISupports* element = mArray[aIndex];
michael@0 212 mArray.RemoveElementAt(aIndex);
michael@0 213 NS_IF_RELEASE(element);
michael@0 214 }
michael@0 215
michael@0 216 bool
michael@0 217 nsCOMArray_base::RemoveObjectsAt(int32_t aIndex, int32_t aCount)
michael@0 218 {
michael@0 219 if (uint32_t(aIndex) + uint32_t(aCount) <= mArray.Length()) {
michael@0 220 nsTArray<nsISupports*> elementsToDestroy(aCount);
michael@0 221 elementsToDestroy.AppendElements(mArray.Elements() + aIndex, aCount);
michael@0 222 mArray.RemoveElementsAt(aIndex, aCount);
michael@0 223 ReleaseObjects(elementsToDestroy);
michael@0 224 return true;
michael@0 225 }
michael@0 226
michael@0 227 return false;
michael@0 228 }
michael@0 229
michael@0 230 void
michael@0 231 nsCOMArray_base::RemoveElementsAt(uint32_t aIndex, uint32_t aCount)
michael@0 232 {
michael@0 233 nsTArray<nsISupports*> elementsToDestroy(aCount);
michael@0 234 elementsToDestroy.AppendElements(mArray.Elements() + aIndex, aCount);
michael@0 235 mArray.RemoveElementsAt(aIndex, aCount);
michael@0 236 ReleaseObjects(elementsToDestroy);
michael@0 237 }
michael@0 238
michael@0 239 // useful for destructors
michael@0 240 void
michael@0 241 ReleaseObjects(nsTArray<nsISupports*> &aArray)
michael@0 242 {
michael@0 243 for (uint32_t i = 0; i < aArray.Length(); i++)
michael@0 244 NS_IF_RELEASE(aArray[i]);
michael@0 245 }
michael@0 246
michael@0 247 void
michael@0 248 nsCOMArray_base::Clear()
michael@0 249 {
michael@0 250 nsTArray<nsISupports*> objects;
michael@0 251 objects.SwapElements(mArray);
michael@0 252 ReleaseObjects(objects);
michael@0 253 }
michael@0 254
michael@0 255 bool
michael@0 256 nsCOMArray_base::SetCount(int32_t aNewCount)
michael@0 257 {
michael@0 258 NS_ASSERTION(aNewCount >= 0,"SetCount(negative index)");
michael@0 259 if (aNewCount < 0)
michael@0 260 return false;
michael@0 261
michael@0 262 int32_t count = mArray.Length();
michael@0 263 if (count > aNewCount)
michael@0 264 RemoveObjectsAt(aNewCount, mArray.Length() - aNewCount);
michael@0 265 mArray.SetLength(aNewCount);
michael@0 266 return true;
michael@0 267 }
michael@0 268
michael@0 269 size_t
michael@0 270 nsCOMArray_base::SizeOfExcludingThis(
michael@0 271 nsBaseArraySizeOfElementIncludingThisFunc aSizeOfElementIncludingThis,
michael@0 272 mozilla::MallocSizeOf aMallocSizeOf, void* aData) const
michael@0 273 {
michael@0 274 size_t n = mArray.SizeOfExcludingThis(aMallocSizeOf);
michael@0 275
michael@0 276 if (aSizeOfElementIncludingThis)
michael@0 277 for (uint32_t index = 0; index < mArray.Length(); index++)
michael@0 278 n += aSizeOfElementIncludingThis(mArray[index], aMallocSizeOf, aData);
michael@0 279
michael@0 280 return n;
michael@0 281 }
michael@0 282
michael@0 283
michael@0 284 void
michael@0 285 nsCOMArray_base::Adopt(nsISupports** aElements, uint32_t aSize)
michael@0 286 {
michael@0 287 Clear();
michael@0 288 mArray.AppendElements(aElements, aSize);
michael@0 289
michael@0 290 // Free the allocated array as well.
michael@0 291 NS_Free(aElements);
michael@0 292 }
michael@0 293
michael@0 294 uint32_t
michael@0 295 nsCOMArray_base::Forget(nsISupports*** elements)
michael@0 296 {
michael@0 297 uint32_t length = Length();
michael@0 298 size_t array_size = sizeof(nsISupports*) * length;
michael@0 299 nsISupports** array = static_cast<nsISupports**>(NS_Alloc(array_size));
michael@0 300 memmove(array, Elements(), array_size);
michael@0 301 *elements = array;
michael@0 302 // Don't Release the contained pointers; the caller of the method will
michael@0 303 // do this eventually.
michael@0 304 mArray.Clear();
michael@0 305
michael@0 306 return length;
michael@0 307 }

mercurial