src/hotspot/share/memory/resourceArea.hpp
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 53244 9807daeb47c4
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 48157
diff changeset
     2
 * Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 48157
diff changeset
    25
#ifndef SHARE_MEMORY_RESOURCEAREA_HPP
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 48157
diff changeset
    26
#define SHARE_MEMORY_RESOURCEAREA_HPP
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
37456
bf26e0f4235f 8153742: Move Thread::current() to thread.hpp
stefank
parents: 34633
diff changeset
    29
#include "runtime/thread.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// The resource area holds temporary data structures in the VM.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// The actual allocation areas are thread local. Typical usage:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
//   ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
//   {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//     ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
//     int foo[] = NEW_RESOURCE_ARRAY(int, 64);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
//     ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
//   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
//   ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//------------------------------ResourceArea-----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
// A ResourceArea is an Arena that supports safe usage of ResourceMark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
class ResourceArea: public Arena {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  friend class ResourceMark;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  friend class DeoptResourceMark;
10547
ea4a2ec31ae2 7088955: add C2 IR support to the SA
never
parents: 7397
diff changeset
    47
  friend class VMStructs;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  debug_only(int _nesting;)             // current # of nested ResourceMarks
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  debug_only(static int _warned;)       // to suppress multiple warnings
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
public:
47537
5390057a4489 8187629: NMT: Memory miscounting in compiler (C2)
zgu
parents: 47216
diff changeset
    52
  ResourceArea(MEMFLAGS flags = mtThread) : Arena(flags) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    debug_only(_nesting = 0;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
47537
5390057a4489 8187629: NMT: Memory miscounting in compiler (C2)
zgu
parents: 47216
diff changeset
    56
  ResourceArea(size_t init_size, MEMFLAGS flags = mtThread) : Arena(flags, init_size) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
    debug_only(_nesting = 0;);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
48157
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47600
diff changeset
    60
  char* allocate_bytes(size_t size, AllocFailType alloc_failmode = AllocFailStrategy::EXIT_OOM);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
47600
5c8607bb3d2d 8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents: 47537
diff changeset
    62
  // Bias this resource area to specific memory type
5c8607bb3d2d 8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents: 47537
diff changeset
    63
  // (by default, ResourceArea is tagged as mtThread, per-thread general purpose storage)
5c8607bb3d2d 8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents: 47537
diff changeset
    64
  void bias_to(MEMFLAGS flags);
5c8607bb3d2d 8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents: 47537
diff changeset
    65
5c8607bb3d2d 8187685: NMT: Tracking compiler memory usage of thread's resource area
zgu
parents: 47537
diff changeset
    66
  debug_only(int nesting() const { return _nesting; })
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
//------------------------------ResourceMark-----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
// A resource mark releases all resources allocated after it was constructed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
// when the destructor is called.  Typically used as a local variable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
class ResourceMark: public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  ResourceArea *_area;          // Resource area to stack allocate
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  Chunk *_chunk;                // saved arena chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  char *_hwm, *_max;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
    78
  size_t _size_in_bytes;
18928
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    79
#ifdef ASSERT
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    80
  Thread* _thread;
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    81
  ResourceMark* _previous_resource_mark;
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    82
#endif //ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  void initialize(Thread *thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
    _area = thread->resource_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    _chunk = _area->_chunk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
    _hwm = _area->_hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    _max= _area->_max;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
    89
    _size_in_bytes = _area->size_in_bytes();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    debug_only(_area->_nesting++;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    assert( _area->_nesting > 0, "must stack allocate RMs" );
18928
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    92
#ifdef ASSERT
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    93
    _thread = thread;
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    94
    _previous_resource_mark = thread->current_resource_mark();
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    95
    thread->set_current_resource_mark(this);
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
    96
#endif // ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
#ifndef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  ResourceMark(Thread *thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
    assert(thread == Thread::current(), "not the current thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    initialize(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  ResourceMark(Thread *thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  ResourceMark()               { initialize(Thread::current()); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  ResourceMark( ResourceArea *r ) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
   113
    _size_in_bytes = r->_size_in_bytes;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    debug_only(_area->_nesting++;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    assert( _area->_nesting > 0, "must stack allocate RMs" );
18928
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   116
#ifdef ASSERT
34633
2a6c7c7b30a7 8132510: Replace ThreadLocalStorage with compiler/language-based thread-local variables
dholmes
parents: 25946
diff changeset
   117
    Thread* thread = Thread::current_or_null();
18928
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   118
    if (thread != NULL) {
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   119
      _thread = thread;
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   120
      _previous_resource_mark = thread->current_resource_mark();
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   121
      thread->set_current_resource_mark(this);
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   122
    } else {
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   123
      _thread = NULL;
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   124
      _previous_resource_mark = NULL;
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   125
    }
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   126
#endif // ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  void reset_to_mark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    if (UseMallocOnly) free_malloced_objects();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
14120
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   132
    if( _chunk->next() ) {       // Delete later chunks
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   133
      // reset arena size before delete chunks. Otherwise, the total
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   134
      // arena size could exceed total chunk size
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   135
      assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check");
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   136
      _area->set_size_in_bytes(size_in_bytes());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      _chunk->next_chop();
14120
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   138
    } else {
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   139
      assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check");
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   140
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    _area->_chunk = _chunk;     // Roll back arena to saved chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    _area->_hwm = _hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    _area->_max = _max;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    // clear out this chunk (to detect allocation bugs)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  ~ResourceMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    assert( _area->_nesting > 0, "must stack allocate RMs" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    debug_only(_area->_nesting--;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    reset_to_mark();
18928
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   153
#ifdef ASSERT
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   154
    if (_thread != NULL) {
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   155
      _thread->set_current_resource_mark(_previous_resource_mark);
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   156
    }
ac0163dd0a50 7143807: ResourceMark nesting problem in stringStream
fparain
parents: 14583
diff changeset
   157
#endif // ASSERT
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  void free_malloced_objects()                                         PRODUCT_RETURN;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
   163
  size_t size_in_bytes() { return _size_in_bytes; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
//------------------------------DeoptResourceMark-----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
// A deopt resource mark releases all resources allocated after it was constructed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
// when the destructor is called.  Typically used as a local variable. It differs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
// from a typical resource more in that it is C-Heap allocated so that deoptimization
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
// can use data structures that are arena based but are not amenable to vanilla
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
// ResourceMarks because deoptimization can not use a stack allocated mark. During
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
// deoptimization we go thru the following steps:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
// 0: start in assembly stub and call either uncommon_trap/fetch_unroll_info
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
// 1: create the vframeArray (contains pointers to Resource allocated structures)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
//   This allocates the DeoptResourceMark.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
// 2: return to assembly stub and remove stub frame and deoptee frame and create
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
//    the new skeletal frames.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
// 3: push new stub frame and call unpack_frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
// 4: retrieve information from the vframeArray to populate the skeletal frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
// 5: release the DeoptResourceMark
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
// 6: return to stub and eventually to interpreter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
// With old style eager deoptimization the vframeArray was created by the vmThread there
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
// was no way for the vframeArray to contain resource allocated objects and so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
// a complex set of data structures to simulate an array of vframes in CHeap memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
// was used. With new style lazy deoptimization the vframeArray is created in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
// the thread that will use it and we can use a much simpler scheme for the vframeArray
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
// leveraging existing data structures if we simply create a way to manage this one
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
// special need for a ResourceMark. If ResourceMark simply inherited from CHeapObj
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
// then existing ResourceMarks would work fine since no one use new to allocate them
22551
9bf46d16dcc6 8025856: Fix typos in the GC code
jwilhelm
parents: 18928
diff changeset
   192
// and they would be stack allocated. This leaves open the possibility of accidental
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
// misuse so we simple duplicate the ResourceMark functionality here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
   195
class DeoptResourceMark: public CHeapObj<mtInternal> {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  ResourceArea *_area;          // Resource area to stack allocate
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  Chunk *_chunk;                // saved arena chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  char *_hwm, *_max;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
   200
  size_t _size_in_bytes;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  void initialize(Thread *thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    _area = thread->resource_area();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    _chunk = _area->_chunk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    _hwm = _area->_hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    _max= _area->_max;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
   207
    _size_in_bytes = _area->size_in_bytes();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    debug_only(_area->_nesting++;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    assert( _area->_nesting > 0, "must stack allocate RMs" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
#ifndef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  DeoptResourceMark(Thread *thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    assert(thread == Thread::current(), "not the current thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    initialize(thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  DeoptResourceMark(Thread *thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
#endif // ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  DeoptResourceMark()               { initialize(Thread::current()); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  DeoptResourceMark( ResourceArea *r ) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    _area(r), _chunk(r->_chunk), _hwm(r->_hwm), _max(r->_max) {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
   227
    _size_in_bytes = _area->size_in_bytes();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    debug_only(_area->_nesting++;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    assert( _area->_nesting > 0, "must stack allocate RMs" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
  void reset_to_mark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    if (UseMallocOnly) free_malloced_objects();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
14120
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   235
    if( _chunk->next() ) {        // Delete later chunks
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   236
      // reset arena size before delete chunks. Otherwise, the total
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   237
      // arena size could exceed total chunk size
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   238
      assert(_area->size_in_bytes() > size_in_bytes(), "Sanity check");
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   239
      _area->set_size_in_bytes(size_in_bytes());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      _chunk->next_chop();
14120
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   241
    } else {
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   242
      assert(_area->size_in_bytes() == size_in_bytes(), "Sanity check");
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 14083
diff changeset
   243
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    _area->_chunk = _chunk;     // Roll back arena to saved chunk
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    _area->_hwm = _hwm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    _area->_max = _max;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
    // clear out this chunk (to detect allocation bugs)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    if (ZapResourceArea) memset(_hwm, badResourceValue, _max - _hwm);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  ~DeoptResourceMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    assert( _area->_nesting > 0, "must stack allocate RMs" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    debug_only(_area->_nesting--;)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    reset_to_mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  void free_malloced_objects()                                         PRODUCT_RETURN;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10565
diff changeset
   261
  size_t size_in_bytes() { return _size_in_bytes; };
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
};
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   263
53244
9807daeb47c4 8216167: Update include guards to reflect correct directories
coleenp
parents: 48157
diff changeset
   264
#endif // SHARE_MEMORY_RESOURCEAREA_HPP