Bug 1172789 (part 3) - Remove PL_DHashTableEnumerate() uses from nsLoadGroup. r=michal.
authorNicholas Nethercote <nnethercote@mozilla.com>
Wed, 10 Jun 2015 13:07:40 -0700
changeset 248242 2f26ece1e25f6ca5f9eecc5dda194e6188434321
parent 248241 757f9f84dcbe6d2017aa24a025984aac23633f89
child 248243 606cb8f0882590c86e09a2186eeb41c9a1c6da43
push id28893
push userkwierso@gmail.com
push dateFri, 12 Jun 2015 00:02:58 +0000
treeherderautoland@8cf9d3e497f9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmichal
bugs1172789
milestone41.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1172789 (part 3) - Remove PL_DHashTableEnumerate() uses from nsLoadGroup. r=michal. This change also factors out some duplicated code.
netwerk/base/nsLoadGroup.cpp
--- a/netwerk/base/nsLoadGroup.cpp
+++ b/netwerk/base/nsLoadGroup.cpp
@@ -241,58 +241,53 @@ nsLoadGroup::GetStatus(nsresult *status)
 {
     if (NS_SUCCEEDED(mStatus) && mDefaultLoadRequest)
         return mDefaultLoadRequest->GetStatus(status);
     
     *status = mStatus;
     return NS_OK; 
 }
 
-// PLDHashTable enumeration callback that appends strong references to
-// all nsIRequest to an nsTArray<nsIRequest*>.
-static PLDHashOperator
-AppendRequestsToArray(PLDHashTable *table, PLDHashEntryHdr *hdr,
-                      uint32_t number, void *arg)
+static bool
+AppendRequestsToArray(PLDHashTable* aTable, nsTArray<nsIRequest*> *aArray)
 {
-    RequestMapEntry *e = static_cast<RequestMapEntry *>(hdr);
-    nsTArray<nsIRequest*> *array = static_cast<nsTArray<nsIRequest*> *>(arg);
+    PLDHashTable::Iterator iter(aTable);
+    while (iter.HasMoreEntries()) {
+        auto e = static_cast<RequestMapEntry*>(iter.NextEntry());
+        nsIRequest *request = e->mKey;
+        NS_ASSERTION(request, "What? Null key in pldhash entry?");
 
-    nsIRequest *request = e->mKey;
-    NS_ASSERTION(request, "What? Null key in pldhash entry?");
-
-    bool ok = array->AppendElement(request) != nullptr;
-
-    if (!ok) {
-        return PL_DHASH_STOP;
+        bool ok = !!aArray->AppendElement(request);
+        if (!ok) {
+           break;
+        }
+        NS_ADDREF(request);
     }
 
-    NS_ADDREF(request);
-
-    return PL_DHASH_NEXT;
+    if (aArray->Length() != aTable->EntryCount()) {
+        for (uint32_t i = 0, len = aArray->Length(); i < len; ++i) {
+            NS_RELEASE((*aArray)[i]);
+        }
+        return false;
+    }
+    return true;
 }
 
 NS_IMETHODIMP
 nsLoadGroup::Cancel(nsresult status)
 {
     MOZ_ASSERT(NS_IsMainThread());
 
     NS_ASSERTION(NS_FAILED(status), "shouldn't cancel with a success code");
     nsresult rv;
     uint32_t count = mRequests.EntryCount();
 
     nsAutoTArray<nsIRequest*, 8> requests;
 
-    PL_DHashTableEnumerate(&mRequests, AppendRequestsToArray,
-                           static_cast<nsTArray<nsIRequest*> *>(&requests));
-
-    if (requests.Length() != count) {
-        for (uint32_t i = 0, len = requests.Length(); i < len; ++i) {
-            NS_RELEASE(requests[i]);
-        }
-
+    if (!AppendRequestsToArray(&mRequests, &requests)) {
         return NS_ERROR_OUT_OF_MEMORY;
     }
 
     // set the load group status to our cancel status while we cancel 
     // all our requests...once the cancel is done, we'll reset it...
     //
     mStatus = status;
 
@@ -354,24 +349,17 @@ nsLoadGroup::Cancel(nsresult status)
 NS_IMETHODIMP
 nsLoadGroup::Suspend()
 {
     nsresult rv, firstError;
     uint32_t count = mRequests.EntryCount();
 
     nsAutoTArray<nsIRequest*, 8> requests;
 
-    PL_DHashTableEnumerate(&mRequests, AppendRequestsToArray,
-                           static_cast<nsTArray<nsIRequest*> *>(&requests));
-
-    if (requests.Length() != count) {
-        for (uint32_t i = 0, len = requests.Length(); i < len; ++i) {
-            NS_RELEASE(requests[i]);
-        }
-
+    if (!AppendRequestsToArray(&mRequests, &requests)) {
         return NS_ERROR_OUT_OF_MEMORY;
     }
 
     firstError = NS_OK;
     //
     // Operate the elements from back to front so that if items get
     // get removed from the list it won't affect our iteration
     //
@@ -406,24 +394,17 @@ nsLoadGroup::Suspend()
 NS_IMETHODIMP
 nsLoadGroup::Resume()
 {
     nsresult rv, firstError;
     uint32_t count = mRequests.EntryCount();
 
     nsAutoTArray<nsIRequest*, 8> requests;
 
-    PL_DHashTableEnumerate(&mRequests, AppendRequestsToArray,
-                           static_cast<nsTArray<nsIRequest*> *>(&requests));
-
-    if (requests.Length() != count) {
-        for (uint32_t i = 0, len = requests.Length(); i < len; ++i) {
-            NS_RELEASE(requests[i]);
-        }
-
+    if (!AppendRequestsToArray(&mRequests, &requests)) {
         return NS_ERROR_OUT_OF_MEMORY;
     }
 
     firstError = NS_OK;
     //
     // Operate the elements from back to front so that if items get
     // get removed from the list it won't affect our iteration
     //