author | rfield |
Tue, 14 Nov 2017 19:33:37 -0800 (2017-11-15) | |
changeset 47840 | e0f08a49f3e3 |
parent 47600 | 5c8607bb3d2d |
child 48157 | 7c4d43c26352 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
47537 | 2 |
* Copyright (c) 1997, 2017, 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_RESOURCEAREA_HPP |
26 |
#define SHARE_VM_MEMORY_RESOURCEAREA_HPP |
|
27 |
||
28 |
#include "memory/allocation.hpp" |
|
37456 | 29 |
#include "runtime/thread.hpp" |
7397 | 30 |
|
1 | 31 |
// The resource area holds temporary data structures in the VM. |
32 |
// The actual allocation areas are thread local. Typical usage: |
|
33 |
// |
|
34 |
// ... |
|
35 |
// { |
|
36 |
// ResourceMark rm; |
|
37 |
// int foo[] = NEW_RESOURCE_ARRAY(int, 64); |
|
38 |
// ... |
|
39 |
// } |
|
40 |
// ... |
|
41 |
||
42 |
//------------------------------ResourceArea----------------------------------- |
|
43 |
// A ResourceArea is an Arena that supports safe usage of ResourceMark. |
|
44 |
class ResourceArea: public Arena { |
|
45 |
friend class ResourceMark; |
|
46 |
friend class DeoptResourceMark; |
|
10547 | 47 |
friend class VMStructs; |
1 | 48 |
debug_only(int _nesting;) // current # of nested ResourceMarks |
49 |
debug_only(static int _warned;) // to suppress multiple warnings |
|
50 |
||
51 |
public: |
|
47537 | 52 |
ResourceArea(MEMFLAGS flags = mtThread) : Arena(flags) { |
1 | 53 |
debug_only(_nesting = 0;) |
54 |
} |
|
55 |
||
47537 | 56 |
ResourceArea(size_t init_size, MEMFLAGS flags = mtThread) : Arena(flags, init_size) { |
1 | 57 |
debug_only(_nesting = 0;); |
58 |
} |
|
59 |
||
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13195
diff
changeset
|
60 |
char* allocate_bytes(size_t size, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) { |
1 | 61 |
#ifdef ASSERT |
62 |
if (_nesting < 1 && !_warned++) |
|
63 |
fatal("memory leak: allocating without ResourceMark"); |
|
64 |
if (UseMallocOnly) { |
|
65 |
// use malloc, but save pointer in res. area for later freeing |
|
66 |
char** save = (char**)internal_malloc_4(sizeof(char*)); |
|
25946 | 67 |
return (*save = (char*)os::malloc(size, mtThread, CURRENT_PC)); |
1 | 68 |
} |
69 |
#endif |
|
14083
103054a71a30
8000617: It should be possible to allocate memory without the VM dying.
nloodin
parents:
13195
diff
changeset
|
70 |
return (char*)Amalloc(size, alloc_failmode); |
1 | 71 |
} |
72 |
||
47600
5c8607bb3d2d
8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents:
47537
diff
changeset
|
73 |
// Bias this resource area to specific memory type |
5c8607bb3d2d
8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents:
47537
diff
changeset
|
74 |
// (by default, ResourceArea is tagged as mtThread, per-thread general purpose storage) |
5c8607bb3d2d
8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents:
47537
diff
changeset
|
75 |
void bias_to(MEMFLAGS flags); |
5c8607bb3d2d
8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents:
47537
diff
changeset
|
76 |
|
5c8607bb3d2d
8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents:
47537
diff
changeset
|
77 |
debug_only(int nesting() const { return _nesting; }) |
1 | 78 |
}; |
79 |
||
80 |
||
81 |
//------------------------------ResourceMark----------------------------------- |
|
82 |
// A resource mark releases all resources allocated after it was constructed |
|
83 |
// when the destructor is called. Typically used as a local variable. |
|
84 |
class ResourceMark: public StackObj { |
|
85 |
protected: |
|
86 |
ResourceArea *_area; // Resource area to stack allocate |
|
87 |
Chunk *_chunk; // saved arena chunk |
|
88 |
char *_hwm, *_max; |
|
13195 | 89 |
size_t _size_in_bytes; |
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
90 |
#ifdef ASSERT |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
91 |
Thread* _thread; |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
92 |
ResourceMark* _previous_resource_mark; |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
93 |
#endif //ASSERT |
1 | 94 |
|
95 |
void initialize(Thread *thread) { |
|
96 |
_area = thread->resource_area(); |
|
97 |
_chunk = _area->_chunk; |
|
98 |
_hwm = _area->_hwm; |
|
99 |
_max= _area->_max; |
|
13195 | 100 |
_size_in_bytes = _area->size_in_bytes(); |
1 | 101 |
debug_only(_area->_nesting++;) |
102 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
103 |
#ifdef ASSERT |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
104 |
_thread = thread; |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
105 |
_previous_resource_mark = thread->current_resource_mark(); |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
106 |
thread->set_current_resource_mark(this); |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
107 |
#endif // ASSERT |
1 | 108 |
} |
109 |
public: |
|
110 |
||
111 |
#ifndef ASSERT |
|
112 |
ResourceMark(Thread *thread) { |
|
113 |
assert(thread == Thread::current(), "not the current thread"); |
|
114 |
initialize(thread); |
|
115 |
} |
|
116 |
#else |
|
117 |
ResourceMark(Thread *thread); |
|
118 |
#endif // ASSERT |
|
119 |
||
120 |
ResourceMark() { initialize(Thread::current()); } |
|
121 |
||
122 |
ResourceMark( ResourceArea *r ) : |
|
123 |
_area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { |
|
13195 | 124 |
_size_in_bytes = r->_size_in_bytes; |
1 | 125 |
debug_only(_area->_nesting++;) |
126 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
127 |
#ifdef ASSERT |
34633
2a6c7c7b30a7
8132510: Replace ThreadLocalStorage with compiler/language-based thread-local variables
dholmes
parents:
25946
diff
changeset
|
128 |
Thread* thread = Thread::current_or_null(); |
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
129 |
if (thread != NULL) { |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
130 |
_thread = thread; |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
131 |
_previous_resource_mark = thread->current_resource_mark(); |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
132 |
thread->set_current_resource_mark(this); |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
133 |
} else { |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
134 |
_thread = NULL; |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
135 |
_previous_resource_mark = NULL; |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
136 |
} |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
137 |
#endif // ASSERT |
1 | 138 |
} |
139 |
||
140 |
void reset_to_mark() { |
|
141 |
if (UseMallocOnly) free_malloced_objects(); |
|
142 |
||
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
143 |
if( _chunk->next() ) { // Delete later chunks |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
144 |
// reset arena size before delete chunks. Otherwise, the total |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
145 |
// arena size could exceed total chunk size |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
146 |
assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check"); |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
147 |
_area->set_size_in_bytes(size_in_bytes()); |
1 | 148 |
_chunk->next_chop(); |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
149 |
} else { |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
150 |
assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check"); |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
151 |
} |
1 | 152 |
_area->_chunk = _chunk; // Roll back arena to saved chunk |
153 |
_area->_hwm = _hwm; |
|
154 |
_area->_max = _max; |
|
155 |
||
156 |
// clear out this chunk (to detect allocation bugs) |
|
157 |
if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); |
|
158 |
} |
|
159 |
||
160 |
~ResourceMark() { |
|
161 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
162 |
debug_only(_area->_nesting--;) |
|
163 |
reset_to_mark(); |
|
18928
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
164 |
#ifdef ASSERT |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
165 |
if (_thread != NULL) { |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
166 |
_thread->set_current_resource_mark(_previous_resource_mark); |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
167 |
} |
ac0163dd0a50
7143807: ResourceMark nesting problem in stringStream
fparain
parents:
14583
diff
changeset
|
168 |
#endif // ASSERT |
1 | 169 |
} |
170 |
||
171 |
||
172 |
private: |
|
173 |
void free_malloced_objects() PRODUCT_RETURN; |
|
13195 | 174 |
size_t size_in_bytes() { return _size_in_bytes; } |
1 | 175 |
}; |
176 |
||
177 |
//------------------------------DeoptResourceMark----------------------------------- |
|
178 |
// A deopt resource mark releases all resources allocated after it was constructed |
|
179 |
// when the destructor is called. Typically used as a local variable. It differs |
|
180 |
// from a typical resource more in that it is C-Heap allocated so that deoptimization |
|
181 |
// can use data structures that are arena based but are not amenable to vanilla |
|
182 |
// ResourceMarks because deoptimization can not use a stack allocated mark. During |
|
183 |
// deoptimization we go thru the following steps: |
|
184 |
// |
|
185 |
// 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info |
|
186 |
// 1: create the vframeArray (contains pointers to Resource allocated structures) |
|
187 |
// This allocates the DeoptResourceMark. |
|
188 |
// 2: return to assembly stub and remove stub frame and deoptee frame and create |
|
189 |
// the new skeletal frames. |
|
190 |
// 3: push new stub frame and call unpack_frames |
|
191 |
// 4: retrieve information from the vframeArray to populate the skeletal frames |
|
192 |
// 5: release the DeoptResourceMark |
|
193 |
// 6: return to stub and eventually to interpreter |
|
194 |
// |
|
195 |
// With old style eager deoptimization the vframeArray was created by the vmThread there |
|
196 |
// was no way for the vframeArray to contain resource allocated objects and so |
|
197 |
// a complex set of data structures to simulate an array of vframes in CHeap memory |
|
198 |
// was used. With new style lazy deoptimization the vframeArray is created in the |
|
199 |
// the thread that will use it and we can use a much simpler scheme for the vframeArray |
|
200 |
// leveraging existing data structures if we simply create a way to manage this one |
|
201 |
// special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj |
|
202 |
// then existing ResourceMarks would work fine since no one use new to allocate them |
|
22551 | 203 |
// and they would be stack allocated. This leaves open the possibility of accidental |
1 | 204 |
// misuse so we simple duplicate the ResourceMark functionality here. |
205 |
||
13195 | 206 |
class DeoptResourceMark: public CHeapObj<mtInternal> { |
1 | 207 |
protected: |
208 |
ResourceArea *_area; // Resource area to stack allocate |
|
209 |
Chunk *_chunk; // saved arena chunk |
|
210 |
char *_hwm, *_max; |
|
13195 | 211 |
size_t _size_in_bytes; |
1 | 212 |
|
213 |
void initialize(Thread *thread) { |
|
214 |
_area = thread->resource_area(); |
|
215 |
_chunk = _area->_chunk; |
|
216 |
_hwm = _area->_hwm; |
|
217 |
_max= _area->_max; |
|
13195 | 218 |
_size_in_bytes = _area->size_in_bytes(); |
1 | 219 |
debug_only(_area->_nesting++;) |
220 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
221 |
} |
|
222 |
||
223 |
public: |
|
224 |
||
225 |
#ifndef ASSERT |
|
226 |
DeoptResourceMark(Thread *thread) { |
|
227 |
assert(thread == Thread::current(), "not the current thread"); |
|
228 |
initialize(thread); |
|
229 |
} |
|
230 |
#else |
|
231 |
DeoptResourceMark(Thread *thread); |
|
232 |
#endif // ASSERT |
|
233 |
||
234 |
DeoptResourceMark() { initialize(Thread::current()); } |
|
235 |
||
236 |
DeoptResourceMark( ResourceArea *r ) : |
|
237 |
_area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { |
|
13195 | 238 |
_size_in_bytes = _area->size_in_bytes(); |
1 | 239 |
debug_only(_area->_nesting++;) |
240 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
241 |
} |
|
242 |
||
243 |
void reset_to_mark() { |
|
244 |
if (UseMallocOnly) free_malloced_objects(); |
|
245 |
||
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
246 |
if( _chunk->next() ) { // Delete later chunks |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
247 |
// reset arena size before delete chunks. Otherwise, the total |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
248 |
// arena size could exceed total chunk size |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
249 |
assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check"); |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
250 |
_area->set_size_in_bytes(size_in_bytes()); |
1 | 251 |
_chunk->next_chop(); |
14120
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
252 |
} else { |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
253 |
assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check"); |
7d298141c258
7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents:
14083
diff
changeset
|
254 |
} |
1 | 255 |
_area->_chunk = _chunk; // Roll back arena to saved chunk |
256 |
_area->_hwm = _hwm; |
|
257 |
_area->_max = _max; |
|
258 |
||
259 |
// clear out this chunk (to detect allocation bugs) |
|
260 |
if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); |
|
261 |
} |
|
262 |
||
263 |
~DeoptResourceMark() { |
|
264 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
265 |
debug_only(_area->_nesting--;) |
|
266 |
reset_to_mark(); |
|
267 |
} |
|
268 |
||
269 |
||
270 |
private: |
|
271 |
void free_malloced_objects() PRODUCT_RETURN; |
|
13195 | 272 |
size_t size_in_bytes() { return _size_in_bytes; }; |
1 | 273 |
}; |
7397 | 274 |
|
275 |
#endif // SHARE_VM_MEMORY_RESOURCEAREA_HPP |