src/share/vm/memory/allocation.inline.hpp

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 7994
04ff2f6cd0eb
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

aoqi@0 1 /*
zgu@7074 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
aoqi@0 27
aoqi@0 28 #include "runtime/atomic.inline.hpp"
aoqi@0 29 #include "runtime/os.hpp"
zgu@7074 30 #include "services/memTracker.hpp"
aoqi@0 31
aoqi@0 32 // Explicit C-heap memory management
aoqi@0 33
aoqi@0 34 void trace_heap_malloc(size_t size, const char* name, void *p);
aoqi@0 35 void trace_heap_free(void *p);
aoqi@0 36
aoqi@0 37 #ifndef PRODUCT
aoqi@0 38 // Increments unsigned long value for statistics (not atomic on MP).
aoqi@0 39 inline void inc_stat_counter(volatile julong* dest, julong add_value) {
aoqi@0 40 #if defined(SPARC) || defined(X86)
aoqi@0 41 // Sparc and X86 have atomic jlong (8 bytes) instructions
aoqi@0 42 julong value = Atomic::load((volatile jlong*)dest);
aoqi@0 43 value += add_value;
aoqi@0 44 Atomic::store((jlong)value, (volatile jlong*)dest);
aoqi@0 45 #else
aoqi@0 46 // possible word-tearing during load/store
aoqi@0 47 *dest += add_value;
aoqi@0 48 #endif
aoqi@0 49 }
aoqi@0 50 #endif
aoqi@0 51
aoqi@0 52 // allocate using malloc; will fail if no memory available
zgu@7074 53 inline char* AllocateHeap(size_t size, MEMFLAGS flags,
zgu@7074 54 const NativeCallStack& stack,
aoqi@0 55 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
zgu@7074 56 char* p = (char*) os::malloc(size, flags, stack);
aoqi@0 57 #ifdef ASSERT
aoqi@0 58 if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p);
aoqi@0 59 #endif
aoqi@0 60 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
aoqi@0 61 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap");
aoqi@0 62 }
aoqi@0 63 return p;
aoqi@0 64 }
ysuenaga@7806 65
ysuenaga@7806 66 #ifdef __GNUC__
ysuenaga@7806 67 __attribute__((always_inline))
ysuenaga@7806 68 #endif
zgu@7074 69 inline char* AllocateHeap(size_t size, MEMFLAGS flags,
zgu@7074 70 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
zgu@7074 71 return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode);
zgu@7074 72 }
aoqi@0 73
ysuenaga@7806 74 #ifdef __GNUC__
ysuenaga@7806 75 __attribute__((always_inline))
ysuenaga@7806 76 #endif
zgu@7074 77 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag,
aoqi@0 78 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
zgu@7074 79 char* p = (char*) os::realloc(old, size, flag, CURRENT_PC);
aoqi@0 80 #ifdef ASSERT
aoqi@0 81 if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p);
aoqi@0 82 #endif
aoqi@0 83 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) {
aoqi@0 84 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap");
aoqi@0 85 }
aoqi@0 86 return p;
aoqi@0 87 }
aoqi@0 88
aoqi@0 89 inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) {
aoqi@0 90 #ifdef ASSERT
aoqi@0 91 if (PrintMallocFree) trace_heap_free(p);
aoqi@0 92 #endif
aoqi@0 93 os::free(p, memflags);
aoqi@0 94 }
aoqi@0 95
aoqi@0 96
aoqi@0 97 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,
zgu@7074 98 const NativeCallStack& stack) throw() {
zgu@7074 99 void* p = (void*)AllocateHeap(size, F, stack);
zgu@7074 100 #ifdef ASSERT
zgu@7074 101 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
zgu@7074 102 #endif
zgu@7074 103 return p;
zgu@7074 104 }
zgu@7074 105
zgu@7074 106 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size) throw() {
zgu@7074 107 return CHeapObj<F>::operator new(size, CALLER_PC);
zgu@7074 108 }
zgu@7074 109
zgu@7074 110 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
zgu@7074 111 const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() {
zgu@7074 112 void* p = (void*)AllocateHeap(size, F, stack,
zgu@7074 113 AllocFailStrategy::RETURN_NULL);
aoqi@0 114 #ifdef ASSERT
aoqi@0 115 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
aoqi@0 116 #endif
aoqi@0 117 return p;
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
zgu@7074 121 const std::nothrow_t& nothrow_constant) throw() {
zgu@7074 122 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
zgu@7074 126 const NativeCallStack& stack) throw() {
zgu@7074 127 return CHeapObj<F>::operator new(size, stack);
zgu@7074 128 }
zgu@7074 129
zgu@7074 130 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size)
zgu@7074 131 throw() {
zgu@7074 132 return CHeapObj<F>::operator new(size, CALLER_PC);
aoqi@0 133 }
aoqi@0 134
aoqi@0 135 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
zgu@7074 136 const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() {
zgu@7074 137 return CHeapObj<F>::operator new(size, nothrow_constant, stack);
zgu@7074 138 }
zgu@7074 139
zgu@7074 140 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size,
zgu@7074 141 const std::nothrow_t& nothrow_constant) throw() {
zgu@7074 142 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC);
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
aoqi@0 146 FreeHeap(p, F);
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){
aoqi@0 150 FreeHeap(p, F);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 template <class E, MEMFLAGS F>
aoqi@0 154 E* ArrayAllocator<E, F>::allocate(size_t length) {
aoqi@0 155 assert(_addr == NULL, "Already in use");
aoqi@0 156
aoqi@0 157 _size = sizeof(E) * length;
aoqi@0 158 _use_malloc = _size < ArrayAllocatorMallocLimit;
aoqi@0 159
aoqi@0 160 if (_use_malloc) {
aoqi@0 161 _addr = AllocateHeap(_size, F);
aoqi@0 162 if (_addr == NULL && _size >= (size_t)os::vm_allocation_granularity()) {
aoqi@0 163 // malloc failed let's try with mmap instead
aoqi@0 164 _use_malloc = false;
aoqi@0 165 } else {
aoqi@0 166 return (E*)_addr;
aoqi@0 167 }
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 int alignment = os::vm_allocation_granularity();
aoqi@0 171 _size = align_size_up(_size, alignment);
aoqi@0 172
aoqi@0 173 _addr = os::reserve_memory(_size, NULL, alignment, F);
aoqi@0 174 if (_addr == NULL) {
aoqi@0 175 vm_exit_out_of_memory(_size, OOM_MMAP_ERROR, "Allocator (reserve)");
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 os::commit_memory_or_exit(_addr, _size, !ExecMem, "Allocator (commit)");
aoqi@0 179
aoqi@0 180 return (E*)_addr;
aoqi@0 181 }
aoqi@0 182
aoqi@0 183 template<class E, MEMFLAGS F>
aoqi@0 184 void ArrayAllocator<E, F>::free() {
aoqi@0 185 if (_addr != NULL) {
aoqi@0 186 if (_use_malloc) {
aoqi@0 187 FreeHeap(_addr, F);
aoqi@0 188 } else {
aoqi@0 189 os::release_memory(_addr, _size);
aoqi@0 190 }
aoqi@0 191 _addr = NULL;
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 #endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP

mercurial