src/share/vm/utilities/stack.hpp

Tue, 12 Feb 2013 12:19:28 -0500

author
zgu
date
Tue, 12 Feb 2013 12:19:28 -0500
changeset 4573
5ee2b330eacd
parent 4153
b9a9ed0f8eeb
child 6876
710a3c8b516e
child 9316
a27880c1288b
permissions
-rw-r--r--

8007950: Undo hs_file permission change
Summary: Reverse hs_err file permission back to 0666, as early push was premature
Reviewed-by: dsamersoff, dcubed, acorn

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

mercurial