src/share/vm/memory/resourceArea.hpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7074
833b0f92429a
parent 6876
710a3c8b516e
child 9122
024be04bb151
permissions
-rw-r--r--

merge

aoqi@0 1 /*
zgu@7074 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_MEMORY_RESOURCEAREA_HPP
aoqi@0 26 #define SHARE_VM_MEMORY_RESOURCEAREA_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "runtime/thread.inline.hpp"
aoqi@0 30
aoqi@0 31 // The resource area holds temporary data structures in the VM.
aoqi@0 32 // The actual allocation areas are thread local. Typical usage:
aoqi@0 33 //
aoqi@0 34 // ...
aoqi@0 35 // {
aoqi@0 36 // ResourceMark rm;
aoqi@0 37 // int foo[] = NEW_RESOURCE_ARRAY(int, 64);
aoqi@0 38 // ...
aoqi@0 39 // }
aoqi@0 40 // ...
aoqi@0 41
aoqi@0 42 //------------------------------ResourceArea-----------------------------------
aoqi@0 43 // A ResourceArea is an Arena that supports safe usage of ResourceMark.
aoqi@0 44 class ResourceArea: public Arena {
aoqi@0 45 friend class ResourceMark;
aoqi@0 46 friend class DeoptResourceMark;
aoqi@0 47 friend class VMStructs;
aoqi@0 48 debug_only(int _nesting;) // current # of nested ResourceMarks
aoqi@0 49 debug_only(static int _warned;) // to suppress multiple warnings
aoqi@0 50
aoqi@0 51 public:
zgu@7074 52 ResourceArea() : Arena(mtThread) {
aoqi@0 53 debug_only(_nesting = 0;)
aoqi@0 54 }
aoqi@0 55
zgu@7074 56 ResourceArea(size_t init_size) : Arena(mtThread, init_size) {
aoqi@0 57 debug_only(_nesting = 0;);
aoqi@0 58 }
aoqi@0 59
aoqi@0 60 char* allocate_bytes(size_t size, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM) {
aoqi@0 61 #ifdef ASSERT
aoqi@0 62 if (_nesting < 1 && !_warned++)
aoqi@0 63 fatal("memory leak: allocating without ResourceMark");
aoqi@0 64 if (UseMallocOnly) {
aoqi@0 65 // use malloc, but save pointer in res. area for later freeing
aoqi@0 66 char** save = (char**)internal_malloc_4(sizeof(char*));
zgu@7074 67 return (*save = (char*)os::malloc(size, mtThread, CURRENT_PC));
aoqi@0 68 }
aoqi@0 69 #endif
aoqi@0 70 return (char*)Amalloc(size, alloc_failmode);
aoqi@0 71 }
aoqi@0 72
aoqi@0 73 debug_only(int nesting() const { return _nesting; });
aoqi@0 74 };
aoqi@0 75
aoqi@0 76
aoqi@0 77 //------------------------------ResourceMark-----------------------------------
aoqi@0 78 // A resource mark releases all resources allocated after it was constructed
aoqi@0 79 // when the destructor is called. Typically used as a local variable.
aoqi@0 80 class ResourceMark: public StackObj {
aoqi@0 81 protected:
aoqi@0 82 ResourceArea *_area; // Resource area to stack allocate
aoqi@0 83 Chunk *_chunk; // saved arena chunk
aoqi@0 84 char *_hwm, *_max;
aoqi@0 85 size_t _size_in_bytes;
aoqi@0 86 #ifdef ASSERT
aoqi@0 87 Thread* _thread;
aoqi@0 88 ResourceMark* _previous_resource_mark;
aoqi@0 89 #endif //ASSERT
aoqi@0 90
aoqi@0 91 void initialize(Thread *thread) {
aoqi@0 92 _area = thread->resource_area();
aoqi@0 93 _chunk = _area->_chunk;
aoqi@0 94 _hwm = _area->_hwm;
aoqi@0 95 _max= _area->_max;
aoqi@0 96 _size_in_bytes = _area->size_in_bytes();
aoqi@0 97 debug_only(_area->_nesting++;)
aoqi@0 98 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 99 #ifdef ASSERT
aoqi@0 100 _thread = thread;
aoqi@0 101 _previous_resource_mark = thread->current_resource_mark();
aoqi@0 102 thread->set_current_resource_mark(this);
aoqi@0 103 #endif // ASSERT
aoqi@0 104 }
aoqi@0 105 public:
aoqi@0 106
aoqi@0 107 #ifndef ASSERT
aoqi@0 108 ResourceMark(Thread *thread) {
aoqi@0 109 assert(thread == Thread::current(), "not the current thread");
aoqi@0 110 initialize(thread);
aoqi@0 111 }
aoqi@0 112 #else
aoqi@0 113 ResourceMark(Thread *thread);
aoqi@0 114 #endif // ASSERT
aoqi@0 115
aoqi@0 116 ResourceMark() { initialize(Thread::current()); }
aoqi@0 117
aoqi@0 118 ResourceMark( ResourceArea *r ) :
aoqi@0 119 _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) {
aoqi@0 120 _size_in_bytes = r->_size_in_bytes;
aoqi@0 121 debug_only(_area->_nesting++;)
aoqi@0 122 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 123 #ifdef ASSERT
aoqi@0 124 Thread* thread = ThreadLocalStorage::thread();
aoqi@0 125 if (thread != NULL) {
aoqi@0 126 _thread = thread;
aoqi@0 127 _previous_resource_mark = thread->current_resource_mark();
aoqi@0 128 thread->set_current_resource_mark(this);
aoqi@0 129 } else {
aoqi@0 130 _thread = NULL;
aoqi@0 131 _previous_resource_mark = NULL;
aoqi@0 132 }
aoqi@0 133 #endif // ASSERT
aoqi@0 134 }
aoqi@0 135
aoqi@0 136 void reset_to_mark() {
aoqi@0 137 if (UseMallocOnly) free_malloced_objects();
aoqi@0 138
aoqi@0 139 if( _chunk->next() ) { // Delete later chunks
aoqi@0 140 // reset arena size before delete chunks. Otherwise, the total
aoqi@0 141 // arena size could exceed total chunk size
aoqi@0 142 assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check");
aoqi@0 143 _area->set_size_in_bytes(size_in_bytes());
aoqi@0 144 _chunk->next_chop();
aoqi@0 145 } else {
aoqi@0 146 assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check");
aoqi@0 147 }
aoqi@0 148 _area->_chunk = _chunk; // Roll back arena to saved chunk
aoqi@0 149 _area->_hwm = _hwm;
aoqi@0 150 _area->_max = _max;
aoqi@0 151
aoqi@0 152 // clear out this chunk (to detect allocation bugs)
aoqi@0 153 if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm);
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 ~ResourceMark() {
aoqi@0 157 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 158 debug_only(_area->_nesting--;)
aoqi@0 159 reset_to_mark();
aoqi@0 160 #ifdef ASSERT
aoqi@0 161 if (_thread != NULL) {
aoqi@0 162 _thread->set_current_resource_mark(_previous_resource_mark);
aoqi@0 163 }
aoqi@0 164 #endif // ASSERT
aoqi@0 165 }
aoqi@0 166
aoqi@0 167
aoqi@0 168 private:
aoqi@0 169 void free_malloced_objects() PRODUCT_RETURN;
aoqi@0 170 size_t size_in_bytes() { return _size_in_bytes; }
aoqi@0 171 };
aoqi@0 172
aoqi@0 173 //------------------------------DeoptResourceMark-----------------------------------
aoqi@0 174 // A deopt resource mark releases all resources allocated after it was constructed
aoqi@0 175 // when the destructor is called. Typically used as a local variable. It differs
aoqi@0 176 // from a typical resource more in that it is C-Heap allocated so that deoptimization
aoqi@0 177 // can use data structures that are arena based but are not amenable to vanilla
aoqi@0 178 // ResourceMarks because deoptimization can not use a stack allocated mark. During
aoqi@0 179 // deoptimization we go thru the following steps:
aoqi@0 180 //
aoqi@0 181 // 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info
aoqi@0 182 // 1: create the vframeArray (contains pointers to Resource allocated structures)
aoqi@0 183 // This allocates the DeoptResourceMark.
aoqi@0 184 // 2: return to assembly stub and remove stub frame and deoptee frame and create
aoqi@0 185 // the new skeletal frames.
aoqi@0 186 // 3: push new stub frame and call unpack_frames
aoqi@0 187 // 4: retrieve information from the vframeArray to populate the skeletal frames
aoqi@0 188 // 5: release the DeoptResourceMark
aoqi@0 189 // 6: return to stub and eventually to interpreter
aoqi@0 190 //
aoqi@0 191 // With old style eager deoptimization the vframeArray was created by the vmThread there
aoqi@0 192 // was no way for the vframeArray to contain resource allocated objects and so
aoqi@0 193 // a complex set of data structures to simulate an array of vframes in CHeap memory
aoqi@0 194 // was used. With new style lazy deoptimization the vframeArray is created in the
aoqi@0 195 // the thread that will use it and we can use a much simpler scheme for the vframeArray
aoqi@0 196 // leveraging existing data structures if we simply create a way to manage this one
aoqi@0 197 // special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj
aoqi@0 198 // then existing ResourceMarks would work fine since no one use new to allocate them
aoqi@0 199 // and they would be stack allocated. This leaves open the possibilty of accidental
aoqi@0 200 // misuse so we simple duplicate the ResourceMark functionality here.
aoqi@0 201
aoqi@0 202 class DeoptResourceMark: public CHeapObj<mtInternal> {
aoqi@0 203 protected:
aoqi@0 204 ResourceArea *_area; // Resource area to stack allocate
aoqi@0 205 Chunk *_chunk; // saved arena chunk
aoqi@0 206 char *_hwm, *_max;
aoqi@0 207 size_t _size_in_bytes;
aoqi@0 208
aoqi@0 209 void initialize(Thread *thread) {
aoqi@0 210 _area = thread->resource_area();
aoqi@0 211 _chunk = _area->_chunk;
aoqi@0 212 _hwm = _area->_hwm;
aoqi@0 213 _max= _area->_max;
aoqi@0 214 _size_in_bytes = _area->size_in_bytes();
aoqi@0 215 debug_only(_area->_nesting++;)
aoqi@0 216 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 public:
aoqi@0 220
aoqi@0 221 #ifndef ASSERT
aoqi@0 222 DeoptResourceMark(Thread *thread) {
aoqi@0 223 assert(thread == Thread::current(), "not the current thread");
aoqi@0 224 initialize(thread);
aoqi@0 225 }
aoqi@0 226 #else
aoqi@0 227 DeoptResourceMark(Thread *thread);
aoqi@0 228 #endif // ASSERT
aoqi@0 229
aoqi@0 230 DeoptResourceMark() { initialize(Thread::current()); }
aoqi@0 231
aoqi@0 232 DeoptResourceMark( ResourceArea *r ) :
aoqi@0 233 _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) {
aoqi@0 234 _size_in_bytes = _area->size_in_bytes();
aoqi@0 235 debug_only(_area->_nesting++;)
aoqi@0 236 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 void reset_to_mark() {
aoqi@0 240 if (UseMallocOnly) free_malloced_objects();
aoqi@0 241
aoqi@0 242 if( _chunk->next() ) { // Delete later chunks
aoqi@0 243 // reset arena size before delete chunks. Otherwise, the total
aoqi@0 244 // arena size could exceed total chunk size
aoqi@0 245 assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check");
aoqi@0 246 _area->set_size_in_bytes(size_in_bytes());
aoqi@0 247 _chunk->next_chop();
aoqi@0 248 } else {
aoqi@0 249 assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check");
aoqi@0 250 }
aoqi@0 251 _area->_chunk = _chunk; // Roll back arena to saved chunk
aoqi@0 252 _area->_hwm = _hwm;
aoqi@0 253 _area->_max = _max;
aoqi@0 254
aoqi@0 255 // clear out this chunk (to detect allocation bugs)
aoqi@0 256 if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm);
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 ~DeoptResourceMark() {
aoqi@0 260 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 261 debug_only(_area->_nesting--;)
aoqi@0 262 reset_to_mark();
aoqi@0 263 }
aoqi@0 264
aoqi@0 265
aoqi@0 266 private:
aoqi@0 267 void free_malloced_objects() PRODUCT_RETURN;
aoqi@0 268 size_t size_in_bytes() { return _size_in_bytes; };
aoqi@0 269 };
aoqi@0 270
aoqi@0 271 #endif // SHARE_VM_MEMORY_RESOURCEAREA_HPP

mercurial