From f32ed5397dcbf9952d681642189205b4934ed10a Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 18 Jul 2012 17:38:10 -0700 Subject: [PATCH] Bug 647367 - Sequester jshash.{h,cpp} in js/jsd/ (attempt 2). r=luke. --HG-- rename : js/src/jshash.cpp => js/jsd/jshash.cpp rename : js/src/jshash.h => js/jsd/jshash.h --- js/jsd/Makefile.in | 4 +++- js/{src => jsd}/jshash.cpp | 26 +++++++++++++------------- js/{src => jsd}/jshash.h | 24 ++++++++++++------------ js/src/Makefile.in | 2 -- js/src/jsatom.cpp | 7 ------- js/src/jsatom.h | 25 ++++--------------------- js/src/jsgc.cpp | 1 - js/src/jsobj.cpp | 1 - js/src/jsobj.h | 1 - js/src/jsscope.cpp | 3 ++- js/src/jsscript.h | 2 +- js/src/jsstr.cpp | 2 +- js/src/jstypedarray.cpp | 1 - js/xpconnect/src/XPCMaps.cpp | 12 ++++++------ mfbt/HashFunctions.h | 8 ++++++++ 15 files changed, 50 insertions(+), 69 deletions(-) rename js/{src => jsd}/jshash.cpp (97%) rename js/{src => jsd}/jshash.h (91%) diff --git a/js/jsd/Makefile.in b/js/jsd/Makefile.in index 9fcbb08fd3df..9fdcd5aa60d4 100644 --- a/js/jsd/Makefile.in +++ b/js/jsd/Makefile.in @@ -17,7 +17,9 @@ include $(DEPTH)/config/autoconf.mk MODULE = jsdebug LIBRARY_NAME = jsd DIRS = idl -CPPSRCS = jsd_xpc.cpp +CPPSRCS = \ + jsd_xpc.cpp \ + jshash.cpp IS_COMPONENT = 1 LIBXUL_LIBRARY = 1 diff --git a/js/src/jshash.cpp b/js/jsd/jshash.cpp similarity index 97% rename from js/src/jshash.cpp rename to js/jsd/jshash.cpp index c634feedd599..dbd7de3a469e 100644 --- a/js/src/jshash.cpp +++ b/js/jsd/jshash.cpp @@ -61,7 +61,7 @@ static JSHashAllocOps defaultHashAllocOps = { DefaultAllocEntry, DefaultFreeEntry }; -JS_PUBLIC_API(JSHashTable *) +JSHashTable * JS_NewHashTable(uint32_t n, JSHashFunction keyHash, JSHashComparator keyCompare, JSHashComparator valueCompare, JSHashAllocOps *allocOps, void *allocPriv) @@ -101,7 +101,7 @@ JS_NewHashTable(uint32_t n, JSHashFunction keyHash, return ht; } -JS_PUBLIC_API(void) +void JS_HashTableDestroy(JSHashTable *ht) { uint32_t i, n; @@ -133,7 +133,7 @@ JS_HashTableDestroy(JSHashTable *ht) #define BUCKET_HEAD(ht, keyHash) \ (&(ht)->buckets[((keyHash) * JS_GOLDEN_RATIO) >> (ht)->shift]) -JS_PUBLIC_API(JSHashEntry **) +JSHashEntry ** JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key) { JSHashEntry *he, **hep, **hep0; @@ -212,7 +212,7 @@ Resize(JSHashTable *ht, uint32_t newshift) return JS_TRUE; } -JS_PUBLIC_API(JSHashEntry *) +JSHashEntry * JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **&hep, JSHashNumber keyHash, const void *key, void *value) { @@ -243,7 +243,7 @@ JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **&hep, return he; } -JS_PUBLIC_API(JSHashEntry *) +JSHashEntry * JS_HashTableAdd(JSHashTable *ht, const void *key, void *value) { JSHashNumber keyHash; @@ -265,7 +265,7 @@ JS_HashTableAdd(JSHashTable *ht, const void *key, void *value) return JS_HashTableRawAdd(ht, hep, keyHash, key, value); } -JS_PUBLIC_API(void) +void JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he) { uint32_t n; @@ -283,7 +283,7 @@ JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he) } } -JS_PUBLIC_API(JSBool) +JSBool JS_HashTableRemove(JSHashTable *ht, const void *key) { JSHashNumber keyHash; @@ -299,7 +299,7 @@ JS_HashTableRemove(JSHashTable *ht, const void *key) return JS_TRUE; } -JS_PUBLIC_API(void *) +void * JS_HashTableLookup(JSHashTable *ht, const void *key) { JSHashNumber keyHash; @@ -318,7 +318,7 @@ JS_HashTableLookup(JSHashTable *ht, const void *key) ** entry found. Stop if "f" says to (return value & JS_ENUMERATE_STOP). ** Return a count of the number of elements scanned. */ -JS_PUBLIC_API(int) +int JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg) { JSHashEntry *he, **hep, **bucket; @@ -367,7 +367,7 @@ out: #ifdef JS_HASHMETER #include -JS_PUBLIC_API(void) +void JS_HashTableDumpMeter(JSHashTable *ht, JSHashEnumerator dump, FILE *fp) { double sqsum, mean, sigma; @@ -413,7 +413,7 @@ JS_HashTableDumpMeter(JSHashTable *ht, JSHashEnumerator dump, FILE *fp) } #endif /* JS_HASHMETER */ -JS_PUBLIC_API(int) +int JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp) { int count; @@ -425,7 +425,7 @@ JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp) return count; } -JS_PUBLIC_API(JSHashNumber) +JSHashNumber JS_HashString(const void *key) { JSHashNumber h; @@ -437,7 +437,7 @@ JS_HashString(const void *key) return h; } -JS_PUBLIC_API(int) +int JS_CompareValues(const void *v1, const void *v2) { return v1 == v2; diff --git a/js/src/jshash.h b/js/jsd/jshash.h similarity index 91% rename from js/src/jshash.h rename to js/jsd/jshash.h index 2b9f8a9e3968..5c44b7ba964a 100644 --- a/js/src/jshash.h +++ b/js/jsd/jshash.h @@ -70,49 +70,49 @@ struct JSHashTable { * Create a new hash table. * If allocOps is null, use default allocator ops built on top of malloc(). */ -extern JS_PUBLIC_API(JSHashTable *) +extern JSHashTable * JS_NewHashTable(uint32_t n, JSHashFunction keyHash, JSHashComparator keyCompare, JSHashComparator valueCompare, JSHashAllocOps *allocOps, void *allocPriv); -extern JS_PUBLIC_API(void) +extern void JS_HashTableDestroy(JSHashTable *ht); /* Low level access methods */ -extern JS_PUBLIC_API(JSHashEntry **) +extern JSHashEntry ** JS_HashTableRawLookup(JSHashTable *ht, JSHashNumber keyHash, const void *key); #ifdef __cplusplus -extern JS_PUBLIC_API(JSHashEntry *) +extern JSHashEntry * JS_HashTableRawAdd(JSHashTable *ht, JSHashEntry **&hep, JSHashNumber keyHash, const void *key, void *value); #endif -extern JS_PUBLIC_API(void) +extern void JS_HashTableRawRemove(JSHashTable *ht, JSHashEntry **hep, JSHashEntry *he); /* Higher level access methods */ -extern JS_PUBLIC_API(JSHashEntry *) +extern JSHashEntry * JS_HashTableAdd(JSHashTable *ht, const void *key, void *value); -extern JS_PUBLIC_API(JSBool) +extern JSBool JS_HashTableRemove(JSHashTable *ht, const void *key); -extern JS_PUBLIC_API(int) +extern int JS_HashTableEnumerateEntries(JSHashTable *ht, JSHashEnumerator f, void *arg); -extern JS_PUBLIC_API(void *) +extern void * JS_HashTableLookup(JSHashTable *ht, const void *key); -extern JS_PUBLIC_API(int) +extern int JS_HashTableDump(JSHashTable *ht, JSHashEnumerator dump, FILE *fp); /* General-purpose C string hash function. */ -extern JS_PUBLIC_API(JSHashNumber) +extern JSHashNumber JS_HashString(const void *key); /* Stub function just returns v1 == v2 */ -extern JS_PUBLIC_API(int) +extern int JS_CompareValues(const void *v1, const void *v2); JS_END_EXTERN_C diff --git a/js/src/Makefile.in b/js/src/Makefile.in index d45e185888ba..c06bacf06aec 100644 --- a/js/src/Makefile.in +++ b/js/src/Makefile.in @@ -82,7 +82,6 @@ CPPSRCS = \ jsfun.cpp \ jsgc.cpp \ jscrashreport.cpp \ - jshash.cpp \ jsinfer.cpp \ jsinterp.cpp \ jsiter.cpp \ @@ -162,7 +161,6 @@ INSTALLED_HEADERS = \ jsdhash.h \ jsfriendapi.h \ jsgc.h \ - jshash.h \ jslock.h \ json.h \ jsproxy.h \ diff --git a/js/src/jsatom.cpp b/js/src/jsatom.cpp index ea50d6e8ee37..fea73a5895a9 100644 --- a/js/src/jsatom.cpp +++ b/js/src/jsatom.cpp @@ -15,7 +15,6 @@ #include "jstypes.h" #include "jsutil.h" -#include "jshash.h" #include "jsprf.h" #include "jsapi.h" #include "jsatom.h" @@ -43,12 +42,6 @@ using namespace js::gc; const size_t JSAtomState::commonAtomsOffset = offsetof(JSAtomState, emptyAtom); -/* - * ATOM_HASH assumes that JSHashNumber is 32-bit even on 64-bit systems. - */ -JS_STATIC_ASSERT(sizeof(JSHashNumber) == 4); -JS_STATIC_ASSERT(sizeof(JSAtom *) == JS_BYTES_PER_WORD); - const char * js_AtomToPrintableString(JSContext *cx, JSAtom *atom, JSAutoByteString *bytes) { diff --git a/js/src/jsatom.h b/js/src/jsatom.h index c1a27f85d611..30872aa6ea28 100644 --- a/js/src/jsatom.h +++ b/js/src/jsatom.h @@ -12,12 +12,12 @@ #include "jsalloc.h" #include "jsapi.h" #include "jsprvtd.h" -#include "jshash.h" #include "jspubtd.h" #include "jslock.h" #include "gc/Barrier.h" #include "js/HashTable.h" +#include "mozilla/HashFunctions.h" struct JSIdArray { int length; @@ -83,23 +83,15 @@ JSID_TO_ATOM(jsid id) return (JSAtom *)JSID_TO_STRING(id); } -JS_STATIC_ASSERT(sizeof(JSHashNumber) == 4); +JS_STATIC_ASSERT(sizeof(js::HashNumber) == 4); JS_STATIC_ASSERT(sizeof(jsid) == JS_BYTES_PER_WORD); namespace js { -static JS_ALWAYS_INLINE JSHashNumber +static JS_ALWAYS_INLINE js::HashNumber HashId(jsid id) { - JSHashNumber n = -#if JS_BYTES_PER_WORD == 4 - JSHashNumber(JSID_BITS(id)); -#elif JS_BYTES_PER_WORD == 8 - JSHashNumber(JSID_BITS(id)) ^ JSHashNumber(JSID_BITS(id) >> 32); -#else -# error "Unsupported configuration" -#endif - return n * JS_GOLDEN_RATIO; + return HashGeneric(JSID_BITS(id)); } static JS_ALWAYS_INLINE Value @@ -135,15 +127,6 @@ struct DefaultHasher } -#if JS_BYTES_PER_WORD == 4 -# define ATOM_HASH(atom) ((JSHashNumber)(atom) >> 2) -#elif JS_BYTES_PER_WORD == 8 -# define ATOM_HASH(atom) (((JSHashNumber)(uintptr_t)(atom) >> 3) ^ \ - (JSHashNumber)((uintptr_t)(atom) >> 32)) -#else -# error "Unsupported configuration" -#endif - /* * Return a printable, lossless char[] representation of a string-type atom. * The lifetime of the result matches the lifetime of bytes. diff --git a/js/src/jsgc.cpp b/js/src/jsgc.cpp index 8952a2bb86b3..92c6b1aecd4f 100644 --- a/js/src/jsgc.cpp +++ b/js/src/jsgc.cpp @@ -44,7 +44,6 @@ #include "jstypes.h" #include "jsutil.h" -#include "jshash.h" #include "jsclist.h" #include "jsprf.h" #include "jsapi.h" diff --git a/js/src/jsobj.cpp b/js/src/jsobj.cpp index 607e58210b30..94f9ade0eea3 100644 --- a/js/src/jsobj.cpp +++ b/js/src/jsobj.cpp @@ -15,7 +15,6 @@ #include "jstypes.h" #include "jsutil.h" -#include "jshash.h" #include "jsprf.h" #include "jsapi.h" #include "jsarray.h" diff --git a/js/src/jsobj.h b/js/src/jsobj.h index cdb7ad34efd3..47d9c9762f5d 100644 --- a/js/src/jsobj.h +++ b/js/src/jsobj.h @@ -21,7 +21,6 @@ #include "jsclass.h" #include "jsfriendapi.h" #include "jsinfer.h" -#include "jshash.h" #include "jspubtd.h" #include "jsprvtd.h" #include "jslock.h" diff --git a/js/src/jsscope.cpp b/js/src/jsscope.cpp index 00d13f92c5e6..5d66dbf3e675 100644 --- a/js/src/jsscope.cpp +++ b/js/src/jsscope.cpp @@ -24,6 +24,7 @@ #include "jsscope.h" #include "jsstr.h" +#include "js/HashTable.h" #include "js/MemoryMetrics.h" #include "jsatominlines.h" @@ -145,7 +146,7 @@ Shape::hashify(JSContext *cx) Shape ** ShapeTable::search(jsid id, bool adding) { - JSHashNumber hash0, hash1, hash2; + js::HashNumber hash0, hash1, hash2; int sizeLog2; Shape *stored, *shape, **spp, **firstRemoved; uint32_t sizeMask; diff --git a/js/src/jsscript.h b/js/src/jsscript.h index e8585e3d809f..4b1e4d41ab58 100644 --- a/js/src/jsscript.h +++ b/js/src/jsscript.h @@ -1165,7 +1165,7 @@ struct ScriptFilenameEntry struct ScriptFilenameHasher { typedef const char *Lookup; - static HashNumber hash(const char *l) { return JS_HashString(l); } + static HashNumber hash(const char *l) { return mozilla::HashString(l); } static bool match(const ScriptFilenameEntry *e, const char *l) { return strcmp(e->filename, l) == 0; } diff --git a/js/src/jsstr.cpp b/js/src/jsstr.cpp index 142febc91819..529ec3240ab8 100644 --- a/js/src/jsstr.cpp +++ b/js/src/jsstr.cpp @@ -23,7 +23,6 @@ #include #include "jstypes.h" #include "jsutil.h" -#include "jshash.h" #include "jsprf.h" #include "jsapi.h" #include "jsarray.h" @@ -42,6 +41,7 @@ #include "jsversion.h" #include "builtin/RegExp.h" +#include "js/HashTable.h" #include "vm/GlobalObject.h" #include "vm/NumericConversions.h" #include "vm/RegExpObject.h" diff --git a/js/src/jstypedarray.cpp b/js/src/jstypedarray.cpp index 174f9d83fbe2..c387f9ab85c8 100644 --- a/js/src/jstypedarray.cpp +++ b/js/src/jstypedarray.cpp @@ -9,7 +9,6 @@ #include "mozilla/FloatingPoint.h" #include "jstypes.h" #include "jsutil.h" -#include "jshash.h" #include "jsprf.h" #include "jsapi.h" #include "jsarray.h" diff --git a/js/xpconnect/src/XPCMaps.cpp b/js/xpconnect/src/XPCMaps.cpp index fab200a42445..9ac523bd249c 100644 --- a/js/xpconnect/src/XPCMaps.cpp +++ b/js/xpconnect/src/XPCMaps.cpp @@ -8,7 +8,7 @@ #include "xpcprivate.h" -#include "jshash.h" +#include "js/HashTable.h" /***************************************************************************/ // static shared... @@ -19,7 +19,7 @@ static JSDHashNumber HashIIDPtrKey(JSDHashTable *table, const void *key) { - return *((JSHashNumber*)key); + return *((js::HashNumber*)key); } static JSBool @@ -56,7 +56,7 @@ HashNativeKey(JSDHashTable *table, const void *key) NS_ASSERTION(Addition, "bad key"); // This would be an XOR like below. // But "0 ^ x == x". So it does not matter. - h = (JSHashNumber) NS_PTR_TO_INT32(Addition) >> 2; + h = (js::HashNumber) NS_PTR_TO_INT32(Addition) >> 2; } else { XPCNativeInterface** Current = Set->GetInterfaceArray(); PRUint16 count = Set->GetInterfaceCount(); @@ -64,13 +64,13 @@ HashNativeKey(JSDHashTable *table, const void *key) count++; for (PRUint16 i = 0; i < count; i++) { if (i == Position) - h ^= (JSHashNumber) NS_PTR_TO_INT32(Addition) >> 2; + h ^= (js::HashNumber) NS_PTR_TO_INT32(Addition) >> 2; else - h ^= (JSHashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2; + h ^= (js::HashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2; } } else { for (PRUint16 i = 0; i < count; i++) - h ^= (JSHashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2; + h ^= (js::HashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2; } } diff --git a/mfbt/HashFunctions.h b/mfbt/HashFunctions.h index 7a19749e556d..badfc3c80861 100644 --- a/mfbt/HashFunctions.h +++ b/mfbt/HashFunctions.h @@ -179,6 +179,14 @@ AddToHash(uint32_t hash, A* a) return detail::AddUintptrToHash(hash, uintptr_t(a)); } +template<> +MOZ_WARN_UNUSED_RESULT +inline uint32_t +AddToHash(uint32_t hash, uintptr_t a) +{ + return detail::AddUintptrToHash(hash, a); +} + template MOZ_WARN_UNUSED_RESULT uint32_t