hotspot/src/share/vm/utilities/array.hpp
author coleenp
Thu, 27 Jan 2011 16:11:27 -0800
changeset 8076 96d498ec7ae1
parent 7397 5b173b4ca846
child 13195 be27e1b6a4b9
permissions -rw-r--r--
6990754: Use native memory and reference counting to implement SymbolTable Summary: move symbols from permgen into C heap and reference count them Reviewed-by: never, acorn, jmasa, stefank
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1623
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1623
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1623
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_UTILITIES_ARRAY_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_UTILITIES_ARRAY_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    30
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// correct linkage required to compile w/o warnings
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// (must be on file level - cannot be local)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
extern "C" { typedef int (*ftype)(const void*, const void*); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
class ResourceArray: public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  int   _length;                                 // the number of array elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  void* _data;                                   // the array memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  int   _nesting;                                // the resource area nesting level
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  // creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  ResourceArray() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
    _length  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    _data    = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
    DEBUG_ONLY(init_nesting();)
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    49
    // client may call initialize, at most once
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  ResourceArray(size_t esize, int length) {
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    54
    DEBUG_ONLY(_data = NULL);
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    55
    initialize(esize, length);
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    56
  }
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    57
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    58
  void initialize(size_t esize, int length) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    assert(length >= 0, "illegal length");
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
    60
    assert(_data == NULL, "must be new object");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
    _length  = length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
    _data    = resource_allocate_bytes(esize * length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    DEBUG_ONLY(init_nesting();)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  void init_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  // helper functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  void sort     (size_t esize, ftype f);         // sort the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  void expand   (size_t esize, int i, int& size);// expand the array to include slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  void remove_at(size_t esize, int i);           // remove the element in slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // standard operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  int  length() const                            { return _length; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  bool is_empty() const                          { return length() == 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
class CHeapArray: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  int   _length;                                 // the number of array elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  void* _data;                                   // the array memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  CHeapArray() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    _length  = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    _data    = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  CHeapArray(size_t esize, int length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    assert(length >= 0, "illegal length");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    _length  = length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    _data    = (void*) NEW_C_HEAP_ARRAY(char *, esize * length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  void init_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // helper functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  void sort     (size_t esize, ftype f);         // sort the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  void expand   (size_t esize, int i, int& size);// expand the array to include slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  void remove_at(size_t esize, int i);           // remove the element in slot i
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // standard operations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  int  length() const                            { return _length; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  bool is_empty() const                          { return length() == 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
#define define_generic_array(array_name,element_type, base_class)                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  class array_name: public base_class {                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
   protected:                                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    typedef element_type etype;                                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    enum { esize = sizeof(etype) };                                                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    void base_remove_at(size_t size, int i) { base_class::remove_at(size, i); }          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
   public:                                                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    /* creation */                                                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    array_name() : base_class()                       {}                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    array_name(const int length) : base_class(esize, length) {}                          \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   127
    array_name(const int length, const etype fx)      { initialize(length, fx); }        \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   128
    void initialize(const int length)     { base_class::initialize(esize, length); }     \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   129
    void initialize(const int length, const etype fx) {                                  \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   130
      initialize(length);                                                                \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
      for (int i = 0; i < length; i++) ((etype*)_data)[i] = fx;                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    /* standard operations */                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    etype& operator [] (const int i) const {                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
      assert(0 <= i && i < length(), "index out of bounds");                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
      return ((etype*)_data)[i];                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    int index_of(const etype x) const {                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
      int i = length();                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      while (i-- > 0 && ((etype*)_data)[i] != x) ;                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      /* i < 0 || ((etype*)_data)_data[i] == x */                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
      return i;                                                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    void sort(int f(etype*, etype*))             { base_class::sort(esize, (ftype)f); }  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    bool contains(const etype x) const           { return index_of(x) >= 0; }            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    /* deprecated operations - for compatibility with GrowableArray only */              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    etype  at(const int i) const                 { return (*this)[i]; }                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    void   at_put(const int i, const etype x)    { (*this)[i] = x; }                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    etype* adr_at(const int i)                   { return &(*this)[i]; }                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    int    find(const etype x)                   { return index_of(x); }                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  };                                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
#define define_array(array_name,element_type)                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  define_generic_array(array_name, element_type, ResourceArray)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
#define define_stack(stack_name,array_name)                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  class stack_name: public array_name {                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
   protected:                                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
    int _size;                                                                           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    void grow(const int i, const etype fx) {                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
      assert(i >= length(), "index too small");                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
      if (i >= size()) expand(esize, i, _size);                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      for (int j = length(); j <= i; j++) ((etype*)_data)[j] = fx;                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      _length = i+1;                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
   public:                                                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    /* creation */                                                                       \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   176
    stack_name() : array_name()                     { _size = 0; }                       \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   177
    stack_name(const int size)                      { initialize(size); }                \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   178
    stack_name(const int size, const etype fx)      { initialize(size, fx); }            \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   179
    void initialize(const int size, const etype fx) {                                    \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   180
      _size = size;                                                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   181
      array_name::initialize(size, fx);                                                  \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   182
      /* _length == size, allocation and size are the same */                            \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   183
    }                                                                                    \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   184
    void initialize(const int size) {                                                    \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   185
      _size = size;                                                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   186
      array_name::initialize(size);                                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   187
      _length = 0;          /* reset length to zero; _size records the allocation */     \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   188
    }                                                                                    \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    /* standard operations */                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    int size() const                             { return _size; }                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
                                                                                         \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   193
    int push(const etype x) {                                                            \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   194
      int len = length();                                                                \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   195
      if (len >= size()) expand(esize, len, _size);                                      \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   196
      ((etype*)_data)[len] = x;                                                          \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   197
      _length = len+1;                                                                   \
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   198
      return len;                                                                        \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    etype pop() {                                                                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
      assert(!is_empty(), "stack is empty");                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
      return ((etype*)_data)[--_length];                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    etype top() const {                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
      assert(!is_empty(), "stack is empty");                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
      return ((etype*)_data)[length() - 1];                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    void push_all(const stack_name* stack) {                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
      const int l = stack->length();                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
      for (int i = 0; i < l; i++) push(((etype*)(stack->_data))[i]);                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    etype at_grow(const int i, const etype fx) {                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      if (i >= length()) grow(i, fx);                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
      return ((etype*)_data)[i];                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    void at_put_grow(const int i, const etype x, const etype fx) {                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      if (i >= length()) grow(i, fx);                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      ((etype*)_data)[i] = x;                                                            \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    void truncate(const int length) {                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
      assert(0 <= length && length <= this->length(), "illegal length");                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
      _length = length;                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    void remove_at(int i)                        { base_remove_at(esize, i); }           \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    void remove(etype x)                         { remove_at(index_of(x)); }             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    /* inserts the given element before the element at index i */                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
    void insert_before(const int i, const etype el)  {                                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      int len = length();                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      int new_length = len + 1;                                                          \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      if (new_length >= size()) expand(esize, new_length, _size);                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
      for (int j = len - 1; j >= i; j--) {                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
        ((etype*)_data)[j + 1] = ((etype*)_data)[j];                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      _length = new_length;                                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
      at_put(i, el);                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    /* inserts contents of the given stack before the element at index i */              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
    void insert_before(const int i, const stack_name *st) {                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
      if (st->length() == 0) return;                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
      int len = length();                                                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      int st_len = st->length();                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      int new_length = len + st_len;                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      if (new_length >= size()) expand(esize, new_length, _size);                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
      int j;                                                                             \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
      for (j = len - 1; j >= i; j--) {                                                   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
        ((etype*)_data)[j + st_len] = ((etype*)_data)[j];                                \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
      for (j = 0; j < st_len; j++) {                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
        ((etype*)_data)[i + j] = ((etype*)st->_data)[j];                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      }                                                                                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
      _length = new_length;                                                              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    }                                                                                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
                                                                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    /* deprecated operations - for compatibility with GrowableArray only */              \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    int  capacity() const                        { return size(); }                      \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    void clear()                                 { truncate(0); }                        \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    void trunc_to(const int length)              { truncate(length); }                   \
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   267
    int  append(const etype x)                   { return push(x); }                     \
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
    void appendAll(const stack_name* stack)      { push_all(stack); }                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
    etype last() const                           { return top(); }                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  };                                                                                     \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
#define define_resource_list(element_type)                                               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  define_generic_array(element_type##Array, element_type, ResourceArray)                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
#define define_resource_pointer_list(element_type)                                       \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  define_generic_array(element_type##Array, element_type *, ResourceArray)               \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
#define define_c_heap_list(element_type)                                                 \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  define_generic_array(element_type##Array, element_type, CHeapArray)                    \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
#define define_c_heap_pointer_list(element_type)                                         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  define_generic_array(element_type##Array, element_type *, CHeapArray)                  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  define_stack(element_type##List, element_type##Array)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
// Arrays for basic types
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
define_array(boolArray, bool)          define_stack(boolStack, boolArray)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
define_array(intArray , int )          define_stack(intStack , intArray )
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   294
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   295
#endif // SHARE_VM_UTILITIES_ARRAY_HPP