nsEventTargetChainItem
author Benjamin Smedberg <benjamin@smedbergs.us>
Sat, 26 Jul 2008 22:49:39 -0400
changeset 167 a4da40849f5436e629c5732f4368c6c48189637f
parent 70 94e945ee06a99c5ffbcfd08ce99176f1694328d3
permissions -rw-r--r--
State as of now

diff --git a/content/events/src/nsEventDispatcher.cpp b/content/events/src/nsEventDispatcher.cpp
--- a/content/events/src/nsEventDispatcher.cpp
+++ b/content/events/src/nsEventDispatcher.cpp
@@ -50,32 +50,11 @@
 #define NS_TARGET_CHAIN_FORCE_CONTENT_DISPATCH (1 << 0)
 
 // nsEventTargetChainItem represents a single item in the event target chain.
-class nsEventTargetChainItem
-{
-private:
+class nsEventTargetChainItem : public XPCOMGCObject
+{
+public:
   nsEventTargetChainItem(nsISupports* aTarget,
                          nsEventTargetChainItem* aChild = nsnull);
-
-  void Destroy(nsFixedSizeAllocator* aAllocator);
-
-public:
-  static nsEventTargetChainItem* Create(nsFixedSizeAllocator* aAllocator, 
-                                        nsISupports* aTarget,
-                                        nsEventTargetChainItem* aChild = nsnull)
-  {
-    void* place = aAllocator->Alloc(sizeof(nsEventTargetChainItem));
-    return place
-      ? ::new (place) nsEventTargetChainItem(aTarget, aChild)
-      : nsnull;
-  }
-
-  static void Destroy(nsFixedSizeAllocator* aAllocator,
-                      nsEventTargetChainItem* aItem)
-  {
-    aItem->Destroy(aAllocator);
-    aItem->~nsEventTargetChainItem();
-    aAllocator->Free(aItem, sizeof(nsEventTargetChainItem));
-  }
 
   PRBool IsValid()
   {
@@ -154,29 +133,13 @@ nsEventTargetChainItem::nsEventTargetCha
                                                nsEventTargetChainItem* aChild)
 : mChild(aChild), mParent(nsnull), mFlags(0), mItemFlags(0)
 {
-  nsCOMPtr<nsPIDOMEventTarget> t = do_QueryInterface(aTarget);
+  nsPIDOMEventTarget* t = do_QueryInterface(aTarget);
   if (t) {
     mTarget = t->GetTargetForEventTargetChain();
   }
   if (mChild) {
     mChild->mParent = this;
   }
-}
-
-void
-nsEventTargetChainItem::Destroy(nsFixedSizeAllocator* aAllocator)
-{
-  if (mChild) {
-    mChild->mParent = nsnull;
-    mChild = nsnull;
-  }
-
-  if (mParent) {
-    Destroy(aAllocator, mParent);
-    mParent = nsnull;
-  }
-
-  mTarget = nsnull;
 }
 
 nsresult
@@ -224,7 +187,7 @@ nsEventTargetChainItem::HandleEventTarge
                                                nsDispatchingCallback* aCallback)
 {
   // Save the target so that it can be restored later.
-  nsCOMPtr<nsISupports> firstTarget = aVisitor.mEvent->target;
+  nsISupports* firstTarget = aVisitor.mEvent->target;
 
   // Capture
   nsEventTargetChainItem* item = this;
@@ -319,48 +282,6 @@ nsEventTargetChainItem::HandleEventTarge
   return NS_OK;
 }
 
-class ChainItemPool {
-public:
-  ChainItemPool() {
-    if (!sEtciPool) {
-      sEtciPool = new nsFixedSizeAllocator();
-      if (sEtciPool) {
-        static const size_t kBucketSizes[] = { sizeof(nsEventTargetChainItem) };
-        static const PRInt32 kNumBuckets = sizeof(kBucketSizes) / sizeof(size_t);
-        static const PRInt32 kInitialPoolSize =
-          NS_SIZE_IN_HEAP(sizeof(nsEventTargetChainItem)) * 128;
-        nsresult rv = sEtciPool->Init("EventTargetChainItem Pool", kBucketSizes,
-                                      kNumBuckets, kInitialPoolSize);
-        if (NS_FAILED(rv)) {
-          delete sEtciPool;
-          sEtciPool = nsnull;
-        }
-      }
-    }
-    if (sEtciPool) {
-      ++sEtciPoolUsers;
-    }
-  }
-
-  ~ChainItemPool() {
-    if (sEtciPool) {
-      --sEtciPoolUsers;
-    }
-    if (!sEtciPoolUsers) {
-      delete sEtciPool;
-      sEtciPool = nsnull;
-    }
-  }
-
-  nsFixedSizeAllocator* GetPool() { return sEtciPool; }
-
-  static nsFixedSizeAllocator* sEtciPool;
-  static PRInt32               sEtciPoolUsers;
-};
-
-nsFixedSizeAllocator* ChainItemPool::sEtciPool = nsnull;
-PRInt32 ChainItemPool::sEtciPoolUsers = 0;
-
 /* static */ nsresult
 nsEventDispatcher::Dispatch(nsISupports* aTarget,
                             nsPresContext* aPresContext,
@@ -380,7 +301,7 @@ nsEventDispatcher::Dispatch(nsISupports*
 
 #ifdef DEBUG
   if (aDOMEvent) {
-    nsCOMPtr<nsIPrivateDOMEvent> privEvt(do_QueryInterface(aDOMEvent));
+    nsIPrivateDOMEvent* privEvt(do_QueryInterface(aDOMEvent));
     if (privEvt) {
       nsEvent* innerEvent = nsnull;
       privEvt->GetInternalNSEvent(&innerEvent);
@@ -395,16 +316,13 @@ nsEventDispatcher::Dispatch(nsISupports*
 
   // If we have a PresContext, make sure it doesn't die before
   // event dispatching is finished.
-  nsCOMPtr<nsPresContext> kungFuDeathGrip(aPresContext);
-  ChainItemPool pool;
-  NS_ENSURE_TRUE(pool.GetPool(), NS_ERROR_OUT_OF_MEMORY);
+  nsPresContext* kungFuDeathGrip(aPresContext);
 
   // Create the event target chain item for the event target.
-  nsEventTargetChainItem* targetEtci =
-    nsEventTargetChainItem::Create(pool.GetPool(), aTarget);
+  nsEventTargetChainItem* targetEtci = new nsEventTargetChainItem(aTarget);
   NS_ENSURE_TRUE(targetEtci, NS_ERROR_OUT_OF_MEMORY);
   if (!targetEtci->IsValid()) {
-    nsEventTargetChainItem::Destroy(pool.GetPool(), targetEtci);
+    NS_GetGC()->Free(targetEtci);
     return NS_ERROR_FAILURE;
   }
 
@@ -420,7 +338,7 @@ nsEventDispatcher::Dispatch(nsISupports*
     //     which are dispatched to |window| but have document as their target.
     //
     // Make sure that the event target points to the right object.
-    nsCOMPtr<nsPIDOMEventTarget> t = do_QueryInterface(aEvent->target);
+    nsPIDOMEventTarget* t = do_QueryInterface(aEvent->target);
     NS_ENSURE_STATE(t);
     aEvent->target = t->GetTargetForEventTargetChain();
     NS_ENSURE_STATE(aEvent->target);
@@ -442,8 +360,7 @@ nsEventDispatcher::Dispatch(nsISupports*
     nsEventTargetChainItem* topEtci = targetEtci;
     while (preVisitor.mParentTarget) {
       nsEventTargetChainItem* parentEtci =
-        nsEventTargetChainItem::Create(pool.GetPool(), preVisitor.mParentTarget,
-                                       topEtci);
+        new nsEventTargetChainItem(preVisitor.mParentTarget, topEtci);
       if (!parentEtci) {
         rv = NS_ERROR_OUT_OF_MEMORY;
         break;
@@ -465,7 +382,7 @@ nsEventDispatcher::Dispatch(nsISupports*
       if (preVisitor.mCanHandle) {
         topEtci = parentEtci;
       } else {
-        nsEventTargetChainItem::Destroy(pool.GetPool(), parentEtci);
+        NS_GetGC()->Free(parentEtci);
         parentEtci = nsnull;
         break;
       }
@@ -486,7 +403,7 @@ nsEventDispatcher::Dispatch(nsISupports*
     }
   }
 
-  nsEventTargetChainItem::Destroy(pool.GetPool(), targetEtci);
+  NS_GetGC()->Free(targetEtci);
   targetEtci = nsnull;
 
   NS_MARK_EVENT_DISPATCH_DONE(aEvent);
@@ -494,7 +411,7 @@ nsEventDispatcher::Dispatch(nsISupports*
   if (!externalDOMEvent && preVisitor.mDOMEvent) {
     // An nsDOMEvent was created while dispatching the event.
     // Duplicate private data if someone holds a pointer to it.
-    nsCOMPtr<nsIPrivateDOMEvent> privateEvent =
+    nsIPrivateDOMEvent* privateEvent =
       do_QueryInterface(preVisitor.mDOMEvent);
     if (privateEvent) {
       privateEvent->DuplicatePrivateData();
@@ -515,14 +432,14 @@ nsEventDispatcher::DispatchDOMEvent(nsIS
                                     nsEventStatus* aEventStatus)
 {
   if (aDOMEvent) {
-    nsCOMPtr<nsIPrivateDOMEvent> privEvt(do_QueryInterface(aDOMEvent));
+    nsIPrivateDOMEvent* privEvt(do_QueryInterface(aDOMEvent));
     if (privEvt) {
       nsEvent* innerEvent = nsnull;
       privEvt->GetInternalNSEvent(&innerEvent);
       NS_ENSURE_TRUE(innerEvent, NS_ERROR_ILLEGAL_VALUE);
 
       PRBool trusted;
-      nsCOMPtr<nsIDOMNSEvent> nsevent(do_QueryInterface(privEvt));
+      nsIDOMNSEvent* nsevent(do_QueryInterface(privEvt));
 
       nsevent->GetIsTrusted(&trusted);