src/share/vm/utilities/stack.inline.hpp

Tue, 14 Apr 2015 11:40:13 +0200

author
stefank
date
Tue, 14 Apr 2015 11:40:13 +0200
changeset 9316
a27880c1288b
parent 4153
b9a9ed0f8eeb
child 9448
73d689add964
child 9997
c7ef664f8649
permissions
-rw-r--r--

8077420: Build failure with SS12u4
Reviewed-by: dholmes, lfoltan

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_INLINE_HPP
stefank@2314 26 #define SHARE_VM_UTILITIES_STACK_INLINE_HPP
stefank@2314 27
stefank@2314 28 #include "utilities/stack.hpp"
stefank@2314 29
zgu@3900 30 template <MEMFLAGS F> StackBase<F>::StackBase(size_t segment_size, size_t max_cache_size,
jcoomes@2191 31 size_t max_size):
jcoomes@2191 32 _seg_size(segment_size),
jcoomes@2191 33 _max_cache_size(max_cache_size),
jcoomes@2191 34 _max_size(adjust_max_size(max_size, segment_size))
jcoomes@2191 35 {
jcoomes@2191 36 assert(_max_size % _seg_size == 0, "not a multiple");
jcoomes@2191 37 }
jcoomes@2191 38
zgu@3900 39 template <MEMFLAGS F> size_t StackBase<F>::adjust_max_size(size_t max_size, size_t seg_size)
jcoomes@2191 40 {
jcoomes@2191 41 assert(seg_size > 0, "cannot be 0");
jcoomes@2191 42 assert(max_size >= seg_size || max_size == 0, "max_size too small");
jcoomes@2191 43 const size_t limit = max_uintx - (seg_size - 1);
jcoomes@2191 44 if (max_size == 0 || max_size > limit) {
jcoomes@2191 45 max_size = limit;
jcoomes@2191 46 }
jcoomes@2191 47 return (max_size + seg_size - 1) / seg_size * seg_size;
jcoomes@2191 48 }
jcoomes@2191 49
zgu@3900 50 template <class E, MEMFLAGS F>
zgu@3900 51 Stack<E, F>::Stack(size_t segment_size, size_t max_cache_size, size_t max_size):
zgu@3900 52 StackBase<F>(adjust_segment_size(segment_size), max_cache_size, max_size)
jcoomes@2191 53 {
jcoomes@2191 54 reset(true);
jcoomes@2191 55 }
jcoomes@2191 56
zgu@3900 57 template <class E, MEMFLAGS F>
zgu@3900 58 void Stack<E, F>::push(E item)
jcoomes@2191 59 {
jcoomes@2191 60 assert(!is_full(), "pushing onto a full stack");
zgu@3900 61 if (this->_cur_seg_size == this->_seg_size) {
jcoomes@2191 62 push_segment();
jcoomes@2191 63 }
zgu@3900 64 this->_cur_seg[this->_cur_seg_size] = item;
zgu@3900 65 ++this->_cur_seg_size;
jcoomes@2191 66 }
jcoomes@2191 67
zgu@3900 68 template <class E, MEMFLAGS F>
zgu@3900 69 E Stack<E, F>::pop()
jcoomes@2191 70 {
jcoomes@2191 71 assert(!is_empty(), "popping from an empty stack");
zgu@3900 72 if (this->_cur_seg_size == 1) {
zgu@3900 73 E tmp = _cur_seg[--this->_cur_seg_size];
jcoomes@2191 74 pop_segment();
jcoomes@2191 75 return tmp;
jcoomes@2191 76 }
zgu@3900 77 return this->_cur_seg[--this->_cur_seg_size];
jcoomes@2191 78 }
jcoomes@2191 79
zgu@3900 80 template <class E, MEMFLAGS F>
zgu@3900 81 void Stack<E, F>::clear(bool clear_cache)
jcoomes@2191 82 {
jcoomes@2191 83 free_segments(_cur_seg);
jcoomes@2191 84 if (clear_cache) free_segments(_cache);
jcoomes@2191 85 reset(clear_cache);
jcoomes@2191 86 }
jcoomes@2191 87
zgu@3900 88 template <class E, MEMFLAGS F>
zgu@3900 89 size_t Stack<E, F>::adjust_segment_size(size_t seg_size)
jcoomes@2191 90 {
jcoomes@2191 91 const size_t elem_sz = sizeof(E);
jcoomes@2191 92 const size_t ptr_sz = sizeof(E*);
jcoomes@2191 93 assert(elem_sz % ptr_sz == 0 || ptr_sz % elem_sz == 0, "bad element size");
jcoomes@2191 94 if (elem_sz < ptr_sz) {
jcoomes@2191 95 return align_size_up(seg_size * elem_sz, ptr_sz) / elem_sz;
jcoomes@2191 96 }
jcoomes@2191 97 return seg_size;
jcoomes@2191 98 }
jcoomes@2191 99
zgu@3900 100 template <class E, MEMFLAGS F>
zgu@3900 101 size_t Stack<E, F>::link_offset() const
jcoomes@2191 102 {
zgu@3900 103 return align_size_up(this->_seg_size * sizeof(E), sizeof(E*));
jcoomes@2191 104 }
jcoomes@2191 105
zgu@3900 106 template <class E, MEMFLAGS F>
zgu@3900 107 size_t Stack<E, F>::segment_bytes() const
jcoomes@2191 108 {
jcoomes@2191 109 return link_offset() + sizeof(E*);
jcoomes@2191 110 }
jcoomes@2191 111
zgu@3900 112 template <class E, MEMFLAGS F>
zgu@3900 113 E** Stack<E, F>::link_addr(E* seg) const
jcoomes@2191 114 {
jcoomes@2191 115 return (E**) ((char*)seg + link_offset());
jcoomes@2191 116 }
jcoomes@2191 117
zgu@3900 118 template <class E, MEMFLAGS F>
zgu@3900 119 E* Stack<E, F>::get_link(E* seg) const
jcoomes@2191 120 {
jcoomes@2191 121 return *link_addr(seg);
jcoomes@2191 122 }
jcoomes@2191 123
zgu@3900 124 template <class E, MEMFLAGS F>
zgu@3900 125 E* Stack<E, F>::set_link(E* new_seg, E* old_seg)
jcoomes@2191 126 {
jcoomes@2191 127 *link_addr(new_seg) = old_seg;
jcoomes@2191 128 return new_seg;
jcoomes@2191 129 }
jcoomes@2191 130
zgu@3900 131 template <class E, MEMFLAGS F>
zgu@3900 132 E* Stack<E, F>::alloc(size_t bytes)
jcoomes@2191 133 {
zgu@3900 134 return (E*) NEW_C_HEAP_ARRAY(char, bytes, F);
jcoomes@2191 135 }
jcoomes@2191 136
zgu@3900 137 template <class E, MEMFLAGS F>
zgu@3900 138 void Stack<E, F>::free(E* addr, size_t bytes)
jcoomes@2191 139 {
zgu@3900 140 FREE_C_HEAP_ARRAY(char, (char*) addr, F);
jcoomes@2191 141 }
jcoomes@2191 142
zgu@3900 143 template <class E, MEMFLAGS F>
zgu@3900 144 void Stack<E, F>::push_segment()
jcoomes@2191 145 {
zgu@3900 146 assert(this->_cur_seg_size == this->_seg_size, "current segment is not full");
jcoomes@2191 147 E* next;
zgu@3900 148 if (this->_cache_size > 0) {
jcoomes@2191 149 // Use a cached segment.
jcoomes@2191 150 next = _cache;
jcoomes@2191 151 _cache = get_link(_cache);
zgu@3900 152 --this->_cache_size;
jcoomes@2191 153 } else {
jcoomes@2191 154 next = alloc(segment_bytes());
jcoomes@2191 155 DEBUG_ONLY(zap_segment(next, true);)
jcoomes@2191 156 }
jcoomes@2191 157 const bool at_empty_transition = is_empty();
zgu@3900 158 this->_cur_seg = set_link(next, _cur_seg);
zgu@3900 159 this->_cur_seg_size = 0;
zgu@3900 160 this->_full_seg_size += at_empty_transition ? 0 : this->_seg_size;
jcoomes@2191 161 DEBUG_ONLY(verify(at_empty_transition);)
jcoomes@2191 162 }
jcoomes@2191 163
zgu@3900 164 template <class E, MEMFLAGS F>
zgu@3900 165 void Stack<E, F>::pop_segment()
jcoomes@2191 166 {
zgu@3900 167 assert(this->_cur_seg_size == 0, "current segment is not empty");
jcoomes@2191 168 E* const prev = get_link(_cur_seg);
zgu@3900 169 if (this->_cache_size < this->_max_cache_size) {
jcoomes@2191 170 // Add the current segment to the cache.
jcoomes@2191 171 DEBUG_ONLY(zap_segment(_cur_seg, false);)
jcoomes@2191 172 _cache = set_link(_cur_seg, _cache);
zgu@3900 173 ++this->_cache_size;
jcoomes@2191 174 } else {
jcoomes@2191 175 DEBUG_ONLY(zap_segment(_cur_seg, true);)
jcoomes@2191 176 free(_cur_seg, segment_bytes());
jcoomes@2191 177 }
jcoomes@2191 178 const bool at_empty_transition = prev == NULL;
zgu@3900 179 this->_cur_seg = prev;
zgu@3900 180 this->_cur_seg_size = this->_seg_size;
zgu@3900 181 this->_full_seg_size -= at_empty_transition ? 0 : this->_seg_size;
jcoomes@2191 182 DEBUG_ONLY(verify(at_empty_transition);)
jcoomes@2191 183 }
jcoomes@2191 184
zgu@3900 185 template <class E, MEMFLAGS F>
zgu@3900 186 void Stack<E, F>::free_segments(E* seg)
jcoomes@2191 187 {
jcoomes@2191 188 const size_t bytes = segment_bytes();
jcoomes@2191 189 while (seg != NULL) {
jcoomes@2191 190 E* const prev = get_link(seg);
jcoomes@2191 191 free(seg, bytes);
jcoomes@2191 192 seg = prev;
jcoomes@2191 193 }
jcoomes@2191 194 }
jcoomes@2191 195
zgu@3900 196 template <class E, MEMFLAGS F>
zgu@3900 197 void Stack<E, F>::reset(bool reset_cache)
jcoomes@2191 198 {
zgu@3900 199 this->_cur_seg_size = this->_seg_size; // So push() will alloc a new segment.
zgu@3900 200 this->_full_seg_size = 0;
jcoomes@2191 201 _cur_seg = NULL;
jcoomes@2191 202 if (reset_cache) {
zgu@3900 203 this->_cache_size = 0;
jcoomes@2191 204 _cache = NULL;
jcoomes@2191 205 }
jcoomes@2191 206 }
jcoomes@2191 207
jcoomes@2191 208 #ifdef ASSERT
zgu@3900 209 template <class E, MEMFLAGS F>
zgu@3900 210 void Stack<E, F>::verify(bool at_empty_transition) const
jcoomes@2191 211 {
zgu@3900 212 assert(size() <= this->max_size(), "stack exceeded bounds");
zgu@3900 213 assert(this->cache_size() <= this->max_cache_size(), "cache exceeded bounds");
zgu@3900 214 assert(this->_cur_seg_size <= this->segment_size(), "segment index exceeded bounds");
jcoomes@2191 215
zgu@3900 216 assert(this->_full_seg_size % this->_seg_size == 0, "not a multiple");
jcoomes@2191 217 assert(at_empty_transition || is_empty() == (size() == 0), "mismatch");
zgu@3900 218 assert((_cache == NULL) == (this->cache_size() == 0), "mismatch");
jcoomes@2191 219
jcoomes@2191 220 if (is_empty()) {
zgu@3900 221 assert(this->_cur_seg_size == this->segment_size(), "sanity");
jcoomes@2191 222 }
jcoomes@2191 223 }
jcoomes@2191 224
zgu@3900 225 template <class E, MEMFLAGS F>
zgu@3900 226 void Stack<E, F>::zap_segment(E* seg, bool zap_link_field) const
jcoomes@2191 227 {
jcoomes@2191 228 if (!ZapStackSegments) return;
jcoomes@2191 229 const size_t zap_bytes = segment_bytes() - (zap_link_field ? 0 : sizeof(E*));
jcoomes@2191 230 uint32_t* cur = (uint32_t*)seg;
jcoomes@2191 231 const uint32_t* end = cur + zap_bytes / sizeof(uint32_t);
jcoomes@2191 232 while (cur < end) {
jcoomes@2191 233 *cur++ = 0xfadfaded;
jcoomes@2191 234 }
jcoomes@2191 235 }
jcoomes@2191 236 #endif
jcoomes@2191 237
zgu@3900 238 template <class E, MEMFLAGS F>
zgu@3900 239 E* ResourceStack<E, F>::alloc(size_t bytes)
jcoomes@2191 240 {
jcoomes@2191 241 return (E*) resource_allocate_bytes(bytes);
jcoomes@2191 242 }
jcoomes@2191 243
zgu@3900 244 template <class E, MEMFLAGS F>
zgu@3900 245 void ResourceStack<E, F>::free(E* addr, size_t bytes)
jcoomes@2191 246 {
jcoomes@2191 247 resource_free_bytes((char*) addr, bytes);
jcoomes@2191 248 }
jcoomes@2191 249
zgu@3900 250 template <class E, MEMFLAGS F>
zgu@3900 251 void StackIterator<E, F>::sync()
jcoomes@2191 252 {
jcoomes@2191 253 _full_seg_size = _stack._full_seg_size;
jcoomes@2191 254 _cur_seg_size = _stack._cur_seg_size;
jcoomes@2191 255 _cur_seg = _stack._cur_seg;
jcoomes@2191 256 }
jcoomes@2191 257
zgu@3900 258 template <class E, MEMFLAGS F>
zgu@3900 259 E* StackIterator<E, F>::next_addr()
jcoomes@2191 260 {
jcoomes@2191 261 assert(!is_empty(), "no items left");
jcoomes@2191 262 if (_cur_seg_size == 1) {
jcoomes@2191 263 E* addr = _cur_seg;
jcoomes@2191 264 _cur_seg = _stack.get_link(_cur_seg);
jcoomes@2191 265 _cur_seg_size = _stack.segment_size();
jcoomes@2191 266 _full_seg_size -= _stack.segment_size();
jcoomes@2191 267 return addr;
jcoomes@2191 268 }
jcoomes@2191 269 return _cur_seg + --_cur_seg_size;
jcoomes@2191 270 }
stefank@2314 271
stefank@2314 272 #endif // SHARE_VM_UTILITIES_STACK_INLINE_HPP

mercurial