8151534: Refactor ArrayAllocator for easier reuse
authorstefank
Wed, 09 Mar 2016 17:03:04 +0100
changeset 37096 42437cf54027
parent 37095 adfe20658079
child 37098 c750e40461d0
child 37106 0856b64af754
child 37117 27aec253e606
8151534: Refactor ArrayAllocator for easier reuse Reviewed-by: tschatzl, brutisso
hotspot/src/share/vm/memory/allocation.hpp
hotspot/src/share/vm/memory/allocation.inline.hpp
--- a/hotspot/src/share/vm/memory/allocation.hpp	Mon Mar 14 20:20:18 2016 +0100
+++ b/hotspot/src/share/vm/memory/allocation.hpp	Wed Mar 09 17:03:04 2016 +0100
@@ -716,9 +716,6 @@
  private:
   static bool should_use_malloc(size_t length);
 
-  static size_t size_for_malloc(size_t length);
-  static size_t size_for_mmap(size_t length);
-
   static E* allocate_malloc(size_t length);
   static E* allocate_mmap(size_t length);
 
@@ -731,4 +728,26 @@
   static void free(E* addr, size_t length);
 };
 
+// Uses mmaped memory for all allocations. All allocations are initially
+// zero-filled. No pre-touching.
+template <class E, MEMFLAGS F>
+class MmapArrayAllocator : public AllStatic {
+ private:
+  static size_t size_for(size_t length);
+
+ public:
+  static E* allocate(size_t length);
+  static void free(E* addr, size_t length);
+};
+
+// Uses malloc:ed memory for all allocations.
+template <class E, MEMFLAGS F>
+class MallocArrayAllocator : public AllStatic {
+ public:
+  static size_t size_for(size_t length);
+
+  static E* allocate(size_t length);
+  static void free(E* addr, size_t length);
+};
+
 #endif // SHARE_VM_MEMORY_ALLOCATION_HPP
--- a/hotspot/src/share/vm/memory/allocation.inline.hpp	Mon Mar 14 20:20:18 2016 +0100
+++ b/hotspot/src/share/vm/memory/allocation.inline.hpp	Wed Mar 09 17:03:04 2016 +0100
@@ -151,30 +151,15 @@
 }
 
 template <class E, MEMFLAGS F>
-size_t ArrayAllocator<E, F>::size_for_malloc(size_t length) {
-  return length * sizeof(E);
-}
-
-template <class E, MEMFLAGS F>
-size_t ArrayAllocator<E, F>::size_for_mmap(size_t length) {
+size_t MmapArrayAllocator<E, F>::size_for(size_t length) {
   size_t size = length * sizeof(E);
   int alignment = os::vm_allocation_granularity();
   return align_size_up(size, alignment);
 }
 
 template <class E, MEMFLAGS F>
-bool ArrayAllocator<E, F>::should_use_malloc(size_t length) {
-  return size_for_malloc(length) < ArrayAllocatorMallocLimit;
-}
-
-template <class E, MEMFLAGS F>
-E* ArrayAllocator<E, F>::allocate_malloc(size_t length) {
-  return (E*)AllocateHeap(size_for_malloc(length), F);
-}
-
-template <class E, MEMFLAGS F>
-E* ArrayAllocator<E, F>::allocate_mmap(size_t length) {
-  size_t size = size_for_mmap(length);
+E* MmapArrayAllocator<E, F>::allocate(size_t length) {
+  size_t size = size_for(length);
   int alignment = os::vm_allocation_granularity();
 
   char* addr = os::reserve_memory(size, NULL, alignment, F);
@@ -188,6 +173,42 @@
 }
 
 template <class E, MEMFLAGS F>
+void MmapArrayAllocator<E, F>::free(E* addr, size_t length) {
+  bool result = os::release_memory((char*)addr, size_for(length));
+  assert(result, "Failed to release memory");
+}
+
+template <class E, MEMFLAGS F>
+size_t MallocArrayAllocator<E, F>::size_for(size_t length) {
+  return length * sizeof(E);
+}
+
+template <class E, MEMFLAGS F>
+E* MallocArrayAllocator<E, F>::allocate(size_t length) {
+  return (E*)AllocateHeap(size_for(length), F);
+}
+
+template<class E, MEMFLAGS F>
+void MallocArrayAllocator<E, F>::free(E* addr, size_t /*length*/) {
+  FreeHeap(addr);
+}
+
+template <class E, MEMFLAGS F>
+bool ArrayAllocator<E, F>::should_use_malloc(size_t length) {
+  return MallocArrayAllocator<E, F>::size_for(length) < ArrayAllocatorMallocLimit;
+}
+
+template <class E, MEMFLAGS F>
+E* ArrayAllocator<E, F>::allocate_malloc(size_t length) {
+  return MallocArrayAllocator<E, F>::allocate(length);
+}
+
+template <class E, MEMFLAGS F>
+E* ArrayAllocator<E, F>::allocate_mmap(size_t length) {
+  return MmapArrayAllocator<E, F>::allocate(length);
+}
+
+template <class E, MEMFLAGS F>
 E* ArrayAllocator<E, F>::allocate(size_t length) {
   if (should_use_malloc(length)) {
     return allocate_malloc(length);
@@ -214,14 +235,13 @@
 }
 
 template<class E, MEMFLAGS F>
-void ArrayAllocator<E, F>::free_malloc(E* addr, size_t /*length*/) {
-  FreeHeap(addr);
+void ArrayAllocator<E, F>::free_malloc(E* addr, size_t length) {
+  MallocArrayAllocator<E, F>::free(addr, length);
 }
 
 template<class E, MEMFLAGS F>
 void ArrayAllocator<E, F>::free_mmap(E* addr, size_t length) {
-  bool result = os::release_memory((char*)addr, size_for_mmap(length));
-  assert(result, "Failed to release memory");
+  MmapArrayAllocator<E, F>::free(addr, length);
 }
 
 template<class E, MEMFLAGS F>