Platform/Memory Reporting: Difference between revisions

Line 85: Line 85:
And here's how you'd write a memory reporter if there was a single global MyString object.
And here's how you'd write a memory reporter if there was a single global MyString object.


  NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN(MyStringMallocSizeOf, "mystring")
 
   MyString *gMyString;
   MyString *gMyString;
    
    
   static PRInt64 GetMyStringSize()
   class MyStringReporter MOZ_FINAL : public MemoryUniReporter
   {
   {
     return gMyString->SizeOfIncludingThis(MyStringMallocSizeOf);
  public:
      
     MyStringReporter()
     // BTW: If gMyString wasn't a pointer, you'd use
      : MemoryUniReporter("mystring", KIND_HEAP, UNITS_BYTES,
    // |gMyString.SizeOfExcludingThis(MyStringMallocSizeOf)| instead.
                          "Memory used for MyString.")
  }
    {}
 
  private:
  NS_MEMORY_REPORTER_IMPLEMENT(MyString,
     int64_t Amount() MOZ_OVERRIDE
     "mystring",
     {
    KIND_HEAP,
      // BTW: If gMyString wasn't a pointer, you'd use
    UNITS_BYTES,
      // |gMyString.SizeOfExcludingThis(MallocSizeOf)| instead.
    GetMyStringSize,
      return gMyString->SizeOfIncludingThis(MallocSizeOf);   
    "Memory used for MyString.");
     }
  };


Note the use of the macro <tt>NS_MEMORY_REPORTER_MALLOC_SIZEOF_FUN</tt> to create <tt>MyStringMallocSizeOf</tt>, which is a function of type <tt>nsMallocSizeOfFun</tt> that is specific to this memory reporter (and will be identified as such in DMD's output).
<tt>MemoryUniReporter</tt> is a class that avoids most of the boilerplate code, and can be
used for most uni-reporters.  Note that <tt>MallocSizeOf</tt> is a member of <tt>MemoryUniReporter</tt>.  It is a function of type <tt>nsMallocSizeOfFun</tt> that is specific to this memory reporter (and will be identified as such in DMD's output).


=== An Example Involving Inheritance ===
=== An Example Involving Inheritance ===
Confirmed users
1,345

edits