< prev index next >

src/hotspot/share/utilities/growableArray.hpp

Print this page




 135     // Relax next assert to allow object allocation on resource area,
 136     // on stack or embedded into an other object.
 137     assert(allocated_on_arena() || allocated_on_stack(),
 138            "growable array must be on arena or on stack if elements are on arena");
 139   }
 140 
 141   void* raw_allocate(int elementSize);
 142 
 143   // some uses pass the Thread explicitly for speed (4990299 tuning)
 144   void* raw_allocate(Thread* thread, int elementSize) {
 145     assert(on_stack(), "fast ResourceObj path only");
 146     return (void*)resource_allocate_bytes(thread, elementSize * _max);
 147   }
 148 
 149   void free_C_heap(void* elements);
 150 };
 151 
 152 template<class E> class GrowableArrayIterator;
 153 template<class E, class UnaryPredicate> class GrowableArrayFilterIterator;
 154 
 155 template<class E>
 156 class CompareClosure : public Closure {
 157 public:
 158     virtual int do_compare(const E&, const E&) = 0;
 159 };
 160 
 161 template<class E> class GrowableArray : public GenericGrowableArray {
 162   friend class VMStructs;
 163 
 164  private:
 165   E*     _data;         // data array
 166 
 167   void grow(int j);
 168   void raw_at_put_grow(int i, const E& p, const E& fill);
 169   void  clear_and_deallocate();
 170  public:
 171   GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
 172     _data = (E*)raw_allocate(thread, sizeof(E));
 173     for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
 174   }
 175 
 176   GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal)
 177     : GenericGrowableArray(initial_size, 0, C_heap, F) {
 178     _data = (E*)raw_allocate(sizeof(E));
 179 // Needed for Visual Studio 2012 and older
 180 #ifdef _MSC_VER


 421   // that element is not already in the list.  Assumes the list is
 422   // already sorted according to compare function.
 423   template <int compare(const E&, const E&)> E insert_sorted(const E& key) {
 424     bool found;
 425     int location = find_sorted<E, compare>(key, found);
 426     if (!found) {
 427       insert_before(location, key);
 428     }
 429     return at(location);
 430   }
 431 
 432   template <typename K, int compare(const K&, const E&)> int find_sorted(const K& key, bool& found) {
 433     found = false;
 434     int min = 0;
 435     int max = length() - 1;
 436 
 437     while (max >= min) {
 438       int mid = (int)(((uint)max + min) / 2);
 439       E value = at(mid);
 440       int diff = compare(key, value);
 441       if (diff > 0) {
 442         min = mid + 1;
 443       } else if (diff < 0) {
 444         max = mid - 1;
 445       } else {
 446         found = true;
 447         return mid;
 448       }
 449     }
 450     return min;
 451   }
 452 
 453   E insert_sorted(CompareClosure<E>* cc, const E& key) {
 454     bool found;
 455     int location = find_sorted(cc, key, found);
 456     if (!found) {
 457       insert_before(location, key);
 458     }
 459     return at(location);
 460   }
 461 
 462   template<typename K>
 463   int find_sorted(CompareClosure<E>* cc, const K& key, bool& found) {
 464     found = false;
 465     int min = 0;
 466     int max = length() - 1;
 467 
 468     while (max >= min) {
 469       int mid = (int)(((uint)max + min) / 2);
 470       E value = at(mid);
 471       int diff = cc->do_compare(key, value);
 472       if (diff > 0) {
 473         min = mid + 1;
 474       } else if (diff < 0) {
 475         max = mid - 1;
 476       } else {
 477         found = true;
 478         return mid;
 479       }
 480     }
 481     return min;
 482   }
 483 };
 484 
 485 // Global GrowableArray methods (one instance in the library per each 'E' type).
 486 
 487 template<class E> void GrowableArray<E>::grow(int j) {
 488     // grow the array by doubling its size (amortized growth)
 489     int old_max = _max;
 490     if (_max == 0) _max = 1; // prevent endless loop
 491     while (j >= _max) _max = _max*2;




 135     // Relax next assert to allow object allocation on resource area,
 136     // on stack or embedded into an other object.
 137     assert(allocated_on_arena() || allocated_on_stack(),
 138            "growable array must be on arena or on stack if elements are on arena");
 139   }
 140 
 141   void* raw_allocate(int elementSize);
 142 
 143   // some uses pass the Thread explicitly for speed (4990299 tuning)
 144   void* raw_allocate(Thread* thread, int elementSize) {
 145     assert(on_stack(), "fast ResourceObj path only");
 146     return (void*)resource_allocate_bytes(thread, elementSize * _max);
 147   }
 148 
 149   void free_C_heap(void* elements);
 150 };
 151 
 152 template<class E> class GrowableArrayIterator;
 153 template<class E, class UnaryPredicate> class GrowableArrayFilterIterator;
 154 






 155 template<class E> class GrowableArray : public GenericGrowableArray {
 156   friend class VMStructs;
 157 
 158  private:
 159   E*     _data;         // data array
 160 
 161   void grow(int j);
 162   void raw_at_put_grow(int i, const E& p, const E& fill);
 163   void  clear_and_deallocate();
 164  public:
 165   GrowableArray(Thread* thread, int initial_size) : GenericGrowableArray(initial_size, 0, false) {
 166     _data = (E*)raw_allocate(thread, sizeof(E));
 167     for (int i = 0; i < _max; i++) ::new ((void*)&_data[i]) E();
 168   }
 169 
 170   GrowableArray(int initial_size, bool C_heap = false, MEMFLAGS F = mtInternal)
 171     : GenericGrowableArray(initial_size, 0, C_heap, F) {
 172     _data = (E*)raw_allocate(sizeof(E));
 173 // Needed for Visual Studio 2012 and older
 174 #ifdef _MSC_VER


 415   // that element is not already in the list.  Assumes the list is
 416   // already sorted according to compare function.
 417   template <int compare(const E&, const E&)> E insert_sorted(const E& key) {
 418     bool found;
 419     int location = find_sorted<E, compare>(key, found);
 420     if (!found) {
 421       insert_before(location, key);
 422     }
 423     return at(location);
 424   }
 425 
 426   template <typename K, int compare(const K&, const E&)> int find_sorted(const K& key, bool& found) {
 427     found = false;
 428     int min = 0;
 429     int max = length() - 1;
 430 
 431     while (max >= min) {
 432       int mid = (int)(((uint)max + min) / 2);
 433       E value = at(mid);
 434       int diff = compare(key, value);































 435       if (diff > 0) {
 436         min = mid + 1;
 437       } else if (diff < 0) {
 438         max = mid - 1;
 439       } else {
 440         found = true;
 441         return mid;
 442       }
 443     }
 444     return min;
 445   }
 446 };
 447 
 448 // Global GrowableArray methods (one instance in the library per each 'E' type).
 449 
 450 template<class E> void GrowableArray<E>::grow(int j) {
 451     // grow the array by doubling its size (amortized growth)
 452     int old_max = _max;
 453     if (_max == 0) _max = 1; // prevent endless loop
 454     while (j >= _max) _max = _max*2;


< prev index next >