Bug 1290587, part 2 - Make XPCNativeSetKey hashing a method. r=mrbkap draft
authorAndrew McCreight <continuation@gmail.com>
Fri, 29 Jul 2016 10:42:05 -0700
changeset 394784 6f87dbf7fd6dc2fd9813deabadde7243851f0f9c
parent 394783 974ec5906fda1f907f9f6baa2e24287524369502
child 394785 045ff15d1b44eb4d4d1b2083386c3a3ed2161920
push id24631
push userbmo:continuation@gmail.com
push dateSun, 31 Jul 2016 18:47:25 +0000
reviewersmrbkap
bugs1290587
milestone50.0a1
Bug 1290587, part 2 - Make XPCNativeSetKey hashing a method. r=mrbkap MozReview-Commit-ID: 67XbmFN4ThW
js/xpconnect/src/XPCMaps.cpp
js/xpconnect/src/XPCWrappedNativeInfo.cpp
js/xpconnect/src/xpcprivate.h
--- a/js/xpconnect/src/XPCMaps.cpp
+++ b/js/xpconnect/src/XPCMaps.cpp
@@ -29,49 +29,19 @@ HashIIDPtrKey(const void* key)
 static bool
 MatchIIDPtrKey(const PLDHashEntryHdr* entry, const void* key)
 {
     return ((const nsID*)key)->
                 Equals(*((const nsID*)((PLDHashEntryStub*)entry)->key));
 }
 
 static PLDHashNumber
-HashNativeKey(const void* key)
+HashNativeKey(const void* data)
 {
-    auto Key = static_cast<const XPCNativeSetKey*>(key);
-
-    PLDHashNumber h = 0;
-
-    XPCNativeSet* Set = Key->GetBaseSet();
-    XPCNativeInterface* Addition = Key->GetAddition();
-    uint16_t Position = Key->GetPosition();
-
-    if (!Set) {
-        MOZ_ASSERT(Addition, "bad key");
-        // This would be an XOR like below.
-        // But "0 ^ x == x". So it does not matter.
-        h = (js::HashNumber) NS_PTR_TO_INT32(Addition) >> 2;
-    } else {
-        XPCNativeInterface** Current = Set->GetInterfaceArray();
-        uint16_t count = Set->GetInterfaceCount();
-        if (Addition) {
-            count++;
-            for (uint16_t i = 0; i < count; i++) {
-                if (i == Position)
-                    h ^= (js::HashNumber) NS_PTR_TO_INT32(Addition) >> 2;
-                else
-                    h ^= (js::HashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2;
-            }
-        } else {
-            for (uint16_t i = 0; i < count; i++)
-                h ^= (js::HashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2;
-        }
-    }
-
-    return h;
+    return static_cast<const XPCNativeSetKey*>(data)->Hash();
 }
 
 /***************************************************************************/
 // implement JSObject2WrappedJSMap...
 
 void
 JSObject2WrappedJSMap::UpdateWeakPointersAfterGC(XPCJSRuntime* runtime)
 {
--- a/js/xpconnect/src/XPCWrappedNativeInfo.cpp
+++ b/js/xpconnect/src/XPCWrappedNativeInfo.cpp
@@ -419,16 +419,53 @@ XPCNativeInterface::DebugDump(int16_t de
         XPC_LOG_ALWAYS(("name is %s", GetNameString()));
         XPC_LOG_ALWAYS(("mMemberCount is %d", mMemberCount));
         XPC_LOG_ALWAYS(("mInfo @ %x", mInfo.get()));
         XPC_LOG_OUTDENT();
 #endif
 }
 
 /***************************************************************************/
+// XPCNativeSetKey
+
+PLDHashNumber
+XPCNativeSetKey::Hash() const
+{
+    PLDHashNumber h = 0;
+
+    XPCNativeSet* Set = GetBaseSet();
+    XPCNativeInterface* Addition = GetAddition();
+    uint16_t Position = GetPosition();
+
+    if (!Set) {
+        MOZ_ASSERT(Addition, "bad key");
+        // This would be an XOR like below.
+        // But "0 ^ x == x". So it does not matter.
+        h = (js::HashNumber) NS_PTR_TO_INT32(Addition) >> 2;
+    } else {
+        XPCNativeInterface** Current = Set->GetInterfaceArray();
+        uint16_t count = Set->GetInterfaceCount();
+        if (Addition) {
+            count++;
+            for (uint16_t i = 0; i < count; i++) {
+                if (i == Position)
+                    h ^= (js::HashNumber) NS_PTR_TO_INT32(Addition) >> 2;
+                else
+                    h ^= (js::HashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2;
+            }
+        } else {
+            for (uint16_t i = 0; i < count; i++)
+                h ^= (js::HashNumber) NS_PTR_TO_INT32(*(Current++)) >> 2;
+        }
+    }
+
+    return h;
+}
+
+/***************************************************************************/
 // XPCNativeSet
 
 // static
 XPCNativeSet*
 XPCNativeSet::GetNewOrUsed(const nsIID* iid)
 {
     AutoJSContext cx;
     AutoMarkingNativeSetPtr set(cx);
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -1296,16 +1296,18 @@ public:
                              uint16_t position = 0)
         : mBaseSet(baseSet), mAddition(addition), mPosition(position) {}
     ~XPCNativeSetKey() {}
 
     XPCNativeSet* GetBaseSet() const {return mBaseSet;}
     XPCNativeInterface* GetAddition() const {return mAddition;}
     uint16_t GetPosition() const {return mPosition;}
 
+    PLDHashNumber Hash() const;
+
     // Allow shallow copy
 
 private:
     XPCNativeSet* mBaseSet;
     XPCNativeInterface* mAddition;
     uint16_t mPosition;
 };