src/share/vm/utilities/stack.hpp

Thu, 27 Jan 2011 16:11:27 -0800

author
coleenp
date
Thu, 27 Jan 2011 16:11:27 -0800
changeset 2497
3582bf76420e
parent 2314
f95d63e2154a
child 3900
d2a62e0f25eb
permissions
-rw-r--r--

6990754: Use native memory and reference counting to implement SymbolTable
Summary: move symbols from permgen into C heap and reference count them
Reviewed-by: never, acorn, jmasa, stefank

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

mercurial