Confirmed users
1,345
edits
Nnethercote (talk | contribs) |
Nnethercote (talk | contribs) |
||
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 | 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:: | void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf, | ||
size_t *foo, size_t* bar) const | |||
{ | |||
*foo += ...; | |||
*bar += ...; | |||
} | |||
Alternatively, you could create a struct: | Alternatively, you could create a struct: | ||
struct | struct FooBarSizes { | ||
size_t foo; | size_t foo; | ||
size_t bar; | size_t bar; | ||
FooBarSizes() { mozilla::PodZero(this); } | |||
} | } | ||
void FooBar:: | void FooBar::AddSizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf, | ||
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. |