src/hotspot/share/utilities/stack.hpp
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 30176 hotspot/src/share/vm/utilities/stack.hpp@90aa2ac76bae
child 48157 7c4d43c26352
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     1
/*
13963
e5b53c306fb5 7197424: update copyright year to match last edit in jdk8 hotspot repository
mikael
parents: 13195
diff changeset
     2
 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     4
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     8
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    13
 * accompanied this code).
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    14
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    18
 *
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    21
 * questions.
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    22
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    23
 */
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    25
#ifndef SHARE_VM_UTILITIES_STACK_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    26
#define SHARE_VM_UTILITIES_STACK_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    27
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    28
#include "memory/allocation.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    29
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
    30
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    31
// Class Stack (below) grows and shrinks by linking together "segments" which
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    32
// are allocated on demand.  Segments are arrays of the element type (E) plus an
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    33
// extra pointer-sized field to store the segment link.  Recently emptied
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    34
// segments are kept in a cache and reused.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    35
//
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    36
// Notes/caveats:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    37
//
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    38
// The size of an element must either evenly divide the size of a pointer or be
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    39
// a multiple of the size of a pointer.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    40
//
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    41
// Destructors are not called for elements popped off the stack, so element
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    42
// types which rely on destructors for things like reference counting will not
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    43
// work properly.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    44
//
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    45
// Class Stack allocates segments from the C heap.  However, two protected
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    46
// virtual methods are used to alloc/free memory which subclasses can override:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    47
//
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    48
//      virtual void* alloc(size_t bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    49
//      virtual void  free(void* addr, size_t bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    50
//
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    51
// The alloc() method must return storage aligned for any use.  The
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    52
// implementation in class Stack assumes that alloc() will terminate the process
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    53
// if the allocation fails.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    54
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    55
template <class E, MEMFLAGS F> class StackIterator;
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    56
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    57
// StackBase holds common data/methods that don't depend on the element type,
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    58
// factored out to reduce template code duplication.
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    59
template <MEMFLAGS F> class StackBase
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    60
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    61
public:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    62
  size_t segment_size()   const { return _seg_size; } // Elements per segment.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    63
  size_t max_size()       const { return _max_size; } // Max elements allowed.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    64
  size_t max_cache_size() const { return _max_cache_size; } // Max segments
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    65
                                                            // allowed in cache.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    66
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    67
  size_t cache_size() const { return _cache_size; }   // Segments in the cache.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    68
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    69
protected:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    70
  // The ctor arguments correspond to the like-named functions above.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    71
  // segment_size:    number of items per segment
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    72
  // max_cache_size:  maxmium number of *segments* to cache
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    73
  // max_size:        maximum number of items allowed, rounded to a multiple of
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    74
  //                  the segment size (0 == unlimited)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    75
  inline StackBase(size_t segment_size, size_t max_cache_size, size_t max_size);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    76
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    77
  // Round max_size to a multiple of the segment size.  Treat 0 as unlimited.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    78
  static inline size_t adjust_max_size(size_t max_size, size_t seg_size);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    79
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    80
protected:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    81
  const size_t _seg_size;       // Number of items per segment.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    82
  const size_t _max_size;       // Maximum number of items allowed in the stack.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    83
  const size_t _max_cache_size; // Maximum number of segments to cache.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    84
  size_t       _cur_seg_size;   // Number of items in the current segment.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    85
  size_t       _full_seg_size;  // Number of items in already-filled segments.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    86
  size_t       _cache_size;     // Number of segments in the cache.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    87
};
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    88
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    89
#ifdef __GNUC__
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    90
#define inline
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    91
#endif // __GNUC__
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    92
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    93
template <class E, MEMFLAGS F>
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    94
class Stack:  public StackBase<F>
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    95
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    96
public:
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    97
  friend class StackIterator<E, F>;
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    98
30176
90aa2ac76bae 8077420: Build failure with SS12u4
stefank
parents: 13963
diff changeset
    99
  // Number of elements that fit in 4K bytes minus the size of two pointers
90aa2ac76bae 8077420: Build failure with SS12u4
stefank
parents: 13963
diff changeset
   100
  // (link field and malloc header).
90aa2ac76bae 8077420: Build failure with SS12u4
stefank
parents: 13963
diff changeset
   101
  static const size_t _default_segment_size =  (4096 - 2 * sizeof(E*)) / sizeof(E);
90aa2ac76bae 8077420: Build failure with SS12u4
stefank
parents: 13963
diff changeset
   102
  static size_t default_segment_size() { return _default_segment_size; }
90aa2ac76bae 8077420: Build failure with SS12u4
stefank
parents: 13963
diff changeset
   103
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   104
  // segment_size:    number of items per segment
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   105
  // max_cache_size:  maxmium number of *segments* to cache
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   106
  // max_size:        maximum number of items allowed, rounded to a multiple of
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   107
  //                  the segment size (0 == unlimited)
30176
90aa2ac76bae 8077420: Build failure with SS12u4
stefank
parents: 13963
diff changeset
   108
  inline Stack(size_t segment_size = _default_segment_size,
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   109
               size_t max_cache_size = 4, size_t max_size = 0);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   110
  inline ~Stack() { clear(true); }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   111
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   112
  inline bool is_empty() const { return this->_cur_seg == NULL; }
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   113
  inline bool is_full()  const { return this->_full_seg_size >= this->max_size(); }
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   114
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   115
  // Performance sensitive code should use is_empty() instead of size() == 0 and
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   116
  // is_full() instead of size() == max_size().  Using a conditional here allows
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   117
  // just one var to be updated when pushing/popping elements instead of two;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   118
  // _full_seg_size is updated only when pushing/popping segments.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   119
  inline size_t size() const {
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   120
    return is_empty() ? 0 : this->_full_seg_size + this->_cur_seg_size;
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   121
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   122
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   123
  inline void push(E elem);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   124
  inline E    pop();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   125
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   126
  // Clear everything from the stack, releasing the associated memory.  If
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   127
  // clear_cache is true, also release any cached segments.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   128
  void clear(bool clear_cache = false);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   129
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   130
protected:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   131
  // Each segment includes space for _seg_size elements followed by a link
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   132
  // (pointer) to the previous segment; the space is allocated as a single block
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   133
  // of size segment_bytes().  _seg_size is rounded up if necessary so the link
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   134
  // is properly aligned.  The C struct for the layout would be:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   135
  //
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   136
  // struct segment {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   137
  //   E     elements[_seg_size];
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   138
  //   E*    link;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   139
  // };
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   140
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   141
  // Round up seg_size to keep the link field aligned.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   142
  static inline size_t adjust_segment_size(size_t seg_size);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   143
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   144
  // Methods for allocation size and getting/setting the link.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   145
  inline size_t link_offset() const;              // Byte offset of link field.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   146
  inline size_t segment_bytes() const;            // Segment size in bytes.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   147
  inline E**    link_addr(E* seg) const;          // Address of the link field.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   148
  inline E*     get_link(E* seg) const;           // Extract the link from seg.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   149
  inline E*     set_link(E* new_seg, E* old_seg); // new_seg.link = old_seg.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   150
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   151
  virtual E*    alloc(size_t bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   152
  virtual void  free(E* addr, size_t bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   153
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   154
  void push_segment();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   155
  void pop_segment();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   156
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   157
  void free_segments(E* seg);          // Free all segments in the list.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   158
  inline void reset(bool reset_cache); // Reset all data fields.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   159
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   160
  DEBUG_ONLY(void verify(bool at_empty_transition) const;)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   161
  DEBUG_ONLY(void zap_segment(E* seg, bool zap_link_field) const;)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   162
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   163
private:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   164
  E* _cur_seg;    // Current segment.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   165
  E* _cache;      // Segment cache to avoid ping-ponging.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   166
};
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   167
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   168
template <class E, MEMFLAGS F> class ResourceStack:  public Stack<E, F>, public ResourceObj
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   169
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   170
public:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   171
  // If this class becomes widely used, it may make sense to save the Thread
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   172
  // and use it when allocating segments.
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   173
//  ResourceStack(size_t segment_size = Stack<E, F>::default_segment_size()):
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   174
  ResourceStack(size_t segment_size): Stack<E, F>(segment_size, max_uintx)
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   175
    { }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   176
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   177
  // Set the segment pointers to NULL so the parent dtor does not free them;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   178
  // that must be done by the ResourceMark code.
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   179
  ~ResourceStack() { Stack<E, F>::reset(true); }
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   180
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   181
protected:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   182
  virtual E*   alloc(size_t bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   183
  virtual void free(E* addr, size_t bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   184
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   185
private:
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   186
  void clear(bool clear_cache = false);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   187
};
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   188
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   189
template <class E, MEMFLAGS F>
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   190
class StackIterator: public StackObj
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   191
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   192
public:
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   193
  StackIterator(Stack<E, F>& stack): _stack(stack) { sync(); }
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   194
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   195
  Stack<E, F>& stack() const { return _stack; }
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   196
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   197
  bool is_empty() const { return _cur_seg == NULL; }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   198
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   199
  E  next() { return *next_addr(); }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   200
  E* next_addr();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   201
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   202
  void sync(); // Sync the iterator's state to the stack's current state.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   203
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   204
private:
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
   205
  Stack<E, F>& _stack;
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   206
  size_t    _cur_seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   207
  E*        _cur_seg;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   208
  size_t    _full_seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   209
};
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   210
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   211
#ifdef __GNUC__
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   212
#undef inline
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   213
#endif // __GNUC__
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
   214
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6762
diff changeset
   215
#endif // SHARE_VM_UTILITIES_STACK_HPP