src/share/vm/memory/resourceArea.hpp

Sat, 07 Nov 2020 10:30:02 +0800

author
aoqi
date
Sat, 07 Nov 2020 10:30:02 +0800
changeset 10026
8c95980d0b66
parent 9122
024be04bb151
permissions
-rw-r--r--

Added tag mips-jdk8u275-b01 for changeset d3b4d62f391f

aoqi@0 1 /*
zgu@9055 2 * Copyright (c) 1997, 2017, 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@9055 52 ResourceArea(MEMFLAGS flags = mtThread) : Arena(flags) {
aoqi@0 53 debug_only(_nesting = 0;)
aoqi@0 54 }
aoqi@0 55
zgu@9055 56 ResourceArea(size_t init_size, MEMFLAGS flags = mtThread) : Arena(flags, 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
zgu@9056 73 // Bias this resource area to specific memory type
zgu@9056 74 // (by default, ResourceArea is tagged as mtThread, per-thread general purpose storage)
zgu@9056 75 void bias_to(MEMFLAGS flags);
zgu@9056 76
zgu@9056 77 debug_only(int nesting() const { return _nesting; })
aoqi@0 78 };
aoqi@0 79
aoqi@0 80
aoqi@0 81 //------------------------------ResourceMark-----------------------------------
aoqi@0 82 // A resource mark releases all resources allocated after it was constructed
aoqi@0 83 // when the destructor is called. Typically used as a local variable.
aoqi@0 84 class ResourceMark: public StackObj {
aoqi@0 85 protected:
aoqi@0 86 ResourceArea *_area; // Resource area to stack allocate
aoqi@0 87 Chunk *_chunk; // saved arena chunk
aoqi@0 88 char *_hwm, *_max;
aoqi@0 89 size_t _size_in_bytes;
aoqi@0 90 #ifdef ASSERT
aoqi@0 91 Thread* _thread;
aoqi@0 92 ResourceMark* _previous_resource_mark;
aoqi@0 93 #endif //ASSERT
aoqi@0 94
aoqi@0 95 void initialize(Thread *thread) {
aoqi@0 96 _area = thread->resource_area();
aoqi@0 97 _chunk = _area->_chunk;
aoqi@0 98 _hwm = _area->_hwm;
aoqi@0 99 _max= _area->_max;
aoqi@0 100 _size_in_bytes = _area->size_in_bytes();
aoqi@0 101 debug_only(_area->_nesting++;)
aoqi@0 102 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 103 #ifdef ASSERT
aoqi@0 104 _thread = thread;
aoqi@0 105 _previous_resource_mark = thread->current_resource_mark();
aoqi@0 106 thread->set_current_resource_mark(this);
aoqi@0 107 #endif // ASSERT
aoqi@0 108 }
aoqi@0 109 public:
aoqi@0 110
aoqi@0 111 #ifndef ASSERT
aoqi@0 112 ResourceMark(Thread *thread) {
aoqi@0 113 assert(thread == Thread::current(), "not the current thread");
aoqi@0 114 initialize(thread);
aoqi@0 115 }
aoqi@0 116 #else
aoqi@0 117 ResourceMark(Thread *thread);
aoqi@0 118 #endif // ASSERT
aoqi@0 119
aoqi@0 120 ResourceMark() { initialize(Thread::current()); }
aoqi@0 121
aoqi@0 122 ResourceMark( ResourceArea *r ) :
aoqi@0 123 _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) {
aoqi@0 124 _size_in_bytes = r->_size_in_bytes;
aoqi@0 125 debug_only(_area->_nesting++;)
aoqi@0 126 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 127 #ifdef ASSERT
aoqi@0 128 Thread* thread = ThreadLocalStorage::thread();
aoqi@0 129 if (thread != NULL) {
aoqi@0 130 _thread = thread;
aoqi@0 131 _previous_resource_mark = thread->current_resource_mark();
aoqi@0 132 thread->set_current_resource_mark(this);
aoqi@0 133 } else {
aoqi@0 134 _thread = NULL;
aoqi@0 135 _previous_resource_mark = NULL;
aoqi@0 136 }
aoqi@0 137 #endif // ASSERT
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 void reset_to_mark() {
aoqi@0 141 if (UseMallocOnly) free_malloced_objects();
aoqi@0 142
aoqi@0 143 if( _chunk->next() ) { // Delete later chunks
aoqi@0 144 // reset arena size before delete chunks. Otherwise, the total
aoqi@0 145 // arena size could exceed total chunk size
aoqi@0 146 assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check");
aoqi@0 147 _area->set_size_in_bytes(size_in_bytes());
aoqi@0 148 _chunk->next_chop();
aoqi@0 149 } else {
aoqi@0 150 assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check");
aoqi@0 151 }
aoqi@0 152 _area->_chunk = _chunk; // Roll back arena to saved chunk
aoqi@0 153 _area->_hwm = _hwm;
aoqi@0 154 _area->_max = _max;
aoqi@0 155
aoqi@0 156 // clear out this chunk (to detect allocation bugs)
aoqi@0 157 if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm);
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 ~ResourceMark() {
aoqi@0 161 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 162 debug_only(_area->_nesting--;)
aoqi@0 163 reset_to_mark();
aoqi@0 164 #ifdef ASSERT
aoqi@0 165 if (_thread != NULL) {
aoqi@0 166 _thread->set_current_resource_mark(_previous_resource_mark);
aoqi@0 167 }
aoqi@0 168 #endif // ASSERT
aoqi@0 169 }
aoqi@0 170
aoqi@0 171
aoqi@0 172 private:
aoqi@0 173 void free_malloced_objects() PRODUCT_RETURN;
aoqi@0 174 size_t size_in_bytes() { return _size_in_bytes; }
aoqi@0 175 };
aoqi@0 176
aoqi@0 177 //------------------------------DeoptResourceMark-----------------------------------
aoqi@0 178 // A deopt resource mark releases all resources allocated after it was constructed
aoqi@0 179 // when the destructor is called. Typically used as a local variable. It differs
aoqi@0 180 // from a typical resource more in that it is C-Heap allocated so that deoptimization
aoqi@0 181 // can use data structures that are arena based but are not amenable to vanilla
aoqi@0 182 // ResourceMarks because deoptimization can not use a stack allocated mark. During
aoqi@0 183 // deoptimization we go thru the following steps:
aoqi@0 184 //
aoqi@0 185 // 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info
aoqi@0 186 // 1: create the vframeArray (contains pointers to Resource allocated structures)
aoqi@0 187 // This allocates the DeoptResourceMark.
aoqi@0 188 // 2: return to assembly stub and remove stub frame and deoptee frame and create
aoqi@0 189 // the new skeletal frames.
aoqi@0 190 // 3: push new stub frame and call unpack_frames
aoqi@0 191 // 4: retrieve information from the vframeArray to populate the skeletal frames
aoqi@0 192 // 5: release the DeoptResourceMark
aoqi@0 193 // 6: return to stub and eventually to interpreter
aoqi@0 194 //
aoqi@0 195 // With old style eager deoptimization the vframeArray was created by the vmThread there
aoqi@0 196 // was no way for the vframeArray to contain resource allocated objects and so
aoqi@0 197 // a complex set of data structures to simulate an array of vframes in CHeap memory
aoqi@0 198 // was used. With new style lazy deoptimization the vframeArray is created in the
aoqi@0 199 // the thread that will use it and we can use a much simpler scheme for the vframeArray
aoqi@0 200 // leveraging existing data structures if we simply create a way to manage this one
aoqi@0 201 // special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj
aoqi@0 202 // then existing ResourceMarks would work fine since no one use new to allocate them
aoqi@0 203 // and they would be stack allocated. This leaves open the possibilty of accidental
aoqi@0 204 // misuse so we simple duplicate the ResourceMark functionality here.
aoqi@0 205
aoqi@0 206 class DeoptResourceMark: public CHeapObj<mtInternal> {
aoqi@0 207 protected:
aoqi@0 208 ResourceArea *_area; // Resource area to stack allocate
aoqi@0 209 Chunk *_chunk; // saved arena chunk
aoqi@0 210 char *_hwm, *_max;
aoqi@0 211 size_t _size_in_bytes;
aoqi@0 212
aoqi@0 213 void initialize(Thread *thread) {
aoqi@0 214 _area = thread->resource_area();
aoqi@0 215 _chunk = _area->_chunk;
aoqi@0 216 _hwm = _area->_hwm;
aoqi@0 217 _max= _area->_max;
aoqi@0 218 _size_in_bytes = _area->size_in_bytes();
aoqi@0 219 debug_only(_area->_nesting++;)
aoqi@0 220 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 221 }
aoqi@0 222
aoqi@0 223 public:
aoqi@0 224
aoqi@0 225 #ifndef ASSERT
aoqi@0 226 DeoptResourceMark(Thread *thread) {
aoqi@0 227 assert(thread == Thread::current(), "not the current thread");
aoqi@0 228 initialize(thread);
aoqi@0 229 }
aoqi@0 230 #else
aoqi@0 231 DeoptResourceMark(Thread *thread);
aoqi@0 232 #endif // ASSERT
aoqi@0 233
aoqi@0 234 DeoptResourceMark() { initialize(Thread::current()); }
aoqi@0 235
aoqi@0 236 DeoptResourceMark( ResourceArea *r ) :
aoqi@0 237 _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) {
aoqi@0 238 _size_in_bytes = _area->size_in_bytes();
aoqi@0 239 debug_only(_area->_nesting++;)
aoqi@0 240 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 241 }
aoqi@0 242
aoqi@0 243 void reset_to_mark() {
aoqi@0 244 if (UseMallocOnly) free_malloced_objects();
aoqi@0 245
aoqi@0 246 if( _chunk->next() ) { // Delete later chunks
aoqi@0 247 // reset arena size before delete chunks. Otherwise, the total
aoqi@0 248 // arena size could exceed total chunk size
aoqi@0 249 assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check");
aoqi@0 250 _area->set_size_in_bytes(size_in_bytes());
aoqi@0 251 _chunk->next_chop();
aoqi@0 252 } else {
aoqi@0 253 assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check");
aoqi@0 254 }
aoqi@0 255 _area->_chunk = _chunk; // Roll back arena to saved chunk
aoqi@0 256 _area->_hwm = _hwm;
aoqi@0 257 _area->_max = _max;
aoqi@0 258
aoqi@0 259 // clear out this chunk (to detect allocation bugs)
aoqi@0 260 if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm);
aoqi@0 261 }
aoqi@0 262
aoqi@0 263 ~DeoptResourceMark() {
aoqi@0 264 assert( _area->_nesting > 0, "must stack allocate RMs" );
aoqi@0 265 debug_only(_area->_nesting--;)
aoqi@0 266 reset_to_mark();
aoqi@0 267 }
aoqi@0 268
aoqi@0 269
aoqi@0 270 private:
aoqi@0 271 void free_malloced_objects() PRODUCT_RETURN;
aoqi@0 272 size_t size_in_bytes() { return _size_in_bytes; };
aoqi@0 273 };
aoqi@0 274
aoqi@0 275 #endif // SHARE_VM_MEMORY_RESOURCEAREA_HPP

mercurial