1 /*
   2  * Copyright 1997-2008 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   // some uses pass the Thread explicitly for speed (4990299 tuning)
 116   void* raw_allocate(Thread* thread, int elementSize) {
 117     assert(on_stack(), "fast ResourceObj path only");
 118     return (void*)resource_allocate_bytes(thread, elementSize * _max);
 119   }
 120 };
 121 
 122 template<class E> class GrowableArray : public GenericGrowableArray {
 123  private:
 124   E*     _data;         // data array
 125 
 126   void grow(int j);
 127   void raw_at_put_grow(int i, const E& p, const E& fill);
 128   void  clear_and_deallocate();
 129  public:
 130   GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
 131     _data = (E*)raw_allocate(thread, sizeof(E));
 132     for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
 133   }
 134 
 135   GrowableArray(int initial_size, bool C_heap = false) : GenericGrowableArray(initial_size, 0, C_heap) {
 136     _data = (E*)raw_allocate(sizeof(E));
 137     for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
 138   }
 139 
 140   GrowableArray(int initial_size, int initial_len, const E& filler, bool C_heap = false) : GenericGrowableArray(initial_size, initial_len, C_heap) {
 141     _data = (E*)raw_allocate(sizeof(E));
 142     int i = 0;
 143     for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
 144     for (; i < _max; i++) ::new ((void*)&_data[i]) E();
 145   }
 146 
 147   GrowableArray(Arena* arena, int initial_size, int initial_len, const E& filler) : GenericGrowableArray(arena, initial_size, initial_len) {
 148     _data = (E*)raw_allocate(sizeof(E));
 149     int i = 0;
 150     for (; i < _len; i++) ::new ((void*)&_data[i]) E(filler);
 151     for (; i < _max; i++) ::new ((void*)&_data[i]) E();
 152   }
 153 
 154   GrowableArray() : GenericGrowableArray(2, 0, false) {
 155     _data = (E*)raw_allocate(sizeof(E));
 156     ::new ((void*)&_data[0]) E();
 157     ::new ((void*)&_data[1]) E();
 158   }
 159 
 160                                 // Does nothing for resource and arena objects
 161   ~GrowableArray()              { if (on_C_heap()) clear_and_deallocate(); }
 162 
 163   void  clear()                 { _len = 0; }
 164   int   length() const          { return _len; }
 165   void  trunc_to(int l)         { assert(l <= _len,"cannot increase length"); _len = l; }
 166   bool  is_empty() const        { return _len == 0; }
 167   bool  is_nonempty() const     { return _len != 0; }
 168   bool  is_full() const         { return _len == _max; }
 169   DEBUG_ONLY(E* data_addr() const      { return _data; })
 170 
 171   void print();
 172 
 173   int append(const E& elem) {
 174     check_nesting();
 175     if (_len == _max) grow(_len);
 176     int idx = _len++;
 177     _data[idx] = elem;
 178     return idx;
 179   }
 180 
 181   void append_if_missing(const E& elem) {
 182     if (!contains(elem)) append(elem);
 183   }
 184 
 185   // inserts the given element before the element at index i
 186   void insert_before(const int idx, const E& elem) {
 187     check_nesting();
 188     if (_len == _max) grow(_len);
 189     for (int j = _len - 1; j >= idx; j--) {
 190       _data[j + 1] = _data[j];
 191     }
 192     _len++;
 193     _data[idx] = elem;
 194   }
 195 
 196   E at(int i) const {
 197     assert(0 <= i && i < _len, "illegal index");
 198     return _data[i];
 199   }
 200 
 201   E* adr_at(int i) const {
 202     assert(0 <= i && i < _len, "illegal index");
 203     return &_data[i];
 204   }
 205 
 206   E first() const {
 207     assert(_len > 0, "empty list");
 208     return _data[0];
 209   }
 210 
 211   E top() const {
 212     assert(_len > 0, "empty list");
 213     return _data[_len-1];
 214   }
 215 
 216   void push(const E& elem) { append(elem); }
 217 
 218   E pop() {
 219     assert(_len > 0, "empty list");
 220     return _data[--_len];
 221   }
 222 
 223   void at_put(int i, const E& elem) {
 224     assert(0 <= i && i < _len, "illegal index");
 225     _data[i] = elem;
 226   }
 227 
 228   E at_grow(int i, const E& fill = E()) {
 229     assert(0 <= i, "negative index");
 230     check_nesting();
 231     if (i >= _len) {
 232       if (i >= _max) grow(i);
 233       for (int j = _len; j <= i; j++)
 234         _data[j] = fill;
 235       _len = i+1;
 236     }
 237     return _data[i];
 238   }
 239 
 240   void at_put_grow(int i, const E& elem, const E& fill = E()) {
 241     assert(0 <= i, "negative index");
 242     check_nesting();
 243     raw_at_put_grow(i, elem, fill);
 244   }
 245 
 246   bool contains(const E& elem) const {
 247     for (int i = 0; i < _len; i++) {
 248       if (_data[i] == elem) return true;
 249     }
 250     return false;
 251   }
 252 
 253   int  find(const E& elem) const {
 254     for (int i = 0; i < _len; i++) {
 255       if (_data[i] == elem) return i;
 256     }
 257     return -1;
 258   }
 259 
 260   int  find(void* token, bool f(void*, E)) const {
 261     for (int i = 0; i < _len; i++) {
 262       if (f(token, _data[i])) return i;
 263     }
 264     return -1;
 265   }
 266 
 267   int  find_at_end(void* token, bool f(void*, E)) const {
 268     // start at the end of the array
 269     for (int i = _len-1; i >= 0; i--) {
 270       if (f(token, _data[i])) return i;
 271     }
 272     return -1;
 273   }
 274 
 275   void remove(const E& elem) {
 276     for (int i = 0; i < _len; i++) {
 277       if (_data[i] == elem) {
 278         for (int j = i + 1; j < _len; j++) _data[j-1] = _data[j];
 279         _len--;
 280         return;
 281       }
 282     }
 283     ShouldNotReachHere();
 284   }
 285 
 286   void remove_at(int index) {
 287     assert(0 <= index && index < _len, "illegal index");
 288     for (int j = index + 1; j < _len; j++) _data[j-1] = _data[j];
 289     _len--;
 290   }
 291 
 292   void appendAll(const GrowableArray<E>* l) {
 293     for (int i = 0; i < l->_len; i++) {
 294       raw_at_put_grow(_len, l->_data[i], 0);
 295     }
 296   }
 297 
 298   void sort(int f(E*,E*)) {
 299     qsort(_data, length(), sizeof(E), (_sort_Fn)f);
 300   }
 301   // sort by fixed-stride sub arrays:
 302   void sort(int f(E*,E*), int stride) {
 303     qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f);
 304   }
 305 };
 306 
 307 // Global GrowableArray methods (one instance in the library per each 'E' type).
 308 
 309 template<class E> void GrowableArray<E>::grow(int j) {
 310     // grow the array by doubling its size (amortized growth)
 311     int old_max = _max;
 312     if (_max == 0) _max = 1; // prevent endless loop
 313     while (j >= _max) _max = _max*2;
 314     // j < _max
 315     E* newData = (E*)raw_allocate(sizeof(E));
 316     int i = 0;
 317     for (     ; i < _len; i++) ::new ((void*)&newData[i]) E(_data[i]);
 318     for (     ; i < _max; i++) ::new ((void*)&newData[i]) E();
 319     for (i = 0; i < old_max; i++) _data[i].~E();
 320     if (on_C_heap() && _data != NULL) {
 321       FreeHeap(_data);
 322     }
 323     _data = newData;
 324 }
 325 
 326 template<class E> void GrowableArray<E>::raw_at_put_grow(int i, const E& p, const E& fill) {
 327     if (i >= _len) {
 328       if (i >= _max) grow(i);
 329       for (int j = _len; j < i; j++)
 330         _data[j] = fill;
 331       _len = i+1;
 332     }
 333     _data[i] = p;
 334 }
 335 
 336 // This function clears and deallocate the data in the growable array that
 337 // has been allocated on the C heap.  It's not public - called by the
 338 // destructor.
 339 template<class E> void GrowableArray<E>::clear_and_deallocate() {
 340     assert(on_C_heap(),
 341            "clear_and_deallocate should only be called when on C heap");
 342     clear();
 343     if (_data != NULL) {
 344       for (int i = 0; i < _max; i++) _data[i].~E();
 345       FreeHeap(_data);
 346       _data = NULL;
 347     }
 348 }
 349 
 350 template<class E> void GrowableArray<E>::print() {
 351     tty->print("Growable Array " INTPTR_FORMAT, this);
 352     tty->print(": length %ld (_max %ld) { ", _len, _max);
 353     for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
 354     tty->print("}\n");
 355 }