Platform/Memory Reporting: Difference between revisions

Line 168: Line 168:
Sometimes you may need variations on the above forms.  For example, if you have a function that just measures one member <tt>Foo</tt> of an object, it might be called <tt>SizeOfFoo</tt>.  Try to make the names descriptive enough that it's clear what's being measured.
Sometimes you may need variations on the above forms.  For example, if you have a function that just measures one member <tt>Foo</tt> of an object, it might be called <tt>SizeOfFoo</tt>.  Try to make the names descriptive enough that it's clear what's being measured.


Sometimes you might want to split the measurements of an object into two or more numbers, e.g. because you want to show them separately in about:memory.  In which case just return the multiple values by reference, like this:
Sometimes you might want to split the measurements of an object into two or more numbers, e.g. because you want to show them separately in about:memory.  In this case it's often clearer to *increment* the numbers rather than *assigning* to them, especially if you're measuring multiple entities and summing their measurements.  For example:


   void FooBar::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
   void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
                                  size_t *foo, size_t* bar) const;
                                      size_t *foo, size_t* bar) const
  {
      *foo += ...;
      *bar += ...;
  }


Alternatively, you could create a struct:
Alternatively, you could create a struct:


   struct FooBarStats {
   struct FooBarSizes {
       size_t foo;
       size_t foo;
       size_t bar;
       size_t bar;
      FooBarSizes() { mozilla::PodZero(this); }
   }
   }
   void FooBar::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
   void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf,
                                   FooBarStats *stats) const;
                                   FooBarSizes *sizes) const
  {
      sizes->foo += ...;
      sizes->bar += ...;
  }
 
Note the <tt>Add</tt> prefix that makes this incrementing behaviour clear.  Obviously, if you increment you have to zero the values at some point.  When using a struct, its constructor is the obvious place for this.


You could even put the <tt>nsMallocSizeOfFun</tt> in <tt>FooBarStats</tt> to reduce the number of arguments.
You could even put the <tt>nsMallocSizeOfFun</tt> in <tt>FooBarStats</tt> to reduce the number of arguments.
Confirmed users
1,345

edits