hotspot/src/share/vm/utilities/growableArray.hpp
changeset 1 489c9b5090e2
child 1551 b431de37a22c
equal deleted inserted replaced
0:fd16c54261b3 1:489c9b5090e2
       
     1 /*
       
     2  * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  *
       
    23  */
       
    24 
       
    25 // A growable array.
       
    26 
       
    27 /*************************************************************************/
       
    28 /*                                                                       */
       
    29 /*     WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING   */
       
    30 /*                                                                       */
       
    31 /* Should you use GrowableArrays to contain handles you must be certain  */
       
    32 /* the the GrowableArray does not outlive the HandleMark that contains   */
       
    33 /* the handles. Since GrowableArrays are typically resource allocated    */
       
    34 /* the following is an example of INCORRECT CODE,                        */
       
    35 /*                                                                       */
       
    36 /* ResourceMark rm;                                                      */
       
    37 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size);         */
       
    38 /* if (blah) {                                                           */
       
    39 /*    while (...) {                                                      */
       
    40 /*      HandleMark hm;                                                   */
       
    41 /*      ...                                                              */
       
    42 /*      Handle h(THREAD, some_oop);                                      */
       
    43 /*      arr->append(h);                                                  */
       
    44 /*    }                                                                  */
       
    45 /* }                                                                     */
       
    46 /* if (arr->length() != 0 ) {                                            */
       
    47 /*    oop bad_oop = arr->at(0)(); // Handle is BAD HERE.                 */
       
    48 /*    ...                                                                */
       
    49 /* }                                                                     */
       
    50 /*                                                                       */
       
    51 /* If the GrowableArrays you are creating is C_Heap allocated then it    */
       
    52 /* hould not old handles since the handles could trivially try and       */
       
    53 /* outlive their HandleMark. In some situations you might need to do     */
       
    54 /* this and it would be legal but be very careful and see if you can do  */
       
    55 /* the code in some other manner.                                        */
       
    56 /*                                                                       */
       
    57 /*************************************************************************/
       
    58 
       
    59 // To call default constructor the placement operator new() is used.
       
    60 // It should be empty (it only returns the passed void* pointer).
       
    61 // The definition of placement operator new(size_t, void*) in the <new>.
       
    62 
       
    63 #include <new>
       
    64 
       
    65 // Need the correct linkage to call qsort without warnings
       
    66 extern "C" {
       
    67   typedef int (*_sort_Fn)(const void *, const void *);
       
    68 }
       
    69 
       
    70 class GenericGrowableArray : public ResourceObj {
       
    71  protected:
       
    72   int    _len;          // current length
       
    73   int    _max;          // maximum length
       
    74   Arena* _arena;        // Indicates where allocation occurs:
       
    75                         //   0 means default ResourceArea
       
    76                         //   1 means on C heap
       
    77                         //   otherwise, allocate in _arena
       
    78 #ifdef ASSERT
       
    79   int    _nesting;      // resource area nesting at creation
       
    80   void   set_nesting();
       
    81   void   check_nesting();
       
    82 #else
       
    83 #define  set_nesting();
       
    84 #define  check_nesting();
       
    85 #endif
       
    86 
       
    87   // Where are we going to allocate memory?
       
    88   bool on_C_heap() { return _arena == (Arena*)1; }
       
    89   bool on_stack () { return _arena == NULL;      }
       
    90   bool on_arena () { return _arena >  (Arena*)1;  }
       
    91 
       
    92   // This GA will use the resource stack for storage if c_heap==false,
       
    93   // Else it will use the C heap.  Use clear_and_deallocate to avoid leaks.
       
    94   GenericGrowableArray(int initial_size, int initial_len, bool c_heap) {
       
    95     _len = initial_len;
       
    96     _max = initial_size;
       
    97     assert(_len >= 0 && _len <= _max, "initial_len too big");
       
    98     _arena = (c_heap ? (Arena*)1 : NULL);
       
    99     set_nesting();
       
   100     assert(!c_heap || allocated_on_C_heap(), "growable array must be on C heap if elements are");
       
   101   }
       
   102 
       
   103   // This GA will use the given arena for storage.
       
   104   // Consider using new(arena) GrowableArray<T> to allocate the header.
       
   105   GenericGrowableArray(Arena* arena, int initial_size, int initial_len) {
       
   106     _len = initial_len;
       
   107     _max = initial_size;
       
   108     assert(_len >= 0 && _len <= _max, "initial_len too big");
       
   109     _arena = arena;
       
   110     assert(on_arena(), "arena has taken on reserved value 0 or 1");
       
   111   }
       
   112 
       
   113   void* raw_allocate(int elementSize);
       
   114 };
       
   115 
       
   116 template<class E> class GrowableArray : public GenericGrowableArray {
       
   117  private:
       
   118   E*     _data;         // data array
       
   119 
       
   120   void grow(int j);
       
   121   void raw_at_put_grow(int i, const E& p, const E& fill);
       
   122   void  clear_and_deallocate();
       
   123  public:
       
   124   GrowableArray(int initial_size, bool C_heap = false) : GenericGrowableArray(initial_size, 0, C_heap) {
       
   125     _data = (E*)raw_allocate(sizeof(E));
       
   126     for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
       
   127   }
       
   128 
       
   129   GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false) : GenericGrowableArray(initial_size, initial_len, C_heap) {
       
   130     _data = (E*)raw_allocate(sizeof(E));
       
   131     int i = 0;
       
   132     for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
       
   133     for (; i < _max; i++) ::new ((void*)&_data[i]) E();
       
   134   }
       
   135 
       
   136   GrowableArray(Arena* arena, int initial_size, int initial_len, const E& filler) : GenericGrowableArray(arena, initial_size, initial_len) {
       
   137     _data = (E*)raw_allocate(sizeof(E));
       
   138     int i = 0;
       
   139     for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
       
   140     for (; i < _max; i++) ::new ((void*)&_data[i]) E();
       
   141   }
       
   142 
       
   143   GrowableArray() : GenericGrowableArray(2, 0, false) {
       
   144     _data = (E*)raw_allocate(sizeof(E));
       
   145     ::new ((void*)&_data[0]) E();
       
   146     ::new ((void*)&_data[1]) E();
       
   147   }
       
   148 
       
   149                                 // Does nothing for resource and arena objects
       
   150   ~GrowableArray()              { if (on_C_heap()) clear_and_deallocate(); }
       
   151 
       
   152   void  clear()                 { _len = 0; }
       
   153   int   length() const          { return _len; }
       
   154   void  trunc_to(int l)         { assert(l <= _len,"cannot increase length"); _len = l; }
       
   155   bool  is_empty() const        { return _len == 0; }
       
   156   bool  is_nonempty() const     { return _len != 0; }
       
   157   bool  is_full() const         { return _len == _max; }
       
   158   DEBUG_ONLY(E* data_addr() const      { return _data; })
       
   159 
       
   160   void print();
       
   161 
       
   162   void append(const E& elem) {
       
   163     check_nesting();
       
   164     if (_len == _max) grow(_len);
       
   165     _data[_len++] = elem;
       
   166   }
       
   167 
       
   168   void append_if_missing(const E& elem) {
       
   169     if (!contains(elem)) append(elem);
       
   170   }
       
   171 
       
   172   E at(int i) const {
       
   173     assert(0 <= i && i < _len, "illegal index");
       
   174     return _data[i];
       
   175   }
       
   176 
       
   177   E* adr_at(int i) const {
       
   178     assert(0 <= i && i < _len, "illegal index");
       
   179     return &_data[i];
       
   180   }
       
   181 
       
   182   E first() const {
       
   183     assert(_len > 0, "empty list");
       
   184     return _data[0];
       
   185   }
       
   186 
       
   187   E top() const {
       
   188     assert(_len > 0, "empty list");
       
   189     return _data[_len-1];
       
   190   }
       
   191 
       
   192   void push(const E& elem) { append(elem); }
       
   193 
       
   194   E pop() {
       
   195     assert(_len > 0, "empty list");
       
   196     return _data[--_len];
       
   197   }
       
   198 
       
   199   void at_put(int i, const E& elem) {
       
   200     assert(0 <= i && i < _len, "illegal index");
       
   201     _data[i] = elem;
       
   202   }
       
   203 
       
   204   E at_grow(int i, const E& fill = E()) {
       
   205     assert(0 <= i, "negative index");
       
   206     check_nesting();
       
   207     if (i >= _len) {
       
   208       if (i >= _max) grow(i);
       
   209       for (int j = _len; j <= i; j++)
       
   210         _data[j] = fill;
       
   211       _len = i+1;
       
   212     }
       
   213     return _data[i];
       
   214   }
       
   215 
       
   216   void at_put_grow(int i, const E& elem, const E& fill = E()) {
       
   217     assert(0 <= i, "negative index");
       
   218     check_nesting();
       
   219     raw_at_put_grow(i, elem, fill);
       
   220   }
       
   221 
       
   222   bool contains(const E& elem) const {
       
   223     for (int i = 0; i < _len; i++) {
       
   224       if (_data[i] == elem) return true;
       
   225     }
       
   226     return false;
       
   227   }
       
   228 
       
   229   int  find(const E& elem) const {
       
   230     for (int i = 0; i < _len; i++) {
       
   231       if (_data[i] == elem) return i;
       
   232     }
       
   233     return -1;
       
   234   }
       
   235 
       
   236   int  find(void* token, bool f(void*, E)) const {
       
   237     for (int i = 0; i < _len; i++) {
       
   238       if (f(token, _data[i])) return i;
       
   239     }
       
   240     return -1;
       
   241   }
       
   242 
       
   243   int  find_at_end(void* token, bool f(void*, E)) const {
       
   244     // start at the end of the array
       
   245     for (int i = _len-1; i >= 0; i--) {
       
   246       if (f(token, _data[i])) return i;
       
   247     }
       
   248     return -1;
       
   249   }
       
   250 
       
   251   void remove(const E& elem) {
       
   252     for (int i = 0; i < _len; i++) {
       
   253       if (_data[i] == elem) {
       
   254         for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
       
   255         _len--;
       
   256         return;
       
   257       }
       
   258     }
       
   259     ShouldNotReachHere();
       
   260   }
       
   261 
       
   262   void remove_at(int index) {
       
   263     assert(0 <= index && index < _len, "illegal index");
       
   264     for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
       
   265     _len--;
       
   266   }
       
   267 
       
   268   void appendAll(const GrowableArray<E>* l) {
       
   269     for (int i = 0; i < l->_len; i++) {
       
   270       raw_at_put_grow(_len, l->_data[i], 0);
       
   271     }
       
   272   }
       
   273 
       
   274   void sort(int f(E*,E*)) {
       
   275     qsort(_data, length(), sizeof(E), (_sort_Fn)f);
       
   276   }
       
   277   // sort by fixed-stride sub arrays:
       
   278   void sort(int f(E*,E*), int stride) {
       
   279     qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
       
   280   }
       
   281 };
       
   282 
       
   283 // Global GrowableArray methods (one instance in the library per each 'E' type).
       
   284 
       
   285 template<class E> void GrowableArray<E>::grow(int j) {
       
   286     // grow the array by doubling its size (amortized growth)
       
   287     int old_max = _max;
       
   288     if (_max == 0) _max = 1; // prevent endless loop
       
   289     while (j >= _max) _max = _max*2;
       
   290     // j < _max
       
   291     E* newData = (E*)raw_allocate(sizeof(E));
       
   292     int i = 0;
       
   293     for (     ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
       
   294     for (     ; i < _max; i++) ::new ((void*)&newData[i]) E();
       
   295     for (i = 0; i < old_max; i++) _data[i].~E();
       
   296     if (on_C_heap() && _data != NULL) {
       
   297       FreeHeap(_data);
       
   298     }
       
   299     _data = newData;
       
   300 }
       
   301 
       
   302 template<class E> void GrowableArray<E>::raw_at_put_grow(int i, const E& p, const E& fill) {
       
   303     if (i >= _len) {
       
   304       if (i >= _max) grow(i);
       
   305       for (int j = _len; j < i; j++)
       
   306         _data[j] = fill;
       
   307       _len = i+1;
       
   308     }
       
   309     _data[i] = p;
       
   310 }
       
   311 
       
   312 // This function clears and deallocate the data in the growable array that
       
   313 // has been allocated on the C heap.  It's not public - called by the
       
   314 // destructor.
       
   315 template<class E> void GrowableArray<E>::clear_and_deallocate() {
       
   316     assert(on_C_heap(),
       
   317            "clear_and_deallocate should only be called when on C heap");
       
   318     clear();
       
   319     if (_data != NULL) {
       
   320       for (int i = 0; i < _max; i++) _data[i].~E();
       
   321       FreeHeap(_data);
       
   322       _data = NULL;
       
   323     }
       
   324 }
       
   325 
       
   326 template<class E> void GrowableArray<E>::print() {
       
   327     tty->print("Growable Array " INTPTR_FORMAT, this);
       
   328     tty->print(": length %ld (_max %ld) { ", _len, _max);
       
   329     for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
       
   330     tty->print("}\n");
       
   331 }