annotate src/share/vm/memory/allocation.inline.hpp @ 10135:5a9fa2ba85f0

8012907: anti-delta fix for 8010992 Summary: anti-delta fix for 8010992 until 8012902 can be fixed Reviewed-by: acorn, minqi, rdurbin
author dcubed
date Sun, 21 Apr 2013 20:41:04 -0700
parents 6f817ce50129
children 746b070f5022
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
8675
63e54c37ac64 8008959: Fix non-PCH build on Linux, Windows and MacOS X
simonis
parents: 6872
diff changeset
2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
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
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
25 #ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
26 #define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
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
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
29 #include "runtime/os.hpp"
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
30
0
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // Explicit C-heap memory management
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 void trace_heap_malloc(size_t size, const char* name, void *p);
a61af66fc99e Initial load
duke
parents:
diff changeset
34 void trace_heap_free(void *p);
a61af66fc99e Initial load
duke
parents:
diff changeset
35
2255
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
36 #ifndef PRODUCT
2250
f7de3327c683 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 1972
diff changeset
37 // Increments unsigned long value for statistics (not atomic on MP).
f7de3327c683 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 1972
diff changeset
38 inline void inc_stat_counter(volatile julong* dest, julong add_value) {
2255
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
39 #if defined(SPARC) || defined(X86)
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
40 // Sparc and X86 have atomic jlong (8 bytes) instructions
2250
f7de3327c683 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 1972
diff changeset
41 julong value = Atomic::load((volatile jlong*)dest);
f7de3327c683 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 1972
diff changeset
42 value += add_value;
f7de3327c683 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 1972
diff changeset
43 Atomic::store((jlong)value, (volatile jlong*)dest);
2255
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
44 #else
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
45 // possible word-tearing during load/store
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
46 *dest += add_value;
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
47 #endif
2250
f7de3327c683 7017124: Fix some VM stats to avoid 32-bit overflow
kvn
parents: 1972
diff changeset
48 }
2255
bf29934d2f4f 7018849: 7017124 fix broke VM build on some platforms
kvn
parents: 2250
diff changeset
49 #endif
0
a61af66fc99e Initial load
duke
parents:
diff changeset
50
a61af66fc99e Initial load
duke
parents:
diff changeset
51 // allocate using malloc; will fail if no memory available
6872
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
52 inline char* AllocateHeap(size_t size, MEMFLAGS flags, address pc = 0,
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
53 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
54 if (pc == 0) {
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
55 pc = CURRENT_PC;
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
56 }
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
57 char* p = (char*) os::malloc(size, flags, pc);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
58 #ifdef ASSERT
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
59 if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
60 #endif
6872
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
61 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) vm_exit_out_of_memory(size, "AllocateHeap");
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
62 return p;
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
63 }
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
64
6872
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
65 inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flags,
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
66 AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
67 char* p = (char*) os::realloc(old, size, flags, CURRENT_PC);
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
68 #ifdef ASSERT
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
69 if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p);
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
70 #endif
6872
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
71 if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) vm_exit_out_of_memory(size, "ReallocateHeap");
0
a61af66fc99e Initial load
duke
parents:
diff changeset
72 return p;
a61af66fc99e Initial load
duke
parents:
diff changeset
73 }
a61af66fc99e Initial load
duke
parents:
diff changeset
74
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
75 inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) {
0
a61af66fc99e Initial load
duke
parents:
diff changeset
76 #ifdef ASSERT
a61af66fc99e Initial load
duke
parents:
diff changeset
77 if (PrintMallocFree) trace_heap_free(p);
a61af66fc99e Initial load
duke
parents:
diff changeset
78 #endif
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
79 os::free(p, memflags);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
81
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
82
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
83 template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size,
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
84 address caller_pc){
10135
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
85 #ifdef ASSERT
10130
6f817ce50129 8010992: Remove calls to global ::operator new[] and new
minqi
parents: 9073
diff changeset
86 void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC));
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
87 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
10135
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
88 return p;
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
89 #else
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
90 return (void *) AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC));
10130
6f817ce50129 8010992: Remove calls to global ::operator new[] and new
minqi
parents: 9073
diff changeset
91 #endif
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
92 }
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
93
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
94 template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size,
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
95 const std::nothrow_t& nothrow_constant, address caller_pc) {
10135
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
96 #ifdef ASSERT
6872
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
97 void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC),
7b5885dadbdc 8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents: 6842
diff changeset
98 AllocFailStrategy::RETURN_NULL);
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
99 if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p);
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
100 return p;
10135
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
101 #else
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
102 return (void *) AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC),
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
103 AllocFailStrategy::RETURN_NULL);
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
104 #endif
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
105 }
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
106
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
107 template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){
10135
5a9fa2ba85f0 8012907: anti-delta fix for 8010992
dcubed
parents: 10130
diff changeset
108 FreeHeap(p, F);
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
109 }
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
110
9073
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
111 template <class E, MEMFLAGS F>
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
112 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
113 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
114
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
115 _size = sizeof(E) * length;
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
116 _use_malloc = _size < ArrayAllocatorMallocLimit;
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
117
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
118 if (_use_malloc) {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
119 _addr = AllocateHeap(_size, F);
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
120 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
121 // 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
122 _use_malloc = false;
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
123 } else {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
124 return (E*)_addr;
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
125 }
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
126 }
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
127
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
128 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
129 _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
130
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
131 _addr = os::reserve_memory(_size, NULL, alignment);
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
132 if (_addr == NULL) {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
133 vm_exit_out_of_memory(_size, "Allocator (reserve)");
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
134 }
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
135
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
136 bool success = os::commit_memory(_addr, _size, false /* executable */);
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
137 if (!success) {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
138 vm_exit_out_of_memory(_size, "Allocator (commit)");
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
139 }
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
140
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
141 return (E*)_addr;
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
142 }
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
143
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
144 template<class E, MEMFLAGS F>
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
145 void ArrayAllocator<E, F>::free() {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
146 if (_addr != NULL) {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
147 if (_use_malloc) {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
148 FreeHeap(_addr, F);
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
149 } else {
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
150 os::release_memory(_addr, _size);
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
151 }
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
152 _addr = NULL;
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
153 }
83f27710f5f7 7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents: 8675
diff changeset
154 }
6197
d2a62e0f25eb 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 2255
diff changeset
155
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
156 #endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP