From 3ad04b51a727e03edffdf92ae616a15b0c99f47d Mon Sep 17 00:00:00 2001 From: Henri Sivonen Date: Fri, 18 Sep 2009 18:13:10 +0300 Subject: [PATCH] Bug 514661 - Introduce scoped atom tables for the HTML5 parser. r=bsmedberg, sr=dbaron. --HG-- extra : rebase_source : 12e9731c0d86691cde40fc1ea4cc5ab4b3fb5168 --- layout/build/nsLayoutStatics.cpp | 2 + parser/html/Makefile.in | 2 + parser/html/nsHtml5Atom.cpp | 109 ++++++++++++++++++++++ parser/html/nsHtml5Atom.h | 63 +++++++++++++ parser/html/nsHtml5AtomTable.cpp | 89 ++++++++++++++++++ parser/html/nsHtml5AtomTable.h | 145 ++++++++++++++++++++++++++++++ xpcom/ds/nsAtomTable.cpp | 63 ++++++++++++- xpcom/ds/nsIAtom.idl | 17 +++- xpcom/tests/Makefile.in | 1 + xpcom/tests/MoreTestingAtomList.h | 39 ++++++++ xpcom/tests/MoreTestingAtoms.cpp | 58 ++++++++++++ xpcom/tests/MoreTestingAtoms.h | 51 +++++++++++ xpcom/tests/TestStaticAtoms.cpp | 108 ++++++++++++++++++++++ xpcom/tests/TestingAtomList.h | 39 ++++++++ xpcom/tests/TestingAtoms.cpp | 57 ++++++++++++ xpcom/tests/TestingAtoms.h | 51 +++++++++++ 16 files changed, 892 insertions(+), 2 deletions(-) create mode 100644 parser/html/nsHtml5Atom.cpp create mode 100644 parser/html/nsHtml5Atom.h create mode 100644 parser/html/nsHtml5AtomTable.cpp create mode 100644 parser/html/nsHtml5AtomTable.h create mode 100644 xpcom/tests/MoreTestingAtomList.h create mode 100755 xpcom/tests/MoreTestingAtoms.cpp create mode 100755 xpcom/tests/MoreTestingAtoms.h create mode 100644 xpcom/tests/TestStaticAtoms.cpp create mode 100644 xpcom/tests/TestingAtomList.h create mode 100755 xpcom/tests/TestingAtoms.cpp create mode 100755 xpcom/tests/TestingAtoms.h diff --git a/layout/build/nsLayoutStatics.cpp b/layout/build/nsLayoutStatics.cpp index 3b34ef03a752..a4efa0908fd1 100644 --- a/layout/build/nsLayoutStatics.cpp +++ b/layout/build/nsLayoutStatics.cpp @@ -288,6 +288,8 @@ nsLayoutStatics::Initialize() return rv; } + NS_SealStaticAtomTable(); + return NS_OK; } diff --git a/parser/html/Makefile.in b/parser/html/Makefile.in index 01fa368b0daf..a8f8cc112f54 100644 --- a/parser/html/Makefile.in +++ b/parser/html/Makefile.in @@ -52,6 +52,8 @@ EXPORTS = \ CPPSRCS = \ nsHtml5Atoms.cpp \ + nsHtml5Atom.cpp \ + nsHtml5AtomTable.cpp \ nsHtml5Parser.cpp \ nsHtml5AttributeName.cpp \ nsHtml5ElementName.cpp \ diff --git a/parser/html/nsHtml5Atom.cpp b/parser/html/nsHtml5Atom.cpp new file mode 100644 index 000000000000..41998bfe30cc --- /dev/null +++ b/parser/html/nsHtml5Atom.cpp @@ -0,0 +1,109 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is HTML Parser C++ Translator code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Henri Sivonen + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsHtml5Atom.h" + +nsHtml5Atom::nsHtml5Atom(const nsAString& aString) + : mData(aString) +{ +} + +nsHtml5Atom::~nsHtml5Atom() +{ +} + +NS_IMETHODIMP_(nsrefcnt) +nsHtml5Atom::AddRef() +{ + NS_NOTREACHED("Attempt to AddRef an nsHtml5Atom."); + return 2; +} + +NS_IMETHODIMP_(nsrefcnt) +nsHtml5Atom::Release() +{ + NS_NOTREACHED("Attempt to Release an nsHtml5Atom."); + return 1; +} + +NS_IMETHODIMP +nsHtml5Atom::QueryInterface(REFNSIID aIID, void** aInstancePtr) +{ + NS_NOTREACHED("Attempt to call QueryInterface an nsHtml5Atom."); + return NS_ERROR_UNEXPECTED; +} + +NS_IMETHODIMP +nsHtml5Atom::ToString(nsAString& aReturn) +{ + aReturn.Assign(mData); + return NS_OK; +} + +NS_IMETHODIMP +nsHtml5Atom::ToUTF8String(nsACString& aReturn) +{ + NS_NOTREACHED("Should not attempt to convert to an UTF-8 string."); + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP +nsHtml5Atom::GetUTF8String(const char **aReturn) +{ + NS_NOTREACHED("Should not attempt to get a UTF-8 string from nsHtml5Atom"); + return NS_ERROR_NOT_IMPLEMENTED; +} + +NS_IMETHODIMP_(PRBool) +nsHtml5Atom::IsStaticAtom() +{ + return PR_FALSE; +} + +NS_IMETHODIMP +nsHtml5Atom::Equals(const nsAString& aString, PRBool *aReturn) +{ + *aReturn = mData.Equals(aString); + return NS_OK; +} + +NS_IMETHODIMP +nsHtml5Atom::EqualsUTF8(const nsACString& aString, PRBool *aReturn) +{ + NS_NOTREACHED("Should not attempt to compare with an UTF-8 string."); + return NS_ERROR_NOT_IMPLEMENTED; +} diff --git a/parser/html/nsHtml5Atom.h b/parser/html/nsHtml5Atom.h new file mode 100644 index 000000000000..5f0abb7b18a2 --- /dev/null +++ b/parser/html/nsHtml5Atom.h @@ -0,0 +1,63 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is HTML Parser C++ Translator code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Henri Sivonen + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsHtml5Atom_h_ +#define nsHtml5Atom_h_ + +#include "nsIAtom.h" + +/** + * A dynamic atom implementation meant for use within the nsHtml5Tokenizer and + * nsHtml5TreeBuilder owned by one nsHtml5Parser or nsHtml5StreamParser + * instance. + * + * Usage is documented in nsHtml5AtomTable and nsIAtom. + */ +class nsHtml5Atom : public nsIAtom +{ + public: + NS_DECL_ISUPPORTS + NS_DECL_NSIATOM + + nsHtml5Atom(const nsAString& aString); + ~nsHtml5Atom(); + + private: + nsString mData; +}; + +#endif // nsHtml5Atom_h_ diff --git a/parser/html/nsHtml5AtomTable.cpp b/parser/html/nsHtml5AtomTable.cpp new file mode 100644 index 000000000000..c6bf53bb94be --- /dev/null +++ b/parser/html/nsHtml5AtomTable.cpp @@ -0,0 +1,89 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is HTML Parser C++ Translator code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Henri Sivonen + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "nsHtml5AtomTable.h" +#include "nsHtml5Atom.h" +#include "nsThreadUtils.h" + +nsHtml5AtomEntry::nsHtml5AtomEntry(KeyTypePointer aStr) + : nsStringHashKey(aStr) + , mAtom(new nsHtml5Atom(*aStr)) +{ +} + +nsHtml5AtomEntry::nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther) + : nsStringHashKey(aOther) + , mAtom(nsnull) +{ + NS_NOTREACHED("nsHtml5AtomTable is broken and tried to copy an entry"); +} + +nsHtml5AtomEntry::~nsHtml5AtomEntry() +{ +} + +nsHtml5AtomTable::nsHtml5AtomTable() +{ +#ifdef DEBUG + NS_GetMainThread(getter_AddRefs(mPermittedLookupThread)); +#endif +} + +nsHtml5AtomTable::~nsHtml5AtomTable() +{ +} + +nsIAtom* +nsHtml5AtomTable::GetAtom(const nsAString& aKey) +{ +#ifdef DEBUG + { + nsCOMPtr currentThread; + NS_GetCurrentThread(getter_AddRefs(currentThread)); + NS_ASSERTION(mPermittedLookupThread == currentThread, "Wrong thread!"); + } +#endif + nsIAtom* atom = NS_GetStaticAtom(aKey); + if (atom) { + return atom; + } + nsHtml5AtomEntry* entry = mTable.PutEntry(aKey); + if (!entry) { + return nsnull; + } + return entry->GetAtom(); +} diff --git a/parser/html/nsHtml5AtomTable.h b/parser/html/nsHtml5AtomTable.h new file mode 100644 index 000000000000..ed90932b05c6 --- /dev/null +++ b/parser/html/nsHtml5AtomTable.h @@ -0,0 +1,145 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is HTML Parser C++ Translator code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Henri Sivonen + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef nsHtml5AtomTable_h_ +#define nsHtml5AtomTable_h_ + +#include "nsHashKeys.h" +#include "nsTHashtable.h" +#include "nsAutoPtr.h" +#include "nsIAtom.h" +#include "nsIThread.h" + +class nsHtml5Atom; + +class nsHtml5AtomEntry : public nsStringHashKey +{ + public: + nsHtml5AtomEntry(KeyTypePointer aStr); + nsHtml5AtomEntry(const nsHtml5AtomEntry& aOther); + ~nsHtml5AtomEntry(); + inline nsHtml5Atom* GetAtom() { + return mAtom; + } + private: + nsAutoPtr mAtom; +}; + +/** + * nsHtml5AtomTable provides non-locking lookup and creation of atoms for + * nsHtml5Parser or nsHtml5StreamParser. + * + * The hashtable holds dynamically allocated atoms that are private to an + * instance of nsHtml5Parser or nsHtml5StreamParser. (Static atoms are used on + * interned nsHtml5ElementNames and interned nsHtml5AttributeNames. Also, when + * the doctype name is 'html', that identifier needs to be represented as a + * static atom.) + * + * Each instance of nsHtml5Parser has a single instance of nsHtml5AtomTable, + * and each instance of nsHtml5StreamParser has a single instance of + * nsHtml5AtomTable. Dynamic atoms obtained from an nsHtml5AtomTable are valid + * for == comparison with each other or with atoms declared in nsHtml5Atoms + * within the nsHtml5Tokenizer and the nsHtml5TreeBuilder instances owned by + * the same nsHtml5Parser/nsHtml5StreamParser instance that owns the + * nsHtml5AtomTable instance. + * + * Dynamic atoms (atoms whose IsStaticAtom() returns PR_FALSE) obtained from + * nsHtml5AtomTable must be re-obtained from another atom table when there's a + * need to migrate atoms from an nsHtml5Parser to its nsHtml5StreamParser + * (re-obtain from the other nsHtml5AtomTable), from an nsHtml5Parser to its + * owner nsHtml5Parser (re-obtain from the other nsHtml5AtomTable) or from the + * parser to the DOM (re-obtain from the application-wide atom table). To + * re-obtain an atom from another atom table, obtain a string from the atom + * using ToString(nsAString&) and look up an atom in the other table using that + * string. + * + * An instance of nsHtml5AtomTable that belongs to an nsHtml5Parser is only + * accessed from the main thread. An instance of nsHtml5AtomTable that belongs + * to an nsHtml5StreamParser is accessed both from the main thread and from the + * thread that executes the runnables of the nsHtml5StreamParser instance. + * However, the threads never access the nsHtml5AtomTable instance concurrently + * in the nsHtml5StreamParser case. + * + * Methods on the atoms obtained from nsHtml5AtomTable may be called on any + * thread, although they only need to be called on the main thread or on the + * thread working for the nsHtml5StreamParser when nsHtml5AtomTable belongs to + * an nsHtml5StreamParser. + * + * Dynamic atoms obtained from nsHtml5AtomTable are deleted when the + * nsHtml5AtomTable itself is destructed, which happens when the owner + * nsHtml5Parser or nsHtml5StreamParser is destructed. + */ +class nsHtml5AtomTable +{ + public: + nsHtml5AtomTable(); + ~nsHtml5AtomTable(); + + /** + * Must be called after the constructor before use. Returns PR_TRUE + * when successful and PR_FALSE on OOM failure. + */ + inline PRBool Init() { + return mTable.Init(); + } + + /** + * Obtains the atom for the given string in the scope of this atom table. + */ + nsIAtom* GetAtom(const nsAString& aKey); + + /** + * Empties the table. + */ + void Clear() { + mTable.Clear(); + } + +#ifdef DEBUG + void SetPermittedLookupThread(nsIThread* aThread) { + mPermittedLookupThread = aThread; + } +#endif + + private: + nsTHashtable mTable; +#ifdef DEBUG + nsCOMPtr mPermittedLookupThread; +#endif +}; + +#endif // nsHtml5AtomTable_h_ diff --git a/xpcom/ds/nsAtomTable.cpp b/xpcom/ds/nsAtomTable.cpp index 695e9ade47e6..49a953736e28 100644 --- a/xpcom/ds/nsAtomTable.cpp +++ b/xpcom/ds/nsAtomTable.cpp @@ -45,6 +45,8 @@ #include "pldhash.h" #include "prenv.h" #include "nsThreadUtils.h" +#include "nsDataHashtable.h" +#include "nsHashKeys.h" #define PL_ARENA_CONST_ALIGN_MASK 3 #include "plarena.h" @@ -66,6 +68,17 @@ static PLDHashTable gAtomTable; static PLArenaPool* gStaticAtomArena = 0; +/** + * A hashtable of static atoms that existed at app startup. This hashtable helps + * nsHtml5AtomTable. + */ +static nsDataHashtable* gStaticAtomTable = 0; + +/** + * Whether it is still OK to add atoms to gStaticAtomTable. + */ +static PRBool gStaticAtomTableSealed = PR_FALSE; + class nsStaticAtomWrapper : public nsIAtom { public: @@ -401,6 +414,8 @@ void PromoteToPermanent(AtomImpl* aAtom) void NS_PurgeAtomTable() { + delete gStaticAtomTable; + if (gAtomTable.ops) { #ifdef DEBUG const char *dumpAtomLeaks = PR_GetEnv("MOZ_DUMP_ATOM_LEAKS"); @@ -549,6 +564,12 @@ AtomImpl::Equals(const nsAString& aString, PRBool* aResult) return NS_OK; } +NS_IMETHODIMP_(PRBool) +AtomImpl::IsStaticAtom() +{ + return PR_FALSE; +} + //---------------------------------------------------------------------- // wrapper class for the nsStaticAtom struct @@ -609,6 +630,13 @@ nsStaticAtomWrapper::Equals(const nsAString& aString, PRBool* aResult) aString) == 0; return NS_OK; } + +NS_IMETHODIMP_(PRBool) +nsStaticAtomWrapper::IsStaticAtom() +{ + return PR_TRUE; +} + //---------------------------------------------------------------------- static nsStaticAtomWrapper* @@ -668,9 +696,19 @@ GetAtomHashEntry(const PRUnichar* aString, PRUint32 aLength) NS_COM nsresult NS_RegisterStaticAtoms(const nsStaticAtom* aAtoms, PRUint32 aAtomCount) { - // this does two things: + // this does three things: // 1) wraps each static atom in a wrapper, if necessary // 2) initializes the address pointed to by each mBits slot + // 3) puts the atom into the static atom table as well + + if (!gStaticAtomTable && !gStaticAtomTableSealed) { + gStaticAtomTable = new nsDataHashtable(); + if (!gStaticAtomTable || !gStaticAtomTable->Init()) { + delete gStaticAtomTable; + gStaticAtomTable = nsnull; + return NS_ERROR_OUT_OF_MEMORY; + } + } for (PRUint32 i=0; iSetStaticAtomWrapper(atom); if (aAtoms[i].mAtom) *aAtoms[i].mAtom = atom; + + if (!gStaticAtomTableSealed) { + nsAutoString key; + atom->ToString(key); + gStaticAtomTable->Put(key, atom); + } } } return NS_OK; @@ -827,3 +871,20 @@ NS_GetNumberOfAtoms(void) return gAtomTable.entryCount; } +NS_COM nsIAtom* +NS_GetStaticAtom(const nsAString& aUTF16String) +{ + NS_PRECONDITION(gStaticAtomTable, "Static atom table not created yet."); + NS_PRECONDITION(gStaticAtomTableSealed, "Static atom table not sealed yet."); + nsIAtom* atom; + if (!gStaticAtomTable->Get(aUTF16String, &atom)) { + atom = nsnull; + } + return atom; +} + +NS_COM void +NS_SealStaticAtomTable() +{ + gStaticAtomTableSealed = PR_TRUE; +} diff --git a/xpcom/ds/nsIAtom.idl b/xpcom/ds/nsIAtom.idl index b4fb6b91a38b..3c865c3f95e7 100644 --- a/xpcom/ds/nsIAtom.idl +++ b/xpcom/ds/nsIAtom.idl @@ -48,7 +48,7 @@ * pointer identity. */ -[scriptable, uuid(3d1b15b0-93b4-11d1-895b-006008911b81)] +[scriptable, uuid(fbffe332-0856-421a-9b83-aaed0081fc40)] interface nsIAtom : nsISupports { /** @@ -85,6 +85,10 @@ interface nsIAtom : nsISupports } %} + /** + * Returns true if the atom is static and false otherwise. + */ + [noscript, notxpcom] boolean isStaticAtom(); }; @@ -158,4 +162,15 @@ inline already_AddRefed do_GetPermanentAtom(const nsAString& aUTF16Stri */ extern NS_COM nsrefcnt NS_GetNumberOfAtoms(void); +/** + * Return a pointer for a static atom for the string or null if there's + * no static atom for this string. + */ +extern NS_COM nsIAtom* NS_GetStaticAtom(const nsAString& aUTF16String); + +/** + * Seal the static atom table + */ +extern NS_COM void NS_SealStaticAtomTable(); + %} diff --git a/xpcom/tests/Makefile.in b/xpcom/tests/Makefile.in index 79b42dfbddd5..4e00a8b34e8f 100644 --- a/xpcom/tests/Makefile.in +++ b/xpcom/tests/Makefile.in @@ -113,6 +113,7 @@ CPP_UNIT_TESTS += \ TestThreads.cpp \ TestTimeStamp.cpp \ TestXPIDLString.cpp \ + TestStaticAtoms.cpp \ $(NULL) endif diff --git a/xpcom/tests/MoreTestingAtomList.h b/xpcom/tests/MoreTestingAtomList.h new file mode 100644 index 000000000000..109bb3bc701a --- /dev/null +++ b/xpcom/tests/MoreTestingAtomList.h @@ -0,0 +1,39 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is HTML Parser C++ Translator code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Henri Sivonen + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +MORE_TESTING_ATOM(qux, "qux") +MORE_TESTING_ATOM(quux, "quux") diff --git a/xpcom/tests/MoreTestingAtoms.cpp b/xpcom/tests/MoreTestingAtoms.cpp new file mode 100755 index 000000000000..6bd307c5e127 --- /dev/null +++ b/xpcom/tests/MoreTestingAtoms.cpp @@ -0,0 +1,58 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2006 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "MoreTestingAtoms.h" +#include "nsStaticAtom.h" +#include "nsMemory.h" + +// define storage for all atoms +#define MORE_TESTING_ATOM(_name, _value) nsIAtom* MoreTestingAtoms::_name; +#include "MoreTestingAtomList.h" +#undef MORE_TESTING_ATOM + +static const nsStaticAtom MoreTestingAtoms_info[] = { + +#define MORE_TESTING_ATOM(name_, value_) { value_, &MoreTestingAtoms::name_ }, +#include "MoreTestingAtomList.h" +#undef MORE_TESTING_ATOM +}; + +void MoreTestingAtoms::AddRefAtoms() +{ + NS_RegisterStaticAtoms(MoreTestingAtoms_info, + NS_ARRAY_LENGTH(MoreTestingAtoms_info)); +} diff --git a/xpcom/tests/MoreTestingAtoms.h b/xpcom/tests/MoreTestingAtoms.h new file mode 100755 index 000000000000..b01036c274bc --- /dev/null +++ b/xpcom/tests/MoreTestingAtoms.h @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2006 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef MoreTestingAtoms_h_ +#define MoreTestingAtoms_h_ + +#include "nsIAtom.h" + +class MoreTestingAtoms { + public: + static void AddRefAtoms(); +#define MORE_TESTING_ATOM(_name, _value) static nsIAtom* _name; +#include "MoreTestingAtomList.h" +#undef MORE_TESTING_ATOM +}; + +#endif /* MoreTestingAtoms_h_ */ diff --git a/xpcom/tests/TestStaticAtoms.cpp b/xpcom/tests/TestStaticAtoms.cpp new file mode 100644 index 000000000000..987088447726 --- /dev/null +++ b/xpcom/tests/TestStaticAtoms.cpp @@ -0,0 +1,108 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is HTML Parser C++ Translator code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Henri Sivonen + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "TestHarness.h" +#include "TestingAtoms.cpp" +#include "MoreTestingAtoms.cpp" + +int main(int argc, char** argv) +{ + ScopedXPCOM xpcom("TestStaticAtoms"); + if (xpcom.failed()) { + return 1; + } + + TestingAtoms::AddRefAtoms(); + + NS_SealStaticAtomTable(); + + nsCOMPtr atom = do_GetAtom("foo"); + if (!atom) { + fail("Didn't get an atom for foo."); + return 1; + } + if (atom->IsStaticAtom()) { + passed("foo is a static atom"); + } else { + fail("foo is not a static atom."); + return 1; + } + if (atom == TestingAtoms::foo) { + passed("foo is the right pointer"); + } else { + fail("foo was not the right pointer"); + return 1; + } + nsIAtom* staticAtom = NS_GetStaticAtom(NS_LITERAL_STRING("foo")); + if (!staticAtom) { + fail("Did not get a static atom for foo"); + return 1; + } + + if (atom == staticAtom) { + passed("do_GetAtom and NS_GetStaticAtom returned the same atom."); + } else { + fail("do_GetAtom and NS_GetStaticAtom returned different atoms."); + return 1; + } + + MoreTestingAtoms::AddRefAtoms(); + + atom = do_GetAtom("qux"); + if (!atom) { + fail("Didn't get an atom for qux."); + return 1; + } + if (atom->IsStaticAtom()) { + passed("qux is a static atom"); + } else { + fail("qux is not a static atom."); + return 1; + } + if (atom == MoreTestingAtoms::qux) { + passed("qux is the right pointer"); + } else { + fail("qux was not the right pointer"); + return 1; + } + staticAtom = NS_GetStaticAtom(NS_LITERAL_STRING("qux")); + if (staticAtom) { + fail("Got an atom for qux. The static atom table was not sealed properly."); + return 1; + } + return 0; +} diff --git a/xpcom/tests/TestingAtomList.h b/xpcom/tests/TestingAtomList.h new file mode 100644 index 000000000000..3b5048e1e1a5 --- /dev/null +++ b/xpcom/tests/TestingAtomList.h @@ -0,0 +1,39 @@ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is HTML Parser C++ Translator code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2009 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * Henri Sivonen + * + * Alternatively, the contents of this file may be used under the terms of + * either the GNU General Public License Version 2 or later (the "GPL"), or + * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +TESTING_ATOM(foo, "foo") +TESTING_ATOM(bar, "bar") diff --git a/xpcom/tests/TestingAtoms.cpp b/xpcom/tests/TestingAtoms.cpp new file mode 100755 index 000000000000..79a8ca449747 --- /dev/null +++ b/xpcom/tests/TestingAtoms.cpp @@ -0,0 +1,57 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2006 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#include "TestingAtoms.h" +#include "nsStaticAtom.h" +#include "nsMemory.h" + +// define storage for all atoms +#define TESTING_ATOM(_name, _value) nsIAtom* TestingAtoms::_name; +#include "TestingAtomList.h" +#undef TESTING_ATOM + +static const nsStaticAtom TestingAtoms_info[] = { + +#define TESTING_ATOM(name_, value_) { value_, &TestingAtoms::name_ }, +#include "TestingAtomList.h" +#undef TESTING_ATOM +}; + +void TestingAtoms::AddRefAtoms() +{ + NS_RegisterStaticAtoms(TestingAtoms_info, NS_ARRAY_LENGTH(TestingAtoms_info)); +} diff --git a/xpcom/tests/TestingAtoms.h b/xpcom/tests/TestingAtoms.h new file mode 100755 index 000000000000..db0e8b901e85 --- /dev/null +++ b/xpcom/tests/TestingAtoms.h @@ -0,0 +1,51 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* ***** BEGIN LICENSE BLOCK ***** + * Version: MPL 1.1/GPL 2.0/LGPL 2.1 + * + * The contents of this file are subject to the Mozilla Public License Version + * 1.1 (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License + * for the specific language governing rights and limitations under the + * License. + * + * The Original Code is mozilla.org code. + * + * The Initial Developer of the Original Code is + * Mozilla Foundation. + * Portions created by the Initial Developer are Copyright (C) 2006 + * the Initial Developer. All Rights Reserved. + * + * Contributor(s): + * + * Alternatively, the contents of this file may be used under the terms of + * either of the GNU General Public License Version 2 or later (the "GPL"), + * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), + * in which case the provisions of the GPL or the LGPL are applicable instead + * of those above. If you wish to allow use of your version of this file only + * under the terms of either the GPL or the LGPL, and not to allow others to + * use your version of this file under the terms of the MPL, indicate your + * decision by deleting the provisions above and replace them with the notice + * and other provisions required by the GPL or the LGPL. If you do not delete + * the provisions above, a recipient may use your version of this file under + * the terms of any one of the MPL, the GPL or the LGPL. + * + * ***** END LICENSE BLOCK ***** */ + +#ifndef TestingAtoms_h_ +#define TestingAtoms_h_ + +#include "nsIAtom.h" + +class TestingAtoms { + public: + static void AddRefAtoms(); +#define TESTING_ATOM(_name, _value) static nsIAtom* _name; +#include "TestingAtomList.h" +#undef TESTING_ATOM +}; + +#endif /* TestingAtoms_h_ */