src/share/vm/utilities/growableArray.hpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File JDK-8015774 Sdiff src/share/vm/utilities

src/share/vm/utilities/growableArray.hpp

Print this page




  59 /*                                                                       */
  60 /* If the GrowableArrays you are creating is C_Heap allocated then it    */
  61 /* hould not old handles since the handles could trivially try and       */
  62 /* outlive their HandleMark. In some situations you might need to do     */
  63 /* this and it would be legal but be very careful and see if you can do  */
  64 /* the code in some other manner.                                        */
  65 /*                                                                       */
  66 /*************************************************************************/
  67 
  68 // To call default constructor the placement operator new() is used.
  69 // It should be empty (it only returns the passed void* pointer).
  70 // The definition of placement operator new(size_t, void*) in the <new>.
  71 
  72 #include <new>
  73 
  74 // Need the correct linkage to call qsort without warnings
  75 extern "C" {
  76   typedef int (*_sort_Fn)(const void *, const void *);
  77 }
  78 



  79 class GenericGrowableArray : public ResourceObj {
  80   friend class VMStructs;
  81 
  82  protected:
  83   int    _len;          // current length
  84   int    _max;          // maximum length
  85   Arena* _arena;        // Indicates where allocation occurs:
  86                         //   0 means default ResourceArea
  87                         //   1 means on C heap
  88                         //   otherwise, allocate in _arena
  89 
  90   MEMFLAGS   _memflags;   // memory type if allocation in C heap
  91 
  92 #ifdef ASSERT
  93   int    _nesting;      // resource area nesting at creation
  94   void   set_nesting();
  95   void   check_nesting();
  96 #else
  97 #define  set_nesting();
  98 #define  check_nesting();


 226   E const& at(int i) const {
 227     assert(0 <= i && i < _len, "illegal index");
 228     return _data[i];
 229   }
 230 
 231   E* adr_at(int i) const {
 232     assert(0 <= i && i < _len, "illegal index");
 233     return &_data[i];
 234   }
 235 
 236   E first() const {
 237     assert(_len > 0, "empty list");
 238     return _data[0];
 239   }
 240 
 241   E top() const {
 242     assert(_len > 0, "empty list");
 243     return _data[_len-1];
 244   }
 245 








 246   void push(const E& elem) { append(elem); }
 247 
 248   E pop() {
 249     assert(_len > 0, "empty list");
 250     return _data[--_len];
 251   }
 252 
 253   void at_put(int i, const E& elem) {
 254     assert(0 <= i && i < _len, "illegal index");
 255     _data[i] = elem;
 256   }
 257 
 258   E at_grow(int i, const E& fill = E()) {
 259     assert(0 <= i, "negative index");
 260     check_nesting();
 261     if (i >= _len) {
 262       if (i >= _max) grow(i);
 263       for (int j = _len; j <= i; j++)
 264         _data[j] = fill;
 265       _len = i+1;


 394 // This function clears and deallocate the data in the growable array that
 395 // has been allocated on the C heap.  It's not public - called by the
 396 // destructor.
 397 template<class E> void GrowableArray<E>::clear_and_deallocate() {
 398     assert(on_C_heap(),
 399            "clear_and_deallocate should only be called when on C heap");
 400     clear();
 401     if (_data != NULL) {
 402       for (int i = 0; i < _max; i++) _data[i].~E();
 403       FreeHeap(_data);
 404       _data = NULL;
 405     }
 406 }
 407 
 408 template<class E> void GrowableArray<E>::print() {
 409     tty->print("Growable Array " INTPTR_FORMAT, this);
 410     tty->print(": length %ld (_max %ld) { ", _len, _max);
 411     for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
 412     tty->print("}\n");
 413 }





















































 414 
 415 #endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP


  59 /*                                                                       */
  60 /* If the GrowableArrays you are creating is C_Heap allocated then it    */
  61 /* hould not old handles since the handles could trivially try and       */
  62 /* outlive their HandleMark. In some situations you might need to do     */
  63 /* this and it would be legal but be very careful and see if you can do  */
  64 /* the code in some other manner.                                        */
  65 /*                                                                       */
  66 /*************************************************************************/
  67 
  68 // To call default constructor the placement operator new() is used.
  69 // It should be empty (it only returns the passed void* pointer).
  70 // The definition of placement operator new(size_t, void*) in the <new>.
  71 
  72 #include <new>
  73 
  74 // Need the correct linkage to call qsort without warnings
  75 extern "C" {
  76   typedef int (*_sort_Fn)(const void *, const void *);
  77 }
  78 
  79 template<class E> class GrowableArrayIterator;
  80 template<class E, class UnaryPredicate> class GrowableArrayFilterIterator;
  81 
  82 class GenericGrowableArray : public ResourceObj {
  83   friend class VMStructs;
  84 
  85  protected:
  86   int    _len;          // current length
  87   int    _max;          // maximum length
  88   Arena* _arena;        // Indicates where allocation occurs:
  89                         //   0 means default ResourceArea
  90                         //   1 means on C heap
  91                         //   otherwise, allocate in _arena
  92 
  93   MEMFLAGS   _memflags;   // memory type if allocation in C heap
  94 
  95 #ifdef ASSERT
  96   int    _nesting;      // resource area nesting at creation
  97   void   set_nesting();
  98   void   check_nesting();
  99 #else
 100 #define  set_nesting();
 101 #define  check_nesting();


 229   E const& at(int i) const {
 230     assert(0 <= i && i < _len, "illegal index");
 231     return _data[i];
 232   }
 233 
 234   E* adr_at(int i) const {
 235     assert(0 <= i && i < _len, "illegal index");
 236     return &_data[i];
 237   }
 238 
 239   E first() const {
 240     assert(_len > 0, "empty list");
 241     return _data[0];
 242   }
 243 
 244   E top() const {
 245     assert(_len > 0, "empty list");
 246     return _data[_len-1];
 247   }
 248 
 249   GrowableArrayIterator<E> begin() const {
 250     return GrowableArrayIterator<E>(this, 0);
 251   }
 252 
 253   GrowableArrayIterator<E> end() const {
 254     return GrowableArrayIterator<E>(this, length());
 255   }
 256 
 257   void push(const E& elem) { append(elem); }
 258 
 259   E pop() {
 260     assert(_len > 0, "empty list");
 261     return _data[--_len];
 262   }
 263 
 264   void at_put(int i, const E& elem) {
 265     assert(0 <= i && i < _len, "illegal index");
 266     _data[i] = elem;
 267   }
 268 
 269   E at_grow(int i, const E& fill = E()) {
 270     assert(0 <= i, "negative index");
 271     check_nesting();
 272     if (i >= _len) {
 273       if (i >= _max) grow(i);
 274       for (int j = _len; j <= i; j++)
 275         _data[j] = fill;
 276       _len = i+1;


 405 // This function clears and deallocate the data in the growable array that
 406 // has been allocated on the C heap.  It's not public - called by the
 407 // destructor.
 408 template<class E> void GrowableArray<E>::clear_and_deallocate() {
 409     assert(on_C_heap(),
 410            "clear_and_deallocate should only be called when on C heap");
 411     clear();
 412     if (_data != NULL) {
 413       for (int i = 0; i < _max; i++) _data[i].~E();
 414       FreeHeap(_data);
 415       _data = NULL;
 416     }
 417 }
 418 
 419 template<class E> void GrowableArray<E>::print() {
 420     tty->print("Growable Array " INTPTR_FORMAT, this);
 421     tty->print(": length %ld (_max %ld) { ", _len, _max);
 422     for (int i = 0; i < _len; i++) tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i]));
 423     tty->print("}\n");
 424 }
 425 
 426 // Custom STL iterator to iterate over GrowableArrays
 427 // It is constructed by invoking GrowableArray::begin() and GrowableArray::end()
 428 template<class E> class GrowableArrayIterator {
 429   friend class GrowableArray<E>;
 430   template<class A, class B> friend class GrowableArrayFilterIterator;
 431 
 432  private:
 433   const GrowableArray<E>* _array; // GrowableArray we iterate over
 434   int _position;                  // The current position in the GrowableArray
 435 
 436   // Private constructor used in GrowableArray::begin() and GrowableArray::end()
 437   GrowableArrayIterator(const GrowableArray<E>* array, int position) : _array(array), _position(position) { }
 438 
 439  public:
 440   GrowableArrayIterator<E>& operator++()                { ++_position; return *this; }
 441   bool operator==(const GrowableArrayIterator<E>& rhs)  { return _position == rhs._position; }
 442   bool operator!=(const GrowableArrayIterator<E>& rhs)  { return _position != rhs._position; }
 443   E operator*()                                         { return _array->at(_position); }
 444 };
 445 
 446 // Custom STL iterator to iterate over elements of a GrowableArray that satisfy a given predicate
 447 template<class E, class UnaryPredicate> class GrowableArrayFilterIterator {
 448   friend class GrowableArray<E>;
 449 
 450  private:
 451   const GrowableArray<E>* _array;   // GrowableArray we iterate over
 452   int _position;                    // Current position in the GrowableArray
 453   UnaryPredicate _predicate;        // Unary predicate the elements of the GrowableArray should satisfy
 454 
 455  public:
 456   GrowableArrayFilterIterator(const GrowableArrayIterator<E>& begin, UnaryPredicate filter_predicate)
 457    : _array(begin._array), _position(begin._position), _predicate(filter_predicate) {
 458     // Advance to first element satisfying the predicate
 459     while(_position != _array->length() && !_predicate(_array->at(_position))) {
 460       ++_position;
 461     }
 462   }
 463 
 464   GrowableArrayFilterIterator<E, UnaryPredicate>& operator++() {
 465     do {
 466       // Advance to next element satisfying the predicate
 467       ++_position;
 468     } while(_position != _array->length() && !_predicate(_array->at(_position)));
 469     return *this;
 470   }
 471 
 472   bool operator==(const GrowableArrayIterator<E>& rhs)  { return _position == rhs._position; }
 473   bool operator!=(const GrowableArrayIterator<E>& rhs)  { return _position != rhs._position; }
 474   bool operator==(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  { return _position == rhs._position; }
 475   bool operator!=(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs)  { return _position != rhs._position; }
 476   E operator*() { return _array->at(_position); }
 477 };
 478 
 479 #endif // SHARE_VM_UTILITIES_GROWABLEARRAY_HPP
src/share/vm/utilities/growableArray.hpp
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File