diff --git a/xpcom/ds/nsAtomTable.cpp b/xpcom/ds/nsAtomTable.cpp index 8bce1c0da4c0..8196b991f07a 100644 --- a/xpcom/ds/nsAtomTable.cpp +++ b/xpcom/ds/nsAtomTable.cpp @@ -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 - (PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD)); + AtomTableEntry* e = + static_cast + (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 - (PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD)); + AtomTableEntry* e = + static_cast + (PL_DHashTableOperate(&gAtomTable, &key, PL_DHASH_ADD)); + if (!e) { + HandleOOM(); + } + return e; } class CheckStaticAtomSizes diff --git a/xpcom/ds/nsIAtom.idl b/xpcom/ds/nsIAtom.idl index 0eb00079c652..3289f337653f 100644 --- a/xpcom/ds/nsIAtom.idl +++ b/xpcom/ds/nsIAtom.idl @@ -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 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 do_GetAtom(const nsACString& aUTF8String) @@ -113,14 +113,14 @@ inline already_AddRefed 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 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);