From 0518680d4271eaca8623f8f218f6ac4cf5725b49 Mon Sep 17 00:00:00 2001 From: "bryner%brianryner.com" Date: Sun, 25 Jan 2004 10:39:02 +0000 Subject: [PATCH] Some cleanup to nsProxyObject: implement refcounting without nsISupports inheritance, inline getters, and fix a few warnings. Bug 232022, r=dougt, sr=dbaron. --- xpcom/proxy/public/nsProxyEvent.h | 28 +++++---- xpcom/proxy/src/nsProxyEvent.cpp | 72 +++++++++--------------- xpcom/proxy/src/nsProxyEventObject.cpp | 5 +- xpcom/proxy/src/nsProxyEventPrivate.h | 2 +- xpcom/proxy/src/nsProxyObjectManager.cpp | 4 +- 5 files changed, 47 insertions(+), 64 deletions(-) diff --git a/xpcom/proxy/public/nsProxyEvent.h b/xpcom/proxy/public/nsProxyEvent.h index 980eed22cd23..bc634bf8c55b 100644 --- a/xpcom/proxy/public/nsProxyEvent.h +++ b/xpcom/proxy/public/nsProxyEvent.h @@ -39,6 +39,7 @@ #define __nsProxyEvent_h_ #include "nsCOMPtr.h" +#include "nsAutoPtr.h" #include "nscore.h" #include "nsISupports.h" #include "nsIFactory.h" @@ -86,21 +87,16 @@ class nsProxyObjectCallInfo; -// Using the ISupports interface just for addrefing. - -class nsProxyObject : public nsISupports +class nsProxyObject { public: - - NS_DECL_ISUPPORTS - NS_DEFINE_STATIC_IID_ACCESSOR(NS_ISUPPORTS_IID) - // wierd, but it shouldn't break. Need to discuss this with dougt - - nsProxyObject(); nsProxyObject(nsIEventQueue *destQueue, PRInt32 proxyType, nsISupports *realObject); nsProxyObject(nsIEventQueue *destQueue, PRInt32 proxyType, const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID); - virtual ~nsProxyObject(); + void AddRef(); + void Release(); + + ~nsProxyObject(); nsresult Post( PRUint32 methodIndex, nsXPTMethodInfo * info, @@ -108,13 +104,15 @@ public: nsIInterfaceInfo * interfaceInfo); nsresult PostAndWait(nsProxyObjectCallInfo *proxyInfo); - nsISupports* GetRealObject() const; - nsIEventQueue* GetQueue() const; + nsISupports* GetRealObject() const { return mRealObject; } + nsIEventQueue* GetQueue() const { return mDestQueue; } PRInt32 GetProxyType() const { return mProxyType; } friend class nsProxyEventObject; private: + nsAutoRefCnt mRefCnt; + PRInt32 mProxyType; nsCOMPtr mDestQueue; /* destination queue */ @@ -142,8 +140,8 @@ public: PRUint32 parameterCount, PLEvent *event); - virtual ~nsProxyObjectCallInfo(); - + ~nsProxyObjectCallInfo(); + PRUint32 GetMethodIndex() const { return mMethodIndex; } nsXPTCVariant* GetParameterList() const { return mParameterList; } PRUint32 GetParameterCount() const { return mParameterCount; } @@ -174,7 +172,7 @@ private: when we are done invoking the method (only PROXY_SYNC). */ - nsCOMPtr mOwner; /* this is the strong referenced nsProxyObject */ + nsRefPtr mOwner; /* this is the strong referenced nsProxyObject */ void RefCountInInterfacePointers(PRBool addRef); void CopyStrings(PRBool copy); diff --git a/xpcom/proxy/src/nsProxyEvent.cpp b/xpcom/proxy/src/nsProxyEvent.cpp index e3e5262a935c..eb3d022a6a4a 100644 --- a/xpcom/proxy/src/nsProxyEvent.cpp +++ b/xpcom/proxy/src/nsProxyEvent.cpp @@ -255,13 +255,6 @@ nsProxyObjectCallInfo::SetCallersQueue(nsIEventQueue* queue) } -NS_IMPL_THREADSAFE_ADDREF(nsProxyObject) -NS_IMPL_THREADSAFE_QUERY_INTERFACE0(nsProxyObject) - -nsProxyObject::nsProxyObject() -{ - // the mac compiler demands that I have this useless constructor. -} nsProxyObject::nsProxyObject(nsIEventQueue *destQueue, PRInt32 proxyType, nsISupports *realObject) { mEventQService = do_GetService(kEventQueueServiceCID); @@ -295,8 +288,14 @@ nsProxyObject::~nsProxyObject() } +void +nsProxyObject::AddRef() +{ + PR_AtomicIncrement((PRInt32 *)&mRefCnt); + NS_LOG_ADDREF(this, mRefCnt, "nsProxyObject", sizeof(*this)); +} -NS_IMETHODIMP_(nsrefcnt) +void nsProxyObject::Release(void) { NS_PRECONDITION(0 != mRefCnt, "dup release"); @@ -308,53 +307,36 @@ nsProxyObject::Release(void) { mRefCnt = 1; /* stabilize */ - PRBool callDirectly; - mDestQueue->IsOnCurrentThread(&callDirectly); + PRBool callDirectly; + mDestQueue->IsOnCurrentThread(&callDirectly); - if (callDirectly) - { - NS_DELETEXPCOM(this); - return 0; - } + if (callDirectly) + { + delete this; + return; + } // need to do something special here so that // the real object will always be deleted on // the correct thread.. - PLEvent *event = PR_NEW(PLEvent); - if (event == nsnull) - { - NS_ASSERTION(0, "Could not create a plevent. Leaking nsProxyObject!"); - return 0; // if this happens we are going to leak. - } + PLEvent *event = PR_NEW(PLEvent); + if (event == nsnull) + { + NS_ASSERTION(0, "Could not create a plevent. Leaking nsProxyObject!"); + return; // if this happens we are going to leak. + } - PL_InitEvent(event, - this, - ProxyDestructorEventHandler, - ProxyDestructorDestroyHandler); + PL_InitEvent(event, + this, + ProxyDestructorEventHandler, + ProxyDestructorDestroyHandler); - mDestQueue->PostEvent(event); - return 0; + mDestQueue->PostEvent(event); } - return count; } -// GetRealObject -// This function must return the real pointer to the object to be proxied. -// It must not be a comptr or be addreffed. -nsISupports* -nsProxyObject::GetRealObject() const -{ - return mRealObject.get(); -} - -nsIEventQueue* -nsProxyObject::GetQueue() const -{ - return mDestQueue; -} - nsresult nsProxyObject::PostAndWait(nsProxyObjectCallInfo *proxyInfo) { @@ -583,8 +565,8 @@ static void* CompletedEventHandler(PLEvent *self) static void* ProxyDestructorEventHandler(PLEvent *self) { - nsProxyObject* owner = (nsProxyObject*) PL_GetEventOwner(self); - NS_DELETEXPCOM(owner); + nsProxyObject* owner = (nsProxyObject*) PL_GetEventOwner(self); + NS_DELETEXPCOM(owner); return nsnull; } diff --git a/xpcom/proxy/src/nsProxyEventObject.cpp b/xpcom/proxy/src/nsProxyEventObject.cpp index 6ffc79e944f6..18dca04f751e 100644 --- a/xpcom/proxy/src/nsProxyEventObject.cpp +++ b/xpcom/proxy/src/nsProxyEventObject.cpp @@ -448,7 +448,10 @@ nsProxyEventObject::~nsProxyEventObject() nsCOMPtr rootObject = do_QueryInterface(mProxyObject->mRealObject); nsCOMPtr rootQueue = do_QueryInterface(mProxyObject->mDestQueue); nsProxyEventKey key(rootObject, rootQueue, mProxyObject->mProxyType); - void* value = realToProxyMap->Remove(&key); +#ifdef DEBUG_dougt + void* value = +#endif + realToProxyMap->Remove(&key); #ifdef DEBUG_dougt NS_ASSERTION(value, "failed to remove from realToProxyMap"); #endif diff --git a/xpcom/proxy/src/nsProxyEventPrivate.h b/xpcom/proxy/src/nsProxyEventPrivate.h index b7d2e9e8b6a4..80b01f4bbdff 100644 --- a/xpcom/proxy/src/nsProxyEventPrivate.h +++ b/xpcom/proxy/src/nsProxyEventPrivate.h @@ -150,7 +150,7 @@ protected: protected: nsCOMPtr mClass; - nsCOMPtr mProxyObject; + nsRefPtr mProxyObject; // Owning reference... nsProxyEventObject *mRoot; diff --git a/xpcom/proxy/src/nsProxyObjectManager.cpp b/xpcom/proxy/src/nsProxyObjectManager.cpp index c70e4bb980db..79d454e185a8 100644 --- a/xpcom/proxy/src/nsProxyObjectManager.cpp +++ b/xpcom/proxy/src/nsProxyObjectManager.cpp @@ -108,8 +108,8 @@ nsProxyObjectManager* nsProxyObjectManager::mInstance = nsnull; NS_IMPL_THREADSAFE_ISUPPORTS1(nsProxyObjectManager, nsIProxyObjectManager) nsProxyObjectManager::nsProxyObjectManager() -: mProxyClassMap(256, PR_TRUE), - mProxyObjectMap(256, PR_TRUE) + : mProxyObjectMap(256, PR_TRUE), + mProxyClassMap(256, PR_TRUE) { mProxyCreationMonitor = PR_NewMonitor(); }