duke@435: /* duke@435: * Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: // The resource area holds temporary data structures in the VM. duke@435: // The actual allocation areas are thread local. Typical usage: duke@435: // duke@435: // ... duke@435: // { duke@435: // ResourceMark rm; duke@435: // int foo[] = NEW_RESOURCE_ARRAY(int, 64); duke@435: // ... duke@435: // } duke@435: // ... duke@435: duke@435: //------------------------------ResourceArea----------------------------------- duke@435: // A ResourceArea is an Arena that supports safe usage of ResourceMark. duke@435: class ResourceArea: public Arena { duke@435: friend class ResourceMark; duke@435: friend class DeoptResourceMark; duke@435: debug_only(int _nesting;) // current # of nested ResourceMarks duke@435: debug_only(static int _warned;) // to suppress multiple warnings duke@435: duke@435: public: duke@435: ResourceArea() { duke@435: debug_only(_nesting = 0;) duke@435: } duke@435: duke@435: ResourceArea(size_t init_size) : Arena(init_size) { duke@435: debug_only(_nesting = 0;); duke@435: } duke@435: duke@435: char* allocate_bytes(size_t size) { duke@435: #ifdef ASSERT duke@435: if (_nesting < 1 && !_warned++) duke@435: fatal("memory leak: allocating without ResourceMark"); duke@435: if (UseMallocOnly) { duke@435: // use malloc, but save pointer in res. area for later freeing duke@435: char** save = (char**)internal_malloc_4(sizeof(char*)); duke@435: return (*save = (char*)os::malloc(size)); duke@435: } duke@435: #endif duke@435: return (char*)Amalloc(size); duke@435: } duke@435: duke@435: debug_only(int nesting() const { return _nesting; }); duke@435: }; duke@435: duke@435: duke@435: //------------------------------ResourceMark----------------------------------- duke@435: // A resource mark releases all resources allocated after it was constructed duke@435: // when the destructor is called. Typically used as a local variable. duke@435: class ResourceMark: public StackObj { duke@435: protected: duke@435: ResourceArea *_area; // Resource area to stack allocate duke@435: Chunk *_chunk; // saved arena chunk duke@435: char *_hwm, *_max; duke@435: NOT_PRODUCT(size_t _size_in_bytes;) duke@435: duke@435: void initialize(Thread *thread) { duke@435: _area = thread->resource_area(); duke@435: _chunk = _area->_chunk; duke@435: _hwm = _area->_hwm; duke@435: _max= _area->_max; duke@435: NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) duke@435: debug_only(_area->_nesting++;) duke@435: assert( _area->_nesting > 0, "must stack allocate RMs" ); duke@435: } duke@435: duke@435: public: duke@435: duke@435: #ifndef ASSERT duke@435: ResourceMark(Thread *thread) { duke@435: assert(thread == Thread::current(), "not the current thread"); duke@435: initialize(thread); duke@435: } duke@435: #else duke@435: ResourceMark(Thread *thread); duke@435: #endif // ASSERT duke@435: duke@435: ResourceMark() { initialize(Thread::current()); } duke@435: duke@435: ResourceMark( ResourceArea *r ) : duke@435: _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { duke@435: NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) duke@435: debug_only(_area->_nesting++;) duke@435: assert( _area->_nesting > 0, "must stack allocate RMs" ); duke@435: } duke@435: duke@435: void reset_to_mark() { duke@435: if (UseMallocOnly) free_malloced_objects(); duke@435: duke@435: if( _chunk->next() ) // Delete later chunks duke@435: _chunk->next_chop(); duke@435: _area->_chunk = _chunk; // Roll back arena to saved chunk duke@435: _area->_hwm = _hwm; duke@435: _area->_max = _max; duke@435: duke@435: // clear out this chunk (to detect allocation bugs) duke@435: if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); duke@435: _area->set_size_in_bytes(size_in_bytes()); duke@435: } duke@435: duke@435: ~ResourceMark() { duke@435: assert( _area->_nesting > 0, "must stack allocate RMs" ); duke@435: debug_only(_area->_nesting--;) duke@435: reset_to_mark(); duke@435: } duke@435: duke@435: duke@435: private: duke@435: void free_malloced_objects() PRODUCT_RETURN; duke@435: size_t size_in_bytes() NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0; duke@435: }; duke@435: duke@435: //------------------------------DeoptResourceMark----------------------------------- duke@435: // A deopt resource mark releases all resources allocated after it was constructed duke@435: // when the destructor is called. Typically used as a local variable. It differs duke@435: // from a typical resource more in that it is C-Heap allocated so that deoptimization duke@435: // can use data structures that are arena based but are not amenable to vanilla duke@435: // ResourceMarks because deoptimization can not use a stack allocated mark. During duke@435: // deoptimization we go thru the following steps: duke@435: // duke@435: // 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info duke@435: // 1: create the vframeArray (contains pointers to Resource allocated structures) duke@435: // This allocates the DeoptResourceMark. duke@435: // 2: return to assembly stub and remove stub frame and deoptee frame and create duke@435: // the new skeletal frames. duke@435: // 3: push new stub frame and call unpack_frames duke@435: // 4: retrieve information from the vframeArray to populate the skeletal frames duke@435: // 5: release the DeoptResourceMark duke@435: // 6: return to stub and eventually to interpreter duke@435: // duke@435: // With old style eager deoptimization the vframeArray was created by the vmThread there duke@435: // was no way for the vframeArray to contain resource allocated objects and so duke@435: // a complex set of data structures to simulate an array of vframes in CHeap memory duke@435: // was used. With new style lazy deoptimization the vframeArray is created in the duke@435: // the thread that will use it and we can use a much simpler scheme for the vframeArray duke@435: // leveraging existing data structures if we simply create a way to manage this one duke@435: // special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj duke@435: // then existing ResourceMarks would work fine since no one use new to allocate them duke@435: // and they would be stack allocated. This leaves open the possibilty of accidental duke@435: // misuse so we simple duplicate the ResourceMark functionality here. duke@435: duke@435: class DeoptResourceMark: public CHeapObj { duke@435: protected: duke@435: ResourceArea *_area; // Resource area to stack allocate duke@435: Chunk *_chunk; // saved arena chunk duke@435: char *_hwm, *_max; duke@435: NOT_PRODUCT(size_t _size_in_bytes;) duke@435: duke@435: void initialize(Thread *thread) { duke@435: _area = thread->resource_area(); duke@435: _chunk = _area->_chunk; duke@435: _hwm = _area->_hwm; duke@435: _max= _area->_max; duke@435: NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) duke@435: debug_only(_area->_nesting++;) duke@435: assert( _area->_nesting > 0, "must stack allocate RMs" ); duke@435: } duke@435: duke@435: public: duke@435: duke@435: #ifndef ASSERT duke@435: DeoptResourceMark(Thread *thread) { duke@435: assert(thread == Thread::current(), "not the current thread"); duke@435: initialize(thread); duke@435: } duke@435: #else duke@435: DeoptResourceMark(Thread *thread); duke@435: #endif // ASSERT duke@435: duke@435: DeoptResourceMark() { initialize(Thread::current()); } duke@435: duke@435: DeoptResourceMark( ResourceArea *r ) : duke@435: _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) { duke@435: NOT_PRODUCT(_size_in_bytes = _area->size_in_bytes();) duke@435: debug_only(_area->_nesting++;) duke@435: assert( _area->_nesting > 0, "must stack allocate RMs" ); duke@435: } duke@435: duke@435: void reset_to_mark() { duke@435: if (UseMallocOnly) free_malloced_objects(); duke@435: duke@435: if( _chunk->next() ) // Delete later chunks duke@435: _chunk->next_chop(); duke@435: _area->_chunk = _chunk; // Roll back arena to saved chunk duke@435: _area->_hwm = _hwm; duke@435: _area->_max = _max; duke@435: duke@435: // clear out this chunk (to detect allocation bugs) duke@435: if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm); duke@435: _area->set_size_in_bytes(size_in_bytes()); duke@435: } duke@435: duke@435: ~DeoptResourceMark() { duke@435: assert( _area->_nesting > 0, "must stack allocate RMs" ); duke@435: debug_only(_area->_nesting--;) duke@435: reset_to_mark(); duke@435: } duke@435: duke@435: duke@435: private: duke@435: void free_malloced_objects() PRODUCT_RETURN; duke@435: size_t size_in_bytes() NOT_PRODUCT({ return _size_in_bytes; }) PRODUCT_RETURN0; duke@435: };