Bug 858671. Make do_GetAtom infallible. r=khuey

This commit is contained in:
Boris Zbarsky 2013-04-08 17:04:21 -04:00
parent 07a59e8ded
commit d0390f6f9c
2 changed files with 33 additions and 24 deletions

View File

@ -478,43 +478,52 @@ NS_SizeOfAtomTablesIncludingThis(nsMallocSizeOfFun aMallocSizeOf) {
#define ATOM_HASHTABLE_INITIAL_SIZE 4096
static inline bool
static void HandleOOM()
{
fputs("Out of memory allocating atom hashtable.\n", stderr);
MOZ_CRASH();
MOZ_NOT_REACHED();
}
static inline void
EnsureTableExists()
{
if (gAtomTable.ops) {
return true;
if (!gAtomTable.ops &&
!PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE)) {
// Initialization failed.
HandleOOM();
}
if (PL_DHashTableInit(&gAtomTable, &AtomTableOps, 0,
sizeof(AtomTableEntry), ATOM_HASHTABLE_INITIAL_SIZE)) {
return true;
}
// Initialization failed.
gAtomTable.ops = nullptr;
return false;
}
static inline AtomTableEntry*
GetAtomHashEntry(const char* aString, uint32_t aLength)
{
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
if (!EnsureTableExists()) {
return nullptr;
}
EnsureTableExists();
AtomTableKey key(aString, aLength);
return static_cast<AtomTableEntry*>
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
AtomTableEntry* e =
static_cast<AtomTableEntry*>
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
if (!e) {
HandleOOM();
}
return e;
}
static inline AtomTableEntry*
GetAtomHashEntry(const PRUnichar* aString, uint32_t aLength)
{
MOZ_ASSERT(NS_IsMainThread(), "wrong thread");
if (!EnsureTableExists()) {
return nullptr;
}
EnsureTableExists();
AtomTableKey key(aString, aLength);
return static_cast<AtomTableEntry*>
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
AtomTableEntry* e =
static_cast<AtomTableEntry*>
(PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD));
if (!e) {
HandleOOM();
}
return e;
}
class CheckStaticAtomSizes

View File

@ -97,7 +97,7 @@ protected:
/**
* Find an atom that matches the given UTF-8 string.
* The string is assumed to be zero terminated.
* The string is assumed to be zero terminated. Never returns null.
*/
extern nsIAtom* NS_NewAtom(const char* aUTF8String);
@ -105,7 +105,7 @@ inline already_AddRefed<nsIAtom> do_GetAtom(const char* aUTF8String)
{ return NS_NewAtom(aUTF8String); }
/**
* Find an atom that matches the given UTF-8 string.
* Find an atom that matches the given UTF-8 string. Never returns null.
*/
extern nsIAtom* NS_NewAtom(const nsACString& aUTF8String);
inline already_AddRefed<nsIAtom> do_GetAtom(const nsACString& aUTF8String)
@ -113,14 +113,14 @@ inline already_AddRefed<nsIAtom> do_GetAtom(const nsACString& aUTF8String)
/**
* Find an atom that matches the given UTF-16 string.
* The string is assumed to be zero terminated.
* The string is assumed to be zero terminated. Never returns null.
*/
extern nsIAtom* NS_NewAtom(const PRUnichar* aUTF16String);
inline already_AddRefed<nsIAtom> do_GetAtom(const PRUnichar* aUTF16String)
{ return NS_NewAtom(aUTF16String); }
/**
* Find an atom that matches the given UTF-16 string.
* Find an atom that matches the given UTF-16 string. Never returns null.
*/
extern nsIAtom* NS_NewAtom(const nsAString& aUTF16String);
extern nsIAtom* NS_NewPermanentAtom(const nsAString& aUTF16String);