Mercurial > hg > graal-jvmci-8
annotate src/share/vm/memory/allocation.inline.hpp @ 23002:ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
Summary: NMT with detail option reports incorrect caller address on Linux.
Reviewed-by: dholmes, coleenp
author | ysuenaga |
---|---|
date | Tue, 28 Apr 2015 19:04:39 +0900 |
parents | 833b0f92429a |
children |
rev | line source |
---|---|
0 | 1 /* |
20360 | 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
0 | 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 * | |
1552
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
diff
changeset
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
diff
changeset
|
20 * or visit www.oracle.com if you need additional information or have any |
c18cbe5936b8
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
0
diff
changeset
|
21 * questions. |
0 | 22 * |
23 */ | |
24 | |
1972 | 25 #ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |
26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP | |
27 | |
8675
63e54c37ac64
8008959: Fix non-PCH build on Linux, Windows and MacOS X
simonis
parents:
6872
diff
changeset
|
28 #include "runtime/atomic.inline.hpp" |
1972 | 29 #include "runtime/os.hpp" |
20360 | 30 #include "services/memTracker.hpp" |
1972 | 31 |
0 | 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 | |
2255 | 37 #ifndef PRODUCT |
2250 | 38 // Increments unsigned long value for statistics (not atomic on MP). |
39 inline void inc_stat_counter(volatile julong* dest, julong add_value) { | |
2255 | 40 #if defined(SPARC) || defined(X86) |
41 // Sparc and X86 have atomic jlong (8 bytes) instructions | |
2250 | 42 julong value = Atomic::load((volatile jlong*)dest); |
43 value += add_value; | |
44 Atomic::store((jlong)value, (volatile jlong*)dest); | |
2255 | 45 #else |
46 // possible word-tearing during load/store | |
47 *dest += add_value; | |
48 #endif | |
2250 | 49 } |
2255 | 50 #endif |
0 | 51 |
52 // allocate using malloc; will fail if no memory available | |
20360 | 53 inline char* AllocateHeap(size_t size, MEMFLAGS flags, |
54 const NativeCallStack& stack, | |
6872
7b5885dadbdc
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
6842
diff
changeset
|
55 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
20360 | 56 char* p = (char*) os::malloc(size, flags, stack); |
0 | 57 #ifdef ASSERT |
6197 | 58 if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p); |
0 | 59 #endif |
10161
746b070f5022
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
10135
diff
changeset
|
60 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
746b070f5022
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
10135
diff
changeset
|
61 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap"); |
746b070f5022
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
10135
diff
changeset
|
62 } |
6197 | 63 return p; |
64 } | |
23002
ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
ysuenaga
parents:
20360
diff
changeset
|
65 |
ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
ysuenaga
parents:
20360
diff
changeset
|
66 #ifdef __GNUC__ |
ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
ysuenaga
parents:
20360
diff
changeset
|
67 __attribute__((always_inline)) |
ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
ysuenaga
parents:
20360
diff
changeset
|
68 #endif |
20360 | 69 inline char* AllocateHeap(size_t size, MEMFLAGS flags, |
70 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { | |
71 return AllocateHeap(size, flags, CURRENT_PC, alloc_failmode); | |
72 } | |
6197 | 73 |
23002
ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
ysuenaga
parents:
20360
diff
changeset
|
74 #ifdef __GNUC__ |
ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
ysuenaga
parents:
20360
diff
changeset
|
75 __attribute__((always_inline)) |
ed0067c67bd7
8076212: AllocateHeap() and ReallocateHeap() should be inlined.
ysuenaga
parents:
20360
diff
changeset
|
76 #endif |
20360 | 77 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flag, |
6872
7b5885dadbdc
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
6842
diff
changeset
|
78 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
20360 | 79 char* p = (char*) os::realloc(old, size, flag, CURRENT_PC); |
6197 | 80 #ifdef ASSERT |
81 if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p); | |
82 #endif | |
10161
746b070f5022
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
10135
diff
changeset
|
83 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
746b070f5022
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
10135
diff
changeset
|
84 vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap"); |
746b070f5022
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
10135
diff
changeset
|
85 } |
0 | 86 return p; |
87 } | |
88 | |
6197 | 89 inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) { |
0 | 90 #ifdef ASSERT |
91 if (PrintMallocFree) trace_heap_free(p); | |
92 #endif | |
6197 | 93 os::free(p, memflags); |
0 | 94 } |
1972 | 95 |
6197 | 96 |
97 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size, | |
20360 | 98 const NativeCallStack& stack) throw() { |
99 void* p = (void*)AllocateHeap(size, F, stack); | |
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) 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 void* p = (void*)AllocateHeap(size, F, stack, | |
113 AllocFailStrategy::RETURN_NULL); | |
10135 | 114 #ifdef ASSERT |
6197 | 115 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
10271
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
116 #endif |
10135 | 117 return p; |
6197 | 118 } |
119 | |
120 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, | |
20360 | 121 const std::nothrow_t& nothrow_constant) throw() { |
122 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC); | |
10271
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
123 } |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
124 |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
125 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
20360 | 126 const NativeCallStack& stack) throw() { |
127 return CHeapObj<F>::operator new(size, stack); | |
128 } | |
129 | |
130 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size) | |
131 throw() { | |
132 return CHeapObj<F>::operator new(size, CALLER_PC); | |
10271
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
133 } |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
134 |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
135 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
20360 | 136 const std::nothrow_t& nothrow_constant, const NativeCallStack& stack) throw() { |
137 return CHeapObj<F>::operator new(size, nothrow_constant, stack); | |
138 } | |
139 | |
140 template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, | |
141 const std::nothrow_t& nothrow_constant) throw() { | |
142 return CHeapObj<F>::operator new(size, nothrow_constant, CALLER_PC); | |
6197 | 143 } |
144 | |
145 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){ | |
10271
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
146 FreeHeap(p, F); |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
147 } |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
148 |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
149 template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){ |
f9be75d21404
8012902: remove use of global operator new - take 2
minqi
parents:
10221
diff
changeset
|
150 FreeHeap(p, F); |
6197 | 151 } |
152 | |
9073
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
153 template <class E, MEMFLAGS F> |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
154 E* ArrayAllocator<E, F>::allocate(size_t length) { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
155 assert(_addr == NULL, "Already in use"); |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
156 |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
157 _size = sizeof(E) * length; |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
158 _use_malloc = _size < ArrayAllocatorMallocLimit; |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
159 |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
160 if (_use_malloc) { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
161 _addr = AllocateHeap(_size, F); |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
162 if (_addr == NULL && _size >= (size_t)os::vm_allocation_granularity()) { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
163 // malloc failed let's try with mmap instead |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
164 _use_malloc = false; |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
165 } else { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
166 return (E*)_addr; |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
167 } |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
168 } |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
169 |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
170 int alignment = os::vm_allocation_granularity(); |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
171 _size = align_size_up(_size, alignment); |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
172 |
10221
c18152e0554e
8013120: NMT: Kitchensink crashes with assert(next_region == NULL || !next_region->is_committed_region()) failed: Sanity check
zgu
parents:
10161
diff
changeset
|
173 _addr = os::reserve_memory(_size, NULL, alignment, F); |
9073
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
174 if (_addr == NULL) { |
10161
746b070f5022
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
10135
diff
changeset
|
175 vm_exit_out_of_memory(_size, OOM_MMAP_ERROR, "Allocator (reserve)"); |
9073
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
176 } |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
177 |
10969
a837fa3d3f86
8013057: assert(_needs_gc || SafepointSynchronize::is_at_safepoint()) failed: only read at safepoint
dcubed
parents:
10271
diff
changeset
|
178 os::commit_memory_or_exit(_addr, _size, !ExecMem, "Allocator (commit)"); |
9073
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
179 |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
180 return (E*)_addr; |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
181 } |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
182 |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
183 template<class E, MEMFLAGS F> |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
184 void ArrayAllocator<E, F>::free() { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
185 if (_addr != NULL) { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
186 if (_use_malloc) { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
187 FreeHeap(_addr, F); |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
188 } else { |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
189 os::release_memory(_addr, _size); |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
190 } |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
191 _addr = NULL; |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
192 } |
83f27710f5f7
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
8675
diff
changeset
|
193 } |
6197 | 194 |
1972 | 195 #endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |