hotspot/src/share/vm/utilities/growableArray.hpp
author xlu
Mon, 06 Apr 2009 15:47:39 -0700
changeset 2526 39a58a50be35
parent 1623 a0dd9009e992
child 4450 6d700b859b3e
permissions -rw-r--r--
6699669: Hotspot server leaves synchronized block with monitor in bad state Summary: Remove usage of _highest_lock field in Thread so that is_lock_owned won't depend on the correct update of that field. Reviewed-by: never, dice, acorn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
1623
a0dd9009e992 6785258: Update copyright year
xdono
parents: 1551
diff changeset
     2
 * Copyright 1997-2008 Sun Microsystems, Inc.  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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// A growable array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
/*************************************************************************/
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
/*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
/* Should you use GrowableArrays to contain handles you must be certain  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
/* the the GrowableArray does not outlive the HandleMark that contains   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
/* the handles. Since GrowableArrays are typically resource allocated    */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
/* the following is an example of INCORRECT CODE,                        */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
/* ResourceMark rm;                                                      */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
/* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
/* if (blah) {                                                           */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
/*    while (...) {                                                      */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
/*      HandleMark hm;                                                   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
/*      ...                                                              */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
/*      Handle h(THREAD, some_oop);                                      */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
/*      arr->append(h);                                                  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
/*    }                                                                  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
/* }                                                                     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
/* if (arr->length() != 0 ) {                                            */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
/*    oop bad_oop = arr->at(0)(); // Handle is BAD HERE.                 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
/*    ...                                                                */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
/* }                                                                     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
/* If the GrowableArrays you are creating is C_Heap allocated then it    */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
/* hould not old handles since the handles could trivially try and       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
/* outlive their HandleMark. In some situations you might need to do     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
/* this and it would be legal but be very careful and see if you can do  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
/* the code in some other manner.                                        */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
/*************************************************************************/
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
// To call default constructor the placement operator new() is used.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
// It should be empty (it only returns the passed void* pointer).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// The definition of placement operator new(size_t, void*) in the <new>.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
#include <new>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
// Need the correct linkage to call qsort without warnings
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
extern "C" {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  typedef int (*_sort_Fn)(const void *, const void *);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
class GenericGrowableArray : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  int    _len;          // current length
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  int    _max;          // maximum length
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  Arena* _arena;        // Indicates where allocation occurs:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
                        //   0 means default ResourceArea
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
                        //   1 means on C heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
                        //   otherwise, allocate in _arena
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  int    _nesting;      // resource area nesting at creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  void   set_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  void   check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
#define  set_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
#define  check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // Where are we going to allocate memory?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  bool on_C_heap() { return _arena == (Arena*)1; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  bool on_stack () { return _arena == NULL;      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  bool on_arena () { return _arena >  (Arena*)1;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // This GA will use the resource stack for storage if c_heap==false,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // Else it will use the C heap.  Use clear_and_deallocate to avoid leaks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  GenericGrowableArray(int initial_size, int initial_len, bool c_heap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    _len = initial_len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
    _max = initial_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    assert(_len >= 0 && _len <= _max, "initial_len too big");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    _arena = (c_heap ? (Arena*)1 : NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    set_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
    assert(!c_heap || allocated_on_C_heap(), "growable array must be on C heap if elements are");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // This GA will use the given arena for storage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  // Consider using new(arena) GrowableArray<T> to allocate the header.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  GenericGrowableArray(Arena* arena, int initial_size, int initial_len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    _len = initial_len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    _max = initial_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    assert(_len >= 0 && _len <= _max, "initial_len too big");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    _arena = arena;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    assert(on_arena(), "arena has taken on reserved value 0 or 1");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  void* raw_allocate(int elementSize);
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   114
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   115
  // some uses pass the Thread explicitly for speed (4990299 tuning)
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   116
  void* raw_allocate(Thread* thread, int elementSize) {
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   117
    assert(on_stack(), "fast ResourceObj path only");
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   118
    return (void*)resource_allocate_bytes(thread, elementSize * _max);
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   119
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
template<class E> class GrowableArray : public GenericGrowableArray {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  E*     _data;         // data array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  void grow(int j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  void raw_at_put_grow(int i, const E& p, const E& fill);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  void  clear_and_deallocate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
 public:
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   130
  GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   131
    _data = (E*)raw_allocate(thread, sizeof(E));
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   132
    for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   133
  }
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   134
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  GrowableArray(int initial_size, bool C_heap = false) : GenericGrowableArray(initial_size, 0, C_heap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false) : GenericGrowableArray(initial_size, initial_len, C_heap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    for (; i < _max; i++) ::new ((void*)&_data[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
  GrowableArray(Arena* arena, int initial_size, int initial_len, const E& filler) : GenericGrowableArray(arena, initial_size, initial_len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    for (; i < _max; i++) ::new ((void*)&_data[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  GrowableArray() : GenericGrowableArray(2, 0, false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
    ::new ((void*)&_data[0]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    ::new ((void*)&_data[1]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
                                // Does nothing for resource and arena objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  ~GrowableArray()              { if (on_C_heap()) clear_and_deallocate(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  void  clear()                 { _len = 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  int   length() const          { return _len; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  void  trunc_to(int l)         { assert(l <= _len,"cannot increase length"); _len = l; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  bool  is_empty() const        { return _len == 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  bool  is_nonempty() const     { return _len != 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  bool  is_full() const         { return _len == _max; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  DEBUG_ONLY(E* data_addr() const      { return _data; })
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  void print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   173
  int append(const E& elem) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    if (_len == _max) grow(_len);
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   176
    int idx = _len++;
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   177
    _data[idx] = elem;
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   178
    return idx;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  void append_if_missing(const E& elem) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    if (!contains(elem)) append(elem);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  E at(int i) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    assert(0 <= i && i < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    return _data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  E* adr_at(int i) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    assert(0 <= i && i < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    return &_data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  E first() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
    assert(_len > 0, "empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    return _data[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  E top() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    assert(_len > 0, "empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    return _data[_len-1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  void push(const E& elem) { append(elem); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  E pop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    assert(_len > 0, "empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
    return _data[--_len];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  void at_put(int i, const E& elem) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    assert(0 <= i && i < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
    _data[i] = elem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  E at_grow(int i, const E& fill = E()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    assert(0 <= i, "negative index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    if (i >= _len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      if (i >= _max) grow(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      for (int j = _len; j <= i; j++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
        _data[j] = fill;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
      _len = i+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    return _data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  void at_put_grow(int i, const E& elem, const E& fill = E()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    assert(0 <= i, "negative index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    raw_at_put_grow(i, elem, fill);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  bool contains(const E& elem) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      if (_data[i] == elem) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
  int  find(const E& elem) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      if (_data[i] == elem) return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  int  find(void* token, bool f(void*, E)) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
      if (f(token, _data[i])) return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  int  find_at_end(void* token, bool f(void*, E)) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    // start at the end of the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    for (int i = _len-1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      if (f(token, _data[i])) return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  void remove(const E& elem) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
      if (_data[i] == elem) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
        for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
        _len--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  void remove_at(int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    assert(0 <= index && index < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    _len--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  void appendAll(const GrowableArray<E>* l) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
    for (int i = 0; i < l->_len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
      raw_at_put_grow(_len, l->_data[i], 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  void sort(int f(E*,E*)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
    qsort(_data, length(), sizeof(E), (_sort_Fn)f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  // sort by fixed-stride sub arrays:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  void sort(int f(E*,E*), int stride) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
    qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
// Global GrowableArray methods (one instance in the library per each 'E' type).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
template<class E> void GrowableArray<E>::grow(int j) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    // grow the array by doubling its size (amortized growth)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    int old_max = _max;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    if (_max == 0) _max = 1; // prevent endless loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    while (j >= _max) _max = _max*2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    // j < _max
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    E* newData = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    for (     ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    for (     ; i < _max; i++) ::new ((void*)&newData[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    for (i = 0; i < old_max; i++) _data[i].~E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    if (on_C_heap() && _data != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
      FreeHeap(_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    _data = newData;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
template<class E> void GrowableArray<E>::raw_at_put_grow(int i, const E& p, const E& fill) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
    if (i >= _len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
      if (i >= _max) grow(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
      for (int j = _len; j < i; j++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
        _data[j] = fill;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
      _len = i+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    _data[i] = p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
// This function clears and deallocate the data in the growable array that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
// has been allocated on the C heap.  It's not public - called by the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
// destructor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
template<class E> void GrowableArray<E>::clear_and_deallocate() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    assert(on_C_heap(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
           "clear_and_deallocate should only be called when on C heap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    if (_data != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
      for (int i = 0; i < _max; i++) _data[i].~E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
      FreeHeap(_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
      _data = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
template<class E> void GrowableArray<E>::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
    tty->print("Growable Array " INTPTR_FORMAT, this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    tty->print(": length %ld (_max %ld) { ", _len, _max);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    tty->print("}\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
}