nativescriptableshared-gcobject
author Benjamin Smedberg <benjamin@smedbergs.us>
Sat, 26 Jul 2008 22:49:39 -0400
changeset 167 a4da40849f5436e629c5732f4368c6c48189637f
parent 162 3ae1ca70680d55840cbf5a60088ebaebfe2bddc5
permissions -rw-r--r--
State as of now

diff --git a/js/src/xpconnect/src/xpcjsruntime.cpp b/js/src/xpconnect/src/xpcjsruntime.cpp
--- a/js/src/xpconnect/src/xpcjsruntime.cpp
+++ b/js/src/xpconnect/src/xpcjsruntime.cpp
@@ -134,43 +134,6 @@ NativeSetGC(JSDHashTable *table, JSDHash
     return JS_DHASH_REMOVE;
 }
 
-JS_STATIC_DLL_CALLBACK(JSDHashOperator)
-JSClassSweeper(JSDHashTable *table, JSDHashEntryHdr *hdr,
-               uint32 number, void *arg)
-{
-    XPCNativeScriptableShared* shared =
-        ((XPCNativeScriptableSharedMap::Entry*) hdr)->key;
-    if(shared->IsMarked())
-    {
-#ifdef off_XPC_REPORT_JSCLASS_FLUSHING
-        printf("+ Marked XPCNativeScriptableShared for: %s @ %x\n",
-               shared->GetJSClass()->name,
-               shared->GetJSClass());
-#endif
-        shared->Unmark();
-        return JS_DHASH_NEXT;
-    }
-
-#ifdef XPC_REPORT_JSCLASS_FLUSHING
-    printf("- Destroying XPCNativeScriptableShared for: %s @ %x\n",
-           shared->GetJSClass()->name,
-           shared->GetJSClass());
-#endif
-
-    delete shared;
-    return JS_DHASH_REMOVE;
-}
-
-JS_STATIC_DLL_CALLBACK(JSDHashOperator)
-DyingProtoKiller(JSDHashTable *table, JSDHashEntryHdr *hdr,
-                 uint32 number, void *arg)
-{
-    XPCWrappedNativeProto* proto =
-        (XPCWrappedNativeProto*)((JSDHashEntryStub*)hdr)->key;
-    delete proto;
-    return JS_DHASH_REMOVE;
-}
-
 // GCCallback calls are chained
 JS_STATIC_DLL_CALLBACK(JSBool)
 ContextCallback(JSContext *cx, uintN operation)
@@ -238,6 +201,8 @@ JSBool XPCJSRuntime::GCCallback(JSContex
 
                 XPCWrappedNativeScope::FinishedMarkPhaseOfGC(cx, self);
 
+                self->mNativeScriptableSharedMap->UnmapDying();
+
                 self->mDoingFinalization = JS_TRUE;
                 break;
             }
@@ -301,15 +266,6 @@ JSBool XPCJSRuntime::GCCallback(JSContex
                 }
 
                 // Do the sweeping...
-
-                // We don't want to sweep the JSClasses at shutdown time.
-                // At this point there may be JSObjects using them that have
-                // been removed from the other maps.
-                if(!self->GetXPConnect()->IsShuttingDown())
-                {
-                    self->mNativeScriptableSharedMap->
-                        Enumerate(JSClassSweeper, nsnull);
-                }
 
                 self->mClassInfo2NativeSetMap->
                     Enumerate(NativeUnMarkedSetRemover, nsnull);
diff --git a/js/src/xpconnect/src/xpcmaps.cpp b/js/src/xpconnect/src/xpcmaps.cpp
--- a/js/src/xpconnect/src/xpcmaps.cpp
+++ b/js/src/xpconnect/src/xpcmaps.cpp
@@ -637,6 +637,22 @@ XPCNativeScriptableSharedMap::GetNewOrUs
     return JS_TRUE;
 }
 
+JSDHashOperator
+XPCNativeScriptableSharedMap::DyingUnmapper(JSDHashTable *table,
+                                            JSDHashEntryHdr *hdr,
+                                            uint32 number,
+                                            void *arg)
+{
+    Entry *entry = static_cast<Entry*>(hdr);
+
+    if(NS_GetGC()->GetMark(entry->key))
+    {
+        return JS_DHASH_NEXT;
+    }
+
+    return JS_DHASH_REMOVE;
+}
+
 /***************************************************************************/
 // implement XPCWrappedNativeProtoMap...
 
diff --git a/js/src/xpconnect/src/xpcmaps.h b/js/src/xpconnect/src/xpcmaps.h
--- a/js/src/xpconnect/src/xpcmaps.h
+++ b/js/src/xpconnect/src/xpcmaps.h
@@ -629,10 +629,20 @@ public:
     inline uint32 Enumerate(JSDHashEnumerator f, void *arg)
         {return JS_DHashTableEnumerate(mTable, f, arg);}
 
+    inline void UnmapDying()
+    {
+        JS_DHashTableEnumerate(mTable, DyingUnmapper, nsnull);
+    }
+
     ~XPCNativeScriptableSharedMap();
 private:
     XPCNativeScriptableSharedMap();    // no implementation
     XPCNativeScriptableSharedMap(int size);
+
+    static JSDHashOperator DyingUnmapper(JSDHashTable *table,
+                                         JSDHashEntryHdr *hdr,
+                                         uint32 number,
+                                         void *arg);
 private:
     JSDHashTable *mTable;
 };
diff --git a/js/src/xpconnect/src/xpcprivate.h b/js/src/xpconnect/src/xpcprivate.h
--- a/js/src/xpconnect/src/xpcprivate.h
+++ b/js/src/xpconnect/src/xpcprivate.h
@@ -1646,7 +1646,9 @@ public:
 // was a big problem when wrappers are reparented to different scopes (and
 // thus different protos (the DOM does this).
 
-class XPCNativeScriptableShared
+// This class is GC-allocated, except when we're doing a hash lookup
+// in XPCNativeScriptableSharedMap, when it's a temporary stack object
+class XPCNativeScriptableShared : public MMgc::GCFinalizable
 {
 public:
     const XPCNativeScriptableFlags& GetFlags() const {return mFlags;}
@@ -1668,9 +1670,10 @@ public:
 
     void PopulateJSClass(JSBool isGlobal);
 
-    void Mark()       {mFlags.Mark();}
-    void Unmark()     {mFlags.Unmark();}
-    JSBool IsMarked() const {return mFlags.IsMarked();}
+    static void* operator new(size_t s)
+    {
+        return XPCOMGCFinalizedObject::operator new(s);
+    }
 
 private:
     XPCNativeScriptableFlags mFlags;
@@ -1705,8 +1708,6 @@ public:
 
     void
     SetScriptableShared(XPCNativeScriptableShared* shared) {mShared = shared;}
-
-    void Mark() {if(mShared) mShared->Mark();}
 
 protected:
     XPCNativeScriptableInfo(nsIXPCScriptable* scriptable = nsnull,
@@ -2886,7 +2887,7 @@ private:
 
 #define BAD_TLS_INDEX ((PRUint32) -1)
 
-class XPCPerThreadData
+class XPCPerThreadData : public MMgc::GCRoot
 {
 public:
     // Get the instance of this object for the current thread
diff --git a/js/src/xpconnect/src/xpcthreadcontext.cpp b/js/src/xpconnect/src/xpcthreadcontext.cpp
--- a/js/src/xpconnect/src/xpcthreadcontext.cpp
+++ b/js/src/xpconnect/src/xpcthreadcontext.cpp
@@ -448,7 +448,8 @@ MOZ_DECL_CTOR_COUNTER(xpcPerThreadData)
 MOZ_DECL_CTOR_COUNTER(xpcPerThreadData)
 
 XPCPerThreadData::XPCPerThreadData()
-    :   mJSContextStack(new XPCJSContextStack()),
+:   MMgc::GCRoot(NS_GetGC()),
+        mJSContextStack(new XPCJSContextStack()),
         mNextThread(nsnull),
         mCallContext(nsnull),
         mResolveName(0),