src/share/vm/utilities/stack.hpp

Wed, 27 Apr 2016 01:25:04 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:25:04 +0800
changeset 0
f90c822e73f8
child 6876
710a3c8b516e
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17

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

mercurial