Thu, 27 Dec 2018 11:43:33 +0800
Merge
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 | |
stefank@9316 | 99 | // Number of elements that fit in 4K bytes minus the size of two pointers |
stefank@9316 | 100 | // (link field and malloc header). |
stefank@9316 | 101 | static const size_t _default_segment_size = (4096 - 2 * sizeof(E*)) / sizeof(E); |
stefank@9316 | 102 | static size_t default_segment_size() { return _default_segment_size; } |
stefank@9316 | 103 | |
aoqi@0 | 104 | // segment_size: number of items per segment |
aoqi@0 | 105 | // max_cache_size: maxmium number of *segments* to cache |
aoqi@0 | 106 | // max_size: maximum number of items allowed, rounded to a multiple of |
aoqi@0 | 107 | // the segment size (0 == unlimited) |
stefank@9316 | 108 | inline Stack(size_t segment_size = _default_segment_size, |
aoqi@0 | 109 | size_t max_cache_size = 4, size_t max_size = 0); |
aoqi@0 | 110 | inline ~Stack() { clear(true); } |
aoqi@0 | 111 | |
aoqi@0 | 112 | inline bool is_empty() const { return this->_cur_seg == NULL; } |
aoqi@0 | 113 | inline bool is_full() const { return this->_full_seg_size >= this->max_size(); } |
aoqi@0 | 114 | |
aoqi@0 | 115 | // Performance sensitive code should use is_empty() instead of size() == 0 and |
aoqi@0 | 116 | // is_full() instead of size() == max_size(). Using a conditional here allows |
aoqi@0 | 117 | // just one var to be updated when pushing/popping elements instead of two; |
aoqi@0 | 118 | // _full_seg_size is updated only when pushing/popping segments. |
aoqi@0 | 119 | inline size_t size() const { |
aoqi@0 | 120 | return is_empty() ? 0 : this->_full_seg_size + this->_cur_seg_size; |
aoqi@0 | 121 | } |
aoqi@0 | 122 | |
aoqi@0 | 123 | inline void push(E elem); |
aoqi@0 | 124 | inline E pop(); |
aoqi@0 | 125 | |
aoqi@0 | 126 | // Clear everything from the stack, releasing the associated memory. If |
aoqi@0 | 127 | // clear_cache is true, also release any cached segments. |
aoqi@0 | 128 | void clear(bool clear_cache = false); |
aoqi@0 | 129 | |
aoqi@0 | 130 | protected: |
aoqi@0 | 131 | // Each segment includes space for _seg_size elements followed by a link |
aoqi@0 | 132 | // (pointer) to the previous segment; the space is allocated as a single block |
aoqi@0 | 133 | // of size segment_bytes(). _seg_size is rounded up if necessary so the link |
aoqi@0 | 134 | // is properly aligned. The C struct for the layout would be: |
aoqi@0 | 135 | // |
aoqi@0 | 136 | // struct segment { |
aoqi@0 | 137 | // E elements[_seg_size]; |
aoqi@0 | 138 | // E* link; |
aoqi@0 | 139 | // }; |
aoqi@0 | 140 | |
aoqi@0 | 141 | // Round up seg_size to keep the link field aligned. |
aoqi@0 | 142 | static inline size_t adjust_segment_size(size_t seg_size); |
aoqi@0 | 143 | |
aoqi@0 | 144 | // Methods for allocation size and getting/setting the link. |
aoqi@0 | 145 | inline size_t link_offset() const; // Byte offset of link field. |
aoqi@0 | 146 | inline size_t segment_bytes() const; // Segment size in bytes. |
aoqi@0 | 147 | inline E** link_addr(E* seg) const; // Address of the link field. |
aoqi@0 | 148 | inline E* get_link(E* seg) const; // Extract the link from seg. |
aoqi@0 | 149 | inline E* set_link(E* new_seg, E* old_seg); // new_seg.link = old_seg. |
aoqi@0 | 150 | |
aoqi@0 | 151 | virtual E* alloc(size_t bytes); |
aoqi@0 | 152 | virtual void free(E* addr, size_t bytes); |
aoqi@0 | 153 | |
aoqi@0 | 154 | void push_segment(); |
aoqi@0 | 155 | void pop_segment(); |
aoqi@0 | 156 | |
aoqi@0 | 157 | void free_segments(E* seg); // Free all segments in the list. |
aoqi@0 | 158 | inline void reset(bool reset_cache); // Reset all data fields. |
aoqi@0 | 159 | |
aoqi@0 | 160 | DEBUG_ONLY(void verify(bool at_empty_transition) const;) |
aoqi@0 | 161 | DEBUG_ONLY(void zap_segment(E* seg, bool zap_link_field) const;) |
aoqi@0 | 162 | |
aoqi@0 | 163 | private: |
aoqi@0 | 164 | E* _cur_seg; // Current segment. |
aoqi@0 | 165 | E* _cache; // Segment cache to avoid ping-ponging. |
aoqi@0 | 166 | }; |
aoqi@0 | 167 | |
aoqi@0 | 168 | template <class E, MEMFLAGS F> class ResourceStack: public Stack<E, F>, public ResourceObj |
aoqi@0 | 169 | { |
aoqi@0 | 170 | public: |
aoqi@0 | 171 | // If this class becomes widely used, it may make sense to save the Thread |
aoqi@0 | 172 | // and use it when allocating segments. |
aoqi@0 | 173 | // ResourceStack(size_t segment_size = Stack<E, F>::default_segment_size()): |
aoqi@0 | 174 | ResourceStack(size_t segment_size): Stack<E, F>(segment_size, max_uintx) |
aoqi@0 | 175 | { } |
aoqi@0 | 176 | |
aoqi@0 | 177 | // Set the segment pointers to NULL so the parent dtor does not free them; |
aoqi@0 | 178 | // that must be done by the ResourceMark code. |
aoqi@0 | 179 | ~ResourceStack() { Stack<E, F>::reset(true); } |
aoqi@0 | 180 | |
aoqi@0 | 181 | protected: |
aoqi@0 | 182 | virtual E* alloc(size_t bytes); |
aoqi@0 | 183 | virtual void free(E* addr, size_t bytes); |
aoqi@0 | 184 | |
aoqi@0 | 185 | private: |
aoqi@0 | 186 | void clear(bool clear_cache = false); |
aoqi@0 | 187 | }; |
aoqi@0 | 188 | |
aoqi@0 | 189 | template <class E, MEMFLAGS F> |
aoqi@0 | 190 | class StackIterator: public StackObj |
aoqi@0 | 191 | { |
aoqi@0 | 192 | public: |
aoqi@0 | 193 | StackIterator(Stack<E, F>& stack): _stack(stack) { sync(); } |
aoqi@0 | 194 | |
aoqi@0 | 195 | Stack<E, F>& stack() const { return _stack; } |
aoqi@0 | 196 | |
aoqi@0 | 197 | bool is_empty() const { return _cur_seg == NULL; } |
aoqi@0 | 198 | |
aoqi@0 | 199 | E next() { return *next_addr(); } |
aoqi@0 | 200 | E* next_addr(); |
aoqi@0 | 201 | |
aoqi@0 | 202 | void sync(); // Sync the iterator's state to the stack's current state. |
aoqi@0 | 203 | |
aoqi@0 | 204 | private: |
aoqi@0 | 205 | Stack<E, F>& _stack; |
aoqi@0 | 206 | size_t _cur_seg_size; |
aoqi@0 | 207 | E* _cur_seg; |
aoqi@0 | 208 | size_t _full_seg_size; |
aoqi@0 | 209 | }; |
aoqi@0 | 210 | |
aoqi@0 | 211 | #ifdef __GNUC__ |
aoqi@0 | 212 | #undef inline |
aoqi@0 | 213 | #endif // __GNUC__ |
aoqi@0 | 214 | |
aoqi@0 | 215 | #endif // SHARE_VM_UTILITIES_STACK_HPP |