From ee376bf1fa37ff24e32429dc8fd5315dfc1932ec Mon Sep 17 00:00:00 2001 From: Jon Coppeard Date: Tue, 18 Jun 2013 11:00:37 +0100 Subject: [PATCH] Bug 877762 - GC: Post-barrier cycle collector participants - 3 Add hashtable type that stores Heap values r=jlebar --- xpcom/glue/Makefile.in | 1 + xpcom/glue/nsJSThingHashtable.h | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 xpcom/glue/nsJSThingHashtable.h diff --git a/xpcom/glue/Makefile.in b/xpcom/glue/Makefile.in index 31679d606868..87bccd39b32b 100644 --- a/xpcom/glue/Makefile.in +++ b/xpcom/glue/Makefile.in @@ -46,6 +46,7 @@ SDK_HEADERS = \ nsISupportsUtils.h \ nsIWeakReferenceUtils.h \ nsInterfaceHashtable.h \ + nsJSThingHashtable.h \ nsMemory.h \ nsQuickSort.h \ nsRefPtrHashtable.h \ diff --git a/xpcom/glue/nsJSThingHashtable.h b/xpcom/glue/nsJSThingHashtable.h new file mode 100644 index 000000000000..6974b5d3476e --- /dev/null +++ b/xpcom/glue/nsJSThingHashtable.h @@ -0,0 +1,58 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef nsJSThingHashtable_h__ +#define nsJSThingHashtable_h__ + +#include "nsHashKeys.h" +#include "nsBaseHashtable.h" + +namespace JS { +template +class Heap; +} /* namespace JS */ + +/** + * A wrapper for hash keys that sets ALLOW_MEMMOVE to false. + * + * This is used in the implementation of nsJSThingHashtable and is not intended + * to be used directly. + * + * It is necessary for hash tables containing JS::Heap values as these must + * be copied rather than memmoved. + */ +template +class nsHashKeyDisallowMemmove : public T +{ + public: + nsHashKeyDisallowMemmove(const T& key) : T(key) {} + enum { ALLOW_MEMMOVE = false }; +}; + + +/** + * Templated hashtable class for use on the heap where the values are JS GC things. + * + * Storing JS GC thing pointers on the heap requires wrapping them in a + * JS::Heap, and this class takes care of that while presenting an interface + * in terms of the wrapped type T. + * + * For example, to store a hashtable mapping strings to JSObject pointers, you + * can declare a data member like this: + * + * nsJSThingHashtable mStringToObjectMap; + * + * See nsBaseHashtable for complete declaration + * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h + * for a complete specification. + * @param DataType the datatype being wrapped, must be a JS GC thing. + * @see nsInterfaceHashtable, nsClassHashtable + */ +template +class nsJSThingHashtable : + public nsBaseHashtable, JS::Heap, DataType> +{ }; + +#endif // nsJSThingHashtable_h__