1 /*
   2  * Copyright (c) 1997, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
  26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
  27 
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/os.hpp"
  30 #include "services/memTracker.hpp"
  31 #include "utilities/align.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 
  34 // Explicit C-heap memory management
  35 
  36 #ifndef PRODUCT
  37 // Increments unsigned long value for statistics (not atomic on MP).
  38 inline void inc_stat_counter(volatile julong* dest, julong add_value) {
  39 #if defined(SPARC) || defined(X86)
  40   // Sparc and X86 have atomic jlong (8 bytes) instructions
  41   julong value = Atomic::load(dest);
  42   value += add_value;
  43   Atomic::store(value, dest);
  44 #else
  45   // possible word-tearing during load/store
  46   *dest += add_value;
  47 #endif
  48 }
  49 #endif
  50 
  51 // allocate using malloc; will fail if no memory available
  52 inline char* AllocateHeap(size_t size, MEMFLAGS flags,
  53     const NativeCallStack& stack,
  54     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  55   char* p = (char*) os::malloc(size, flags, stack);
  56   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  57     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");
  58   }
  59   return p;
  60 }
  61 
  62 ALWAYSINLINE char* AllocateHeap(size_t size, MEMFLAGS flags,
  63     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  64   return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode);
  65 }
  66 
  67 ALWAYSINLINE char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag,
  68     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  69   char* p = (char*) os::realloc(old, size, flag, CURRENT_PC);
  70   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  71     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");
  72   }
  73   return p;
  74 }
  75 
  76 inline void FreeHeap(void* p) {
  77   os::free(p);
  78 }
  79 
  80 
  81 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,
  82       const NativeCallStack& stack) throw() {
  83   return (void*)AllocateHeap(size, F, stack);
  84 }
  85 
  86 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size) throw() {
  87   return CHeapObj<F>::operator new(size, CALLER_PC);
  88 }
  89 
  90 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
  91       const std::nothrow_t&  nothrow_constant, const NativeCallStack& stack) throw() {
  92   return (void*)AllocateHeap(size, F, stack, AllocFailStrategy::RETURN_NULL);
  93 }
  94 
  95 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
  96   const std::nothrow_t& nothrow_constant) throw() {
  97   return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);
  98 }
  99 
 100 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 101       const NativeCallStack& stack) throw() {
 102   return CHeapObj<F>::operator new(size, stack);
 103 }
 104 
 105 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size)
 106   throw() {
 107   return CHeapObj<F>::operator new(size, CALLER_PC);
 108 }
 109 
 110 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 111   const std::nothrow_t&  nothrow_constant, const NativeCallStack& stack) throw() {
 112   return CHeapObj<F>::operator new(size, nothrow_constant, stack);
 113 }
 114 
 115 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 116   const std::nothrow_t& nothrow_constant) throw() {
 117   return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);
 118 }
 119 
 120 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
 121     FreeHeap(p);
 122 }
 123 
 124 template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){
 125     FreeHeap(p);
 126 }
 127 
 128 template <class E>
 129 size_t MmapArrayAllocator<E>::size_for(size_t length) {
 130   size_t size = length * sizeof(E);
 131   int alignment = os::vm_allocation_granularity();
 132   return align_up(size, alignment);
 133 }
 134 
 135 template <class E>
 136 E* MmapArrayAllocator<E>::allocate_or_null(size_t length, MEMFLAGS flags) {
 137   size_t size = size_for(length);
 138   int alignment = os::vm_allocation_granularity();
 139 
 140   char* addr = os::reserve_memory(size, NULL, alignment, flags);
 141   if (addr == NULL) {
 142     return NULL;
 143   }
 144 
 145   if (os::commit_memory(addr, size, !ExecMem)) {
 146     return (E*)addr;
 147   } else {
 148     os::release_memory(addr, size);
 149     return NULL;
 150   }
 151 }
 152 
 153 template <class E>
 154 E* MmapArrayAllocator<E>::allocate(size_t length, MEMFLAGS flags) {
 155   size_t size = size_for(length);
 156   int alignment = os::vm_allocation_granularity();
 157 
 158   char* addr = os::reserve_memory(size, NULL, alignment, flags);
 159   if (addr == NULL) {
 160     vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "Allocator (reserve)");
 161   }
 162 
 163   os::commit_memory_or_exit(addr, size, !ExecMem, "Allocator (commit)");
 164 
 165   return (E*)addr;
 166 }
 167 
 168 template <class E>
 169 void MmapArrayAllocator<E>::free(E* addr, size_t length) {
 170   bool result = os::release_memory((char*)addr, size_for(length));
 171   assert(result, "Failed to release memory");
 172 }
 173 
 174 template <class E>
 175 size_t MallocArrayAllocator<E>::size_for(size_t length) {
 176   return length * sizeof(E);
 177 }
 178 
 179 template <class E>
 180 E* MallocArrayAllocator<E>::allocate(size_t length, MEMFLAGS flags) {
 181   return (E*)AllocateHeap(size_for(length), flags);
 182 }
 183 
 184 template<class E>
 185 void MallocArrayAllocator<E>::free(E* addr, size_t /*length*/) {
 186   FreeHeap(addr);
 187 }
 188 
 189 template <class E>
 190 bool ArrayAllocator<E>::should_use_malloc(size_t length) {
 191   return MallocArrayAllocator<E>::size_for(length) < ArrayAllocatorMallocLimit;
 192 }
 193 
 194 template <class E>
 195 E* ArrayAllocator<E>::allocate_malloc(size_t length, MEMFLAGS flags) {
 196   return MallocArrayAllocator<E>::allocate(length, flags);
 197 }
 198 
 199 template <class E>
 200 E* ArrayAllocator<E>::allocate_mmap(size_t length, MEMFLAGS flags) {
 201   return MmapArrayAllocator<E>::allocate(length, flags);
 202 }
 203 
 204 template <class E>
 205 E* ArrayAllocator<E>::allocate(size_t length, MEMFLAGS flags) {
 206   if (should_use_malloc(length)) {
 207     return allocate_malloc(length, flags);
 208   }
 209 
 210   return allocate_mmap(length, flags);
 211 }
 212 
 213 template <class E>
 214 E* ArrayAllocator<E>::reallocate(E* old_addr, size_t old_length, size_t new_length, MEMFLAGS flags) {
 215   E* new_addr = (new_length > 0)
 216       ? allocate(new_length, flags)
 217       : NULL;
 218 
 219   if (new_addr != NULL && old_addr != NULL) {
 220     memcpy(new_addr, old_addr, MIN2(old_length, new_length) * sizeof(E));
 221   }
 222 
 223   if (old_addr != NULL) {
 224     free(old_addr, old_length);
 225   }
 226 
 227   return new_addr;
 228 }
 229 
 230 template<class E>
 231 void ArrayAllocator<E>::free_malloc(E* addr, size_t length) {
 232   MallocArrayAllocator<E>::free(addr, length);
 233 }
 234 
 235 template<class E>
 236 void ArrayAllocator<E>::free_mmap(E* addr, size_t length) {
 237   MmapArrayAllocator<E>::free(addr, length);
 238 }
 239 
 240 template<class E>
 241 void ArrayAllocator<E>::free(E* addr, size_t length) {
 242   if (addr != NULL) {
 243     if (should_use_malloc(length)) {
 244       free_malloc(addr, length);
 245     } else {
 246       free_mmap(addr, length);
 247     }
 248   }
 249 }
 250 
 251 #endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP