XPCOMGC/Stack Pointers: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(Initial spec)
 
No edit summary
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
Automatic rewriting spec for [[XPCOMGC]]:
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. Also remove use of getter_AddRefs with these objects.
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.
e.g.


<pre>  nsFoo::Function(nsISupports* gah)
<pre>  nsFoo::Function(nsISupports* gah)
   {
   {
    //rewrite stack declarations
-  nsCOMPtr<nsIBar> bar;
-  nsCOMPtr<nsIBar> bar;
+  nsIBar* bar;
+  nsIBar* bar;
    
    
-  nsCOMPtr<nsIBaz> gahbaz = do_QueryInterface(gah);
    // support multi-var declarations
+  nsIBaz* gahbaz = do_QueryInterface(gah);
-  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(getter_AddRefs(bar));
+  nsresult rv = gahbaz->GetBar(&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));
   }</pre>
   }</pre>
Command to make the patch useful:
<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>
[https://bugzilla.mozilla.org/show_bug.cgi?id=409088 Attribute-based rewrites]
Functions that take COMPtr& parameters make life difficult(especially if those are templated). COMPtr& gets the & converted to a *, a couple of other funky cases exist.

Latest revision as of 19:16, 27 December 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

Attribute-based rewrites

Functions that take COMPtr& parameters make life difficult(especially if those are templated). COMPtr& gets the & converted to a *, a couple of other funky cases exist.