author | mgerdin |
Wed, 02 Apr 2014 14:17:34 +0200 | |
changeset 23545 | 2c14fb03a06d |
parent 19696 | bd5a0131bde1 |
child 25946 | 1572c9f03fb9 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
15855
2ac9ebea17f3
8008959: Fix non-PCH build on Linux, Windows and MacOS X
simonis
parents:
14083
diff
changeset
|
2 |
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#ifndef SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |
26 |
#define SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |
|
27 |
||
15855
2ac9ebea17f3
8008959: Fix non-PCH build on Linux, Windows and MacOS X
simonis
parents:
14083
diff
changeset
|
28 |
#include "runtime/atomic.inline.hpp" |
7397 | 29 |
#include "runtime/os.hpp" |
30 |
||
1 | 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 |
||
8325 | 36 |
#ifndef PRODUCT |
8320 | 37 |
// Increments unsigned long value for statistics (not atomic on MP). |
38 |
inline void inc_stat_counter(volatile julong* dest, julong add_value) { |
|
8325 | 39 |
#if defined(SPARC) || defined(X86) |
40 |
// Sparc and X86 have atomic jlong (8 bytes) instructions |
|
8320 | 41 |
julong value = Atomic::load((volatile jlong*)dest); |
42 |
value += add_value; |
|
43 |
Atomic::store((jlong)value, (volatile jlong*)dest); |
|
8325 | 44 |
#else |
45 |
// possible word-tearing during load/store |
|
46 |
*dest += add_value; |
|
47 |
#endif |
|
8320 | 48 |
} |
8325 | 49 |
#endif |
1 | 50 |
|
51 |
// allocate using malloc; will fail if no memory available |
|
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
52 |
inline char* AllocateHeap(size_t size, MEMFLAGS flags, address pc = 0, |
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
53 |
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
13195 | 54 |
if (pc == 0) { |
55 |
pc = CURRENT_PC; |
|
56 |
} |
|
57 |
char* p = (char*) os::malloc(size, flags, pc); |
|
1 | 58 |
#ifdef ASSERT |
13195 | 59 |
if (PrintMallocFree) trace_heap_malloc(size, "AllocateHeap", p); |
1 | 60 |
#endif |
17087
f0b76c4c93a0
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
17031
diff
changeset
|
61 |
if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
f0b76c4c93a0
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
17031
diff
changeset
|
62 |
vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "AllocateHeap"); |
f0b76c4c93a0
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
17031
diff
changeset
|
63 |
} |
13195 | 64 |
return p; |
65 |
} |
|
66 |
||
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
67 |
inline char* ReallocateHeap(char *old, size_t size, MEMFLAGS flags, |
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
68 |
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
13195 | 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 |
|
17087
f0b76c4c93a0
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
17031
diff
changeset
|
73 |
if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) { |
f0b76c4c93a0
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
17031
diff
changeset
|
74 |
vm_exit_out_of_memory(size, OOM_MALLOC_ERROR, "ReallocateHeap"); |
f0b76c4c93a0
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
ccheung
parents:
17031
diff
changeset
|
75 |
} |
1 | 76 |
return p; |
77 |
} |
|
78 |
||
13195 | 79 |
inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) { |
1 | 80 |
#ifdef ASSERT |
81 |
if (PrintMallocFree) trace_heap_free(p); |
|
82 |
#endif |
|
13195 | 83 |
os::free(p, memflags); |
1 | 84 |
} |
7397 | 85 |
|
13195 | 86 |
|
87 |
template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size, |
|
19696
bd5a0131bde1
8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents:
18069
diff
changeset
|
88 |
address caller_pc) throw() { |
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
89 |
void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC)); |
17031 | 90 |
#ifdef ASSERT |
13195 | 91 |
if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
92 |
#endif |
17031 | 93 |
return p; |
13195 | 94 |
} |
95 |
||
96 |
template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, |
|
19696
bd5a0131bde1
8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents:
18069
diff
changeset
|
97 |
const std::nothrow_t& nothrow_constant, address caller_pc) throw() { |
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
98 |
void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC), |
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
99 |
AllocFailStrategy::RETURN_NULL); |
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
100 |
#ifdef ASSERT |
13195 | 101 |
if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
102 |
#endif |
13195 | 103 |
return p; |
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
104 |
} |
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
105 |
|
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
106 |
template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
19696
bd5a0131bde1
8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents:
18069
diff
changeset
|
107 |
address caller_pc) throw() { |
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
108 |
return CHeapObj<F>::operator new(size, caller_pc); |
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
109 |
} |
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
110 |
|
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
111 |
template <MEMFLAGS F> void* CHeapObj<F>::operator new [](size_t size, |
19696
bd5a0131bde1
8021954: VM SIGSEGV during classloading on MacOS; hs_err_pid file produced
coleenp
parents:
18069
diff
changeset
|
112 |
const std::nothrow_t& nothrow_constant, address caller_pc) throw() { |
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
113 |
return CHeapObj<F>::operator new(size, nothrow_constant, caller_pc); |
13195 | 114 |
} |
115 |
||
116 |
template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){ |
|
17376
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
117 |
FreeHeap(p, F); |
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
118 |
} |
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
119 |
|
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
120 |
template <MEMFLAGS F> void CHeapObj<F>::operator delete [](void* p){ |
4ee999c3c007
8012902: remove use of global operator new - take 2
minqi
parents:
17302
diff
changeset
|
121 |
FreeHeap(p, F); |
13195 | 122 |
} |
123 |
||
16682
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
124 |
template <class E, MEMFLAGS F> |
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
125 |
char* ArrayAllocator<E, F>::allocate_inner(size_t &size, bool &use_malloc) { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
126 |
char* addr = NULL; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
127 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
128 |
if (use_malloc) { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
129 |
addr = AllocateHeap(size, F); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
130 |
if (addr == NULL && size >= (size_t)os::vm_allocation_granularity()) { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
131 |
// malloc failed let's try with mmap instead |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
132 |
use_malloc = false; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
133 |
} else { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
134 |
return addr; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
135 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
136 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
137 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
138 |
int alignment = os::vm_allocation_granularity(); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
139 |
size = align_size_up(size, alignment); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
140 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
141 |
addr = os::reserve_memory(size, NULL, alignment, F); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
142 |
if (addr == NULL) { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
143 |
vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "Allocator (reserve)"); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
144 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
145 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
146 |
os::commit_memory_or_exit(addr, size, !ExecMem, "Allocator (commit)"); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
147 |
return addr; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
148 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
149 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
150 |
template <class E, MEMFLAGS F> |
16682
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
151 |
E* ArrayAllocator<E, F>::allocate(size_t length) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
152 |
assert(_addr == NULL, "Already in use"); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
153 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
154 |
_size = sizeof(E) * length; |
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
155 |
_use_malloc = should_use_malloc(_size); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
156 |
_addr = allocate_inner(_size, _use_malloc); |
16682
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
157 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
158 |
return (E*)_addr; |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
159 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
160 |
|
23545
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
161 |
template <class E, MEMFLAGS F> |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
162 |
E* ArrayAllocator<E, F>::reallocate(size_t new_length) { |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
163 |
size_t new_size = sizeof(E) * new_length; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
164 |
bool use_malloc = should_use_malloc(new_size); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
165 |
char* new_addr = allocate_inner(new_size, use_malloc); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
166 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
167 |
memcpy(new_addr, _addr, MIN2(new_size, _size)); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
168 |
|
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
169 |
free(); |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
170 |
_size = new_size; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
171 |
_use_malloc = use_malloc; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
172 |
_addr = new_addr; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
173 |
return (E*)new_addr; |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
174 |
} |
2c14fb03a06d
8037959: BitMap::resize frees old map before copying memory if !in_resource_area
mgerdin
parents:
19696
diff
changeset
|
175 |
|
16682
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
176 |
template<class E, MEMFLAGS F> |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
177 |
void ArrayAllocator<E, F>::free() { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
178 |
if (_addr != NULL) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
179 |
if (_use_malloc) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
180 |
FreeHeap(_addr, F); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
181 |
} else { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
182 |
os::release_memory(_addr, _size); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
183 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
184 |
_addr = NULL; |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
185 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
186 |
} |
13195 | 187 |
|
7397 | 188 |
#endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |