Bug 1233234 - part 2 - use UniquePtr<T[]> instead of nsAutoArrayPtr<T> in pref_savePrefs; r=njn

Returning outparams with UniquePtr is not convenient or idiomatic, so in
addition to removing nsAutoArrayPtr usage, let's return a UniquePtr from
the function directly.
This commit is contained in:
Nathan Froyd 2015-12-06 09:06:02 -05:00
parent ef07260396
commit 23a987ee55
3 changed files with 15 additions and 11 deletions

View File

@ -10,6 +10,7 @@
#include "mozilla/ArrayUtils.h"
#include "mozilla/Attributes.h"
#include "mozilla/HashFunctions.h"
#include "mozilla/UniquePtrExtensions.h"
#include "nsXULAppAPI.h"
@ -937,20 +938,17 @@ Preferences::WritePrefFile(nsIFile* aFile)
if (NS_FAILED(rv))
return rv;
nsAutoArrayPtr<char*> valueArray(new char*[gHashTable->EntryCount()]);
memset(valueArray, 0, gHashTable->EntryCount() * sizeof(char*));
// get the lines that we're supposed to be writing to the file
pref_savePrefs(gHashTable, valueArray);
UniquePtr<char*[]> valueArray = pref_savePrefs(gHashTable);
/* Sort the preferences to make a readable file on disk */
NS_QuickSort(valueArray, gHashTable->EntryCount(), sizeof(char *),
NS_QuickSort(valueArray.get(), gHashTable->EntryCount(), sizeof(char *),
pref_CompareStrings, nullptr);
// write out the file header
outStream->Write(outHeader, sizeof(outHeader) - 1, &writeAmount);
char** walker = valueArray;
char** walker = valueArray.get();
for (uint32_t valueIdx = 0; valueIdx < gHashTable->EntryCount(); valueIdx++, walker++) {
if (*walker) {
outStream->Write(*walker, strlen(*walker), &writeAmount);

View File

@ -318,9 +318,12 @@ pref_SetPref(const dom::PrefSetting& aPref)
return rv;
}
void
pref_savePrefs(PLDHashTable* aTable, char** aPrefArray)
UniquePtr<char*[]>
pref_savePrefs(PLDHashTable* aTable)
{
auto savedPrefs = MakeUnique<char*[]>(aTable->EntryCount());
memset(savedPrefs.get(), 0, aTable->EntryCount() * sizeof(char*));
int32_t j = 0;
for (auto iter = aTable->Iter(); !iter.Done(); iter.Next()) {
auto pref = static_cast<PrefHashEntry*>(iter.Get());
@ -360,12 +363,14 @@ pref_savePrefs(PLDHashTable* aTable, char** aPrefArray)
nsAutoCString prefName;
str_escape(pref->key, prefName);
aPrefArray[j++] = ToNewCString(prefPrefix +
savedPrefs[j++] = ToNewCString(prefPrefix +
prefName +
NS_LITERAL_CSTRING("\", ") +
prefValue +
NS_LITERAL_CSTRING(");"));
}
return savedPrefs;
}
static void

View File

@ -9,6 +9,7 @@
#define prefapi_private_data_h
#include "mozilla/MemoryReporting.h"
#include "mozilla/UniquePtr.h"
extern PLDHashTable* gHashTable;
extern bool gDirty;
@ -19,8 +20,8 @@ class PrefSetting;
} // namespace dom
} // namespace mozilla
void
pref_savePrefs(PLDHashTable* aTable, char** aPrefArray);
mozilla::UniquePtr<char*[]>
pref_savePrefs(PLDHashTable* aTable);
nsresult
pref_SetPref(const mozilla::dom::PrefSetting& aPref);