From e7e7541033f61d3f6d72156fd72aa8b097c00490 Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Thu, 9 Jul 2015 16:54:59 -0700 Subject: [PATCH] Bug 1181445 (part 13) - Use nsBaseHashTable::Iterator in xpcom/ds/. r=froydnj. --- xpcom/ds/nsHashPropertyBag.cpp | 19 ++++-------- xpcom/ds/nsProperties.cpp | 55 ++++++++++------------------------ 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/xpcom/ds/nsHashPropertyBag.cpp b/xpcom/ds/nsHashPropertyBag.cpp index a6fa25f13e2..d77596e8283 100644 --- a/xpcom/ds/nsHashPropertyBag.cpp +++ b/xpcom/ds/nsHashPropertyBag.cpp @@ -114,18 +114,6 @@ nsSimpleProperty::GetValue(nsIVariant** aValue) // end nsSimpleProperty -static PLDHashOperator -PropertyHashToArrayFunc(const nsAString& aKey, - nsIVariant* aData, - void* aUserArg) -{ - nsIMutableArray* propertyArray = static_cast(aUserArg); - nsSimpleProperty* sprop = new nsSimpleProperty(aKey, aData); - propertyArray->AppendElement(sprop, false); - return PL_DHASH_NEXT; -} - - NS_IMETHODIMP nsHashPropertyBagBase::GetEnumerator(nsISimpleEnumerator** aResult) { @@ -134,7 +122,12 @@ nsHashPropertyBagBase::GetEnumerator(nsISimpleEnumerator** aResult) return NS_ERROR_OUT_OF_MEMORY; } - mPropertyHash.EnumerateRead(PropertyHashToArrayFunc, propertyArray.get()); + for (auto iter = mPropertyHash.Iter(); !iter.Done(); iter.Next()) { + const nsAString& key = iter.GetKey(); + nsIVariant* data = iter.GetUserData(); + nsSimpleProperty* sprop = new nsSimpleProperty(key, data); + propertyArray->AppendElement(sprop, false); + } return NS_NewArrayEnumerator(aResult, propertyArray); } diff --git a/xpcom/ds/nsProperties.cpp b/xpcom/ds/nsProperties.cpp index 66687d8e246..1df9e8c0944 100644 --- a/xpcom/ds/nsProperties.cpp +++ b/xpcom/ds/nsProperties.cpp @@ -65,28 +65,6 @@ nsProperties::Has(const char* prop, bool* result) return NS_OK; } -struct GetKeysEnumData -{ - char** keys; - uint32_t next; - nsresult res; -}; - -PLDHashOperator -GetKeysEnumerate(const char* aKey, nsISupports* aData, void* aArg) -{ - GetKeysEnumData* gkedp = (GetKeysEnumData*)aArg; - gkedp->keys[gkedp->next] = strdup(aKey); - - if (!gkedp->keys[gkedp->next]) { - gkedp->res = NS_ERROR_OUT_OF_MEMORY; - return PL_DHASH_STOP; - } - - gkedp->next++; - return PL_DHASH_NEXT; -} - NS_IMETHODIMP nsProperties::GetKeys(uint32_t* aCount, char*** aKeys) { @@ -94,28 +72,27 @@ nsProperties::GetKeys(uint32_t* aCount, char*** aKeys) return NS_ERROR_INVALID_ARG; } - uint32_t n = Count(); - char** k = (char**)moz_xmalloc(n * sizeof(char*)); + uint32_t count = Count(); + char** keys = (char**)moz_xmalloc(count * sizeof(char*)); + uint32_t j = 0; - GetKeysEnumData gked; - gked.keys = k; - gked.next = 0; - gked.res = NS_OK; + for (auto iter = this->Iter(); !iter.Done(); iter.Next()) { + const char* key = iter.GetKey(); + keys[j] = strdup(key); - EnumerateRead(GetKeysEnumerate, &gked); - - nsresult rv = gked.res; - if (NS_FAILED(rv)) { - // Free 'em all - for (uint32_t i = 0; i < gked.next; i++) { - free(k[i]); + if (!keys[j]) { + // Free 'em all + for (uint32_t i = 0; i < j; i++) { + free(keys[i]); + } + free(keys); + return NS_ERROR_OUT_OF_MEMORY; } - free(k); - return rv; + j++; } - *aCount = n; - *aKeys = k; + *aCount = count; + *aKeys = keys; return NS_OK; }