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