hotspot/src/share/vm/utilities/stack.inline.hpp
author jcoomes
Tue, 28 Sep 2010 15:56:15 -0700
changeset 6762 f8d1b560700e
child 7397 5b173b4ca846
permissions -rw-r--r--
6423256: GC stacks should use a better data structure 6942771: SEGV in ParScanThreadState::take_from_overflow_stack Reviewed-by: apetrusenko, ysr, pbk
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6762
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     1
/*
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     2
 * Copyright 2009 Sun Microsystems, Inc.  All Rights Reserved.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     4
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     8
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    13
 * accompanied this code).
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    14
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    18
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    21
 * have any questions.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    22
 *
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    23
 */
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    24
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    25
StackBase::StackBase(size_t segment_size, size_t max_cache_size,
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    26
                     size_t max_size):
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    27
  _seg_size(segment_size),
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    28
  _max_cache_size(max_cache_size),
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    29
  _max_size(adjust_max_size(max_size, segment_size))
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    30
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    31
  assert(_max_size % _seg_size == 0, "not a multiple");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    32
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    33
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    34
size_t StackBase::adjust_max_size(size_t max_size, size_t seg_size)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    35
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    36
  assert(seg_size > 0, "cannot be 0");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    37
  assert(max_size >= seg_size || max_size == 0, "max_size too small");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    38
  const size_t limit = max_uintx - (seg_size - 1);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    39
  if (max_size == 0 || max_size > limit) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    40
    max_size = limit;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    41
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    42
  return (max_size + seg_size - 1) / seg_size * seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    43
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    44
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    45
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    46
Stack<E>::Stack(size_t segment_size, size_t max_cache_size, size_t max_size):
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    47
  StackBase(adjust_segment_size(segment_size), max_cache_size, max_size)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    48
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    49
  reset(true);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    50
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    51
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    52
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    53
void Stack<E>::push(E item)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    54
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    55
  assert(!is_full(), "pushing onto a full stack");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    56
  if (_cur_seg_size == _seg_size) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    57
    push_segment();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    58
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    59
  _cur_seg[_cur_seg_size] = item;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    60
  ++_cur_seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    61
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    62
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    63
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    64
E Stack<E>::pop()
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    65
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    66
  assert(!is_empty(), "popping from an empty stack");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    67
  if (_cur_seg_size == 1) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    68
    E tmp = _cur_seg[--_cur_seg_size];
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    69
    pop_segment();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    70
    return tmp;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    71
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    72
  return _cur_seg[--_cur_seg_size];
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    73
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    74
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    75
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    76
void Stack<E>::clear(bool clear_cache)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    77
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    78
  free_segments(_cur_seg);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    79
  if (clear_cache) free_segments(_cache);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    80
  reset(clear_cache);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    81
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    82
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    83
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    84
size_t Stack<E>::default_segment_size()
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    85
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    86
  // Number of elements that fit in 4K bytes minus the size of two pointers
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    87
  // (link field and malloc header).
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    88
  return (4096 - 2 * sizeof(E*)) / sizeof(E);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    89
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    90
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    91
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    92
size_t Stack<E>::adjust_segment_size(size_t seg_size)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    93
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    94
  const size_t elem_sz = sizeof(E);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    95
  const size_t ptr_sz = sizeof(E*);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    96
  assert(elem_sz % ptr_sz == 0 || ptr_sz % elem_sz == 0, "bad element size");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    97
  if (elem_sz < ptr_sz) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    98
    return align_size_up(seg_size * elem_sz, ptr_sz) / elem_sz;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
    99
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   100
  return seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   101
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   102
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   103
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   104
size_t Stack<E>::link_offset() const
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   105
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   106
  return align_size_up(_seg_size * sizeof(E), sizeof(E*));
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   107
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   108
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   109
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   110
size_t Stack<E>::segment_bytes() const
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   111
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   112
  return link_offset() + sizeof(E*);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   113
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   114
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   115
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   116
E** Stack<E>::link_addr(E* seg) const
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   117
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   118
  return (E**) ((char*)seg + link_offset());
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   119
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   120
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   121
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   122
E* Stack<E>::get_link(E* seg) const
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   123
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   124
  return *link_addr(seg);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   125
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   126
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   127
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   128
E* Stack<E>::set_link(E* new_seg, E* old_seg)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   129
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   130
  *link_addr(new_seg) = old_seg;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   131
  return new_seg;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   132
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   133
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   134
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   135
E* Stack<E>::alloc(size_t bytes)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   136
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   137
  return (E*) NEW_C_HEAP_ARRAY(char, bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   138
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   139
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   140
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   141
void Stack<E>::free(E* addr, size_t bytes)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   142
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   143
  FREE_C_HEAP_ARRAY(char, (char*) addr);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   144
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   145
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   146
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   147
void Stack<E>::push_segment()
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   148
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   149
  assert(_cur_seg_size == _seg_size, "current segment is not full");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   150
  E* next;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   151
  if (_cache_size > 0) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   152
    // Use a cached segment.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   153
    next = _cache;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   154
    _cache = get_link(_cache);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   155
    --_cache_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   156
  } else {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   157
    next = alloc(segment_bytes());
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   158
    DEBUG_ONLY(zap_segment(next, true);)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   159
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   160
  const bool at_empty_transition = is_empty();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   161
  _cur_seg = set_link(next, _cur_seg);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   162
  _cur_seg_size = 0;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   163
  _full_seg_size += at_empty_transition ? 0 : _seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   164
  DEBUG_ONLY(verify(at_empty_transition);)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   165
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   166
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   167
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   168
void Stack<E>::pop_segment()
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   169
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   170
  assert(_cur_seg_size == 0, "current segment is not empty");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   171
  E* const prev = get_link(_cur_seg);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   172
  if (_cache_size < _max_cache_size) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   173
    // Add the current segment to the cache.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   174
    DEBUG_ONLY(zap_segment(_cur_seg, false);)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   175
    _cache = set_link(_cur_seg, _cache);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   176
    ++_cache_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   177
  } else {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   178
    DEBUG_ONLY(zap_segment(_cur_seg, true);)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   179
    free(_cur_seg, segment_bytes());
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   180
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   181
  const bool at_empty_transition = prev == NULL;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   182
  _cur_seg = prev;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   183
  _cur_seg_size = _seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   184
  _full_seg_size -= at_empty_transition ? 0 : _seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   185
  DEBUG_ONLY(verify(at_empty_transition);)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   186
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   187
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   188
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   189
void Stack<E>::free_segments(E* seg)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   190
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   191
  const size_t bytes = segment_bytes();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   192
  while (seg != NULL) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   193
    E* const prev = get_link(seg);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   194
    free(seg, bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   195
    seg = prev;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   196
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   197
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   198
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   199
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   200
void Stack<E>::reset(bool reset_cache)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   201
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   202
  _cur_seg_size = _seg_size; // So push() will alloc a new segment.
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   203
  _full_seg_size = 0;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   204
  _cur_seg = NULL;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   205
  if (reset_cache) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   206
    _cache_size = 0;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   207
    _cache = NULL;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   208
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   209
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   210
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   211
#ifdef ASSERT
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   212
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   213
void Stack<E>::verify(bool at_empty_transition) const
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   214
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   215
  assert(size() <= max_size(), "stack exceeded bounds");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   216
  assert(cache_size() <= max_cache_size(), "cache exceeded bounds");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   217
  assert(_cur_seg_size <= segment_size(), "segment index exceeded bounds");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   218
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   219
  assert(_full_seg_size % _seg_size == 0, "not a multiple");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   220
  assert(at_empty_transition || is_empty() == (size() == 0), "mismatch");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   221
  assert((_cache == NULL) == (cache_size() == 0), "mismatch");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   222
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   223
  if (is_empty()) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   224
    assert(_cur_seg_size == segment_size(), "sanity");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   225
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   226
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   227
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   228
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   229
void Stack<E>::zap_segment(E* seg, bool zap_link_field) const
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   230
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   231
  if (!ZapStackSegments) return;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   232
  const size_t zap_bytes = segment_bytes() - (zap_link_field ? 0 : sizeof(E*));
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   233
  uint32_t* cur = (uint32_t*)seg;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   234
  const uint32_t* end = cur + zap_bytes / sizeof(uint32_t);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   235
  while (cur < end) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   236
    *cur++ = 0xfadfaded;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   237
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   238
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   239
#endif
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   240
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   241
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   242
E* ResourceStack<E>::alloc(size_t bytes)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   243
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   244
  return (E*) resource_allocate_bytes(bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   245
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   246
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   247
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   248
void ResourceStack<E>::free(E* addr, size_t bytes)
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   249
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   250
  resource_free_bytes((char*) addr, bytes);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   251
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   252
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   253
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   254
void StackIterator<E>::sync()
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   255
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   256
  _full_seg_size = _stack._full_seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   257
  _cur_seg_size = _stack._cur_seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   258
  _cur_seg = _stack._cur_seg;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   259
}
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   260
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   261
template <class E>
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   262
E* StackIterator<E>::next_addr()
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   263
{
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   264
  assert(!is_empty(), "no items left");
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   265
  if (_cur_seg_size == 1) {
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   266
    E* addr = _cur_seg;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   267
    _cur_seg = _stack.get_link(_cur_seg);
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   268
    _cur_seg_size = _stack.segment_size();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   269
    _full_seg_size -= _stack.segment_size();
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   270
    return addr;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   271
  }
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   272
  return _cur_seg + --_cur_seg_size;
f8d1b560700e 6423256: GC stacks should use a better data structure
jcoomes
parents:
diff changeset
   273
}