Bug 1051102 - Make mPackagesHash be a more modern hashtable. r=bsmedberg

This commit is contained in:
Blake Kaplan 2014-08-11 11:13:36 -07:00
parent 164fd2b0e8
commit 8d384d16d1
2 changed files with 44 additions and 135 deletions

View File

@ -49,13 +49,7 @@ using namespace mozilla;
using mozilla::dom::ContentParent;
using mozilla::dom::PContentParent;
static PLDHashOperator
RemoveAll(PLDHashTable *table, PLDHashEntryHdr *entry, uint32_t number, void *arg)
{
return (PLDHashOperator) (PL_DHASH_NEXT | PL_DHASH_REMOVE);
}
// We use a "best-fit" algorithm for matching locales and themes.
// We use a "best-fit" algorithm for matching locales and themes.
// 1) the exact selected locale/theme
// 2) (locales only) same language, different country
// e.g. en-GB is the selected locale, only en-US is available
@ -82,7 +76,7 @@ LanguagesMatch(const nsACString& a, const nsACString& b)
while (*as == *bs) {
if (*as == '-')
return true;
++as; ++bs;
// reached the end
@ -105,13 +99,10 @@ nsChromeRegistryChrome::nsChromeRegistryChrome()
: mProfileLoaded(false)
, mDynamicRegistration(true)
{
mPackagesHash.ops = nullptr;
}
nsChromeRegistryChrome::~nsChromeRegistryChrome()
{
if (mPackagesHash.ops)
PL_DHashTableFinish(&mPackagesHash);
}
nsresult
@ -124,8 +115,6 @@ nsChromeRegistryChrome::Init()
mSelectedLocale = NS_LITERAL_CSTRING("en-US");
mSelectedSkin = NS_LITERAL_CSTRING("classic/1.0");
PL_DHashTableInit(&mPackagesHash, &kTableOps, nullptr, sizeof(PackageEntry));
bool safeMode = false;
nsCOMPtr<nsIXULRuntime> xulrun (do_GetService(XULAPPINFO_SERVICE_CONTRACTID));
if (xulrun)
@ -199,12 +188,8 @@ nsChromeRegistryChrome::GetLocalesForPackage(const nsACString& aPackage,
if (!a)
return NS_ERROR_OUT_OF_MEMORY;
PackageEntry* entry =
static_cast<PackageEntry*>(PL_DHashTableOperate(&mPackagesHash,
& realpackage,
PL_DHASH_LOOKUP));
if (PL_DHASH_ENTRY_IS_BUSY(entry)) {
PackageEntry* entry;
if (mPackagesHash.Get(realpackage, &entry)) {
entry->locales.EnumerateToArray(a);
}
@ -248,7 +233,7 @@ nsChromeRegistryChrome::IsLocaleRTL(const nsACString& package, bool *aResult)
nsCOMPtr<nsIPrefBranch> prefBranch (do_GetService(NS_PREFSERVICE_CONTRACTID));
if (!prefBranch)
return NS_OK;
nsXPIDLCString dir;
prefBranch->GetCharPref(prefString.get(), getter_Copies(dir));
if (dir.IsEmpty()) {
@ -270,12 +255,8 @@ nsChromeRegistryChrome::GetSelectedLocale(const nsACString& aPackage,
nsresult rv = OverrideLocalePackage(aPackage, realpackage);
if (NS_FAILED(rv))
return rv;
PackageEntry* entry =
static_cast<PackageEntry*>(PL_DHashTableOperate(&mPackagesHash,
& realpackage,
PL_DHASH_LOOKUP));
if (PL_DHASH_ENTRY_IS_FREE(entry))
PackageEntry* entry;
if (!mPackagesHash.Get(realpackage, &entry))
return NS_ERROR_FILE_NOT_FOUND;
aLocale = entry->locales.GetSelected(mSelectedLocale, nsProviderArray::LOCALE);
@ -388,7 +369,7 @@ nsChromeRegistryChrome::Observe(nsISupports *aSubject, const char *aTopic,
NS_IMETHODIMP
nsChromeRegistryChrome::CheckForNewChrome()
{
PL_DHashTableEnumerate(&mPackagesHash, RemoveAll, nullptr);
mPackagesHash.Clear();
mOverlayHash.Clear();
mStyleHash.Clear();
mOverrideTable.Clear();
@ -444,7 +425,7 @@ EnumerateOverride(nsIURI* aURIKey,
SerializeURI(aURIKey, chromeURI);
SerializeURI(aURI, overrideURI);
OverrideMapping override = {
chromeURI, overrideURI
};
@ -470,7 +451,7 @@ nsChromeRegistryChrome::SendRegisteredChrome(
EnumerationArgs args = {
packages, mSelectedLocale, mSelectedSkin
};
PL_DHashTableEnumerate(&mPackagesHash, CollectPackages, &args);
mPackagesHash.EnumerateRead(CollectPackages, &args);
nsCOMPtr<nsIIOService> io (do_GetIOService());
NS_ENSURE_TRUE_VOID(io);
@ -506,7 +487,8 @@ nsChromeRegistryChrome::SendRegisteredChrome(
}
/* static */ void
nsChromeRegistryChrome::ChromePackageFromPackageEntry(PackageEntry* aPackage,
nsChromeRegistryChrome::ChromePackageFromPackageEntry(const nsACString& aPackageName,
PackageEntry* aPackage,
ChromePackage* aChromePackage,
const nsCString& aSelectedLocale,
const nsCString& aSelectedSkin)
@ -517,24 +499,22 @@ nsChromeRegistryChrome::ChromePackageFromPackageEntry(PackageEntry* aPackage,
aChromePackage->localeBaseURI);
SerializeURI(aPackage->skins.GetBase(aSelectedSkin, nsProviderArray::ANY),
aChromePackage->skinBaseURI);
aChromePackage->package = aPackage->package;
aChromePackage->package = aPackageName;
aChromePackage->flags = aPackage->flags;
}
PLDHashOperator
nsChromeRegistryChrome::CollectPackages(PLDHashTable *table,
PLDHashEntryHdr *entry,
uint32_t number,
void *arg)
nsChromeRegistryChrome::CollectPackages(const nsACString &aKey,
PackageEntry *package,
void *arg)
{
EnumerationArgs* args = static_cast<EnumerationArgs*>(arg);
PackageEntry* package = static_cast<PackageEntry*>(entry);
ChromePackage chromePackage;
ChromePackageFromPackageEntry(package, &chromePackage,
ChromePackageFromPackageEntry(aKey, package, &chromePackage,
args->selectedLocale, args->selectedSkin);
args->packages.AppendElement(chromePackage);
return (PLDHashOperator)PL_DHASH_NEXT;
return PL_DHASH_NEXT;
}
static bool
@ -552,12 +532,8 @@ nsChromeRegistryChrome::GetBaseURIFromPackage(const nsCString& aPackage,
const nsCString& aProvider,
const nsCString& aPath)
{
PackageEntry* entry =
static_cast<PackageEntry*>(PL_DHashTableOperate(&mPackagesHash,
&aPackage,
PL_DHASH_LOOKUP));
if (PL_DHASH_ENTRY_IS_FREE(entry)) {
PackageEntry* entry;
if (!mPackagesHash.Get(aPackage, &entry)) {
if (!mInitialized)
return nullptr;
@ -583,62 +559,14 @@ nsresult
nsChromeRegistryChrome::GetFlagsFromPackage(const nsCString& aPackage,
uint32_t* aFlags)
{
PackageEntry* entry =
static_cast<PackageEntry*>(PL_DHashTableOperate(&mPackagesHash,
& (nsACString&) aPackage,
PL_DHASH_LOOKUP));
if (PL_DHASH_ENTRY_IS_FREE(entry))
PackageEntry* entry;
if (!mPackagesHash.Get(aPackage, &entry))
return NS_ERROR_FILE_NOT_FOUND;
*aFlags = entry->flags;
return NS_OK;
}
PLHashNumber
nsChromeRegistryChrome::HashKey(PLDHashTable *table, const void *key)
{
const nsACString& str = *reinterpret_cast<const nsACString*>(key);
return HashString(str);
}
bool
nsChromeRegistryChrome::MatchKey(PLDHashTable *table, const PLDHashEntryHdr *entry,
const void *key)
{
const nsACString& str = *reinterpret_cast<const nsACString*>(key);
const PackageEntry* pentry = static_cast<const PackageEntry*>(entry);
return str.Equals(pentry->package);
}
void
nsChromeRegistryChrome::ClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry)
{
PackageEntry* pentry = static_cast<PackageEntry*>(entry);
pentry->~PackageEntry();
}
bool
nsChromeRegistryChrome::InitEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
const void *key)
{
const nsACString& str = *reinterpret_cast<const nsACString*>(key);
new (entry) PackageEntry(str);
return true;
}
const PLDHashTableOps
nsChromeRegistryChrome::kTableOps = {
PL_DHashAllocTable,
PL_DHashFreeTable,
HashKey,
MatchKey,
PL_DHashMoveEntryStub,
ClearEntry,
PL_DHashFinalizeStub,
InitEntry
};
nsChromeRegistryChrome::ProviderEntry*
nsChromeRegistryChrome::nsProviderArray::GetProvider(const nsACString& aPreferred, MatchType aType)
{
@ -854,13 +782,8 @@ nsChromeRegistryChrome::ManifestContent(ManifestProcessingContext& cx, int linen
return;
}
PackageEntry* entry =
static_cast<PackageEntry*>(PL_DHashTableOperate(&mPackagesHash,
& (const nsACString&) nsDependentCString(package),
PL_DHASH_ADD));
if (!entry)
return;
nsDependentCString packageName(package);
PackageEntry* entry = mPackagesHash.LookupOrAdd(packageName);
entry->baseURI = resolved;
if (platform)
@ -870,7 +793,8 @@ nsChromeRegistryChrome::ManifestContent(ManifestProcessingContext& cx, int linen
if (mDynamicRegistration) {
ChromePackage chromePackage;
ChromePackageFromPackageEntry(entry, &chromePackage, mSelectedLocale, mSelectedSkin);
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
mSelectedLocale, mSelectedSkin);
SendManifestEntry(chromePackage);
}
}
@ -900,18 +824,14 @@ nsChromeRegistryChrome::ManifestLocale(ManifestProcessingContext& cx, int lineno
return;
}
PackageEntry* entry =
static_cast<PackageEntry*>(PL_DHashTableOperate(&mPackagesHash,
& (const nsACString&) nsDependentCString(package),
PL_DHASH_ADD));
if (!entry)
return;
nsDependentCString packageName(package);
PackageEntry* entry = mPackagesHash.LookupOrAdd(packageName);
entry->locales.SetBase(nsDependentCString(provider), resolved);
if (mDynamicRegistration) {
ChromePackage chromePackage;
ChromePackageFromPackageEntry(entry, &chromePackage, mSelectedLocale, mSelectedSkin);
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
mSelectedLocale, mSelectedSkin);
SendManifestEntry(chromePackage);
}
}
@ -941,18 +861,14 @@ nsChromeRegistryChrome::ManifestSkin(ManifestProcessingContext& cx, int lineno,
return;
}
PackageEntry* entry =
static_cast<PackageEntry*>(PL_DHashTableOperate(&mPackagesHash,
& (const nsACString&) nsDependentCString(package),
PL_DHASH_ADD));
if (!entry)
return;
nsDependentCString packageName(package);
PackageEntry* entry = mPackagesHash.LookupOrAdd(packageName);
entry->skins.SetBase(nsDependentCString(provider), resolved);
if (mDynamicRegistration) {
ChromePackage chromePackage;
ChromePackageFromPackageEntry(entry, &chromePackage, mSelectedLocale, mSelectedSkin);
ChromePackageFromPackageEntry(packageName, entry, &chromePackage,
mSelectedLocale, mSelectedSkin);
SendManifestEntry(chromePackage);
}
}
@ -1063,7 +979,7 @@ nsChromeRegistryChrome::ManifestResource(ManifestProcessingContext& cx, int line
nsresult rv = io->GetProtocolHandler("resource", getter_AddRefs(ph));
if (NS_FAILED(rv))
return;
nsCOMPtr<nsIResProtocolHandler> rph = do_QueryInterface(ph);
bool exists = false;

View File

@ -10,6 +10,7 @@
#include "nsChromeRegistry.h"
#include "nsTArray.h"
#include "mozilla/Move.h"
#include "nsClassHashtable.h"
namespace mozilla {
namespace dom {
@ -53,13 +54,14 @@ class nsChromeRegistryChrome : public nsChromeRegistry
private:
struct PackageEntry;
static void ChromePackageFromPackageEntry(PackageEntry* aPackage,
static void ChromePackageFromPackageEntry(const nsACString& aPackageName,
PackageEntry* aPackage,
ChromePackage* aChromePackage,
const nsCString& aSelectedLocale,
const nsCString& aSelectedSkin);
static PLDHashOperator CollectPackages(PLDHashTable *table,
PLDHashEntryHdr *entry,
uint32_t number, void *arg);
static PLDHashOperator CollectPackages(const nsACString &aKey,
PackageEntry *package,
void *arg);
nsresult OverrideLocalePackage(const nsACString& aPackage,
nsACString& aOverride);
@ -71,14 +73,6 @@ class nsChromeRegistryChrome : public nsChromeRegistry
nsresult GetFlagsFromPackage(const nsCString& aPackage,
uint32_t* aFlags) MOZ_OVERRIDE;
static const PLDHashTableOps kTableOps;
static PLDHashNumber HashKey(PLDHashTable *table, const void *key);
static bool MatchKey(PLDHashTable *table, const PLDHashEntryHdr *entry,
const void *key);
static void ClearEntry(PLDHashTable *table, PLDHashEntryHdr *entry);
static bool InitEntry(PLDHashTable *table, PLDHashEntryHdr *entry,
const void *key);
struct ProviderEntry
{
ProviderEntry(const nsACString& aProvider, nsIURI* aBase) :
@ -117,11 +111,10 @@ class nsChromeRegistryChrome : public nsChromeRegistry
struct PackageEntry : public PLDHashEntryHdr
{
PackageEntry(const nsACString& package)
: package(package), flags(0) { }
PackageEntry()
: flags(0) { }
~PackageEntry() { }
nsCString package;
nsCOMPtr<nsIURI> baseURI;
uint32_t flags;
nsProviderArray locales;
@ -170,7 +163,7 @@ class nsChromeRegistryChrome : public nsChromeRegistry
nsCString mSelectedSkin;
// Hash of package names ("global") to PackageEntry objects
PLDHashTable mPackagesHash;
nsClassHashtable<nsCStringHashKey, PackageEntry> mPackagesHash;
virtual void ManifestContent(ManifestProcessingContext& cx, int lineno,
char *const * argv, bool platform,