src/share/vm/memory/resourceArea.hpp

Mon, 23 Mar 2020 17:57:13 +0000

author
poonam
date
Mon, 23 Mar 2020 17:57:13 +0000
changeset 9965
c39172598323
parent 9056
940519c00887
child 9122
024be04bb151
permissions
-rw-r--r--

8231779: crash HeapWord*ParallelScavengeHeap::failed_mem_allocate
Reviewed-by: dlong, tschatzl, pliden

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

mercurial