1 /* 2 * Copyright (c) 1997, 2014, 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 #include "services/memTracker.hpp" 31 32 // Explicit C-heap memory management 33 34 void trace_heap_malloc(size_t size, const char* name, void *p); 35 void trace_heap_free(void *p); 36 37 #ifndef PRODUCT 38 // Increments unsigned long value for statistics (not atomic on MP). 39 inline void inc_stat_counter(volatile julong* dest, julong add_value) { 40 #if defined(SPARC) || defined(X86) 41 // Sparc and X86 have atomic jlong (8 bytes) instructions 42 julong value = Atomic::load((volatile jlong*)dest); 43 value += add_value; 44 Atomic::store((jlong)value, (volatile jlong*)dest); 45 #else 46 // possible word-tearing during load/store 47 *dest += add_value; 48 #endif 49 } 50 #endif 51 52 // allocate using malloc; will fail if no memory available 53 inline char* AllocateHeap(size_t size, MEMFLAGS flags, 54 const NativeCallStack& stack, 55 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { 56 char* p = (char*) os::malloc(size, flags, stack); 57 #ifdef ASSERT 58 if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p); 59 #endif 60 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { 61 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap"); 62 } 63 return p; 64 } 65 inline char* AllocateHeap(size_t size, MEMFLAGS flags, 66 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { 67 return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode); 68 } 69 70 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag, 71 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { 72 char* p = (char*) os::realloc(old, size, flag, CURRENT_PC); 73 #ifdef ASSERT 74 if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p); 75 #endif 76 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { 77 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap"); 78 } 79 return p; 80 } 81 82 inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) { 83 #ifdef ASSERT 84 if (PrintMallocFree) trace_heap_free(p); 85 #endif 86 os::free(p, memflags); 87 } 88 89 90 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size, 91 const NativeCallStack& stack) throw() { 92 void* p = (void*)AllocateHeap(size, F, stack); 93 #ifdef ASSERT 94 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); 95 #endif 96 return p; 97 } 98 99 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size) throw() { 100 return CHeapObj<F>::operator new(size, CALLER_PC); 101 } 102 103 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, 104 const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() { 105 void* p = (void*)AllocateHeap(size, F, stack, 106 AllocFailStrategy::RETURN_NULL); 107 #ifdef ASSERT 108 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); 109 #endif 110 return p; 111 } 112 113 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, 114 const std::nothrow_t& nothrow_constant) throw() { 115 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC); 116 } 117 118 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, 119 const NativeCallStack& stack) throw() { 120 return CHeapObj<F>::operator new(size, stack); 121 } 122 123 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size) 124 throw() { 125 return CHeapObj<F>::operator new(size, CALLER_PC); 126 } 127 128 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, 129 const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() { 130 return CHeapObj<F>::operator new(size, nothrow_constant, stack); 131 } 132 133 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, 134 const std::nothrow_t& nothrow_constant) throw() { 135 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC); 136 } 137 138 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){ 139 FreeHeap(p, F); 140 } 141 142 template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){ 143 FreeHeap(p, F); 144 } 145 146 template <class E, MEMFLAGS F> 147 E* ArrayAllocator<E, F>::allocate(size_t length) { 148 assert(_addr == NULL, "Already in use"); 149 150 _size = sizeof(E) * length; 151 _use_malloc = _size < ArrayAllocatorMallocLimit; 152 153 if (_use_malloc) { 154 _addr = AllocateHeap(_size, F); 155 if (_addr == NULL && _size >= (size_t)os::vm_allocation_granularity()) { 156 // malloc failed let's try with mmap instead 157 _use_malloc = false; 158 } else { 159 return (E*)_addr; 160 } 161 } 162 163 int alignment = os::vm_allocation_granularity(); 164 _size = align_size_up(_size, alignment); 165 166 _addr = os::reserve_memory(_size, NULL, alignment, F); 167 if (_addr == NULL) { 168 vm_exit_out_of_memory(_size, OOM_MMAP_ERROR, "Allocator (reserve)"); 169 } 170 171 os::commit_memory_or_exit(_addr, _size, !ExecMem, "Allocator (commit)"); 172 173 return (E*)_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