author | dcubed |
Sun, 21 Apr 2013 20:41:04 -0700 | |
changeset 17031 | 9a576749065b |
parent 17026 | 72b2233861f1 |
child 17087 | f0b76c4c93a0 |
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 |
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
61 |
if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) vm_exit_out_of_memory(size, "AllocateHeap"); |
13195 | 62 |
return p; |
63 |
} |
|
64 |
||
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
65 |
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
|
66 |
AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
13195 | 67 |
char* p = (char*) os::realloc(old, size, flags, CURRENT_PC); |
68 |
#ifdef ASSERT |
|
69 |
if (PrintMallocFree) trace_heap_malloc(size, "ReallocateHeap", p); |
|
70 |
#endif |
|
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
71 |
if (p == NULL && alloc_failmode == AllocFailStrategy::EXIT_OOM) vm_exit_out_of_memory(size, "ReallocateHeap"); |
1 | 72 |
return p; |
73 |
} |
|
74 |
||
13195 | 75 |
inline void FreeHeap(void* p, MEMFLAGS memflags = mtInternal) { |
1 | 76 |
#ifdef ASSERT |
77 |
if (PrintMallocFree) trace_heap_free(p); |
|
78 |
#endif |
|
13195 | 79 |
os::free(p, memflags); |
1 | 80 |
} |
7397 | 81 |
|
13195 | 82 |
|
83 |
template <MEMFLAGS F> void* CHeapObj<F>::operator new(size_t size, |
|
84 |
address caller_pc){ |
|
17031 | 85 |
#ifdef ASSERT |
17026
72b2233861f1
8010992: Remove calls to global ::operator new[] and new
minqi
parents:
16682
diff
changeset
|
86 |
void* p = (void*)AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC)); |
13195 | 87 |
if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
17031 | 88 |
return p; |
89 |
#else |
|
90 |
return (void *) AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC)); |
|
17026
72b2233861f1
8010992: Remove calls to global ::operator new[] and new
minqi
parents:
16682
diff
changeset
|
91 |
#endif |
13195 | 92 |
} |
93 |
||
94 |
template <MEMFLAGS F> void* CHeapObj<F>::operator new (size_t size, |
|
95 |
const std::nothrow_t& nothrow_constant, address caller_pc) { |
|
17031 | 96 |
#ifdef ASSERT |
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13963
diff
changeset
|
97 |
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
|
98 |
AllocFailStrategy::RETURN_NULL); |
13195 | 99 |
if (PrintMallocFree) trace_heap_malloc(size, "CHeapObj-new", p); |
100 |
return p; |
|
17031 | 101 |
#else |
102 |
return (void *) AllocateHeap(size, F, (caller_pc != 0 ? caller_pc : CALLER_PC), |
|
103 |
AllocFailStrategy::RETURN_NULL); |
|
104 |
#endif |
|
13195 | 105 |
} |
106 |
||
107 |
template <MEMFLAGS F> void CHeapObj<F>::operator delete(void* p){ |
|
17031 | 108 |
FreeHeap(p, F); |
13195 | 109 |
} |
110 |
||
16682
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
111 |
template <class E, MEMFLAGS F> |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
112 |
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
|
113 |
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
|
114 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
115 |
_size = sizeof(E) * length; |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
116 |
_use_malloc = _size < ArrayAllocatorMallocLimit; |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
117 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
118 |
if (_use_malloc) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
119 |
_addr = AllocateHeap(_size, F); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
120 |
if (_addr == NULL && _size >= (size_t)os::vm_allocation_granularity()) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
121 |
// malloc failed let's try with mmap instead |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
122 |
_use_malloc = false; |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
123 |
} else { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
124 |
return (E*)_addr; |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
125 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
126 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
127 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
128 |
int alignment = os::vm_allocation_granularity(); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
129 |
_size = align_size_up(_size, alignment); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
130 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
131 |
_addr = os::reserve_memory(_size, NULL, alignment); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
132 |
if (_addr == NULL) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
133 |
vm_exit_out_of_memory(_size, "Allocator (reserve)"); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
134 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
135 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
136 |
bool success = os::commit_memory(_addr, _size, false /* executable */); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
137 |
if (!success) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
138 |
vm_exit_out_of_memory(_size, "Allocator (commit)"); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
139 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
140 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
141 |
return (E*)_addr; |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
142 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
143 |
|
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
144 |
template<class E, MEMFLAGS F> |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
145 |
void ArrayAllocator<E, F>::free() { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
146 |
if (_addr != NULL) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
147 |
if (_use_malloc) { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
148 |
FreeHeap(_addr, F); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
149 |
} else { |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
150 |
os::release_memory(_addr, _size); |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
151 |
} |
4e30a46f6b76
7197666: java -d64 -version core dumps in a box with lots of memory
brutisso
parents:
15855
diff
changeset
|
152 |
_addr = NULL; |
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 |
} |
13195 | 155 |
|
7397 | 156 |
#endif // SHARE_VM_MEMORY_ALLOCATION_INLINE_HPP |