1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/utilities/stack.inline.hpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,280 @@ 1.4 +/* 1.5 + * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +#ifndef SHARE_VM_UTILITIES_STACK_INLINE_HPP 1.29 +#define SHARE_VM_UTILITIES_STACK_INLINE_HPP 1.30 + 1.31 +#include "utilities/stack.hpp" 1.32 + 1.33 +template <MEMFLAGS F> StackBase<F>::StackBase(size_t segment_size, size_t max_cache_size, 1.34 + size_t max_size): 1.35 + _seg_size(segment_size), 1.36 + _max_cache_size(max_cache_size), 1.37 + _max_size(adjust_max_size(max_size, segment_size)) 1.38 +{ 1.39 + assert(_max_size % _seg_size == 0, "not a multiple"); 1.40 +} 1.41 + 1.42 +template <MEMFLAGS F> size_t StackBase<F>::adjust_max_size(size_t max_size, size_t seg_size) 1.43 +{ 1.44 + assert(seg_size > 0, "cannot be 0"); 1.45 + assert(max_size >= seg_size || max_size == 0, "max_size too small"); 1.46 + const size_t limit = max_uintx - (seg_size - 1); 1.47 + if (max_size == 0 || max_size > limit) { 1.48 + max_size = limit; 1.49 + } 1.50 + return (max_size + seg_size - 1) / seg_size * seg_size; 1.51 +} 1.52 + 1.53 +template <class E, MEMFLAGS F> 1.54 +Stack<E, F>::Stack(size_t segment_size, size_t max_cache_size, size_t max_size): 1.55 + StackBase<F>(adjust_segment_size(segment_size), max_cache_size, max_size) 1.56 +{ 1.57 + reset(true); 1.58 +} 1.59 + 1.60 +template <class E, MEMFLAGS F> 1.61 +void Stack<E, F>::push(E item) 1.62 +{ 1.63 + assert(!is_full(), "pushing onto a full stack"); 1.64 + if (this->_cur_seg_size == this->_seg_size) { 1.65 + push_segment(); 1.66 + } 1.67 + this->_cur_seg[this->_cur_seg_size] = item; 1.68 + ++this->_cur_seg_size; 1.69 +} 1.70 + 1.71 +template <class E, MEMFLAGS F> 1.72 +E Stack<E, F>::pop() 1.73 +{ 1.74 + assert(!is_empty(), "popping from an empty stack"); 1.75 + if (this->_cur_seg_size == 1) { 1.76 + E tmp = _cur_seg[--this->_cur_seg_size]; 1.77 + pop_segment(); 1.78 + return tmp; 1.79 + } 1.80 + return this->_cur_seg[--this->_cur_seg_size]; 1.81 +} 1.82 + 1.83 +template <class E, MEMFLAGS F> 1.84 +void Stack<E, F>::clear(bool clear_cache) 1.85 +{ 1.86 + free_segments(_cur_seg); 1.87 + if (clear_cache) free_segments(_cache); 1.88 + reset(clear_cache); 1.89 +} 1.90 + 1.91 +template <class E, MEMFLAGS F> 1.92 +size_t Stack<E, F>::default_segment_size() 1.93 +{ 1.94 + // Number of elements that fit in 4K bytes minus the size of two pointers 1.95 + // (link field and malloc header). 1.96 + return (4096 - 2 * sizeof(E*)) / sizeof(E); 1.97 +} 1.98 + 1.99 +template <class E, MEMFLAGS F> 1.100 +size_t Stack<E, F>::adjust_segment_size(size_t seg_size) 1.101 +{ 1.102 + const size_t elem_sz = sizeof(E); 1.103 + const size_t ptr_sz = sizeof(E*); 1.104 + assert(elem_sz % ptr_sz == 0 || ptr_sz % elem_sz == 0, "bad element size"); 1.105 + if (elem_sz < ptr_sz) { 1.106 + return align_size_up(seg_size * elem_sz, ptr_sz) / elem_sz; 1.107 + } 1.108 + return seg_size; 1.109 +} 1.110 + 1.111 +template <class E, MEMFLAGS F> 1.112 +size_t Stack<E, F>::link_offset() const 1.113 +{ 1.114 + return align_size_up(this->_seg_size * sizeof(E), sizeof(E*)); 1.115 +} 1.116 + 1.117 +template <class E, MEMFLAGS F> 1.118 +size_t Stack<E, F>::segment_bytes() const 1.119 +{ 1.120 + return link_offset() + sizeof(E*); 1.121 +} 1.122 + 1.123 +template <class E, MEMFLAGS F> 1.124 +E** Stack<E, F>::link_addr(E* seg) const 1.125 +{ 1.126 + return (E**) ((char*)seg + link_offset()); 1.127 +} 1.128 + 1.129 +template <class E, MEMFLAGS F> 1.130 +E* Stack<E, F>::get_link(E* seg) const 1.131 +{ 1.132 + return *link_addr(seg); 1.133 +} 1.134 + 1.135 +template <class E, MEMFLAGS F> 1.136 +E* Stack<E, F>::set_link(E* new_seg, E* old_seg) 1.137 +{ 1.138 + *link_addr(new_seg) = old_seg; 1.139 + return new_seg; 1.140 +} 1.141 + 1.142 +template <class E, MEMFLAGS F> 1.143 +E* Stack<E, F>::alloc(size_t bytes) 1.144 +{ 1.145 + return (E*) NEW_C_HEAP_ARRAY(char, bytes, F); 1.146 +} 1.147 + 1.148 +template <class E, MEMFLAGS F> 1.149 +void Stack<E, F>::free(E* addr, size_t bytes) 1.150 +{ 1.151 + FREE_C_HEAP_ARRAY(char, (char*) addr, F); 1.152 +} 1.153 + 1.154 +template <class E, MEMFLAGS F> 1.155 +void Stack<E, F>::push_segment() 1.156 +{ 1.157 + assert(this->_cur_seg_size == this->_seg_size, "current segment is not full"); 1.158 + E* next; 1.159 + if (this->_cache_size > 0) { 1.160 + // Use a cached segment. 1.161 + next = _cache; 1.162 + _cache = get_link(_cache); 1.163 + --this->_cache_size; 1.164 + } else { 1.165 + next = alloc(segment_bytes()); 1.166 + DEBUG_ONLY(zap_segment(next, true);) 1.167 + } 1.168 + const bool at_empty_transition = is_empty(); 1.169 + this->_cur_seg = set_link(next, _cur_seg); 1.170 + this->_cur_seg_size = 0; 1.171 + this->_full_seg_size += at_empty_transition ? 0 : this->_seg_size; 1.172 + DEBUG_ONLY(verify(at_empty_transition);) 1.173 +} 1.174 + 1.175 +template <class E, MEMFLAGS F> 1.176 +void Stack<E, F>::pop_segment() 1.177 +{ 1.178 + assert(this->_cur_seg_size == 0, "current segment is not empty"); 1.179 + E* const prev = get_link(_cur_seg); 1.180 + if (this->_cache_size < this->_max_cache_size) { 1.181 + // Add the current segment to the cache. 1.182 + DEBUG_ONLY(zap_segment(_cur_seg, false);) 1.183 + _cache = set_link(_cur_seg, _cache); 1.184 + ++this->_cache_size; 1.185 + } else { 1.186 + DEBUG_ONLY(zap_segment(_cur_seg, true);) 1.187 + free(_cur_seg, segment_bytes()); 1.188 + } 1.189 + const bool at_empty_transition = prev == NULL; 1.190 + this->_cur_seg = prev; 1.191 + this->_cur_seg_size = this->_seg_size; 1.192 + this->_full_seg_size -= at_empty_transition ? 0 : this->_seg_size; 1.193 + DEBUG_ONLY(verify(at_empty_transition);) 1.194 +} 1.195 + 1.196 +template <class E, MEMFLAGS F> 1.197 +void Stack<E, F>::free_segments(E* seg) 1.198 +{ 1.199 + const size_t bytes = segment_bytes(); 1.200 + while (seg != NULL) { 1.201 + E* const prev = get_link(seg); 1.202 + free(seg, bytes); 1.203 + seg = prev; 1.204 + } 1.205 +} 1.206 + 1.207 +template <class E, MEMFLAGS F> 1.208 +void Stack<E, F>::reset(bool reset_cache) 1.209 +{ 1.210 + this->_cur_seg_size = this->_seg_size; // So push() will alloc a new segment. 1.211 + this->_full_seg_size = 0; 1.212 + _cur_seg = NULL; 1.213 + if (reset_cache) { 1.214 + this->_cache_size = 0; 1.215 + _cache = NULL; 1.216 + } 1.217 +} 1.218 + 1.219 +#ifdef ASSERT 1.220 +template <class E, MEMFLAGS F> 1.221 +void Stack<E, F>::verify(bool at_empty_transition) const 1.222 +{ 1.223 + assert(size() <= this->max_size(), "stack exceeded bounds"); 1.224 + assert(this->cache_size() <= this->max_cache_size(), "cache exceeded bounds"); 1.225 + assert(this->_cur_seg_size <= this->segment_size(), "segment index exceeded bounds"); 1.226 + 1.227 + assert(this->_full_seg_size % this->_seg_size == 0, "not a multiple"); 1.228 + assert(at_empty_transition || is_empty() == (size() == 0), "mismatch"); 1.229 + assert((_cache == NULL) == (this->cache_size() == 0), "mismatch"); 1.230 + 1.231 + if (is_empty()) { 1.232 + assert(this->_cur_seg_size == this->segment_size(), "sanity"); 1.233 + } 1.234 +} 1.235 + 1.236 +template <class E, MEMFLAGS F> 1.237 +void Stack<E, F>::zap_segment(E* seg, bool zap_link_field) const 1.238 +{ 1.239 + if (!ZapStackSegments) return; 1.240 + const size_t zap_bytes = segment_bytes() - (zap_link_field ? 0 : sizeof(E*)); 1.241 + uint32_t* cur = (uint32_t*)seg; 1.242 + const uint32_t* end = cur + zap_bytes / sizeof(uint32_t); 1.243 + while (cur < end) { 1.244 + *cur++ = 0xfadfaded; 1.245 + } 1.246 +} 1.247 +#endif 1.248 + 1.249 +template <class E, MEMFLAGS F> 1.250 +E* ResourceStack<E, F>::alloc(size_t bytes) 1.251 +{ 1.252 + return (E*) resource_allocate_bytes(bytes); 1.253 +} 1.254 + 1.255 +template <class E, MEMFLAGS F> 1.256 +void ResourceStack<E, F>::free(E* addr, size_t bytes) 1.257 +{ 1.258 + resource_free_bytes((char*) addr, bytes); 1.259 +} 1.260 + 1.261 +template <class E, MEMFLAGS F> 1.262 +void StackIterator<E, F>::sync() 1.263 +{ 1.264 + _full_seg_size = _stack._full_seg_size; 1.265 + _cur_seg_size = _stack._cur_seg_size; 1.266 + _cur_seg = _stack._cur_seg; 1.267 +} 1.268 + 1.269 +template <class E, MEMFLAGS F> 1.270 +E* StackIterator<E, F>::next_addr() 1.271 +{ 1.272 + assert(!is_empty(), "no items left"); 1.273 + if (_cur_seg_size == 1) { 1.274 + E* addr = _cur_seg; 1.275 + _cur_seg = _stack.get_link(_cur_seg); 1.276 + _cur_seg_size = _stack.segment_size(); 1.277 + _full_seg_size -= _stack.segment_size(); 1.278 + return addr; 1.279 + } 1.280 + return _cur_seg + --_cur_seg_size; 1.281 +} 1.282 + 1.283 +#endif // SHARE_VM_UTILITIES_STACK_INLINE_HPP