Bug 1402174 - Add a helper class implementing the base allocator functions for a given arena. r?njn draft
authorMike Hommey <mh+mozilla@glandium.org>
Thu, 21 Sep 2017 14:24:37 +0900
changeset 669300 059e9f31fd24a604d9453cb9bd2350617e6deff9
parent 669299 16cd277ea46c6846d48a456c493ed7c56f880a9e
child 669301 6d70b1b24c9e5b8731073e55135211e8f7c41ea3
push id81294
push userbmo:mh+mozilla@glandium.org
push dateFri, 22 Sep 2017 21:53:13 +0000
reviewersnjn
bugs1402174
milestone58.0a1
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);