1 /*
   2  * Copyright (c) 1997, 2013, 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.inline.hpp"
  29 #include "runtime/os.hpp"
  30 
  31 // Explicit C-heap memory management
  32 
  33 void trace_heap_malloc(size_t size, const char* name, void *p);
  34 void trace_heap_free(void *p);
  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((volatile jlong*)dest);
  42   value += add_value;
  43   Atomic::store((jlong)value, (volatile jlong*)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, address pc = 0,
  53     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  54   if (pc == 0) {
  55     pc = CURRENT_PC;
  56   }
  57   char* p = (char*) os::malloc(size, flags, pc);
  58   #ifdef ASSERT
  59   if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p);
  60   #endif
  61   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  62     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");
  63   }
  64   return p;
  65 }
  66 
  67 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flags,
  68     AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
  69   char* p = (char*) os::realloc(old, size, flags, CURRENT_PC);
  70   #ifdef ASSERT
  71   if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p);
  72   #endif
  73   if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
  74     vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");
  75   }
  76   return p;
  77 }
  78 
  79 inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) {
  80   #ifdef ASSERT
  81   if (PrintMallocFree) trace_heap_free(p);
  82   #endif
  83   os::free(p, memflags);
  84 }
  85 
  86 
  87 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,
  88       address caller_pc) throw() {
  89     void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC));
  90 #ifdef ASSERT
  91     if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
  92 #endif
  93     return p;
  94   }
  95 
  96 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
  97   const std::nothrow_t&  nothrow_constant, address caller_pc) throw() {
  98   void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC),
  99       AllocFailStrategy::RETURN_NULL);
 100 #ifdef ASSERT
 101     if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
 102 #endif
 103     return p;
 104 }
 105 
 106 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 107       address caller_pc) throw() {
 108     return CHeapObj<F>::operator new(size, caller_pc);
 109 }
 110 
 111 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
 112   const std::nothrow_t&  nothrow_constant, address caller_pc) throw() {
 113     return CHeapObj<F>::operator new(size, nothrow_constant, caller_pc);
 114 }
 115 
 116 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
 117     FreeHeap(p, F);
 118 }
 119 
 120 template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){
 121     FreeHeap(p, F);
 122 }
 123 
 124 template <class E, MEMFLAGS F>
 125 char* ArrayAllocator<E, F>::allocate_inner(size_t &size, bool &use_malloc) {
 126   char* addr = NULL;
 127 
 128   if (use_malloc) {
 129     addr = AllocateHeap(size, F);
 130     if (addr == NULL && size >= (size_t)os::vm_allocation_granularity()) {
 131       // malloc failed let's try with mmap instead
 132       use_malloc = false;
 133     } else {
 134       return addr;
 135     }
 136   }
 137 
 138   int alignment = os::vm_allocation_granularity();
 139   size = align_size_up(size, alignment);
 140 
 141   addr = os::reserve_memory(size, NULL, alignment, F);
 142   if (addr == NULL) {
 143     vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "Allocator (reserve)");
 144   }
 145 
 146   os::commit_memory_or_exit(addr, size, !ExecMem, "Allocator (commit)");
 147   return addr;
 148 }
 149 
 150 template <class E, MEMFLAGS F>
 151 E* ArrayAllocator<E, F>::allocate(size_t length) {
 152   assert(_addr == NULL, "Already in use");
 153 
 154   _size = sizeof(E) * length;
 155   _use_malloc = should_use_malloc(_size);
 156   _addr = allocate_inner(_size, _use_malloc);
 157 
 158   return (E*)_addr;
 159 }
 160 
 161 template <class E, MEMFLAGS F>
 162 E* ArrayAllocator<E, F>::reallocate(size_t new_length) {
 163   size_t new_size = sizeof(E) * new_length;
 164   bool use_malloc = should_use_malloc(new_size);
 165   char* new_addr = allocate_inner(new_size, use_malloc);
 166 
 167   memcpy(new_addr, _addr, MIN2(new_size, _size));
 168 
 169   free();
 170   _size = new_size;
 171   _use_malloc = use_malloc;
 172   _addr = new_addr;
 173   return (E*)new_addr;
 174 }
 175 
 176 template<class E, MEMFLAGS F>
 177 void ArrayAllocator<E, F>::free() {
 178   if (_addr != NULL) {
 179     if (_use_malloc) {
 180       FreeHeap(_addr, F);
 181     } else {
 182       os::release_memory(_addr, _size);
 183     }
 184     _addr = NULL;
 185   }
 186 }
 187 
 188 #endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP