Bug 1420355 - Don't declare replace_* functions in replace_malloc.h. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Tue, 28 Nov 2017 07:18:15 +0900
changeset 704801 ef8d52dd28a9a086e7eece3fca0da9a0a3e4e538
parent 704800 47afa94a3fa4db110d22925d19108be7cbcf8f5a
child 704802 3c2416559b383e65423ac2826fb153a6ed6c5532
push id91252
push userbmo:mh+mozilla@glandium.org
push dateWed, 29 Nov 2017 00:22:21 +0000
reviewersnjn
bugs1420355, 1420353
milestone59.0a1
Bug 1420355 - Don't declare replace_* functions in replace_malloc.h. r?njn The original purpose of those declarations was to avoid the function definitions being wrong, as well as forcing them being exported properly (as extern "C", as weak symbols when necessary, etc.), but: - The implementations being C++, function overloads simply allowed functions with the same name to have a different signature. - As of bug 1420353, the functions don't need to be exported anymore, nor do we care whether their symbols are mangled. Furthermore, they're now being assigned to function table fields, meaning there is type checking in place, now. So all in all, these declarations can be removed. Also, as further down the line we're going to statically link the replace-malloc libraries, avoid symbol conflicts by making those functions static.
memory/build/replace_malloc.h
memory/replace/dmd/DMD.cpp
memory/replace/logalloc/LogAlloc.cpp
--- a/memory/build/replace_malloc.h
+++ b/memory/build/replace_malloc.h
@@ -85,16 +85,11 @@ MOZ_BEGIN_EXTERN_C
 #ifndef MOZ_REPLACE_WEAK
 #define MOZ_REPLACE_WEAK
 #endif
 
 // Replace-malloc library initialization function. See top of this file
 MOZ_EXPORT void
 replace_init(malloc_table_t*, struct ReplaceMallocBridge**) MOZ_REPLACE_WEAK;
 
-// Define the replace_* functions as not exported.
-#define MALLOC_DECL(name, return_type, ...)                                    \
-  return_type replace_##name(__VA_ARGS__);
-#include "malloc_decls.h"
-
 MOZ_END_EXTERN_C
 
 #endif // replace_malloc_h
--- a/memory/replace/dmd/DMD.cpp
+++ b/memory/replace/dmd/DMD.cpp
@@ -1259,27 +1259,17 @@ FreeCallback(void* aPtr, Thread* aT, Dea
 // malloc/free interception
 //---------------------------------------------------------------------------
 
 static void Init(malloc_table_t* aMallocTable);
 
 } // namespace dmd
 } // namespace mozilla
 
-void
-replace_init(malloc_table_t* aMallocTable, ReplaceMallocBridge** aBridge)
-{
-  mozilla::dmd::Init(aMallocTable);
-#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
-#define MALLOC_DECL(name, ...) aMallocTable->name = replace_ ## name;
-#include "malloc_decls.h"
-  *aBridge = mozilla::dmd::gDMDBridge;
-}
-
-void*
+static void*
 replace_malloc(size_t aSize)
 {
   using namespace mozilla::dmd;
 
   if (!gIsDMDInitialized) {
     // DMD hasn't started up, either because it wasn't enabled by the user, or
     // we're still in Init() and something has indirectly called malloc.  Do a
     // vanilla malloc.  (In the latter case, if it fails we'll crash.  But
@@ -1295,17 +1285,17 @@ replace_malloc(size_t aSize)
   }
 
   // This must be a call to malloc from outside DMD.  Intercept it.
   void* ptr = gMallocTable.malloc(aSize);
   AllocCallback(ptr, aSize, t);
   return ptr;
 }
 
-void*
+static void*
 replace_calloc(size_t aCount, size_t aSize)
 {
   using namespace mozilla::dmd;
 
   if (!gIsDMDInitialized) {
     return gMallocTable.calloc(aCount, aSize);
   }
 
@@ -1314,17 +1304,17 @@ replace_calloc(size_t aCount, size_t aSi
     return InfallibleAllocPolicy::calloc_(aCount * aSize);
   }
 
   void* ptr = gMallocTable.calloc(aCount, aSize);
   AllocCallback(ptr, aCount * aSize, t);
   return ptr;
 }
 
-void*
+static void*
 replace_realloc(void* aOldPtr, size_t aSize)
 {
   using namespace mozilla::dmd;
 
   if (!gIsDMDInitialized) {
     return gMallocTable.realloc(aOldPtr, aSize);
   }
 
@@ -1355,17 +1345,17 @@ replace_realloc(void* aOldPtr, size_t aS
     // block will end up looking like it was allocated for the first time here,
     // which is untrue, and the slop bytes will be zero, which may be untrue.
     // But this case is rare and doing better isn't worth the effort.
     AllocCallback(aOldPtr, gMallocTable.malloc_usable_size(aOldPtr), t);
   }
   return ptr;
 }
 
-void*
+static void*
 replace_memalign(size_t aAlignment, size_t aSize)
 {
   using namespace mozilla::dmd;
 
   if (!gIsDMDInitialized) {
     return gMallocTable.memalign(aAlignment, aSize);
   }
 
@@ -1374,17 +1364,17 @@ replace_memalign(size_t aAlignment, size
     return InfallibleAllocPolicy::memalign_(aAlignment, aSize);
   }
 
   void* ptr = gMallocTable.memalign(aAlignment, aSize);
   AllocCallback(ptr, aSize, t);
   return ptr;
 }
 
-void
+static void
 replace_free(void* aPtr)
 {
   using namespace mozilla::dmd;
 
   if (!gIsDMDInitialized) {
     gMallocTable.free(aPtr);
     return;
   }
@@ -1398,16 +1388,26 @@ replace_free(void* aPtr)
   // could call malloc and get the freed block and update the table, and then
   // our update here would remove the newly-malloc'd block.
   DeadBlock db;
   FreeCallback(aPtr, t, &db);
   MaybeAddToDeadBlockTable(db);
   gMallocTable.free(aPtr);
 }
 
+void
+replace_init(malloc_table_t* aMallocTable, ReplaceMallocBridge** aBridge)
+{
+  mozilla::dmd::Init(aMallocTable);
+#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
+#define MALLOC_DECL(name, ...) aMallocTable->name = replace_ ## name;
+#include "malloc_decls.h"
+  *aBridge = mozilla::dmd::gDMDBridge;
+}
+
 namespace mozilla {
 namespace dmd {
 
 //---------------------------------------------------------------------------
 // Options (Part 2)
 //---------------------------------------------------------------------------
 
 // Given an |aOptionName| like "foo", succeed if |aArg| has the form "foo=blah"
--- a/memory/replace/logalloc/LogAlloc.cpp
+++ b/memory/replace/logalloc/LogAlloc.cpp
@@ -68,16 +68,125 @@ class LogAllocBridge : public ReplaceMal
 {
   virtual void InitDebugFd(mozilla::DebugFdRegistry& aRegistry) override {
     if (!sStdoutOrStderr) {
       aRegistry.RegisterHandle(sFd);
     }
   }
 };
 
+/* Do a simple, text-form, log of all calls to replace-malloc functions.
+ * Use locking to guarantee that an allocation that did happen is logged
+ * before any other allocation/free happens.
+ */
+
+static void*
+replace_malloc(size_t aSize)
+{
+  AutoLock lock(sLock);
+  void* ptr = sFuncs.malloc(aSize);
+  if (ptr) {
+    FdPrintf(sFd, "%zu %zu malloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr);
+  }
+  return ptr;
+}
+
+#ifndef LOGALLOC_MINIMAL
+static int
+replace_posix_memalign(void** aPtr, size_t aAlignment, size_t aSize)
+{
+  AutoLock lock(sLock);
+  int ret = sFuncs.posix_memalign(aPtr, aAlignment, aSize);
+  if (ret == 0) {
+    FdPrintf(sFd, "%zu %zu posix_memalign(%zu,%zu)=%p\n", GetPid(), GetTid(),
+             aAlignment, aSize, *aPtr);
+  }
+  return ret;
+}
+
+static void*
+replace_aligned_alloc(size_t aAlignment, size_t aSize)
+{
+  AutoLock lock(sLock);
+  void* ptr = sFuncs.aligned_alloc(aAlignment, aSize);
+  if (ptr) {
+    FdPrintf(sFd, "%zu %zu aligned_alloc(%zu,%zu)=%p\n", GetPid(), GetTid(),
+             aAlignment, aSize, ptr);
+  }
+  return ptr;
+}
+#endif
+
+static void*
+replace_calloc(size_t aNum, size_t aSize)
+{
+  AutoLock lock(sLock);
+  void* ptr = sFuncs.calloc(aNum, aSize);
+  if (ptr) {
+    FdPrintf(sFd, "%zu %zu calloc(%zu,%zu)=%p\n", GetPid(), GetTid(), aNum,
+             aSize, ptr);
+  }
+  return ptr;
+}
+
+static void*
+replace_realloc(void* aPtr, size_t aSize)
+{
+  AutoLock lock(sLock);
+  void* new_ptr = sFuncs.realloc(aPtr, aSize);
+  if (new_ptr || !aSize) {
+    FdPrintf(sFd, "%zu %zu realloc(%p,%zu)=%p\n", GetPid(), GetTid(), aPtr,
+             aSize, new_ptr);
+  }
+  return new_ptr;
+}
+
+static void
+replace_free(void* aPtr)
+{
+  AutoLock lock(sLock);
+  if (aPtr) {
+    FdPrintf(sFd, "%zu %zu free(%p)\n", GetPid(), GetTid(), aPtr);
+  }
+  sFuncs.free(aPtr);
+}
+
+static void*
+replace_memalign(size_t aAlignment, size_t aSize)
+{
+  AutoLock lock(sLock);
+  void* ptr = sFuncs.memalign(aAlignment, aSize);
+  if (ptr) {
+    FdPrintf(sFd, "%zu %zu memalign(%zu,%zu)=%p\n", GetPid(), GetTid(),
+             aAlignment, aSize, ptr);
+  }
+  return ptr;
+}
+
+#ifndef LOGALLOC_MINIMAL
+static void*
+replace_valloc(size_t aSize)
+{
+  AutoLock lock(sLock);
+  void* ptr = sFuncs.valloc(aSize);
+  if (ptr) {
+    FdPrintf(sFd, "%zu %zu valloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr);
+  }
+  return ptr;
+}
+#endif
+
+static void
+replace_jemalloc_stats(jemalloc_stats_t* aStats)
+{
+  AutoLock lock(sLock);
+  sFuncs.jemalloc_stats(aStats);
+  FdPrintf(sFd, "%zu %zu jemalloc_stats()\n", GetPid(), GetTid());
+}
+
 void
 replace_init(malloc_table_t* aTable, ReplaceMallocBridge** aBridge)
 {
   static LogAllocBridge bridge;
   sFuncs = *aTable;
 #define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
 #define MALLOC_DECL(name, ...) aTable->name = replace_ ## name;
 #include "malloc_decls.h"
@@ -164,117 +273,8 @@ replace_init(malloc_table_t* aTable, Rep
       fd = open(log, O_WRONLY | O_CREAT | O_APPEND, 0644);
     }
     if (fd > 0) {
       sFd = fd;
     }
 #endif
   }
 }
-
-/* Do a simple, text-form, log of all calls to replace-malloc functions.
- * Use locking to guarantee that an allocation that did happen is logged
- * before any other allocation/free happens.
- */
-
-void*
-replace_malloc(size_t aSize)
-{
-  AutoLock lock(sLock);
-  void* ptr = sFuncs.malloc(aSize);
-  if (ptr) {
-    FdPrintf(sFd, "%zu %zu malloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr);
-  }
-  return ptr;
-}
-
-#ifndef LOGALLOC_MINIMAL
-int
-replace_posix_memalign(void** aPtr, size_t aAlignment, size_t aSize)
-{
-  AutoLock lock(sLock);
-  int ret = sFuncs.posix_memalign(aPtr, aAlignment, aSize);
-  if (ret == 0) {
-    FdPrintf(sFd, "%zu %zu posix_memalign(%zu,%zu)=%p\n", GetPid(), GetTid(),
-             aAlignment, aSize, *aPtr);
-  }
-  return ret;
-}
-
-void*
-replace_aligned_alloc(size_t aAlignment, size_t aSize)
-{
-  AutoLock lock(sLock);
-  void* ptr = sFuncs.aligned_alloc(aAlignment, aSize);
-  if (ptr) {
-    FdPrintf(sFd, "%zu %zu aligned_alloc(%zu,%zu)=%p\n", GetPid(), GetTid(),
-             aAlignment, aSize, ptr);
-  }
-  return ptr;
-}
-#endif
-
-void*
-replace_calloc(size_t aNum, size_t aSize)
-{
-  AutoLock lock(sLock);
-  void* ptr = sFuncs.calloc(aNum, aSize);
-  if (ptr) {
-    FdPrintf(sFd, "%zu %zu calloc(%zu,%zu)=%p\n", GetPid(), GetTid(), aNum,
-             aSize, ptr);
-  }
-  return ptr;
-}
-
-void*
-replace_realloc(void* aPtr, size_t aSize)
-{
-  AutoLock lock(sLock);
-  void* new_ptr = sFuncs.realloc(aPtr, aSize);
-  if (new_ptr || !aSize) {
-    FdPrintf(sFd, "%zu %zu realloc(%p,%zu)=%p\n", GetPid(), GetTid(), aPtr,
-             aSize, new_ptr);
-  }
-  return new_ptr;
-}
-
-void
-replace_free(void* aPtr)
-{
-  AutoLock lock(sLock);
-  if (aPtr) {
-    FdPrintf(sFd, "%zu %zu free(%p)\n", GetPid(), GetTid(), aPtr);
-  }
-  sFuncs.free(aPtr);
-}
-
-void*
-replace_memalign(size_t aAlignment, size_t aSize)
-{
-  AutoLock lock(sLock);
-  void* ptr = sFuncs.memalign(aAlignment, aSize);
-  if (ptr) {
-    FdPrintf(sFd, "%zu %zu memalign(%zu,%zu)=%p\n", GetPid(), GetTid(),
-             aAlignment, aSize, ptr);
-  }
-  return ptr;
-}
-
-#ifndef LOGALLOC_MINIMAL
-void*
-replace_valloc(size_t aSize)
-{
-  AutoLock lock(sLock);
-  void* ptr = sFuncs.valloc(aSize);
-  if (ptr) {
-    FdPrintf(sFd, "%zu %zu valloc(%zu)=%p\n", GetPid(), GetTid(), aSize, ptr);
-  }
-  return ptr;
-}
-#endif
-
-void
-replace_jemalloc_stats(jemalloc_stats_t* aStats)
-{
-  AutoLock lock(sLock);
-  sFuncs.jemalloc_stats(aStats);
-  FdPrintf(sFd, "%zu %zu jemalloc_stats()\n", GetPid(), GetTid());
-}