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