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