Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
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 <string.h> |
michael@0 | 7 | #include "mozilla/MathAlgorithms.h" |
michael@0 | 8 | #include "nsSupportsArray.h" |
michael@0 | 9 | #include "nsSupportsArrayEnumerator.h" |
michael@0 | 10 | #include "nsIObjectInputStream.h" |
michael@0 | 11 | #include "nsIObjectOutputStream.h" |
michael@0 | 12 | |
michael@0 | 13 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 14 | #define MAXSUPPORTS 20 |
michael@0 | 15 | |
michael@0 | 16 | class SupportsStats { |
michael@0 | 17 | public: |
michael@0 | 18 | SupportsStats(); |
michael@0 | 19 | ~SupportsStats(); |
michael@0 | 20 | |
michael@0 | 21 | }; |
michael@0 | 22 | |
michael@0 | 23 | static int sizesUsed; // number of the elements of the arrays used |
michael@0 | 24 | static int sizesAlloced[MAXSUPPORTS]; // sizes of the allocations. sorted |
michael@0 | 25 | static int NumberOfSize[MAXSUPPORTS]; // number of this allocation size (1 per array) |
michael@0 | 26 | static int AllocedOfSize[MAXSUPPORTS]; // number of this allocation size (each size for array used) |
michael@0 | 27 | static int GrowInPlace[MAXSUPPORTS]; |
michael@0 | 28 | |
michael@0 | 29 | // these are per-allocation |
michael@0 | 30 | static int MaxElements[3000]; |
michael@0 | 31 | |
michael@0 | 32 | // very evil |
michael@0 | 33 | #define ADD_TO_STATS(x,size) do {int i; for (i = 0; i < sizesUsed; i++) \ |
michael@0 | 34 | { \ |
michael@0 | 35 | if (sizesAlloced[i] == (int)(size)) \ |
michael@0 | 36 | { ((x)[i])++; break; } \ |
michael@0 | 37 | } \ |
michael@0 | 38 | if (i >= sizesUsed && sizesUsed < MAXSUPPORTS) \ |
michael@0 | 39 | { sizesAlloced[sizesUsed] = (size); \ |
michael@0 | 40 | ((x)[sizesUsed++])++; break; \ |
michael@0 | 41 | } \ |
michael@0 | 42 | } while (0); |
michael@0 | 43 | |
michael@0 | 44 | #define SUB_FROM_STATS(x,size) do {int i; for (i = 0; i < sizesUsed; i++) \ |
michael@0 | 45 | { \ |
michael@0 | 46 | if (sizesAlloced[i] == (int)(size)) \ |
michael@0 | 47 | { ((x)[i])--; break; } \ |
michael@0 | 48 | } \ |
michael@0 | 49 | } while (0); |
michael@0 | 50 | |
michael@0 | 51 | |
michael@0 | 52 | SupportsStats::SupportsStats() |
michael@0 | 53 | { |
michael@0 | 54 | sizesUsed = 1; |
michael@0 | 55 | sizesAlloced[0] = 0; |
michael@0 | 56 | } |
michael@0 | 57 | |
michael@0 | 58 | SupportsStats::~SupportsStats() |
michael@0 | 59 | { |
michael@0 | 60 | int i; |
michael@0 | 61 | for (i = 0; i < sizesUsed; i++) |
michael@0 | 62 | { |
michael@0 | 63 | printf("Size %d:\n",sizesAlloced[i]); |
michael@0 | 64 | printf("\tNumber of SupportsArrays this size (max): %d\n",NumberOfSize[i]); |
michael@0 | 65 | printf("\tNumber of allocations this size (total): %d\n",AllocedOfSize[i]); |
michael@0 | 66 | printf("\tNumber of GrowsInPlace this size (total): %d\n",GrowInPlace[i]); |
michael@0 | 67 | } |
michael@0 | 68 | printf("Max Size of SupportsArray:\n"); |
michael@0 | 69 | for (i = 0; i < (int)(sizeof(MaxElements)/sizeof(MaxElements[0])); i++) |
michael@0 | 70 | { |
michael@0 | 71 | if (MaxElements[i]) |
michael@0 | 72 | printf("\t%d: %d\n",i,MaxElements[i]); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | // Just so constructor/destructor get called |
michael@0 | 77 | SupportsStats gSupportsStats; |
michael@0 | 78 | #endif |
michael@0 | 79 | |
michael@0 | 80 | nsresult |
michael@0 | 81 | nsQueryElementAt::operator()( const nsIID& aIID, void** aResult ) const |
michael@0 | 82 | { |
michael@0 | 83 | nsresult status = mCollection |
michael@0 | 84 | ? mCollection->QueryElementAt(mIndex, aIID, aResult) |
michael@0 | 85 | : NS_ERROR_NULL_POINTER; |
michael@0 | 86 | |
michael@0 | 87 | if ( mErrorPtr ) |
michael@0 | 88 | *mErrorPtr = status; |
michael@0 | 89 | |
michael@0 | 90 | return status; |
michael@0 | 91 | } |
michael@0 | 92 | |
michael@0 | 93 | static const int32_t kGrowArrayBy = 8; |
michael@0 | 94 | static const int32_t kLinearThreshold = 16 * sizeof(nsISupports *); |
michael@0 | 95 | |
michael@0 | 96 | nsSupportsArray::nsSupportsArray() |
michael@0 | 97 | { |
michael@0 | 98 | mArray = mAutoArray; |
michael@0 | 99 | mArraySize = kAutoArraySize; |
michael@0 | 100 | mCount = 0; |
michael@0 | 101 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 102 | mMaxCount = 0; |
michael@0 | 103 | mMaxSize = 0; |
michael@0 | 104 | ADD_TO_STATS(NumberOfSize,kAutoArraySize*sizeof(mArray[0])); |
michael@0 | 105 | MaxElements[0]++; |
michael@0 | 106 | #endif |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | nsSupportsArray::~nsSupportsArray() |
michael@0 | 110 | { |
michael@0 | 111 | DeleteArray(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | void nsSupportsArray::GrowArrayBy(int32_t aGrowBy) |
michael@0 | 115 | { |
michael@0 | 116 | // We have to grow the array. Grow by kGrowArrayBy slots if we're smaller |
michael@0 | 117 | // than kLinearThreshold bytes, or a power of two if we're larger. |
michael@0 | 118 | // This is much more efficient with most memory allocators, especially |
michael@0 | 119 | // if it's very large, or of the allocator is binned. |
michael@0 | 120 | if (aGrowBy < kGrowArrayBy) |
michael@0 | 121 | aGrowBy = kGrowArrayBy; |
michael@0 | 122 | |
michael@0 | 123 | uint32_t newCount = mArraySize + aGrowBy; // Minimum increase |
michael@0 | 124 | uint32_t newSize = sizeof(mArray[0]) * newCount; |
michael@0 | 125 | |
michael@0 | 126 | if (newSize >= (uint32_t) kLinearThreshold) |
michael@0 | 127 | { |
michael@0 | 128 | // newCount includes enough space for at least kGrowArrayBy new slots. |
michael@0 | 129 | // Select the next power-of-two size in bytes above that if newSize is |
michael@0 | 130 | // not a power of two. |
michael@0 | 131 | if (newSize & (newSize - 1)) |
michael@0 | 132 | newSize = 1u << mozilla::CeilingLog2(newSize); |
michael@0 | 133 | |
michael@0 | 134 | newCount = newSize / sizeof(mArray[0]); |
michael@0 | 135 | } |
michael@0 | 136 | // XXX This would be far more efficient in many allocators if we used |
michael@0 | 137 | // XXX PR_Realloc(), etc |
michael@0 | 138 | nsISupports** oldArray = mArray; |
michael@0 | 139 | |
michael@0 | 140 | mArray = new nsISupports*[newCount]; |
michael@0 | 141 | mArraySize = newCount; |
michael@0 | 142 | |
michael@0 | 143 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 144 | if (oldArray == mArray) // can't happen without use of realloc |
michael@0 | 145 | ADD_TO_STATS(GrowInPlace,mCount); |
michael@0 | 146 | ADD_TO_STATS(AllocedOfSize,mArraySize*sizeof(mArray[0])); |
michael@0 | 147 | if (mArraySize > mMaxSize) |
michael@0 | 148 | { |
michael@0 | 149 | ADD_TO_STATS(NumberOfSize,mArraySize*sizeof(mArray[0])); |
michael@0 | 150 | if (oldArray != &(mAutoArray[0])) |
michael@0 | 151 | SUB_FROM_STATS(NumberOfSize,mCount*sizeof(mArray[0])); |
michael@0 | 152 | mMaxSize = mArraySize; |
michael@0 | 153 | } |
michael@0 | 154 | #endif |
michael@0 | 155 | if (oldArray) { // need to move old data |
michael@0 | 156 | if (0 < mCount) { |
michael@0 | 157 | ::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*)); |
michael@0 | 158 | } |
michael@0 | 159 | if (oldArray != &(mAutoArray[0])) { |
michael@0 | 160 | delete[] oldArray; |
michael@0 | 161 | } |
michael@0 | 162 | } |
michael@0 | 163 | } |
michael@0 | 164 | |
michael@0 | 165 | nsresult |
michael@0 | 166 | nsSupportsArray::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) |
michael@0 | 167 | { |
michael@0 | 168 | if (aOuter) |
michael@0 | 169 | return NS_ERROR_NO_AGGREGATION; |
michael@0 | 170 | |
michael@0 | 171 | nsCOMPtr<nsISupportsArray> it = new nsSupportsArray(); |
michael@0 | 172 | |
michael@0 | 173 | return it->QueryInterface(aIID, aResult); |
michael@0 | 174 | } |
michael@0 | 175 | |
michael@0 | 176 | NS_IMPL_ISUPPORTS(nsSupportsArray, nsISupportsArray, nsICollection, nsISerializable) |
michael@0 | 177 | |
michael@0 | 178 | NS_IMETHODIMP |
michael@0 | 179 | nsSupportsArray::Read(nsIObjectInputStream *aStream) |
michael@0 | 180 | { |
michael@0 | 181 | nsresult rv; |
michael@0 | 182 | |
michael@0 | 183 | uint32_t newArraySize; |
michael@0 | 184 | rv = aStream->Read32(&newArraySize); |
michael@0 | 185 | |
michael@0 | 186 | if (newArraySize <= kAutoArraySize) { |
michael@0 | 187 | if (mArray != mAutoArray) { |
michael@0 | 188 | delete[] mArray; |
michael@0 | 189 | mArray = mAutoArray; |
michael@0 | 190 | } |
michael@0 | 191 | newArraySize = kAutoArraySize; |
michael@0 | 192 | } |
michael@0 | 193 | else { |
michael@0 | 194 | if (newArraySize <= mArraySize) { |
michael@0 | 195 | // Keep non-default-size mArray, it's more than big enough. |
michael@0 | 196 | newArraySize = mArraySize; |
michael@0 | 197 | } |
michael@0 | 198 | else { |
michael@0 | 199 | nsISupports** array = new nsISupports*[newArraySize]; |
michael@0 | 200 | if (mArray != mAutoArray) |
michael@0 | 201 | delete[] mArray; |
michael@0 | 202 | mArray = array; |
michael@0 | 203 | } |
michael@0 | 204 | } |
michael@0 | 205 | mArraySize = newArraySize; |
michael@0 | 206 | |
michael@0 | 207 | rv = aStream->Read32(&mCount); |
michael@0 | 208 | if (NS_FAILED(rv)) return rv; |
michael@0 | 209 | |
michael@0 | 210 | NS_ASSERTION(mCount <= mArraySize, "overlarge mCount!"); |
michael@0 | 211 | if (mCount > mArraySize) |
michael@0 | 212 | mCount = mArraySize; |
michael@0 | 213 | |
michael@0 | 214 | for (uint32_t i = 0; i < mCount; i++) { |
michael@0 | 215 | rv = aStream->ReadObject(true, &mArray[i]); |
michael@0 | 216 | if (NS_FAILED(rv)) return rv; |
michael@0 | 217 | } |
michael@0 | 218 | |
michael@0 | 219 | return NS_OK; |
michael@0 | 220 | } |
michael@0 | 221 | |
michael@0 | 222 | NS_IMETHODIMP |
michael@0 | 223 | nsSupportsArray::Write(nsIObjectOutputStream *aStream) |
michael@0 | 224 | { |
michael@0 | 225 | nsresult rv; |
michael@0 | 226 | |
michael@0 | 227 | rv = aStream->Write32(mArraySize); |
michael@0 | 228 | if (NS_FAILED(rv)) return rv; |
michael@0 | 229 | |
michael@0 | 230 | rv = aStream->Write32(mCount); |
michael@0 | 231 | if (NS_FAILED(rv)) return rv; |
michael@0 | 232 | |
michael@0 | 233 | for (uint32_t i = 0; i < mCount; i++) { |
michael@0 | 234 | rv = aStream->WriteObject(mArray[i], true); |
michael@0 | 235 | if (NS_FAILED(rv)) return rv; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | return NS_OK; |
michael@0 | 239 | } |
michael@0 | 240 | |
michael@0 | 241 | void nsSupportsArray::DeleteArray(void) |
michael@0 | 242 | { |
michael@0 | 243 | Clear(); |
michael@0 | 244 | if (mArray != &(mAutoArray[0])) { |
michael@0 | 245 | delete[] mArray; |
michael@0 | 246 | mArray = mAutoArray; |
michael@0 | 247 | mArraySize = kAutoArraySize; |
michael@0 | 248 | } |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | |
michael@0 | 252 | NS_IMETHODIMP_(bool) |
michael@0 | 253 | nsSupportsArray::Equals(const nsISupportsArray* aOther) |
michael@0 | 254 | { |
michael@0 | 255 | if (aOther) { |
michael@0 | 256 | uint32_t countOther; |
michael@0 | 257 | nsISupportsArray* other = const_cast<nsISupportsArray*>(aOther); |
michael@0 | 258 | nsresult rv = other->Count(&countOther); |
michael@0 | 259 | if (NS_FAILED( rv )) |
michael@0 | 260 | return false; |
michael@0 | 261 | |
michael@0 | 262 | if (mCount == countOther) { |
michael@0 | 263 | uint32_t index = mCount; |
michael@0 | 264 | nsCOMPtr<nsISupports> otherElem; |
michael@0 | 265 | while (index--) { |
michael@0 | 266 | if (NS_FAILED(other->GetElementAt(index, getter_AddRefs(otherElem)))) |
michael@0 | 267 | return false; |
michael@0 | 268 | if (mArray[index] != otherElem) |
michael@0 | 269 | return false; |
michael@0 | 270 | } |
michael@0 | 271 | return true; |
michael@0 | 272 | } |
michael@0 | 273 | } |
michael@0 | 274 | return false; |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | NS_IMETHODIMP |
michael@0 | 278 | nsSupportsArray::GetElementAt(uint32_t aIndex, nsISupports **aOutPtr) |
michael@0 | 279 | { |
michael@0 | 280 | *aOutPtr = nullptr; |
michael@0 | 281 | if (aIndex < mCount) { |
michael@0 | 282 | NS_IF_ADDREF(*aOutPtr = mArray[aIndex]); |
michael@0 | 283 | } |
michael@0 | 284 | return NS_OK; |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | NS_IMETHODIMP_(int32_t) |
michael@0 | 288 | nsSupportsArray::IndexOf(const nsISupports* aPossibleElement) |
michael@0 | 289 | { |
michael@0 | 290 | return IndexOfStartingAt(aPossibleElement, 0); |
michael@0 | 291 | } |
michael@0 | 292 | |
michael@0 | 293 | NS_IMETHODIMP_(int32_t) |
michael@0 | 294 | nsSupportsArray::IndexOfStartingAt(const nsISupports* aPossibleElement, |
michael@0 | 295 | uint32_t aStartIndex) |
michael@0 | 296 | { |
michael@0 | 297 | if (aStartIndex < mCount) { |
michael@0 | 298 | const nsISupports** start = (const nsISupports**)mArray; // work around goofy compiler behavior |
michael@0 | 299 | const nsISupports** ep = (start + aStartIndex); |
michael@0 | 300 | const nsISupports** end = (start + mCount); |
michael@0 | 301 | while (ep < end) { |
michael@0 | 302 | if (aPossibleElement == *ep) { |
michael@0 | 303 | return (ep - start); |
michael@0 | 304 | } |
michael@0 | 305 | ep++; |
michael@0 | 306 | } |
michael@0 | 307 | } |
michael@0 | 308 | return -1; |
michael@0 | 309 | } |
michael@0 | 310 | |
michael@0 | 311 | NS_IMETHODIMP_(int32_t) |
michael@0 | 312 | nsSupportsArray::LastIndexOf(const nsISupports* aPossibleElement) |
michael@0 | 313 | { |
michael@0 | 314 | if (0 < mCount) { |
michael@0 | 315 | const nsISupports** start = (const nsISupports**)mArray; // work around goofy compiler behavior |
michael@0 | 316 | const nsISupports** ep = (start + mCount); |
michael@0 | 317 | while (start <= --ep) { |
michael@0 | 318 | if (aPossibleElement == *ep) { |
michael@0 | 319 | return (ep - start); |
michael@0 | 320 | } |
michael@0 | 321 | } |
michael@0 | 322 | } |
michael@0 | 323 | return -1; |
michael@0 | 324 | } |
michael@0 | 325 | |
michael@0 | 326 | NS_IMETHODIMP_(bool) |
michael@0 | 327 | nsSupportsArray::InsertElementAt(nsISupports* aElement, uint32_t aIndex) |
michael@0 | 328 | { |
michael@0 | 329 | if (aIndex <= mCount) { |
michael@0 | 330 | if (mArraySize < (mCount + 1)) { |
michael@0 | 331 | // need to grow the array |
michael@0 | 332 | GrowArrayBy(1); |
michael@0 | 333 | } |
michael@0 | 334 | |
michael@0 | 335 | // Could be slightly more efficient if GrowArrayBy knew about the |
michael@0 | 336 | // split, but the difference is trivial. |
michael@0 | 337 | uint32_t slide = (mCount - aIndex); |
michael@0 | 338 | if (0 < slide) { |
michael@0 | 339 | ::memmove(mArray + aIndex + 1, mArray + aIndex, slide * sizeof(nsISupports*)); |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | mArray[aIndex] = aElement; |
michael@0 | 343 | NS_IF_ADDREF(aElement); |
michael@0 | 344 | mCount++; |
michael@0 | 345 | |
michael@0 | 346 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 347 | if (mCount > mMaxCount && |
michael@0 | 348 | mCount < (int32_t)(sizeof(MaxElements)/sizeof(MaxElements[0]))) |
michael@0 | 349 | { |
michael@0 | 350 | MaxElements[mCount]++; |
michael@0 | 351 | MaxElements[mMaxCount]--; |
michael@0 | 352 | mMaxCount = mCount; |
michael@0 | 353 | } |
michael@0 | 354 | #endif |
michael@0 | 355 | return true; |
michael@0 | 356 | } |
michael@0 | 357 | return false; |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | NS_IMETHODIMP_(bool) |
michael@0 | 361 | nsSupportsArray::InsertElementsAt(nsISupportsArray* aElements, uint32_t aIndex) |
michael@0 | 362 | { |
michael@0 | 363 | if (!aElements) { |
michael@0 | 364 | return false; |
michael@0 | 365 | } |
michael@0 | 366 | uint32_t countElements; |
michael@0 | 367 | if (NS_FAILED( aElements->Count( &countElements ) )) |
michael@0 | 368 | return false; |
michael@0 | 369 | |
michael@0 | 370 | if (aIndex <= mCount) { |
michael@0 | 371 | if (mArraySize < (mCount + countElements)) { |
michael@0 | 372 | // need to grow the array |
michael@0 | 373 | GrowArrayBy(countElements); |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | // Could be slightly more efficient if GrowArrayBy knew about the |
michael@0 | 377 | // split, but the difference is trivial. |
michael@0 | 378 | uint32_t slide = (mCount - aIndex); |
michael@0 | 379 | if (0 < slide) { |
michael@0 | 380 | ::memmove(mArray + aIndex + countElements, mArray + aIndex, |
michael@0 | 381 | slide * sizeof(nsISupports*)); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | for (uint32_t i = 0; i < countElements; ++i, ++mCount) { |
michael@0 | 385 | // use GetElementAt to copy and do AddRef for us |
michael@0 | 386 | if (NS_FAILED( aElements->GetElementAt( i, mArray + aIndex + i) )) |
michael@0 | 387 | return false; |
michael@0 | 388 | } |
michael@0 | 389 | |
michael@0 | 390 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 391 | if (mCount > mMaxCount && |
michael@0 | 392 | mCount < (int32_t)(sizeof(MaxElements)/sizeof(MaxElements[0]))) |
michael@0 | 393 | { |
michael@0 | 394 | MaxElements[mCount]++; |
michael@0 | 395 | MaxElements[mMaxCount]--; |
michael@0 | 396 | mMaxCount = mCount; |
michael@0 | 397 | } |
michael@0 | 398 | #endif |
michael@0 | 399 | return true; |
michael@0 | 400 | } |
michael@0 | 401 | return false; |
michael@0 | 402 | } |
michael@0 | 403 | |
michael@0 | 404 | NS_IMETHODIMP_(bool) |
michael@0 | 405 | nsSupportsArray::ReplaceElementAt(nsISupports* aElement, uint32_t aIndex) |
michael@0 | 406 | { |
michael@0 | 407 | if (aIndex < mCount) { |
michael@0 | 408 | NS_IF_ADDREF(aElement); // addref first in case it's the same object! |
michael@0 | 409 | NS_IF_RELEASE(mArray[aIndex]); |
michael@0 | 410 | mArray[aIndex] = aElement; |
michael@0 | 411 | return true; |
michael@0 | 412 | } |
michael@0 | 413 | return false; |
michael@0 | 414 | } |
michael@0 | 415 | |
michael@0 | 416 | NS_IMETHODIMP_(bool) |
michael@0 | 417 | nsSupportsArray::RemoveElementsAt(uint32_t aIndex, uint32_t aCount) |
michael@0 | 418 | { |
michael@0 | 419 | if (aIndex + aCount <= mCount) { |
michael@0 | 420 | for (uint32_t i = 0; i < aCount; i++) |
michael@0 | 421 | NS_IF_RELEASE(mArray[aIndex+i]); |
michael@0 | 422 | mCount -= aCount; |
michael@0 | 423 | int32_t slide = (mCount - aIndex); |
michael@0 | 424 | if (0 < slide) { |
michael@0 | 425 | ::memmove(mArray + aIndex, mArray + aIndex + aCount, |
michael@0 | 426 | slide * sizeof(nsISupports*)); |
michael@0 | 427 | } |
michael@0 | 428 | return true; |
michael@0 | 429 | } |
michael@0 | 430 | return false; |
michael@0 | 431 | } |
michael@0 | 432 | |
michael@0 | 433 | NS_IMETHODIMP_(bool) |
michael@0 | 434 | nsSupportsArray::RemoveElement(const nsISupports* aElement, uint32_t aStartIndex) |
michael@0 | 435 | { |
michael@0 | 436 | int32_t theIndex = IndexOfStartingAt(aElement,aStartIndex); |
michael@0 | 437 | if (theIndex >= 0) |
michael@0 | 438 | return RemoveElementAt(theIndex); |
michael@0 | 439 | |
michael@0 | 440 | return false; |
michael@0 | 441 | } |
michael@0 | 442 | |
michael@0 | 443 | NS_IMETHODIMP_(bool) |
michael@0 | 444 | nsSupportsArray::RemoveLastElement(const nsISupports* aElement) |
michael@0 | 445 | { |
michael@0 | 446 | int32_t theIndex = LastIndexOf(aElement); |
michael@0 | 447 | if (theIndex >= 0) |
michael@0 | 448 | return RemoveElementAt(theIndex); |
michael@0 | 449 | |
michael@0 | 450 | return false; |
michael@0 | 451 | } |
michael@0 | 452 | |
michael@0 | 453 | NS_IMETHODIMP_(bool) |
michael@0 | 454 | nsSupportsArray::MoveElement(int32_t aFrom, int32_t aTo) |
michael@0 | 455 | { |
michael@0 | 456 | nsISupports *tempElement; |
michael@0 | 457 | |
michael@0 | 458 | if (aTo == aFrom) |
michael@0 | 459 | return true; |
michael@0 | 460 | |
michael@0 | 461 | if (aTo < 0 || aFrom < 0 || |
michael@0 | 462 | (uint32_t) aTo >= mCount || (uint32_t) aFrom >= mCount) |
michael@0 | 463 | { |
michael@0 | 464 | // can't extend the array when moving an element. Also catches mImpl = null |
michael@0 | 465 | return false; |
michael@0 | 466 | } |
michael@0 | 467 | tempElement = mArray[aFrom]; |
michael@0 | 468 | |
michael@0 | 469 | if (aTo < aFrom) |
michael@0 | 470 | { |
michael@0 | 471 | // Moving one element closer to the head; the elements inbetween move down |
michael@0 | 472 | ::memmove(mArray + aTo + 1, mArray + aTo, |
michael@0 | 473 | (aFrom-aTo) * sizeof(mArray[0])); |
michael@0 | 474 | mArray[aTo] = tempElement; |
michael@0 | 475 | } |
michael@0 | 476 | else // already handled aFrom == aTo |
michael@0 | 477 | { |
michael@0 | 478 | // Moving one element closer to the tail; the elements inbetween move up |
michael@0 | 479 | ::memmove(mArray + aFrom, mArray + aFrom + 1, |
michael@0 | 480 | (aTo-aFrom) * sizeof(mArray[0])); |
michael@0 | 481 | mArray[aTo] = tempElement; |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | return true; |
michael@0 | 485 | } |
michael@0 | 486 | |
michael@0 | 487 | NS_IMETHODIMP |
michael@0 | 488 | nsSupportsArray::Clear(void) |
michael@0 | 489 | { |
michael@0 | 490 | if (0 < mCount) { |
michael@0 | 491 | do { |
michael@0 | 492 | --mCount; |
michael@0 | 493 | NS_IF_RELEASE(mArray[mCount]); |
michael@0 | 494 | } while (0 != mCount); |
michael@0 | 495 | } |
michael@0 | 496 | return NS_OK; |
michael@0 | 497 | } |
michael@0 | 498 | |
michael@0 | 499 | NS_IMETHODIMP |
michael@0 | 500 | nsSupportsArray::Compact(void) |
michael@0 | 501 | { |
michael@0 | 502 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 503 | uint32_t oldArraySize = mArraySize; |
michael@0 | 504 | #endif |
michael@0 | 505 | if ((mArraySize != mCount) && (kAutoArraySize < mArraySize)) { |
michael@0 | 506 | nsISupports** oldArray = mArray; |
michael@0 | 507 | if (mCount <= kAutoArraySize) { |
michael@0 | 508 | mArray = mAutoArray; |
michael@0 | 509 | mArraySize = kAutoArraySize; |
michael@0 | 510 | } |
michael@0 | 511 | else { |
michael@0 | 512 | mArray = new nsISupports*[mCount]; |
michael@0 | 513 | if (!mArray) { |
michael@0 | 514 | mArray = oldArray; |
michael@0 | 515 | return NS_OK; |
michael@0 | 516 | } |
michael@0 | 517 | mArraySize = mCount; |
michael@0 | 518 | } |
michael@0 | 519 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 520 | if (oldArray == mArray && |
michael@0 | 521 | oldArray != &(mAutoArray[0])) // can't happen without use of realloc |
michael@0 | 522 | ADD_TO_STATS(GrowInPlace,oldArraySize); |
michael@0 | 523 | if (oldArray != &(mAutoArray[0])) |
michael@0 | 524 | ADD_TO_STATS(AllocedOfSize,mArraySize*sizeof(mArray[0])); |
michael@0 | 525 | #endif |
michael@0 | 526 | ::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*)); |
michael@0 | 527 | delete[] oldArray; |
michael@0 | 528 | } |
michael@0 | 529 | return NS_OK; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | NS_IMETHODIMP_(bool) |
michael@0 | 533 | nsSupportsArray::SizeTo(int32_t aSize) |
michael@0 | 534 | { |
michael@0 | 535 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 536 | uint32_t oldArraySize = mArraySize; |
michael@0 | 537 | #endif |
michael@0 | 538 | NS_ASSERTION(aSize >= 0, "negative aSize!"); |
michael@0 | 539 | |
michael@0 | 540 | // XXX for aSize < mCount we could resize to mCount |
michael@0 | 541 | if (mArraySize == (uint32_t) aSize || (uint32_t) aSize < mCount) |
michael@0 | 542 | return true; // nothing to do |
michael@0 | 543 | |
michael@0 | 544 | // switch back to autoarray if possible |
michael@0 | 545 | nsISupports** oldArray = mArray; |
michael@0 | 546 | if ((uint32_t) aSize <= kAutoArraySize) { |
michael@0 | 547 | mArray = mAutoArray; |
michael@0 | 548 | mArraySize = kAutoArraySize; |
michael@0 | 549 | } |
michael@0 | 550 | else { |
michael@0 | 551 | mArray = new nsISupports*[aSize]; |
michael@0 | 552 | if (!mArray) { |
michael@0 | 553 | mArray = oldArray; |
michael@0 | 554 | return false; |
michael@0 | 555 | } |
michael@0 | 556 | mArraySize = aSize; |
michael@0 | 557 | } |
michael@0 | 558 | #if DEBUG_SUPPORTSARRAY |
michael@0 | 559 | if (oldArray == mArray && |
michael@0 | 560 | oldArray != &(mAutoArray[0])) // can't happen without use of realloc |
michael@0 | 561 | ADD_TO_STATS(GrowInPlace,oldArraySize); |
michael@0 | 562 | if (oldArray != &(mAutoArray[0])) |
michael@0 | 563 | ADD_TO_STATS(AllocedOfSize,mArraySize*sizeof(mArray[0])); |
michael@0 | 564 | #endif |
michael@0 | 565 | ::memcpy(mArray, oldArray, mCount * sizeof(nsISupports*)); |
michael@0 | 566 | if (oldArray != mAutoArray) |
michael@0 | 567 | delete[] oldArray; |
michael@0 | 568 | |
michael@0 | 569 | return true; |
michael@0 | 570 | } |
michael@0 | 571 | |
michael@0 | 572 | NS_IMETHODIMP |
michael@0 | 573 | nsSupportsArray::Enumerate(nsIEnumerator* *result) |
michael@0 | 574 | { |
michael@0 | 575 | nsSupportsArrayEnumerator* e = new nsSupportsArrayEnumerator(this); |
michael@0 | 576 | if (!e) |
michael@0 | 577 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 578 | *result = e; |
michael@0 | 579 | NS_ADDREF(e); |
michael@0 | 580 | return NS_OK; |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | NS_IMETHODIMP |
michael@0 | 584 | nsSupportsArray::Clone(nsISupportsArray** aResult) |
michael@0 | 585 | { |
michael@0 | 586 | nsCOMPtr<nsISupportsArray> newArray; |
michael@0 | 587 | nsresult rv = NS_NewISupportsArray(getter_AddRefs(newArray)); |
michael@0 | 588 | if (NS_WARN_IF(NS_FAILED(rv))) |
michael@0 | 589 | return rv; |
michael@0 | 590 | |
michael@0 | 591 | uint32_t count = 0; |
michael@0 | 592 | Count(&count); |
michael@0 | 593 | for (uint32_t i = 0; i < count; i++) { |
michael@0 | 594 | if (!newArray->InsertElementAt(mArray[i], i)) { |
michael@0 | 595 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 596 | } |
michael@0 | 597 | } |
michael@0 | 598 | |
michael@0 | 599 | newArray.forget(aResult); |
michael@0 | 600 | return NS_OK; |
michael@0 | 601 | } |
michael@0 | 602 | |
michael@0 | 603 | nsresult |
michael@0 | 604 | NS_NewISupportsArray(nsISupportsArray** aInstancePtrResult) |
michael@0 | 605 | { |
michael@0 | 606 | nsresult rv; |
michael@0 | 607 | rv = nsSupportsArray::Create(nullptr, NS_GET_IID(nsISupportsArray), |
michael@0 | 608 | (void**)aInstancePtrResult); |
michael@0 | 609 | return rv; |
michael@0 | 610 | } |