stack.inline.hpp 6.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
/*
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
 * 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.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

StackBase::StackBase(size_t segment_size, size_t max_cache_size,
                     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");
}

size_t StackBase::adjust_max_size(size_t max_size, size_t seg_size)
{
  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;
}

template <class E>
Stack<E>::Stack(size_t segment_size, size_t max_cache_size, size_t max_size):
  StackBase(adjust_segment_size(segment_size), max_cache_size, max_size)
{
  reset(true);
}

template <class E>
void Stack<E>::push(E item)
{
  assert(!is_full(), "pushing onto a full stack");
  if (_cur_seg_size == _seg_size) {
    push_segment();
  }
  _cur_seg[_cur_seg_size] = item;
  ++_cur_seg_size;
}

template <class E>
E Stack<E>::pop()
{
  assert(!is_empty(), "popping from an empty stack");
  if (_cur_seg_size == 1) {
    E tmp = _cur_seg[--_cur_seg_size];
    pop_segment();
    return tmp;
  }
  return _cur_seg[--_cur_seg_size];
}

template <class E>
void Stack<E>::clear(bool clear_cache)
{
  free_segments(_cur_seg);
  if (clear_cache) free_segments(_cache);
  reset(clear_cache);
}

template <class E>
size_t Stack<E>::default_segment_size()
{
  // 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);
}

template <class E>
size_t Stack<E>::adjust_segment_size(size_t seg_size)
{
  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;
}

template <class E>
size_t Stack<E>::link_offset() const
{
  return align_size_up(_seg_size * sizeof(E), sizeof(E*));
}

template <class E>
size_t Stack<E>::segment_bytes() const
{
  return link_offset() + sizeof(E*);
}

template <class E>
E** Stack<E>::link_addr(E* seg) const
{
  return (E**) ((char*)seg + link_offset());
}

template <class E>
E* Stack<E>::get_link(E* seg) const
{
  return *link_addr(seg);
}

template <class E>
E* Stack<E>::set_link(E* new_seg, E* old_seg)
{
  *link_addr(new_seg) = old_seg;
  return new_seg;
}

template <class E>
E* Stack<E>::alloc(size_t bytes)
{
  return (E*) NEW_C_HEAP_ARRAY(char, bytes);
}

template <class E>
void Stack<E>::free(E* addr, size_t bytes)
{
  FREE_C_HEAP_ARRAY(char, (char*) addr);
}

template <class E>
void Stack<E>::push_segment()
{
  assert(_cur_seg_size == _seg_size, "current segment is not full");
  E* next;
  if (_cache_size > 0) {
    // Use a cached segment.
    next = _cache;
    _cache = get_link(_cache);
    --_cache_size;
  } else {
    next = alloc(segment_bytes());
    DEBUG_ONLY(zap_segment(next, true);)
  }
  const bool at_empty_transition = is_empty();
  _cur_seg = set_link(next, _cur_seg);
  _cur_seg_size = 0;
  _full_seg_size += at_empty_transition ? 0 : _seg_size;
  DEBUG_ONLY(verify(at_empty_transition);)
}

template <class E>
void Stack<E>::pop_segment()
{
  assert(_cur_seg_size == 0, "current segment is not empty");
  E* const prev = get_link(_cur_seg);
  if (_cache_size < _max_cache_size) {
    // Add the current segment to the cache.
    DEBUG_ONLY(zap_segment(_cur_seg, false);)
    _cache = set_link(_cur_seg, _cache);
    ++_cache_size;
  } else {
    DEBUG_ONLY(zap_segment(_cur_seg, true);)
    free(_cur_seg, segment_bytes());
  }
  const bool at_empty_transition = prev == NULL;
  _cur_seg = prev;
  _cur_seg_size = _seg_size;
  _full_seg_size -= at_empty_transition ? 0 : _seg_size;
  DEBUG_ONLY(verify(at_empty_transition);)
}

template <class E>
void Stack<E>::free_segments(E* seg)
{
  const size_t bytes = segment_bytes();
  while (seg != NULL) {
    E* const prev = get_link(seg);
    free(seg, bytes);
    seg = prev;
  }
}

template <class E>
void Stack<E>::reset(bool reset_cache)
{
  _cur_seg_size = _seg_size; // So push() will alloc a new segment.
  _full_seg_size = 0;
  _cur_seg = NULL;
  if (reset_cache) {
    _cache_size = 0;
    _cache = NULL;
  }
}

#ifdef ASSERT
template <class E>
void Stack<E>::verify(bool at_empty_transition) const
{
  assert(size() <= max_size(), "stack exceeded bounds");
  assert(cache_size() <= max_cache_size(), "cache exceeded bounds");
  assert(_cur_seg_size <= segment_size(), "segment index exceeded bounds");

  assert(_full_seg_size % _seg_size == 0, "not a multiple");
  assert(at_empty_transition || is_empty() == (size() == 0), "mismatch");
  assert((_cache == NULL) == (cache_size() == 0), "mismatch");

  if (is_empty()) {
    assert(_cur_seg_size == segment_size(), "sanity");
  }
}

template <class E>
void Stack<E>::zap_segment(E* seg, bool zap_link_field) const
{
  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

template <class E>
E* ResourceStack<E>::alloc(size_t bytes)
{
  return (E*) resource_allocate_bytes(bytes);
}

template <class E>
void ResourceStack<E>::free(E* addr, size_t bytes)
{
  resource_free_bytes((char*) addr, bytes);
}

template <class E>
void StackIterator<E>::sync()
{
  _full_seg_size = _stack._full_seg_size;
  _cur_seg_size = _stack._cur_seg_size;
  _cur_seg = _stack._cur_seg;
}

template <class E>
E* StackIterator<E>::next_addr()
{
  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;
}