XPCOMGC/Stack Pointers: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 35: Line 35:
<pre>
<pre>
sed 's/.home.tglek.work.actionmonkey.//' /tmp/nsgenerichtmlelement.diff |filterdiff -x xpcom/glue/nsCOMPtr.h -x xpcom/glue/nsIWeakReferenceUtils.h -x xpcom/glue/nsThreadUtils.h -x xpcom/threads/nsThread.h -x /\* |patch -p0 --dry-run</pre>
sed 's/.home.tglek.work.actionmonkey.//' /tmp/nsgenerichtmlelement.diff |filterdiff -x xpcom/glue/nsCOMPtr.h -x xpcom/glue/nsIWeakReferenceUtils.h -x xpcom/glue/nsThreadUtils.h -x xpcom/threads/nsThread.h -x /\* |patch -p0 --dry-run</pre>
Functions that take COMPtr& parameters make life difficult(especially if those are templated).
Given <pre>template<class T> void getter(nsCOMPtr<T>& ptr) </pre>
rewrite it to <pre>template<class T> void getter(T*& ptr) </pre>
Then rewrite code in the function as usual.
However callers now turn into 2 cases.
a) Being called with a stack variable -> change code as usual
b) Called with a heap var -> wrap with getter_AddRefs (This way write barrier will be triggered).

Revision as of 00:04, 25 October 2007

Automatic rewriting spec for XPCOMGC:

Detect every instance of nsCOMPtr that is used as a stack variable, and rewrite it to be a raw pointer.

  • Remove use of getter_AddRefs with these objects.
  • Rewrite nsCOMPtr::swap()
  • Remove nsCOMPtr::get() for stack vars
  • Function return values & parameters are part of the stack
    • Rewrite already_AddRefed<Foo> return values
    • Rewrite nsCOMPtr &bla parameters

e.g.

  nsFoo::Function(nsISupports* gah)
  {
    //rewrite stack declarations
-   nsCOMPtr<nsIBar> bar;
+   nsIBar* bar;
  
    // support multi-var declarations
-   nsCOMPtr<nsIBaz> boo, gahbaz = do_QueryInterface(gah);
+   nsIBaz* boo, *gahbaz = do_QueryInterface(gah);

    // Don't use getter_AddRefs for stack variables
-   nsresult rv = gahbaz->GetBar(getter_AddRefs(bar));
+   nsresult rv = gahbaz->GetBar(&bar);

    // Make the getter_AddRefs downcasting magic explicit
-   Function(getter_AddRefs(boo));
+   Function((nsISupports**)&boo);
    
    // keep heap-allocated variables untouched
    gahbaz->GetBar(getter_AddRefs(mClassMember));
  }

Command to make the patch useful:

sed 's/.home.tglek.work.actionmonkey.//' /tmp/nsgenerichtmlelement.diff |filterdiff -x xpcom/glue/nsCOMPtr.h -x xpcom/glue/nsIWeakReferenceUtils.h -x xpcom/glue/nsThreadUtils.h -x xpcom/threads/nsThread.h -x /\* |patch -p0 --dry-run

Functions that take COMPtr& parameters make life difficult(especially if those are templated).

Given

template<class T> void getter(nsCOMPtr<T>& ptr) 

rewrite it to

template<class T> void getter(T*& ptr) 

Then rewrite code in the function as usual. However callers now turn into 2 cases. a) Being called with a stack variable -> change code as usual b) Called with a heap var -> wrap with getter_AddRefs (This way write barrier will be triggered).