Bug 653627 - Don't use char** in nsIMemoryReporter. r=khuey, sr=bz.
authorNicholas Nethercote <nnethercote@mozilla.com>
Fri, 08 Jul 2011 12:45:16 +1000
changeset 72932 43fa785282e3776f121cac8016f3bf039bda9acd
parent 72931 22d8bb992dc2130493cdd3966992e77d85250c06
child 72933 f10db06207903e593de5821d631fe69fb61c085a
push idunknown
push userunknown
push dateunknown
reviewerskhuey, bz
bugs653627
milestone8.0a1
Bug 653627 - Don't use char** in nsIMemoryReporter. r=khuey, sr=bz.
dom/base/nsDOMMemoryReporter.cpp
dom/base/nsDOMMemoryReporter.h
dom/ipc/ContentChild.cpp
gfx/thebes/gfxASurface.cpp
ipc/glue/SharedMemory.cpp
modules/libpr0n/src/imgLoader.cpp
storage/src/mozStorageConnection.cpp
xpcom/base/nsIMemoryReporter.idl
xpcom/base/nsMemoryReporterManager.cpp
xpcom/base/nsMemoryReporterManager.h
--- a/dom/base/nsDOMMemoryReporter.cpp
+++ b/dom/base/nsDOMMemoryReporter.cpp
@@ -49,41 +49,41 @@ NS_IMPL_ISUPPORTS1(nsDOMMemoryReporter, 
 void
 nsDOMMemoryReporter::Init()
 {
   // The memory reporter manager is going to own this object.
   NS_RegisterMemoryReporter(new nsDOMMemoryReporter());
 }
 
 NS_IMETHODIMP
-nsDOMMemoryReporter::GetProcess(char** aProcess)
+nsDOMMemoryReporter::GetProcess(nsACString &aProcess)
 {
   // "" means the main process.
-  *aProcess = strdup("");
+  aProcess.Truncate();
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsDOMMemoryReporter::GetPath(char** aMemoryPath)
+nsDOMMemoryReporter::GetPath(nsACString &aMemoryPath)
 {
-  *aMemoryPath = strdup("explicit/dom");
+  aMemoryPath.AssignLiteral("explicit/dom");
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsDOMMemoryReporter::GetKind(int* aKind)
+nsDOMMemoryReporter::GetKind(PRInt32* aKind)
 {
   *aKind = KIND_HEAP;
   return NS_OK;
 }
 
 NS_IMETHODIMP
-nsDOMMemoryReporter::GetDescription(char** aDescription)
+nsDOMMemoryReporter::GetDescription(nsACString &aDescription)
 {
-  *aDescription = strdup("Memory used by the DOM.");
+  aDescription.AssignLiteral("Memory used by the DOM.");
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDOMMemoryReporter::GetUnits(PRInt32* aUnits)
 {
   *aUnits = UNITS_BYTES;
   return NS_OK;
--- a/dom/base/nsDOMMemoryReporter.h
+++ b/dom/base/nsDOMMemoryReporter.h
@@ -39,25 +39,19 @@
 #define nsDOMMemoryReporter_h__
 
 #include "nsIMemoryReporter.h"
 
 
 class nsDOMMemoryReporter: public nsIMemoryReporter {
 public:
   NS_DECL_ISUPPORTS
+  NS_DECL_NSIMEMORYREPORTER
 
   static void Init();
 
-  NS_IMETHOD GetProcess(char** aProcess);
-  NS_IMETHOD GetPath(char** aMemoryPath);
-  NS_IMETHOD GetKind(int* aKnd);
-  NS_IMETHOD GetDescription(char** aDescription);
-  NS_IMETHOD GetUnits(PRInt32* aUnits);
-  NS_IMETHOD GetAmount(PRInt64* aAmount);
-
 private:
   // Protect ctor, use Init() instead.
   nsDOMMemoryReporter();
 };
 
 #endif // nsDOMMemoryReporter_h__
 
--- a/dom/ipc/ContentChild.cpp
+++ b/dom/ipc/ContentChild.cpp
@@ -358,21 +358,21 @@ ContentChild::RecvPMemoryReportRequestCo
       nsCOMPtr<nsIMemoryReporter> r;
       e->GetNext(getter_AddRefs(r));
 
       nsCString path;
       PRInt32 kind;
       PRInt32 units;
       PRInt64 amount;
       nsCString desc;
-      r->GetPath(getter_Copies(path));
+      r->GetPath(path);
       r->GetKind(&kind);
       r->GetUnits(&units);
       r->GetAmount(&amount);
-      r->GetDescription(getter_Copies(desc));
+      r->GetDescription(desc);
 
       MemoryReport memreport(process, path, kind, units, amount, desc);
       reports.AppendElement(memreport);
     }
 
     // Then do the memory multi-reporters, by calling CollectReports on each
     // one, whereupon the callback will turn each measurement into a
     // MemoryReport.
--- a/gfx/thebes/gfxASurface.cpp
+++ b/gfx/thebes/gfxASurface.cpp
@@ -595,23 +595,23 @@ class SurfaceMemoryReporter :
 {
 public:
     SurfaceMemoryReporter(gfxASurface::gfxSurfaceType aType)
         : mType(aType)
     { }
 
     NS_DECL_ISUPPORTS
 
-    NS_IMETHOD GetProcess(char **process) {
-        *process = strdup("");
+    NS_IMETHOD GetProcess(nsACString &process) {
+        process.Truncate();
         return NS_OK;
     }
 
-    NS_IMETHOD GetPath(char **memoryPath) {
-        *memoryPath = strdup(SurfaceMemoryReporterPathForType(mType));
+    NS_IMETHOD GetPath(nsACString &path) {
+        path.Assign(SurfaceMemoryReporterPathForType(mType));
         return NS_OK;
     }
 
     NS_IMETHOD GetKind(PRInt32 *kind) {
         *kind = KIND_OTHER;
         return NS_OK;
     }
     
@@ -620,18 +620,18 @@ public:
         return NS_OK;
     }
 
     NS_IMETHOD GetAmount(PRInt64 *amount) {
         *amount = gSurfaceMemoryUsed[mType];
         return NS_OK;
     }
 
-    NS_IMETHOD GetDescription(char **desc) {
-        *desc = strdup("Memory used by gfx surface of the given type.");
+    NS_IMETHOD GetDescription(nsACString &desc) {
+        desc.AssignLiteral("Memory used by gfx surface of the given type.");
         return NS_OK;
     }
 
     gfxASurface::gfxSurfaceType mType;
 };
 
 NS_IMPL_ISUPPORTS1(SurfaceMemoryReporter, nsIMemoryReporter)
 
--- a/ipc/glue/SharedMemory.cpp
+++ b/ipc/glue/SharedMemory.cpp
@@ -35,16 +35,17 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include <math.h>
 
+#include "nsString.h"
 #include "nsIMemoryReporter.h"
 #include "mozilla/ipc/SharedMemory.h"
 
 namespace mozilla {
 namespace ipc {
 
 static PRInt64 gShmemAllocated;
 static PRInt64 gShmemMapped;
--- a/modules/libpr0n/src/imgLoader.cpp
+++ b/modules/libpr0n/src/imgLoader.cpp
@@ -153,40 +153,40 @@ public:
   };
 
   imgMemoryReporter(ReporterType aType)
     : mType(aType)
   { }
 
   NS_DECL_ISUPPORTS
 
-  NS_IMETHOD GetProcess(char **process)
+  NS_IMETHOD GetProcess(nsACString &process)
   {
-    *process = strdup("");
+    process.Truncate();
     return NS_OK;
   }
 
-  NS_IMETHOD GetPath(char **memoryPath)
+  NS_IMETHOD GetPath(nsACString &path)
   {
     if (mType == ChromeUsedRaw) {
-      *memoryPath = strdup("explicit/images/chrome/used/raw");
+      path.AssignLiteral("explicit/images/chrome/used/raw");
     } else if (mType == ChromeUsedUncompressed) {
-      *memoryPath = strdup("explicit/images/chrome/used/uncompressed");
+      path.AssignLiteral("explicit/images/chrome/used/uncompressed");
     } else if (mType == ChromeUnusedRaw) {
-      *memoryPath = strdup("explicit/images/chrome/unused/raw");
+      path.AssignLiteral("explicit/images/chrome/unused/raw");
     } else if (mType == ChromeUnusedUncompressed) {
-      *memoryPath = strdup("explicit/images/chrome/unused/uncompressed");
+      path.AssignLiteral("explicit/images/chrome/unused/uncompressed");
     } else if (mType == ContentUsedRaw) {
-      *memoryPath = strdup("explicit/images/content/used/raw");
+      path.AssignLiteral("explicit/images/content/used/raw");
     } else if (mType == ContentUsedUncompressed) {
-      *memoryPath = strdup("explicit/images/content/used/uncompressed");
+      path.AssignLiteral("explicit/images/content/used/uncompressed");
     } else if (mType == ContentUnusedRaw) {
-      *memoryPath = strdup("explicit/images/content/unused/raw");
+      path.AssignLiteral("explicit/images/content/unused/raw");
     } else if (mType == ContentUnusedUncompressed) {
-      *memoryPath = strdup("explicit/images/content/unused/uncompressed");
+      path.AssignLiteral("explicit/images/content/unused/uncompressed");
     }
     return NS_OK;
   }
 
   NS_IMETHOD GetKind(PRInt32 *kind)
   {
     *kind = KIND_HEAP;
     return NS_OK;
@@ -244,34 +244,34 @@ public:
     } else {
       imgLoader::sCache.EnumerateRead(EnumEntries, &arg);
     }
 
     *amount = arg.value;
     return NS_OK;
   }
 
-  NS_IMETHOD GetDescription(char **desc)
+  NS_IMETHOD GetDescription(nsACString &desc)
   {
     if (mType == ChromeUsedRaw) {
-      *desc = strdup("Memory used by in-use chrome images (compressed data).");
+      desc.AssignLiteral("Memory used by in-use chrome images (compressed data).");
     } else if (mType == ChromeUsedUncompressed) {
-      *desc = strdup("Memory used by in-use chrome images (uncompressed data).");
+      desc.AssignLiteral("Memory used by in-use chrome images (uncompressed data).");
     } else if (mType == ChromeUnusedRaw) {
-      *desc = strdup("Memory used by not in-use chrome images (compressed data).");
+      desc.AssignLiteral("Memory used by not in-use chrome images (compressed data).");
     } else if (mType == ChromeUnusedUncompressed) {
-      *desc = strdup("Memory used by not in-use chrome images (uncompressed data).");
+      desc.AssignLiteral("Memory used by not in-use chrome images (uncompressed data).");
     } else if (mType == ContentUsedRaw) {
-      *desc = strdup("Memory used by in-use content images (compressed data).");
+      desc.AssignLiteral("Memory used by in-use content images (compressed data).");
     } else if (mType == ContentUsedUncompressed) {
-      *desc = strdup("Memory used by in-use content images (uncompressed data).");
+      desc.AssignLiteral("Memory used by in-use content images (uncompressed data).");
     } else if (mType == ContentUnusedRaw) {
-      *desc = strdup("Memory used by not in-use content images (compressed data).");
+      desc.AssignLiteral("Memory used by not in-use content images (compressed data).");
     } else if (mType == ContentUnusedUncompressed) {
-      *desc = strdup("Memory used by not in-use content images (uncompressed data).");
+      desc.AssignLiteral("Memory used by not in-use content images (uncompressed data).");
     }
     return NS_OK;
   }
 
   ReporterType mType;
 };
 
 NS_IMPL_ISUPPORTS1(imgMemoryReporter, nsIMemoryReporter)
--- a/storage/src/mozStorageConnection.cpp
+++ b/storage/src/mozStorageConnection.cpp
@@ -343,40 +343,36 @@ public:
   StorageMemoryReporter(Connection &aDBConn,
                         ReporterType aType)
   : mDBConn(aDBConn)
   , mType(aType)
   {
   }
 
 
-  NS_IMETHOD GetProcess(char **process)
+  NS_IMETHOD GetProcess(nsACString &process)
   {
-    *process = strdup("");
+    process.Truncate();
     return NS_OK;
   }
 
-  NS_IMETHOD GetPath(char **memoryPath)
+  NS_IMETHOD GetPath(nsACString &path)
   {
-    nsCString path;
-
-    path.AppendLiteral("explicit/storage/sqlite/");
+    path.AssignLiteral("explicit/storage/sqlite/");
     path.Append(mDBConn.getFilename());
 
     if (mType == Cache_Used) {
       path.AppendLiteral("/cache-used");
     }
     else if (mType == Schema_Used) {
       path.AppendLiteral("/schema-used");
     }
     else if (mType == Stmt_Used) {
       path.AppendLiteral("/stmt-used");
     }
-
-    *memoryPath = ::ToNewCString(path);
     return NS_OK;
   }
 
   NS_IMETHOD GetKind(PRInt32 *kind)
   {
     *kind = KIND_HEAP;
     return NS_OK;
   }
@@ -401,27 +397,27 @@ public:
     }
 
     int cur=0, max=0;
     int rc = ::sqlite3_db_status(mDBConn, type, &cur, &max, 0);
     *amount = cur;
     return convertResultCode(rc);
   }
 
-  NS_IMETHOD GetDescription(char **desc)
+  NS_IMETHOD GetDescription(nsACString &desc)
   {
     if (mType == Cache_Used) {
-      *desc = ::strdup("Memory (approximate) used by all pager caches.");
+      desc.AssignLiteral("Memory (approximate) used by all pager caches.");
     }
     else if (mType == Schema_Used) {
-      *desc = ::strdup("Memory (approximate) used to store the schema "
-                       "for all databases associated with the connection");
+      desc.AssignLiteral("Memory (approximate) used to store the schema "
+                          "for all databases associated with the connection");
     }
     else if (mType == Stmt_Used) {
-      *desc = ::strdup("Memory (approximate) used by all prepared statements");
+      desc.AssignLiteral("Memory (approximate) used by all prepared statements");
     }
     return NS_OK;
   }
 
   Connection &mDBConn;
   nsCString mFileName;
   ReporterType mType;
 };
--- a/xpcom/base/nsIMemoryReporter.idl
+++ b/xpcom/base/nsIMemoryReporter.idl
@@ -45,27 +45,27 @@ interface nsISimpleEnumerator;
  * Use this when it makes sense to gather this measurement without gathering
  * related measurements at the same time.
  *
  * Note that the |amount| field may be implemented as a function, and so
  * accessing it can trigger significant computation;  the other fields can
  * be accessed without triggering this computation.  (Compare and contrast
  * this with nsIMemoryMultiReporter.)  
  */
-[scriptable, uuid(37d18434-9819-4ce1-922f-15d8b63da066)]
+[scriptable, uuid(b2c39f65-1799-4b92-a806-ab3cf6af3cfa)]
 interface nsIMemoryReporter : nsISupports
 {
   /*
    * The name of the process containing this reporter.  Each reporter initially
    * has "" in this field, indicating that it applies to the current process.
    * (This is true even for reporters in a child process.)  When a reporter
    * from a child process is copied into the main process, the copy has its
    * 'process' field set appropriately.
    */
-  readonly attribute string process;
+  readonly attribute ACString process;
 
   /*
    * The path that this memory usage should be reported under.  Paths are
    * '/'-delimited, eg. "a/b/c".  There are two categories of paths.
    *
    * - Paths starting with "explicit" represent regions of memory that have
    *   been explicitly allocated with an OS-level allocation (eg.
    *   mmap/VirtualAlloc/vm_allocate) or a heap-level allocation (eg.
@@ -89,17 +89,17 @@ interface nsIMemoryReporter : nsISupport
    *
    *   A node's children divide their parent's memory into disjoint pieces.
    *   So in the example above, |a| may not count any allocations counted by
    *   |d|, and vice versa.
    *
    * - All other paths represent cross-cutting values and may overlap with any
    *   other reporter.
    */
-  readonly attribute string path;
+  readonly attribute AUTF8String path;
 
   /*
    * There are three categories of memory reporters:
    *
    *  - MAPPED: memory allocated directly by the OS, e.g. by calling mmap,
    *    VirtualAlloc, or vm_allocate.  Reporters in this category must have
    *    units UNITS_BYTES and must have a path starting with "explicit".
    *
@@ -144,17 +144,17 @@ interface nsIMemoryReporter : nsISupport
    * The numeric value reported by this memory reporter.  -1 means "unknown",
    * ie. something went wrong when getting the amount.
    */
   readonly attribute PRInt64 amount;
 
   /*
    * A human-readable description of this memory usage report.
    */
-  readonly attribute string description;
+  readonly attribute AUTF8String description;
 };
 
 [scriptable, function, uuid(5b15f3fa-ba15-443c-8337-7770f5f0ce5d)]
 interface nsIMemoryMultiReporterCallback : nsISupports
 {
   void callback(in ACString process, in AUTF8String path, in PRInt32 kind,
                 in PRInt32 units, in PRInt64 amount,
                 in AUTF8String description, in nsISupports closure);
@@ -242,26 +242,26 @@ interface nsIMemoryReporterManager : nsI
   readonly attribute PRInt64 explicit;
 };
 
 %{C++
 
 /*
  * Note that this defaults 'process' to "", which is usually what's desired.
  */
-#define NS_MEMORY_REPORTER_IMPLEMENT(_classname, _path, _kind, _units, _usageFunction, _desc) \
+#define NS_MEMORY_REPORTER_IMPLEMENT(_classname, _path, _kind, _units, _amountFunction, _desc) \
     class MemoryReporter_##_classname : public nsIMemoryReporter {                            \
     public:                                                                                   \
       NS_DECL_ISUPPORTS                                                                       \
-      NS_IMETHOD GetProcess(char **process) { *process = strdup(""); return NS_OK; }          \
-      NS_IMETHOD GetPath(char **memoryPath) { *memoryPath = strdup(_path); return NS_OK; }    \
+      NS_IMETHOD GetProcess(nsACString &process) { process.Truncate(); return NS_OK; } \
+      NS_IMETHOD GetPath(nsACString &memoryPath) { memoryPath.Assign(_path); return NS_OK; } \
       NS_IMETHOD GetKind(int *kind) { *kind = _kind; return NS_OK; }                          \
       NS_IMETHOD GetUnits(int *units) { *units = _units; return NS_OK; }                      \
-      NS_IMETHOD GetAmount(PRInt64 *amount) { *amount = _usageFunction(); return NS_OK; }     \
-      NS_IMETHOD GetDescription(char **desc) { *desc = strdup(_desc); return NS_OK; }         \
+      NS_IMETHOD GetAmount(PRInt64 *amount) { *amount = _amountFunction(); return NS_OK; }     \
+      NS_IMETHOD GetDescription(nsACString &desc) { desc.Assign(_desc); return NS_OK; } \
     };                                                                                        \
     NS_IMPL_ISUPPORTS1(MemoryReporter_##_classname, nsIMemoryReporter)
 
 #define NS_MEMORY_REPORTER_NAME(_classname)  MemoryReporter_##_classname
 
 NS_COM nsresult NS_RegisterMemoryReporter (nsIMemoryReporter *reporter);
 NS_COM nsresult NS_RegisterMemoryMultiReporter (nsIMemoryMultiReporter *reporter);
 
--- a/xpcom/base/nsMemoryReporterManager.cpp
+++ b/xpcom/base/nsMemoryReporterManager.cpp
@@ -598,32 +598,32 @@ nsMemoryReporterManager::GetExplicit(PRI
         e->GetNext(getter_AddRefs(r));
 
         PRInt32 kind;
         nsresult rv = r->GetKind(&kind);
         NS_ENSURE_SUCCESS(rv, rv);
 
         if (kind == nsIMemoryReporter::KIND_MAPPED) {
             nsCString path;
-            rv = r->GetPath(getter_Copies(path));
+            rv = r->GetPath(path);
             NS_ENSURE_SUCCESS(rv, rv);
 
             PRInt64 amount;
             rv = r->GetAmount(&amount);
             NS_ENSURE_SUCCESS(rv, rv);
 
             // Just skip any MAPPED reporters that fail, because "heap-used" is
             // the most important one.
             if (amount != PRInt64(-1)) {
                 MemoryReport mr(path, amount);
                 mapped.AppendElement(mr);
             }
         } else {
             nsCString path;
-            rv = r->GetPath(getter_Copies(path));
+            rv = r->GetPath(path);
             NS_ENSURE_SUCCESS(rv, rv);
 
             if (path.Equals("heap-used")) {
                 rv = r->GetAmount(&heapUsed);
                 NS_ENSURE_SUCCESS(rv, rv);
                 // If "heap-used" fails, we give up, because the result would be
                 // horribly inaccurate.
                 if (heapUsed == PRInt64(-1)) {
@@ -669,44 +669,44 @@ nsMemoryReporterManager::GetExplicit(PRI
         *aExplicit += mapped[i].amount;
     }
 
     return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS1(nsMemoryReporter, nsIMemoryReporter)
 
-nsMemoryReporter::nsMemoryReporter(nsCString& process,
-                                   nsCString& path,
+nsMemoryReporter::nsMemoryReporter(nsACString& process,
+                                   nsACString& path,
                                    PRInt32 kind,
                                    PRInt32 units,
                                    PRInt64 amount,
-                                   nsCString& desc)
+                                   nsACString& desc)
 : mProcess(process)
 , mPath(path)
 , mKind(kind)
 , mUnits(units)
 , mAmount(amount)
 , mDesc(desc)
 {
 }
 
 nsMemoryReporter::~nsMemoryReporter()
 {
 }
 
-NS_IMETHODIMP nsMemoryReporter::GetProcess(char **aProcess)
+NS_IMETHODIMP nsMemoryReporter::GetProcess(nsACString &aProcess)
 {
-    *aProcess = strdup(mProcess.get());
+    aProcess.Assign(mProcess);
     return NS_OK;
 }
 
-NS_IMETHODIMP nsMemoryReporter::GetPath(char **aPath)
+NS_IMETHODIMP nsMemoryReporter::GetPath(nsACString &aPath)
 {
-    *aPath = strdup(mPath.get());
+    aPath.Assign(mPath);
     return NS_OK;
 }
 
 NS_IMETHODIMP nsMemoryReporter::GetKind(PRInt32 *aKind)
 {
     *aKind = mKind;
     return NS_OK;
 }
@@ -718,19 +718,19 @@ NS_IMETHODIMP nsMemoryReporter::GetUnits
 }
 
 NS_IMETHODIMP nsMemoryReporter::GetAmount(PRInt64 *aAmount)
 {
     *aAmount = mAmount;
     return NS_OK;
 }
 
-NS_IMETHODIMP nsMemoryReporter::GetDescription(char **aDescription)
+NS_IMETHODIMP nsMemoryReporter::GetDescription(nsACString &aDescription)
 {
-    *aDescription = strdup(mDesc.get());
+    aDescription.Assign(mDesc);
     return NS_OK;
 }
 
 NS_COM nsresult
 NS_RegisterMemoryReporter (nsIMemoryReporter *reporter)
 {
     nsCOMPtr<nsIMemoryReporterManager> mgr = do_GetService("@mozilla.org/memory-reporter-manager;1");
     if (mgr == nsnull)
--- a/xpcom/base/nsMemoryReporterManager.h
+++ b/xpcom/base/nsMemoryReporterManager.h
@@ -6,22 +6,22 @@
 using mozilla::Mutex;
 
 class nsMemoryReporter : public nsIMemoryReporter
 {
 public:
   NS_DECL_ISUPPORTS
   NS_DECL_NSIMEMORYREPORTER
 
-  nsMemoryReporter(nsCString& process,
-                   nsCString& path, 
+  nsMemoryReporter(nsACString& process,
+                   nsACString& path, 
                    PRInt32 kind,
                    PRInt32 units,
                    PRInt64 amount,
-                   nsCString& desc);
+                   nsACString& desc);
 
   ~nsMemoryReporter();
 
 protected:
   nsCString mProcess;
   nsCString mPath;
   PRInt32   mKind;
   PRInt32   mUnits;