stack.inline.hpp 7.7 KB
Newer Older
1
/*
2
 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
22 23 24
 *
 */

25 26 27 28 29
#ifndef SHARE_VM_UTILITIES_STACK_INLINE_HPP
#define SHARE_VM_UTILITIES_STACK_INLINE_HPP

#include "utilities/stack.hpp"

Z
zgu 已提交
30
template <MEMFLAGS F> StackBase<F>::StackBase(size_t segment_size, size_t max_cache_size,
31 32 33 34 35 36 37 38
                     size_t max_size):
  _seg_size(segment_size),
  _max_cache_size(max_cache_size),
  _max_size(adjust_max_size(max_size, segment_size))
{
  assert(_max_size % _seg_size == 0, "not a multiple");
}

Z
zgu 已提交
39
template <MEMFLAGS F> size_t StackBase<F>::adjust_max_size(size_t max_size, size_t seg_size)
40 41 42 43 44 45 46 47 48 49
{
  assert(seg_size > 0, "cannot be 0");
  assert(max_size >= seg_size || max_size == 0, "max_size too small");
  const size_t limit = max_uintx - (seg_size - 1);
  if (max_size == 0 || max_size > limit) {
    max_size = limit;
  }
  return (max_size + seg_size - 1) / seg_size * seg_size;
}

Z
zgu 已提交
50 51 52
template <class E, MEMFLAGS F>
Stack<E, F>::Stack(size_t segment_size, size_t max_cache_size, size_t max_size):
  StackBase<F>(adjust_segment_size(segment_size), max_cache_size, max_size)
53 54 55 56
{
  reset(true);
}

Z
zgu 已提交
57 58
template <class E, MEMFLAGS F>
void Stack<E, F>::push(E item)
59 60
{
  assert(!is_full(), "pushing onto a full stack");
Z
zgu 已提交
61
  if (this->_cur_seg_size == this->_seg_size) {
62 63
    push_segment();
  }
Z
zgu 已提交
64 65
  this->_cur_seg[this->_cur_seg_size] = item;
  ++this->_cur_seg_size;
66 67
}

Z
zgu 已提交
68 69
template <class E, MEMFLAGS F>
E Stack<E, F>::pop()
70 71
{
  assert(!is_empty(), "popping from an empty stack");
Z
zgu 已提交
72 73
  if (this->_cur_seg_size == 1) {
    E tmp = _cur_seg[--this->_cur_seg_size];
74 75 76
    pop_segment();
    return tmp;
  }
Z
zgu 已提交
77
  return this->_cur_seg[--this->_cur_seg_size];
78 79
}

Z
zgu 已提交
80 81
template <class E, MEMFLAGS F>
void Stack<E, F>::clear(bool clear_cache)
82 83 84 85 86 87
{
  free_segments(_cur_seg);
  if (clear_cache) free_segments(_cache);
  reset(clear_cache);
}

Z
zgu 已提交
88 89
template <class E, MEMFLAGS F>
size_t Stack<E, F>::default_segment_size()
90 91 92 93 94 95
{
  // Number of elements that fit in 4K bytes minus the size of two pointers
  // (link field and malloc header).
  return (4096 - 2 * sizeof(E*)) / sizeof(E);
}

Z
zgu 已提交
96 97
template <class E, MEMFLAGS F>
size_t Stack<E, F>::adjust_segment_size(size_t seg_size)
98 99 100 101 102 103 104 105 106 107
{
  const size_t elem_sz = sizeof(E);
  const size_t ptr_sz = sizeof(E*);
  assert(elem_sz % ptr_sz == 0 || ptr_sz % elem_sz == 0, "bad element size");
  if (elem_sz < ptr_sz) {
    return align_size_up(seg_size * elem_sz, ptr_sz) / elem_sz;
  }
  return seg_size;
}

Z
zgu 已提交
108 109
template <class E, MEMFLAGS F>
size_t Stack<E, F>::link_offset() const
110
{
Z
zgu 已提交
111
  return align_size_up(this->_seg_size * sizeof(E), sizeof(E*));
112 113
}

Z
zgu 已提交
114 115
template <class E, MEMFLAGS F>
size_t Stack<E, F>::segment_bytes() const
116 117 118 119
{
  return link_offset() + sizeof(E*);
}

Z
zgu 已提交
120 121
template <class E, MEMFLAGS F>
E** Stack<E, F>::link_addr(E* seg) const
122 123 124 125
{
  return (E**) ((char*)seg + link_offset());
}

Z
zgu 已提交
126 127
template <class E, MEMFLAGS F>
E* Stack<E, F>::get_link(E* seg) const
128 129 130 131
{
  return *link_addr(seg);
}

Z
zgu 已提交
132 133
template <class E, MEMFLAGS F>
E* Stack<E, F>::set_link(E* new_seg, E* old_seg)
134 135 136 137 138
{
  *link_addr(new_seg) = old_seg;
  return new_seg;
}

Z
zgu 已提交
139 140
template <class E, MEMFLAGS F>
E* Stack<E, F>::alloc(size_t bytes)
141
{
Z
zgu 已提交
142
  return (E*) NEW_C_HEAP_ARRAY(char, bytes, F);
143 144
}

Z
zgu 已提交
145 146
template <class E, MEMFLAGS F>
void Stack<E, F>::free(E* addr, size_t bytes)
147
{
Z
zgu 已提交
148
  FREE_C_HEAP_ARRAY(char, (char*) addr, F);
149 150
}

Z
zgu 已提交
151 152
template <class E, MEMFLAGS F>
void Stack<E, F>::push_segment()
153
{
Z
zgu 已提交
154
  assert(this->_cur_seg_size == this->_seg_size, "current segment is not full");
155
  E* next;
Z
zgu 已提交
156
  if (this->_cache_size > 0) {
157 158 159
    // Use a cached segment.
    next = _cache;
    _cache = get_link(_cache);
Z
zgu 已提交
160
    --this->_cache_size;
161 162 163 164 165
  } else {
    next = alloc(segment_bytes());
    DEBUG_ONLY(zap_segment(next, true);)
  }
  const bool at_empty_transition = is_empty();
Z
zgu 已提交
166 167 168
  this->_cur_seg = set_link(next, _cur_seg);
  this->_cur_seg_size = 0;
  this->_full_seg_size += at_empty_transition ? 0 : this->_seg_size;
169 170 171
  DEBUG_ONLY(verify(at_empty_transition);)
}

Z
zgu 已提交
172 173
template <class E, MEMFLAGS F>
void Stack<E, F>::pop_segment()
174
{
Z
zgu 已提交
175
  assert(this->_cur_seg_size == 0, "current segment is not empty");
176
  E* const prev = get_link(_cur_seg);
Z
zgu 已提交
177
  if (this->_cache_size < this->_max_cache_size) {
178 179 180
    // Add the current segment to the cache.
    DEBUG_ONLY(zap_segment(_cur_seg, false);)
    _cache = set_link(_cur_seg, _cache);
Z
zgu 已提交
181
    ++this->_cache_size;
182 183 184 185 186
  } else {
    DEBUG_ONLY(zap_segment(_cur_seg, true);)
    free(_cur_seg, segment_bytes());
  }
  const bool at_empty_transition = prev == NULL;
Z
zgu 已提交
187 188 189
  this->_cur_seg = prev;
  this->_cur_seg_size = this->_seg_size;
  this->_full_seg_size -= at_empty_transition ? 0 : this->_seg_size;
190 191 192
  DEBUG_ONLY(verify(at_empty_transition);)
}

Z
zgu 已提交
193 194
template <class E, MEMFLAGS F>
void Stack<E, F>::free_segments(E* seg)
195 196 197 198 199 200 201 202 203
{
  const size_t bytes = segment_bytes();
  while (seg != NULL) {
    E* const prev = get_link(seg);
    free(seg, bytes);
    seg = prev;
  }
}

Z
zgu 已提交
204 205
template <class E, MEMFLAGS F>
void Stack<E, F>::reset(bool reset_cache)
206
{
Z
zgu 已提交
207 208
  this->_cur_seg_size = this->_seg_size; // So push() will alloc a new segment.
  this->_full_seg_size = 0;
209 210
  _cur_seg = NULL;
  if (reset_cache) {
Z
zgu 已提交
211
    this->_cache_size = 0;
212 213 214 215 216
    _cache = NULL;
  }
}

#ifdef ASSERT
Z
zgu 已提交
217 218
template <class E, MEMFLAGS F>
void Stack<E, F>::verify(bool at_empty_transition) const
219
{
Z
zgu 已提交
220 221 222
  assert(size() <= this->max_size(), "stack exceeded bounds");
  assert(this->cache_size() <= this->max_cache_size(), "cache exceeded bounds");
  assert(this->_cur_seg_size <= this->segment_size(), "segment index exceeded bounds");
223

Z
zgu 已提交
224
  assert(this->_full_seg_size % this->_seg_size == 0, "not a multiple");
225
  assert(at_empty_transition || is_empty() == (size() == 0), "mismatch");
Z
zgu 已提交
226
  assert((_cache == NULL) == (this->cache_size() == 0), "mismatch");
227 228

  if (is_empty()) {
Z
zgu 已提交
229
    assert(this->_cur_seg_size == this->segment_size(), "sanity");
230 231 232
  }
}

Z
zgu 已提交
233 234
template <class E, MEMFLAGS F>
void Stack<E, F>::zap_segment(E* seg, bool zap_link_field) const
235 236 237 238 239 240 241 242 243 244 245
{
  if (!ZapStackSegments) return;
  const size_t zap_bytes = segment_bytes() - (zap_link_field ? 0 : sizeof(E*));
  uint32_t* cur = (uint32_t*)seg;
  const uint32_t* end = cur + zap_bytes / sizeof(uint32_t);
  while (cur < end) {
    *cur++ = 0xfadfaded;
  }
}
#endif

Z
zgu 已提交
246 247
template <class E, MEMFLAGS F>
E* ResourceStack<E, F>::alloc(size_t bytes)
248 249 250 251
{
  return (E*) resource_allocate_bytes(bytes);
}

Z
zgu 已提交
252 253
template <class E, MEMFLAGS F>
void ResourceStack<E, F>::free(E* addr, size_t bytes)
254 255 256 257
{
  resource_free_bytes((char*) addr, bytes);
}

Z
zgu 已提交
258 259
template <class E, MEMFLAGS F>
void StackIterator<E, F>::sync()
260 261 262 263 264 265
{
  _full_seg_size = _stack._full_seg_size;
  _cur_seg_size = _stack._cur_seg_size;
  _cur_seg = _stack._cur_seg;
}

Z
zgu 已提交
266 267
template <class E, MEMFLAGS F>
E* StackIterator<E, F>::next_addr()
268 269 270 271 272 273 274 275 276 277 278
{
  assert(!is_empty(), "no items left");
  if (_cur_seg_size == 1) {
    E* addr = _cur_seg;
    _cur_seg = _stack.get_link(_cur_seg);
    _cur_seg_size = _stack.segment_size();
    _full_seg_size -= _stack.segment_size();
    return addr;
  }
  return _cur_seg + --_cur_seg_size;
}
279 280

#endif // SHARE_VM_UTILITIES_STACK_INLINE_HPP