Bug 1402174 - Add a helper class implementing the base allocator functions for a given arena. r=njn
☠☠ backed out by cc3be123aa1f ☠ ☠
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 21 Sep 2017 14:24:37 +0900
changeset 382427 c589ad71d9cafce2d7e4e6b216bbe6a33b2bd799
parent 382426 e356ac32e2978d2631f771ab270309f9e5f2ca65
child 382428 b23c870c7e45e3b9cc3f519eba1d9e58389a3c3d
push id32558
push userkwierso@gmail.com
push dateFri, 22 Sep 2017 21:29:46 +0000
treeherdermozilla-central@61e58a7d800b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnjn
bugs1402174
milestone58.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1402174 - Add a helper class implementing the base allocator functions for a given arena. r=njn
memory/build/mozjemalloc.cpp
--- a/memory/build/mozjemalloc.cpp
+++ b/memory/build/mozjemalloc.cpp
@@ -4671,63 +4671,90 @@ MALLOC_OUT:
 
 /*
  * End general internal functions.
  */
 /******************************************************************************/
 /*
  * Begin malloc(3)-compatible functions.
  */
-template<> inline void*
-MozJemalloc::malloc(size_t aSize)
+
+/* The BaseAllocator class is a helper class that implements the base allocator
+ * functions (malloc, calloc, realloc, free, memalign) for a given arena,
+ * or an appropriately chosen arena (per choose_arena()) when none is given. */
+struct BaseAllocator {
+#define MALLOC_DECL(name, return_type, ...) \
+  inline return_type name(__VA_ARGS__);
+
+#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
+#include "malloc_decls.h"
+
+  explicit BaseAllocator(arena_t* aArena) : mArena(aArena) { }
+
+private:
+  arena_t* mArena;
+};
+
+#define MALLOC_DECL(name, return_type, ...) \
+  template<> inline return_type \
+  MozJemalloc::name(ARGS_HELPER(TYPED_ARGS, ##__VA_ARGS__)) \
+  { \
+    BaseAllocator allocator(nullptr); \
+    return allocator.name(ARGS_HELPER(ARGS, ##__VA_ARGS__)); \
+  }
+#define MALLOC_FUNCS MALLOC_FUNCS_MALLOC_BASE
+#include "malloc_decls.h"
+
+inline void*
+BaseAllocator::malloc(size_t aSize)
 {
   void* ret;
 
   if (malloc_init()) {
     ret = nullptr;
     goto RETURN;
   }
 
   if (aSize == 0) {
     aSize = 1;
   }
 
-  ret = imalloc(aSize, /* zero = */ false, nullptr);
+  ret = imalloc(aSize, /* zero = */ false, mArena);
 
 RETURN:
   if (!ret) {
     errno = ENOMEM;
   }
 
   return ret;
 }
 
-template<> inline void*
-MozJemalloc::memalign(size_t aAlignment, size_t aSize)
+inline void*
+BaseAllocator::memalign(size_t aAlignment, size_t aSize)
 {
   void* ret;
 
   MOZ_ASSERT(((aAlignment - 1) & aAlignment) == 0);
 
   if (malloc_init()) {
     return nullptr;
   }
 
   if (aSize == 0) {
     aSize = 1;
   }
 
   aAlignment = aAlignment < sizeof(void*) ? sizeof(void*) : aAlignment;
-  ret = ipalloc(aAlignment, aSize, nullptr);
+  ret = ipalloc(aAlignment, aSize, mArena);
 
   return ret;
 }
 
-template<> inline void*
-MozJemalloc::calloc(size_t aNum, size_t aSize)
+inline void*
+BaseAllocator::calloc(size_t aNum, size_t aSize)
 {
   void *ret;
   size_t num_size;
 
   if (malloc_init()) {
     num_size = 0;
     ret = nullptr;
     goto RETURN;
@@ -4743,60 +4770,60 @@ MozJemalloc::calloc(size_t aNum, size_t 
    */
   } else if (((aNum | aSize) & (SIZE_T_MAX << (sizeof(size_t) << 2)))
       && (num_size / aSize != aNum)) {
     /* size_t overflow. */
     ret = nullptr;
     goto RETURN;
   }
 
-  ret = imalloc(num_size, /* zero = */ true, nullptr);
+  ret = imalloc(num_size, /* zero = */ true, mArena);
 
 RETURN:
   if (!ret) {
     errno = ENOMEM;
   }
 
   return ret;
 }
 
-template<> inline void*
-MozJemalloc::realloc(void* aPtr, size_t aSize)
+inline void*
+BaseAllocator::realloc(void* aPtr, size_t aSize)
 {
   void* ret;
 
   if (aSize == 0) {
     aSize = 1;
   }
 
   if (aPtr) {
     MOZ_ASSERT(malloc_initialized);
 
-    ret = iralloc(aPtr, aSize, nullptr);
+    ret = iralloc(aPtr, aSize, mArena);
 
     if (!ret) {
       errno = ENOMEM;
     }
   } else {
     if (malloc_init()) {
       ret = nullptr;
     } else {
-      ret = imalloc(aSize, /* zero = */ false, nullptr);
+      ret = imalloc(aSize, /* zero = */ false, mArena);
     }
 
     if (!ret) {
       errno = ENOMEM;
     }
   }
 
   return ret;
 }
 
-template<> inline void
-MozJemalloc::free(void* aPtr)
+inline void
+BaseAllocator::free(void* aPtr)
 {
   size_t offset;
 
   /*
    * A version of idalloc that checks for nullptr pointer but only for
    * huge allocations assuming that CHUNK_ADDR2OFFSET(nullptr) == 0.
    */
   MOZ_ASSERT(CHUNK_ADDR2OFFSET(nullptr) == 0);