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

Sat, 18 May 2013 20:41:01 -0700

author
iklam
date
Sat, 18 May 2013 20:41:01 -0700
changeset 5144
a5d6f0c3585f
parent 4153
b9a9ed0f8eeb
child 6876
710a3c8b516e
child 9316
a27880c1288b
permissions
-rw-r--r--

8014262: PrintStringTableStatistics should include more footprint info
Summary: Added info for the string/symbol objects and the hash entries
Reviewed-by: coleenp, rbackman

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>::default_segment_size()
jcoomes@2191 90 {
jcoomes@2191 91 // Number of elements that fit in 4K bytes minus the size of two pointers
jcoomes@2191 92 // (link field and malloc header).
jcoomes@2191 93 return (4096 - 2 * sizeof(E*)) / sizeof(E);
jcoomes@2191 94 }
jcoomes@2191 95
zgu@3900 96 template <class E, MEMFLAGS F>
zgu@3900 97 size_t Stack<E, F>::adjust_segment_size(size_t seg_size)
jcoomes@2191 98 {
jcoomes@2191 99 const size_t elem_sz = sizeof(E);
jcoomes@2191 100 const size_t ptr_sz = sizeof(E*);
jcoomes@2191 101 assert(elem_sz % ptr_sz == 0 || ptr_sz % elem_sz == 0, "bad element size");
jcoomes@2191 102 if (elem_sz < ptr_sz) {
jcoomes@2191 103 return align_size_up(seg_size * elem_sz, ptr_sz) / elem_sz;
jcoomes@2191 104 }
jcoomes@2191 105 return seg_size;
jcoomes@2191 106 }
jcoomes@2191 107
zgu@3900 108 template <class E, MEMFLAGS F>
zgu@3900 109 size_t Stack<E, F>::link_offset() const
jcoomes@2191 110 {
zgu@3900 111 return align_size_up(this->_seg_size * sizeof(E), sizeof(E*));
jcoomes@2191 112 }
jcoomes@2191 113
zgu@3900 114 template <class E, MEMFLAGS F>
zgu@3900 115 size_t Stack<E, F>::segment_bytes() const
jcoomes@2191 116 {
jcoomes@2191 117 return link_offset() + sizeof(E*);
jcoomes@2191 118 }
jcoomes@2191 119
zgu@3900 120 template <class E, MEMFLAGS F>
zgu@3900 121 E** Stack<E, F>::link_addr(E* seg) const
jcoomes@2191 122 {
jcoomes@2191 123 return (E**) ((char*)seg + link_offset());
jcoomes@2191 124 }
jcoomes@2191 125
zgu@3900 126 template <class E, MEMFLAGS F>
zgu@3900 127 E* Stack<E, F>::get_link(E* seg) const
jcoomes@2191 128 {
jcoomes@2191 129 return *link_addr(seg);
jcoomes@2191 130 }
jcoomes@2191 131
zgu@3900 132 template <class E, MEMFLAGS F>
zgu@3900 133 E* Stack<E, F>::set_link(E* new_seg, E* old_seg)
jcoomes@2191 134 {
jcoomes@2191 135 *link_addr(new_seg) = old_seg;
jcoomes@2191 136 return new_seg;
jcoomes@2191 137 }
jcoomes@2191 138
zgu@3900 139 template <class E, MEMFLAGS F>
zgu@3900 140 E* Stack<E, F>::alloc(size_t bytes)
jcoomes@2191 141 {
zgu@3900 142 return (E*) NEW_C_HEAP_ARRAY(char, bytes, F);
jcoomes@2191 143 }
jcoomes@2191 144
zgu@3900 145 template <class E, MEMFLAGS F>
zgu@3900 146 void Stack<E, F>::free(E* addr, size_t bytes)
jcoomes@2191 147 {
zgu@3900 148 FREE_C_HEAP_ARRAY(char, (char*) addr, F);
jcoomes@2191 149 }
jcoomes@2191 150
zgu@3900 151 template <class E, MEMFLAGS F>
zgu@3900 152 void Stack<E, F>::push_segment()
jcoomes@2191 153 {
zgu@3900 154 assert(this->_cur_seg_size == this->_seg_size, "current segment is not full");
jcoomes@2191 155 E* next;
zgu@3900 156 if (this->_cache_size > 0) {
jcoomes@2191 157 // Use a cached segment.
jcoomes@2191 158 next = _cache;
jcoomes@2191 159 _cache = get_link(_cache);
zgu@3900 160 --this->_cache_size;
jcoomes@2191 161 } else {
jcoomes@2191 162 next = alloc(segment_bytes());
jcoomes@2191 163 DEBUG_ONLY(zap_segment(next, true);)
jcoomes@2191 164 }
jcoomes@2191 165 const bool at_empty_transition = is_empty();
zgu@3900 166 this->_cur_seg = set_link(next, _cur_seg);
zgu@3900 167 this->_cur_seg_size = 0;
zgu@3900 168 this->_full_seg_size += at_empty_transition ? 0 : this->_seg_size;
jcoomes@2191 169 DEBUG_ONLY(verify(at_empty_transition);)
jcoomes@2191 170 }
jcoomes@2191 171
zgu@3900 172 template <class E, MEMFLAGS F>
zgu@3900 173 void Stack<E, F>::pop_segment()
jcoomes@2191 174 {
zgu@3900 175 assert(this->_cur_seg_size == 0, "current segment is not empty");
jcoomes@2191 176 E* const prev = get_link(_cur_seg);
zgu@3900 177 if (this->_cache_size < this->_max_cache_size) {
jcoomes@2191 178 // Add the current segment to the cache.
jcoomes@2191 179 DEBUG_ONLY(zap_segment(_cur_seg, false);)
jcoomes@2191 180 _cache = set_link(_cur_seg, _cache);
zgu@3900 181 ++this->_cache_size;
jcoomes@2191 182 } else {
jcoomes@2191 183 DEBUG_ONLY(zap_segment(_cur_seg, true);)
jcoomes@2191 184 free(_cur_seg, segment_bytes());
jcoomes@2191 185 }
jcoomes@2191 186 const bool at_empty_transition = prev == NULL;
zgu@3900 187 this->_cur_seg = prev;
zgu@3900 188 this->_cur_seg_size = this->_seg_size;
zgu@3900 189 this->_full_seg_size -= at_empty_transition ? 0 : this->_seg_size;
jcoomes@2191 190 DEBUG_ONLY(verify(at_empty_transition);)
jcoomes@2191 191 }
jcoomes@2191 192
zgu@3900 193 template <class E, MEMFLAGS F>
zgu@3900 194 void Stack<E, F>::free_segments(E* seg)
jcoomes@2191 195 {
jcoomes@2191 196 const size_t bytes = segment_bytes();
jcoomes@2191 197 while (seg != NULL) {
jcoomes@2191 198 E* const prev = get_link(seg);
jcoomes@2191 199 free(seg, bytes);
jcoomes@2191 200 seg = prev;
jcoomes@2191 201 }
jcoomes@2191 202 }
jcoomes@2191 203
zgu@3900 204 template <class E, MEMFLAGS F>
zgu@3900 205 void Stack<E, F>::reset(bool reset_cache)
jcoomes@2191 206 {
zgu@3900 207 this->_cur_seg_size = this->_seg_size; // So push() will alloc a new segment.
zgu@3900 208 this->_full_seg_size = 0;
jcoomes@2191 209 _cur_seg = NULL;
jcoomes@2191 210 if (reset_cache) {
zgu@3900 211 this->_cache_size = 0;
jcoomes@2191 212 _cache = NULL;
jcoomes@2191 213 }
jcoomes@2191 214 }
jcoomes@2191 215
jcoomes@2191 216 #ifdef ASSERT
zgu@3900 217 template <class E, MEMFLAGS F>
zgu@3900 218 void Stack<E, F>::verify(bool at_empty_transition) const
jcoomes@2191 219 {
zgu@3900 220 assert(size() <= this->max_size(), "stack exceeded bounds");
zgu@3900 221 assert(this->cache_size() <= this->max_cache_size(), "cache exceeded bounds");
zgu@3900 222 assert(this->_cur_seg_size <= this->segment_size(), "segment index exceeded bounds");
jcoomes@2191 223
zgu@3900 224 assert(this->_full_seg_size % this->_seg_size == 0, "not a multiple");
jcoomes@2191 225 assert(at_empty_transition || is_empty() == (size() == 0), "mismatch");
zgu@3900 226 assert((_cache == NULL) == (this->cache_size() == 0), "mismatch");
jcoomes@2191 227
jcoomes@2191 228 if (is_empty()) {
zgu@3900 229 assert(this->_cur_seg_size == this->segment_size(), "sanity");
jcoomes@2191 230 }
jcoomes@2191 231 }
jcoomes@2191 232
zgu@3900 233 template <class E, MEMFLAGS F>
zgu@3900 234 void Stack<E, F>::zap_segment(E* seg, bool zap_link_field) const
jcoomes@2191 235 {
jcoomes@2191 236 if (!ZapStackSegments) return;
jcoomes@2191 237 const size_t zap_bytes = segment_bytes() - (zap_link_field ? 0 : sizeof(E*));
jcoomes@2191 238 uint32_t* cur = (uint32_t*)seg;
jcoomes@2191 239 const uint32_t* end = cur + zap_bytes / sizeof(uint32_t);
jcoomes@2191 240 while (cur < end) {
jcoomes@2191 241 *cur++ = 0xfadfaded;
jcoomes@2191 242 }
jcoomes@2191 243 }
jcoomes@2191 244 #endif
jcoomes@2191 245
zgu@3900 246 template <class E, MEMFLAGS F>
zgu@3900 247 E* ResourceStack<E, F>::alloc(size_t bytes)
jcoomes@2191 248 {
jcoomes@2191 249 return (E*) resource_allocate_bytes(bytes);
jcoomes@2191 250 }
jcoomes@2191 251
zgu@3900 252 template <class E, MEMFLAGS F>
zgu@3900 253 void ResourceStack<E, F>::free(E* addr, size_t bytes)
jcoomes@2191 254 {
jcoomes@2191 255 resource_free_bytes((char*) addr, bytes);
jcoomes@2191 256 }
jcoomes@2191 257
zgu@3900 258 template <class E, MEMFLAGS F>
zgu@3900 259 void StackIterator<E, F>::sync()
jcoomes@2191 260 {
jcoomes@2191 261 _full_seg_size = _stack._full_seg_size;
jcoomes@2191 262 _cur_seg_size = _stack._cur_seg_size;
jcoomes@2191 263 _cur_seg = _stack._cur_seg;
jcoomes@2191 264 }
jcoomes@2191 265
zgu@3900 266 template <class E, MEMFLAGS F>
zgu@3900 267 E* StackIterator<E, F>::next_addr()
jcoomes@2191 268 {
jcoomes@2191 269 assert(!is_empty(), "no items left");
jcoomes@2191 270 if (_cur_seg_size == 1) {
jcoomes@2191 271 E* addr = _cur_seg;
jcoomes@2191 272 _cur_seg = _stack.get_link(_cur_seg);
jcoomes@2191 273 _cur_seg_size = _stack.segment_size();
jcoomes@2191 274 _full_seg_size -= _stack.segment_size();
jcoomes@2191 275 return addr;
jcoomes@2191 276 }
jcoomes@2191 277 return _cur_seg + --_cur_seg_size;
jcoomes@2191 278 }
stefank@2314 279
stefank@2314 280 #endif // SHARE_VM_UTILITIES_STACK_INLINE_HPP

mercurial