hotspot/src/share/vm/utilities/growableArray.hpp
author kamg
Thu, 11 Oct 2012 12:25:42 -0400
changeset 14385 959bbcc16725
parent 13728 882756847a04
child 19339 d247781beec7
permissions -rw-r--r--
7200776: Implement default methods in interfaces Summary: Add generic type analysis and default method selection algorithms Reviewed-by: coleenp, acorn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
12158
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
     2
 * Copyright (c) 1997, 2012, 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: 4450
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4450
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: 4450
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: 6180
diff changeset
    25
#ifndef SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    26
#define SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    29
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    30
#include "utilities/debug.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    31
#include "utilities/globalDefinitions.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    32
#include "utilities/top.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
    33
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// A growable array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
/*************************************************************************/
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
/*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
/* Should you use GrowableArrays to contain handles you must be certain  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
/* the the GrowableArray does not outlive the HandleMark that contains   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
/* the handles. Since GrowableArrays are typically resource allocated    */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
/* the following is an example of INCORRECT CODE,                        */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
/* ResourceMark rm;                                                      */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
/* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
/* if (blah) {                                                           */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
/*    while (...) {                                                      */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
/*      HandleMark hm;                                                   */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
/*      ...                                                              */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
/*      Handle h(THREAD, some_oop);                                      */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
/*      arr->append(h);                                                  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
/*    }                                                                  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
/* }                                                                     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
/* if (arr->length() != 0 ) {                                            */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
/*    oop bad_oop = arr->at(0)(); // Handle is BAD HERE.                 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
/*    ...                                                                */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
/* }                                                                     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
/* If the GrowableArrays you are creating is C_Heap allocated then it    */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
/* hould not old handles since the handles could trivially try and       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
/* outlive their HandleMark. In some situations you might need to do     */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
/* this and it would be legal but be very careful and see if you can do  */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
/* the code in some other manner.                                        */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
/*                                                                       */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
/*************************************************************************/
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
// To call default constructor the placement operator new() is used.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
// It should be empty (it only returns the passed void* pointer).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
// The definition of placement operator new(size_t, void*) in the <new>.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
#include <new>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
// Need the correct linkage to call qsort without warnings
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
extern "C" {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  typedef int (*_sort_Fn)(const void *, const void *);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
class GenericGrowableArray : public ResourceObj {
10547
ea4a2ec31ae2 7088955: add C2 IR support to the SA
never
parents: 7397
diff changeset
    80
  friend class VMStructs;
ea4a2ec31ae2 7088955: add C2 IR support to the SA
never
parents: 7397
diff changeset
    81
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  int    _len;          // current length
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  int    _max;          // maximum length
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  Arena* _arena;        // Indicates where allocation occurs:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
                        //   0 means default ResourceArea
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
                        //   1 means on C heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
                        //   otherwise, allocate in _arena
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
    89
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
    90
  MEMFLAGS   _memflags;   // memory type if allocation in C heap
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
    91
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  int    _nesting;      // resource area nesting at creation
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  void   set_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  void   check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
#define  set_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
#define  check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Where are we going to allocate memory?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  bool on_C_heap() { return _arena == (Arena*)1; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  bool on_stack () { return _arena == NULL;      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  bool on_arena () { return _arena >  (Arena*)1;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  // This GA will use the resource stack for storage if c_heap==false,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  // Else it will use the C heap.  Use clear_and_deallocate to avoid leaks.
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   108
  GenericGrowableArray(int initial_size, int initial_len, bool c_heap, MEMFLAGS flags = mtNone) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    _len = initial_len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    _max = initial_size;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   111
    _memflags = flags;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   112
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   113
    // memory type has to be specified for C heap allocation
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   114
    assert(!(c_heap && flags == mtNone), "memory type not specified for C heap object");
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   115
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    assert(_len >= 0 && _len <= _max, "initial_len too big");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    _arena = (c_heap ? (Arena*)1 : NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    set_nesting();
6180
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   119
    assert(!on_C_heap() || allocated_on_C_heap(), "growable array must be on C heap if elements are");
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   120
    assert(!on_stack() ||
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   121
           (allocated_on_res_area() || allocated_on_stack()),
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   122
           "growable array must be on stack if elements are not on arena and not on C heap");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  // This GA will use the given arena for storage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  // Consider using new(arena) GrowableArray<T> to allocate the header.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  GenericGrowableArray(Arena* arena, int initial_size, int initial_len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    _len = initial_len;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    _max = initial_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    assert(_len >= 0 && _len <= _max, "initial_len too big");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    _arena = arena;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   132
    _memflags = mtNone;
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   133
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    assert(on_arena(), "arena has taken on reserved value 0 or 1");
6180
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   135
    // Relax next assert to allow object allocation on resource area,
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   136
    // on stack or embedded into an other object.
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   137
    assert(allocated_on_arena() || allocated_on_stack(),
53c1bf468c81 6973963: SEGV in ciBlock::start_bci() with EA
kvn
parents: 5547
diff changeset
   138
           "growable array must be on arena or on stack if elements are on arena");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  void* raw_allocate(int elementSize);
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   142
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   143
  // some uses pass the Thread explicitly for speed (4990299 tuning)
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   144
  void* raw_allocate(Thread* thread, int elementSize) {
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   145
    assert(on_stack(), "fast ResourceObj path only");
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   146
    return (void*)resource_allocate_bytes(thread, elementSize * _max);
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   147
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
template<class E> class GrowableArray : public GenericGrowableArray {
10547
ea4a2ec31ae2 7088955: add C2 IR support to the SA
never
parents: 7397
diff changeset
   151
  friend class VMStructs;
ea4a2ec31ae2 7088955: add C2 IR support to the SA
never
parents: 7397
diff changeset
   152
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  E*     _data;         // data array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  void grow(int j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  void raw_at_put_grow(int i, const E& p, const E& fill);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  void  clear_and_deallocate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
 public:
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   160
  GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   161
    _data = (E*)raw_allocate(thread, sizeof(E));
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   162
    for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   163
  }
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   164
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   165
  GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal)
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   166
    : GenericGrowableArray(initial_size, 0, C_heap, F) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   171
  GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false, MEMFLAGS memflags = mtInternal)
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 12158
diff changeset
   172
    : GenericGrowableArray(initial_size, initial_len, C_heap, memflags) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    for (; i < _max; i++) ::new ((void*)&_data[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  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
   180
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
    for (; i < _max; i++) ::new ((void*)&_data[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
  GrowableArray() : GenericGrowableArray(2, 0, false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
    _data = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
    ::new ((void*)&_data[0]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
    ::new ((void*)&_data[1]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
                                // Does nothing for resource and arena objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
  ~GrowableArray()              { if (on_C_heap()) clear_and_deallocate(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  void  clear()                 { _len = 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  int   length() const          { return _len; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  void  trunc_to(int l)         { assert(l <= _len,"cannot increase length"); _len = l; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  bool  is_empty() const        { return _len == 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  bool  is_nonempty() const     { return _len != 0; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  bool  is_full() const         { return _len == _max; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  DEBUG_ONLY(E* data_addr() const      { return _data; })
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  void print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   205
  int append(const E& elem) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
    check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    if (_len == _max) grow(_len);
1551
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   208
    int idx = _len++;
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   209
    _data[idx] = elem;
b431de37a22c 6770949: minor tweaks before 6655638
jrose
parents: 1
diff changeset
   210
    return idx;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
12158
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   213
  bool append_if_missing(const E& elem) {
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   214
    // Returns TRUE if elem is added.
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   215
    bool missed = !contains(elem);
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   216
    if (missed) append(elem);
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   217
    return missed;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
14385
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents: 13728
diff changeset
   220
  E& at(int i) {
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents: 13728
diff changeset
   221
    assert(0 <= i && i < _len, "illegal index");
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents: 13728
diff changeset
   222
    return _data[i];
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents: 13728
diff changeset
   223
  }
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents: 13728
diff changeset
   224
959bbcc16725 7200776: Implement default methods in interfaces
kamg
parents: 13728
diff changeset
   225
  E const& at(int i) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
    assert(0 <= i && i < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    return _data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  E* adr_at(int i) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    assert(0 <= i && i < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    return &_data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  E first() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
    assert(_len > 0, "empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    return _data[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  E top() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
    assert(_len > 0, "empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
    return _data[_len-1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  void push(const E& elem) { append(elem); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  E pop() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
    assert(_len > 0, "empty list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    return _data[--_len];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  void at_put(int i, const E& elem) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    assert(0 <= i && i < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    _data[i] = elem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  E at_grow(int i, const E& fill = E()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    assert(0 <= i, "negative index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
    check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    if (i >= _len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
      if (i >= _max) grow(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
      for (int j = _len; j <= i; j++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
        _data[j] = fill;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
      _len = i+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
    return _data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
  void at_put_grow(int i, const E& elem, const E& fill = E()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
    assert(0 <= i, "negative index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
    check_nesting();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
    raw_at_put_grow(i, elem, fill);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  bool contains(const E& elem) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
      if (_data[i] == elem) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  int  find(const E& elem) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
      if (_data[i] == elem) return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   289
  int  find_from_end(const E& elem) const {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   290
    for (int i = _len-1; i >= 0; i--) {
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   291
      if (_data[i] == elem) return i;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   292
    }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   293
    return -1;
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   294
  }
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   295
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  int  find(void* token, bool f(void*, E)) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
      if (f(token, _data[i])) return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   303
  int  find_from_end(void* token, bool f(void*, E)) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    // start at the end of the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    for (int i = _len-1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      if (f(token, _data[i])) return i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    return -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  void remove(const E& elem) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    for (int i = 0; i < _len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
      if (_data[i] == elem) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
        for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
        _len--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
12158
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   322
  // The order is preserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  void remove_at(int index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
    assert(0 <= index && index < _len, "illegal index");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    _len--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
12158
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   329
  // The order is changed.
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   330
  void delete_at(int index) {
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   331
    assert(0 <= index && index < _len, "illegal index");
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   332
    if (index < --_len) {
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   333
      // Replace removed element with last one.
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   334
      _data[index] = _data[_len];
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   335
    }
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   336
  }
f24f2560da32 7147744: CTW: assert(false) failed: infinite EA connection graph build
kvn
parents: 10547
diff changeset
   337
4450
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   338
  // inserts the given element before the element at index i
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   339
  void insert_before(const int idx, const E& elem) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   340
    check_nesting();
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   341
    if (_len == _max) grow(_len);
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   342
    for (int j = _len - 1; j >= idx; j--) {
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   343
      _data[j + 1] = _data[j];
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   344
    }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   345
    _len++;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   346
    _data[idx] = elem;
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   347
  }
6d700b859b3e 6892658: C2 should optimize some stringbuilder patterns
never
parents: 1623
diff changeset
   348
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  void appendAll(const GrowableArray<E>* l) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    for (int i = 0; i < l->_len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
      raw_at_put_grow(_len, l->_data[i], 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  void sort(int f(E*,E*)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    qsort(_data, length(), sizeof(E), (_sort_Fn)f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
  // sort by fixed-stride sub arrays:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
  void sort(int f(E*,E*), int stride) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
// Global GrowableArray methods (one instance in the library per each 'E' type).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
template<class E> void GrowableArray<E>::grow(int j) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    // grow the array by doubling its size (amortized growth)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
    int old_max = _max;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
    if (_max == 0) _max = 1; // prevent endless loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
    while (j >= _max) _max = _max*2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
    // j < _max
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
    E* newData = (E*)raw_allocate(sizeof(E));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
    int i = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
    for (     ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    for (     ; i < _max; i++) ::new ((void*)&newData[i]) E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    for (i = 0; i < old_max; i++) _data[i].~E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    if (on_C_heap() && _data != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
      FreeHeap(_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    _data = newData;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
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
   384
    if (i >= _len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
      if (i >= _max) grow(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
      for (int j = _len; j < i; j++)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
        _data[j] = fill;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
      _len = i+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    _data[i] = p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
// This function clears and deallocate the data in the growable array that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
// has been allocated on the C heap.  It's not public - called by the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
// destructor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
template<class E> void GrowableArray<E>::clear_and_deallocate() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
    assert(on_C_heap(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
           "clear_and_deallocate should only be called when on C heap");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
    clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    if (_data != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
      for (int i = 0; i < _max; i++) _data[i].~E();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
      FreeHeap(_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
      _data = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
template<class E> void GrowableArray<E>::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
    tty->print("Growable Array " INTPTR_FORMAT, this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
    tty->print(": length %ld (_max %ld) { ", _len, _max);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
    for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
    tty->print("}\n");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
}
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
   413
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6180
diff changeset
   414
#endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP