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

Tue, 18 Jul 2017 09:53:54 +0200

author
shade
date
Tue, 18 Jul 2017 09:53:54 +0200
changeset 9997
c7ef664f8649
parent 9316
a27880c1288b
child 10015
eb7ce841ccec
permissions
-rw-r--r--

8184762: ZapStackSegments should use optimized memset
Reviewed-by: rkennke, mgerdin

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

mercurial