Path: blob/master/src/hotspot/share/memory/resourceArea.hpp
40949 views
/*1* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_MEMORY_RESOURCEAREA_HPP25#define SHARE_MEMORY_RESOURCEAREA_HPP2627#include "memory/allocation.hpp"28#include "runtime/thread.hpp"2930// The resource area holds temporary data structures in the VM.31// The actual allocation areas are thread local. Typical usage:32//33// ...34// {35// ResourceMark rm;36// int foo[] = NEW_RESOURCE_ARRAY(int, 64);37// ...38// }39// ...4041//------------------------------ResourceArea-----------------------------------42// A ResourceArea is an Arena that supports safe usage of ResourceMark.43class ResourceArea: public Arena {44friend class VMStructs;4546#ifdef ASSERT47int _nesting; // current # of nested ResourceMarks48void verify_has_resource_mark();49#endif // ASSERT5051public:52ResourceArea(MEMFLAGS flags = mtThread) :53Arena(flags) DEBUG_ONLY(COMMA _nesting(0)) {}5455ResourceArea(size_t init_size, MEMFLAGS flags = mtThread) :56Arena(flags, init_size) DEBUG_ONLY(COMMA _nesting(0)) {}5758char* allocate_bytes(size_t size, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);5960// Bias this resource area to specific memory type61// (by default, ResourceArea is tagged as mtThread, per-thread general purpose storage)62void bias_to(MEMFLAGS flags);6364DEBUG_ONLY(int nesting() const { return _nesting; })6566// Capture the state of a ResourceArea needed by a ResourceMark for67// rollback to that mark.68class SavedState {69friend class ResourceArea;70Chunk* _chunk;71char* _hwm;72char* _max;73size_t _size_in_bytes;74DEBUG_ONLY(int _nesting;)7576public:77SavedState(ResourceArea* area) :78_chunk(area->_chunk),79_hwm(area->_hwm),80_max(area->_max),81_size_in_bytes(area->_size_in_bytes)82DEBUG_ONLY(COMMA _nesting(area->_nesting))83{}84};8586// Check and adjust debug-only nesting level.87void activate_state(const SavedState& state) {88assert(_nesting == state._nesting, "precondition");89assert(_nesting >= 0, "precondition");90assert(_nesting < INT_MAX, "nesting overflow");91DEBUG_ONLY(++_nesting;)92}9394// Check and adjust debug-only nesting level.95void deactivate_state(const SavedState& state) {96assert(_nesting > state._nesting, "deactivating inactive mark");97assert((_nesting - state._nesting) == 1, "deactivating across another mark");98DEBUG_ONLY(--_nesting;)99}100101// Roll back the allocation state to the indicated state values.102// The state must be the current state for this thread.103void rollback_to(const SavedState& state) {104assert(_nesting > state._nesting, "rollback to inactive mark");105assert((_nesting - state._nesting) == 1, "rollback across another mark");106107if (UseMallocOnly) {108free_malloced_objects(state._chunk, state._hwm, state._max, _hwm);109}110111if (state._chunk->next() != nullptr) { // Delete later chunks.112// Reset size before deleting chunks. Otherwise, the total113// size could exceed the total chunk size.114assert(size_in_bytes() > state._size_in_bytes,115"size: " SIZE_FORMAT ", saved size: " SIZE_FORMAT,116size_in_bytes(), state._size_in_bytes);117set_size_in_bytes(state._size_in_bytes);118state._chunk->next_chop();119} else {120assert(size_in_bytes() == state._size_in_bytes, "Sanity check");121}122_chunk = state._chunk; // Roll back to saved chunk.123_hwm = state._hwm;124_max = state._max;125126// Clear out this chunk (to detect allocation bugs)127if (ZapResourceArea) {128memset(state._hwm, badResourceValue, state._max - state._hwm);129}130}131};132133134//------------------------------ResourceMark-----------------------------------135// A resource mark releases all resources allocated after it was constructed136// when the destructor is called. Typically used as a local variable.137138// Shared part of implementation for ResourceMark and DeoptResourceMark.139class ResourceMarkImpl {140ResourceArea* _area; // Resource area to stack allocate141ResourceArea::SavedState _saved_state;142143NONCOPYABLE(ResourceMarkImpl);144145public:146explicit ResourceMarkImpl(ResourceArea* area) :147_area(area),148_saved_state(area)149{150_area->activate_state(_saved_state);151}152153explicit ResourceMarkImpl(Thread* thread)154: ResourceMarkImpl(thread->resource_area()) {}155156~ResourceMarkImpl() {157reset_to_mark();158_area->deactivate_state(_saved_state);159}160161void reset_to_mark() const {162_area->rollback_to(_saved_state);163}164};165166class ResourceMark: public StackObj {167const ResourceMarkImpl _impl;168#ifdef ASSERT169Thread* _thread;170ResourceMark* _previous_resource_mark;171#endif // ASSERT172173NONCOPYABLE(ResourceMark);174175// Helper providing common constructor implementation.176#ifndef ASSERT177ResourceMark(ResourceArea* area, Thread* thread) : _impl(area) {}178#else179ResourceMark(ResourceArea* area, Thread* thread) :180_impl(area),181_thread(thread),182_previous_resource_mark(nullptr)183{184if (_thread != nullptr) {185assert(_thread == Thread::current(), "not the current thread");186_previous_resource_mark = _thread->current_resource_mark();187_thread->set_current_resource_mark(this);188}189}190#endif // ASSERT191192public:193194ResourceMark() : ResourceMark(Thread::current()) {}195196explicit ResourceMark(Thread* thread)197: ResourceMark(thread->resource_area(), thread) {}198199explicit ResourceMark(ResourceArea* area)200: ResourceMark(area, DEBUG_ONLY(Thread::current_or_null()) NOT_DEBUG(nullptr)) {}201202#ifdef ASSERT203~ResourceMark() {204if (_thread != nullptr) {205_thread->set_current_resource_mark(_previous_resource_mark);206}207}208#endif // ASSERT209210void reset_to_mark() { _impl.reset_to_mark(); }211};212213//------------------------------DeoptResourceMark-----------------------------------214// A deopt resource mark releases all resources allocated after it was constructed215// when the destructor is called. Typically used as a local variable. It differs216// from a typical resource more in that it is C-Heap allocated so that deoptimization217// can use data structures that are arena based but are not amenable to vanilla218// ResourceMarks because deoptimization can not use a stack allocated mark. During219// deoptimization we go thru the following steps:220//221// 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info222// 1: create the vframeArray (contains pointers to Resource allocated structures)223// This allocates the DeoptResourceMark.224// 2: return to assembly stub and remove stub frame and deoptee frame and create225// the new skeletal frames.226// 3: push new stub frame and call unpack_frames227// 4: retrieve information from the vframeArray to populate the skeletal frames228// 5: release the DeoptResourceMark229// 6: return to stub and eventually to interpreter230//231// With old style eager deoptimization the vframeArray was created by the vmThread there232// was no way for the vframeArray to contain resource allocated objects and so233// a complex set of data structures to simulate an array of vframes in CHeap memory234// was used. With new style lazy deoptimization the vframeArray is created in the235// the thread that will use it and we can use a much simpler scheme for the vframeArray236// leveraging existing data structures if we simply create a way to manage this one237// special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj238// then existing ResourceMarks would work fine since no one use new to allocate them239// and they would be stack allocated. This leaves open the possibility of accidental240// misuse so we duplicate the ResourceMark functionality via a shared implementation241// class.242243class DeoptResourceMark: public CHeapObj<mtInternal> {244const ResourceMarkImpl _impl;245246NONCOPYABLE(DeoptResourceMark);247248public:249explicit DeoptResourceMark(Thread* thread) : _impl(thread) {}250251void reset_to_mark() { _impl.reset_to_mark(); }252};253254#endif // SHARE_MEMORY_RESOURCEAREA_HPP255256257