author | zgu |
Thu, 28 Jun 2012 17:03:16 -0400 | |
changeset 13195 | be27e1b6a4b9 |
parent 10565 | dc90c239f4ec |
child 14083 | 103054a71a30 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
13195 | 2 |
* Copyright (c) 1997, 2012, 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" |
|
29 |
#ifdef TARGET_OS_FAMILY_linux |
|
30 |
# include "thread_linux.inline.hpp" |
|
31 |
#endif |
|
32 |
#ifdef TARGET_OS_FAMILY_solaris |
|
33 |
# include "thread_solaris.inline.hpp" |
|
34 |
#endif |
|
35 |
#ifdef TARGET_OS_FAMILY_windows |
|
36 |
# include "thread_windows.inline.hpp" |
|
37 |
#endif |
|
10565 | 38 |
#ifdef TARGET_OS_FAMILY_bsd |
39 |
# include "thread_bsd.inline.hpp" |
|
40 |
#endif |
|
7397 | 41 |
|
1 | 42 |
// The resource area holds temporary data structures in the VM. |
43 |
// The actual allocation areas are thread local. Typical usage: |
|
44 |
// |
|
45 |
// ... |
|
46 |
// { |
|
47 |
// ResourceMark rm; |
|
48 |
// int foo[] = NEW_RESOURCE_ARRAY(int, 64); |
|
49 |
// ... |
|
50 |
// } |
|
51 |
// ... |
|
52 |
||
53 |
//------------------------------ResourceArea----------------------------------- |
|
54 |
// A ResourceArea is an Arena that supports safe usage of ResourceMark. |
|
55 |
class ResourceArea: public Arena { |
|
56 |
friend class ResourceMark; |
|
57 |
friend class DeoptResourceMark; |
|
10547 | 58 |
friend class VMStructs; |
1 | 59 |
debug_only(int _nesting;) // current # of nested ResourceMarks |
60 |
debug_only(static int _warned;) // to suppress multiple warnings |
|
61 |
||
62 |
public: |
|
63 |
ResourceArea() { |
|
64 |
debug_only(_nesting = 0;) |
|
65 |
} |
|
66 |
||
67 |
ResourceArea(size_t init_size) : Arena(init_size) { |
|
68 |
debug_only(_nesting = 0;); |
|
69 |
} |
|
70 |
||
71 |
char* allocate_bytes(size_t size) { |
|
72 |
#ifdef ASSERT |
|
73 |
if (_nesting < 1 && !_warned++) |
|
74 |
fatal("memory leak: allocating without ResourceMark"); |
|
75 |
if (UseMallocOnly) { |
|
76 |
// use malloc, but save pointer in res. area for later freeing |
|
77 |
char** save = (char**)internal_malloc_4(sizeof(char*)); |
|
13195 | 78 |
return (*save = (char*)os::malloc(size, mtThread)); |
1 | 79 |
} |
80 |
#endif |
|
81 |
return (char*)Amalloc(size); |
|
82 |
} |
|
83 |
||
84 |
debug_only(int nesting() const { return _nesting; }); |
|
85 |
}; |
|
86 |
||
87 |
||
88 |
//------------------------------ResourceMark----------------------------------- |
|
89 |
// A resource mark releases all resources allocated after it was constructed |
|
90 |
// when the destructor is called. Typically used as a local variable. |
|
91 |
class ResourceMark: public StackObj { |
|
92 |
protected: |
|
93 |
ResourceArea *_area; // Resource area to stack allocate |
|
94 |
Chunk *_chunk; // saved arena chunk |
|
95 |
char *_hwm, *_max; |
|
13195 | 96 |
size_t _size_in_bytes; |
1 | 97 |
|
98 |
void initialize(Thread *thread) { |
|
99 |
_area = thread->resource_area(); |
|
100 |
_chunk = _area->_chunk; |
|
101 |
_hwm = _area->_hwm; |
|
102 |
_max= _area->_max; |
|
13195 | 103 |
_size_in_bytes = _area->size_in_bytes(); |
1 | 104 |
debug_only(_area->_nesting++;) |
105 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
106 |
} |
|
107 |
public: |
|
108 |
||
109 |
#ifndef ASSERT |
|
110 |
ResourceMark(Thread *thread) { |
|
111 |
assert(thread == Thread::current(), "not the current thread"); |
|
112 |
initialize(thread); |
|
113 |
} |
|
114 |
#else |
|
115 |
ResourceMark(Thread *thread); |
|
116 |
#endif // ASSERT |
|
117 |
||
118 |
ResourceMark() { initialize(Thread::current()); } |
|
119 |
||
120 |
ResourceMark( ResourceArea *r ) : |
|
121 |
_area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { |
|
13195 | 122 |
_size_in_bytes = r->_size_in_bytes; |
1 | 123 |
debug_only(_area->_nesting++;) |
124 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
125 |
} |
|
126 |
||
127 |
void reset_to_mark() { |
|
128 |
if (UseMallocOnly) free_malloced_objects(); |
|
129 |
||
130 |
if( _chunk->next() ) // Delete later chunks |
|
131 |
_chunk->next_chop(); |
|
132 |
_area->_chunk = _chunk; // Roll back arena to saved chunk |
|
133 |
_area->_hwm = _hwm; |
|
134 |
_area->_max = _max; |
|
135 |
||
136 |
// clear out this chunk (to detect allocation bugs) |
|
137 |
if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); |
|
138 |
_area->set_size_in_bytes(size_in_bytes()); |
|
139 |
} |
|
140 |
||
141 |
~ResourceMark() { |
|
142 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
143 |
debug_only(_area->_nesting--;) |
|
144 |
reset_to_mark(); |
|
145 |
} |
|
146 |
||
147 |
||
148 |
private: |
|
149 |
void free_malloced_objects() PRODUCT_RETURN; |
|
13195 | 150 |
size_t size_in_bytes() { return _size_in_bytes; } |
1 | 151 |
}; |
152 |
||
153 |
//------------------------------DeoptResourceMark----------------------------------- |
|
154 |
// A deopt resource mark releases all resources allocated after it was constructed |
|
155 |
// when the destructor is called. Typically used as a local variable. It differs |
|
156 |
// from a typical resource more in that it is C-Heap allocated so that deoptimization |
|
157 |
// can use data structures that are arena based but are not amenable to vanilla |
|
158 |
// ResourceMarks because deoptimization can not use a stack allocated mark. During |
|
159 |
// deoptimization we go thru the following steps: |
|
160 |
// |
|
161 |
// 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info |
|
162 |
// 1: create the vframeArray (contains pointers to Resource allocated structures) |
|
163 |
// This allocates the DeoptResourceMark. |
|
164 |
// 2: return to assembly stub and remove stub frame and deoptee frame and create |
|
165 |
// the new skeletal frames. |
|
166 |
// 3: push new stub frame and call unpack_frames |
|
167 |
// 4: retrieve information from the vframeArray to populate the skeletal frames |
|
168 |
// 5: release the DeoptResourceMark |
|
169 |
// 6: return to stub and eventually to interpreter |
|
170 |
// |
|
171 |
// With old style eager deoptimization the vframeArray was created by the vmThread there |
|
172 |
// was no way for the vframeArray to contain resource allocated objects and so |
|
173 |
// a complex set of data structures to simulate an array of vframes in CHeap memory |
|
174 |
// was used. With new style lazy deoptimization the vframeArray is created in the |
|
175 |
// the thread that will use it and we can use a much simpler scheme for the vframeArray |
|
176 |
// leveraging existing data structures if we simply create a way to manage this one |
|
177 |
// special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj |
|
178 |
// then existing ResourceMarks would work fine since no one use new to allocate them |
|
179 |
// and they would be stack allocated. This leaves open the possibilty of accidental |
|
180 |
// misuse so we simple duplicate the ResourceMark functionality here. |
|
181 |
||
13195 | 182 |
class DeoptResourceMark: public CHeapObj<mtInternal> { |
1 | 183 |
protected: |
184 |
ResourceArea *_area; // Resource area to stack allocate |
|
185 |
Chunk *_chunk; // saved arena chunk |
|
186 |
char *_hwm, *_max; |
|
13195 | 187 |
size_t _size_in_bytes; |
1 | 188 |
|
189 |
void initialize(Thread *thread) { |
|
190 |
_area = thread->resource_area(); |
|
191 |
_chunk = _area->_chunk; |
|
192 |
_hwm = _area->_hwm; |
|
193 |
_max= _area->_max; |
|
13195 | 194 |
_size_in_bytes = _area->size_in_bytes(); |
1 | 195 |
debug_only(_area->_nesting++;) |
196 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
197 |
} |
|
198 |
||
199 |
public: |
|
200 |
||
201 |
#ifndef ASSERT |
|
202 |
DeoptResourceMark(Thread *thread) { |
|
203 |
assert(thread == Thread::current(), "not the current thread"); |
|
204 |
initialize(thread); |
|
205 |
} |
|
206 |
#else |
|
207 |
DeoptResourceMark(Thread *thread); |
|
208 |
#endif // ASSERT |
|
209 |
||
210 |
DeoptResourceMark() { initialize(Thread::current()); } |
|
211 |
||
212 |
DeoptResourceMark( ResourceArea *r ) : |
|
213 |
_area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { |
|
13195 | 214 |
_size_in_bytes = _area->size_in_bytes(); |
1 | 215 |
debug_only(_area->_nesting++;) |
216 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
217 |
} |
|
218 |
||
219 |
void reset_to_mark() { |
|
220 |
if (UseMallocOnly) free_malloced_objects(); |
|
221 |
||
222 |
if( _chunk->next() ) // Delete later chunks |
|
223 |
_chunk->next_chop(); |
|
224 |
_area->_chunk = _chunk; // Roll back arena to saved chunk |
|
225 |
_area->_hwm = _hwm; |
|
226 |
_area->_max = _max; |
|
227 |
||
228 |
// clear out this chunk (to detect allocation bugs) |
|
229 |
if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); |
|
230 |
_area->set_size_in_bytes(size_in_bytes()); |
|
231 |
} |
|
232 |
||
233 |
~DeoptResourceMark() { |
|
234 |
assert( _area->_nesting > 0, "must stack allocate RMs" ); |
|
235 |
debug_only(_area->_nesting--;) |
|
236 |
reset_to_mark(); |
|
237 |
} |
|
238 |
||
239 |
||
240 |
private: |
|
241 |
void free_malloced_objects() PRODUCT_RETURN; |
|
13195 | 242 |
size_t size_in_bytes() { return _size_in_bytes; }; |
1 | 243 |
}; |
7397 | 244 |
|
245 |
#endif // SHARE_VM_MEMORY_RESOURCEAREA_HPP |