1 /* 2 * Copyright (c) 1997, 2023, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_UTILITIES_GROWABLEARRAY_HPP 26 #define SHARE_UTILITIES_GROWABLEARRAY_HPP 27 28 #include "memory/allocation.hpp" 29 #include "memory/iterator.hpp" 30 #include "utilities/debug.hpp" 31 #include "utilities/globalDefinitions.hpp" 32 #include "utilities/ostream.hpp" 33 #include "utilities/powerOfTwo.hpp" 34 35 // A growable array. 36 37 /*************************************************************************/ 38 /* */ 39 /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING */ 40 /* */ 41 /* Should you use GrowableArrays to contain handles you must be certain */ 42 /* that the GrowableArray does not outlive the HandleMark that contains */ 43 /* the handles. Since GrowableArrays are typically resource allocated */ 44 /* the following is an example of INCORRECT CODE, */ 45 /* */ 46 /* ResourceMark rm; */ 47 /* GrowableArray<Handle>* arr = new GrowableArray<Handle>(size); */ 48 /* if (blah) { */ 49 /* while (...) { */ 50 /* HandleMark hm; */ 51 /* ... */ 52 /* Handle h(THREAD, some_oop); */ 53 /* arr->append(h); */ 54 /* } */ 55 /* } */ 56 /* if (arr->length() != 0 ) { */ 57 /* oop bad_oop = arr->at(0)(); // Handle is BAD HERE. */ 58 /* ... */ 59 /* } */ 60 /* */ 61 /* If the GrowableArrays you are creating is C_Heap allocated then it */ 62 /* should not hold handles since the handles could trivially try and */ 63 /* outlive their HandleMark. In some situations you might need to do */ 64 /* this and it would be legal but be very careful and see if you can do */ 65 /* the code in some other manner. */ 66 /* */ 67 /*************************************************************************/ 68 69 // Non-template base class responsible for handling the length and max. 70 71 72 class GrowableArrayBase : public AnyObj { 73 friend class VMStructs; 74 75 protected: 76 // Current number of accessible elements 77 int _len; 78 // Current number of allocated elements 79 int _capacity; 80 81 GrowableArrayBase(int capacity, int initial_len) : 82 _len(initial_len), 83 _capacity(capacity) { 84 assert(_len >= 0 && _len <= _capacity, "initial_len too big"); 85 } 86 87 ~GrowableArrayBase() {} 88 89 public: 90 int length() const { return _len; } 91 int capacity() const { return _capacity; } 92 93 bool is_empty() const { return _len == 0; } 94 bool is_nonempty() const { return _len != 0; } 95 bool is_full() const { return _len == _capacity; } 96 97 void clear() { _len = 0; } 98 void trunc_to(int length) { 99 assert(length <= _len,"cannot increase length"); 100 _len = length; 101 } 102 }; 103 104 template <typename E> class GrowableArrayIterator; 105 template <typename E, typename UnaryPredicate> class GrowableArrayFilterIterator; 106 107 // Extends GrowableArrayBase with a typed data array. 108 // 109 // E: Element type 110 // 111 // The "view" adds function that don't grow or deallocate 112 // the _data array, so there's no need for an allocator. 113 // 114 // The "view" can be used to type erase the allocator classes 115 // of GrowableArrayWithAllocator. 116 template <typename E> 117 class GrowableArrayView : public GrowableArrayBase { 118 protected: 119 E* _data; // data array 120 121 GrowableArrayView<E>(E* data, int capacity, int initial_len) : 122 GrowableArrayBase(capacity, initial_len), _data(data) {} 123 124 ~GrowableArrayView() {} 125 126 public: 127 const static GrowableArrayView EMPTY; 128 129 bool operator==(const GrowableArrayView<E>& rhs) const { 130 if (_len != rhs._len) 131 return false; 132 for (int i = 0; i < _len; i++) { 133 if (at(i) != rhs.at(i)) { 134 return false; 135 } 136 } 137 return true; 138 } 139 140 bool operator!=(const GrowableArrayView<E>& rhs) const { 141 return !(*this == rhs); 142 } 143 144 E& at(int i) { 145 assert(0 <= i && i < _len, "illegal index %d for length %d", i, _len); 146 return _data[i]; 147 } 148 149 E const& at(int i) const { 150 assert(0 <= i && i < _len, "illegal index %d for length %d", i, _len); 151 return _data[i]; 152 } 153 154 E* adr_at(int i) const { 155 assert(0 <= i && i < _len, "illegal index %d for length %d", i, _len); 156 return &_data[i]; 157 } 158 159 E first() const { 160 assert(_len > 0, "empty"); 161 return _data[0]; 162 } 163 164 E top() const { 165 assert(_len > 0, "empty"); 166 return _data[_len-1]; 167 } 168 169 E last() const { 170 return top(); 171 } 172 173 GrowableArrayIterator<E> begin() const { 174 return GrowableArrayIterator<E>(this, 0); 175 } 176 177 GrowableArrayIterator<E> end() const { 178 return GrowableArrayIterator<E>(this, length()); 179 } 180 181 E pop() { 182 assert(_len > 0, "empty list"); 183 return _data[--_len]; 184 } 185 186 void at_put(int i, const E& elem) { 187 assert(0 <= i && i < _len, "illegal index %d for length %d", i, _len); 188 _data[i] = elem; 189 } 190 191 bool contains(const E& elem) const { 192 for (int i = 0; i < _len; i++) { 193 if (_data[i] == elem) return true; 194 } 195 return false; 196 } 197 198 int find(const E& elem) const { 199 for (int i = 0; i < _len; i++) { 200 if (_data[i] == elem) return i; 201 } 202 return -1; 203 } 204 205 int find_from_end(const E& elem) const { 206 for (int i = _len-1; i >= 0; i--) { 207 if (_data[i] == elem) return i; 208 } 209 return -1; 210 } 211 212 // Find first element that matches the given predicate. 213 // 214 // Predicate: bool predicate(const E& elem) 215 // 216 // Returns the index of the element or -1 if no element matches the predicate 217 template<typename Predicate> 218 int find_if(Predicate predicate) const { 219 for (int i = 0; i < _len; i++) { 220 if (predicate(_data[i])) return i; 221 } 222 return -1; 223 } 224 225 // Find last element that matches the given predicate. 226 // 227 // Predicate: bool predicate(const E& elem) 228 // 229 // Returns the index of the element or -1 if no element matches the predicate 230 template<typename Predicate> 231 int find_from_end_if(Predicate predicate) const { 232 // start at the end of the array 233 for (int i = _len-1; i >= 0; i--) { 234 if (predicate(_data[i])) return i; 235 } 236 return -1; 237 } 238 239 // Order preserving remove operations. 240 241 void remove(const E& elem) { 242 // Assuming that element does exist. 243 bool removed = remove_if_existing(elem); 244 if (removed) return; 245 ShouldNotReachHere(); 246 } 247 248 bool remove_if_existing(const E& elem) { 249 // Returns TRUE if elem is removed. 250 for (int i = 0; i < _len; i++) { 251 if (_data[i] == elem) { 252 remove_at(i); 253 return true; 254 } 255 } 256 return false; 257 } 258 259 void remove_at(int index) { 260 assert(0 <= index && index < _len, "illegal index %d for length %d", index, _len); 261 for (int j = index + 1; j < _len; j++) { 262 _data[j-1] = _data[j]; 263 } 264 _len--; 265 } 266 267 // Remove all elements up to the index (exclusive). The order is preserved. 268 void remove_till(int idx) { 269 remove_range(0, idx); 270 } 271 272 // Remove all elements in the range [start - end). The order is preserved. 273 void remove_range(int start, int end) { 274 assert(0 <= start, "illegal start index %d", start); 275 assert(start < end && end <= _len, "erase called with invalid range (%d, %d) for length %d", start, end, _len); 276 277 for (int i = start, j = end; j < length(); i++, j++) { 278 at_put(i, at(j)); 279 } 280 trunc_to(length() - (end - start)); 281 } 282 283 // The order is changed. 284 void delete_at(int index) { 285 assert(0 <= index && index < _len, "illegal index %d for length %d", index, _len); 286 if (index < --_len) { 287 // Replace removed element with last one. 288 _data[index] = _data[_len]; 289 } 290 } 291 292 void sort(int f(E*, E*)) { 293 qsort(_data, length(), sizeof(E), (_sort_Fn)f); 294 } 295 // sort by fixed-stride sub arrays: 296 void sort(int f(E*, E*), int stride) { 297 qsort(_data, length() / stride, sizeof(E) * stride, (_sort_Fn)f); 298 } 299 300 template <typename K, int compare(const K&, const E&)> int find_sorted(const K& key, bool& found) const { 301 found = false; 302 int min = 0; 303 int max = length() - 1; 304 305 while (max >= min) { 306 int mid = (int)(((uint)max + min) / 2); 307 E value = at(mid); 308 int diff = compare(key, value); 309 if (diff > 0) { 310 min = mid + 1; 311 } else if (diff < 0) { 312 max = mid - 1; 313 } else { 314 found = true; 315 return mid; 316 } 317 } 318 return min; 319 } 320 321 template <typename K> 322 int find_sorted(CompareClosure<E>* cc, const K& key, bool& found) { 323 found = false; 324 int min = 0; 325 int max = length() - 1; 326 327 while (max >= min) { 328 int mid = (int)(((uint)max + min) / 2); 329 E value = at(mid); 330 int diff = cc->do_compare(key, value); 331 if (diff > 0) { 332 min = mid + 1; 333 } else if (diff < 0) { 334 max = mid - 1; 335 } else { 336 found = true; 337 return mid; 338 } 339 } 340 return min; 341 } 342 343 void print() const { 344 tty->print("Growable Array " PTR_FORMAT, p2i(this)); 345 tty->print(": length %d (capacity %d) { ", _len, _capacity); 346 for (int i = 0; i < _len; i++) { 347 tty->print(INTPTR_FORMAT " ", *(intptr_t*)&(_data[i])); 348 } 349 tty->print("}\n"); 350 } 351 }; 352 353 template<typename E> 354 const GrowableArrayView<E> GrowableArrayView<E>::EMPTY(nullptr, 0, 0); 355 356 template <typename E> 357 class GrowableArrayFromArray : public GrowableArrayView<E> { 358 public: 359 360 GrowableArrayFromArray<E>(E* data, int len) : 361 GrowableArrayView<E>(data, len, len) {} 362 }; 363 364 // GrowableArrayWithAllocator extends the "view" with 365 // the capability to grow and deallocate the data array. 366 // 367 // The allocator responsibility is delegated to the sub-class. 368 // 369 // Derived: The sub-class responsible for allocation / deallocation 370 // - E* Derived::allocate() - member function responsible for allocation 371 // - void Derived::deallocate(E*) - member function responsible for deallocation 372 template <typename E, typename Derived> 373 class GrowableArrayWithAllocator : public GrowableArrayView<E> { 374 friend class VMStructs; 375 376 void expand_to(int j); 377 void grow(int j); 378 379 protected: 380 GrowableArrayWithAllocator(E* data, int capacity) : 381 GrowableArrayView<E>(data, capacity, 0) { 382 for (int i = 0; i < capacity; i++) { 383 ::new ((void*)&data[i]) E(); 384 } 385 } 386 387 GrowableArrayWithAllocator(E* data, int capacity, int initial_len, const E& filler) : 388 GrowableArrayView<E>(data, capacity, initial_len) { 389 int i = 0; 390 for (; i < initial_len; i++) { 391 ::new ((void*)&data[i]) E(filler); 392 } 393 for (; i < capacity; i++) { 394 ::new ((void*)&data[i]) E(); 395 } 396 } 397 398 ~GrowableArrayWithAllocator() {} 399 400 public: 401 int append(const E& elem) { 402 if (this->_len == this->_capacity) grow(this->_len); 403 int idx = this->_len++; 404 this->_data[idx] = elem; 405 return idx; 406 } 407 408 bool append_if_missing(const E& elem) { 409 // Returns TRUE if elem is added. 410 bool missed = !this->contains(elem); 411 if (missed) append(elem); 412 return missed; 413 } 414 415 void push(const E& elem) { append(elem); } 416 417 E at_grow(int i, const E& fill = E()) { 418 assert(0 <= i, "negative index %d", i); 419 if (i >= this->_len) { 420 if (i >= this->_capacity) grow(i); 421 for (int j = this->_len; j <= i; j++) 422 this->_data[j] = fill; 423 this->_len = i+1; 424 } 425 return this->_data[i]; 426 } 427 428 void at_put_grow(int i, const E& elem, const E& fill = E()) { 429 assert(0 <= i, "negative index %d", i); 430 if (i >= this->_len) { 431 if (i >= this->_capacity) grow(i); 432 for (int j = this->_len; j < i; j++) 433 this->_data[j] = fill; 434 this->_len = i+1; 435 } 436 this->_data[i] = elem; 437 } 438 439 // inserts the given element before the element at index i 440 void insert_before(const int idx, const E& elem) { 441 assert(0 <= idx && idx <= this->_len, "illegal index %d for length %d", idx, this->_len); 442 if (this->_len == this->_capacity) grow(this->_len); 443 for (int j = this->_len - 1; j >= idx; j--) { 444 this->_data[j + 1] = this->_data[j]; 445 } 446 this->_len++; 447 this->_data[idx] = elem; 448 } 449 450 void insert_before(const int idx, const GrowableArrayView<E>* array) { 451 assert(0 <= idx && idx <= this->_len, "illegal index %d for length %d", idx, this->_len); 452 int array_len = array->length(); 453 int new_len = this->_len + array_len; 454 if (new_len >= this->_capacity) grow(new_len); 455 456 for (int j = this->_len - 1; j >= idx; j--) { 457 this->_data[j + array_len] = this->_data[j]; 458 } 459 460 for (int j = 0; j < array_len; j++) { 461 this->_data[idx + j] = array->at(j); 462 } 463 464 this->_len += array_len; 465 } 466 467 void appendAll(const GrowableArrayView<E>* l) { 468 for (int i = 0; i < l->length(); i++) { 469 this->at_put_grow(this->_len, l->at(i), E()); 470 } 471 } 472 473 // Binary search and insertion utility. Search array for element 474 // matching key according to the static compare function. Insert 475 // that element if not already in the list. Assumes the list is 476 // already sorted according to compare function. 477 template <int compare(const E&, const E&)> E insert_sorted(const E& key) { 478 bool found; 479 int location = GrowableArrayView<E>::template find_sorted<E, compare>(key, found); 480 if (!found) { 481 insert_before(location, key); 482 } 483 return this->at(location); 484 } 485 486 E insert_sorted(CompareClosure<E>* cc, const E& key) { 487 bool found; 488 int location = find_sorted(cc, key, found); 489 if (!found) { 490 insert_before(location, key); 491 } 492 return this->at(location); 493 } 494 495 void swap(GrowableArrayWithAllocator<E, Derived>* other) { 496 ::swap(this->_data, other->_data); 497 ::swap(this->_len, other->_len); 498 ::swap(this->_capacity, other->_capacity); 499 } 500 501 // Ensure capacity is at least new_capacity. 502 void reserve(int new_capacity); 503 504 // Reduce capacity to length. 505 void shrink_to_fit(); 506 507 void clear_and_deallocate(); 508 }; 509 510 template <typename E, typename Derived> 511 void GrowableArrayWithAllocator<E, Derived>::expand_to(int new_capacity) { 512 int old_capacity = this->_capacity; 513 assert(new_capacity > old_capacity, 514 "expected growth but %d <= %d", new_capacity, old_capacity); 515 this->_capacity = new_capacity; 516 E* newData = static_cast<Derived*>(this)->allocate(); 517 int i = 0; 518 for ( ; i < this->_len; i++) ::new ((void*)&newData[i]) E(this->_data[i]); 519 for ( ; i < this->_capacity; i++) ::new ((void*)&newData[i]) E(); 520 for (i = 0; i < old_capacity; i++) this->_data[i].~E(); 521 if (this->_data != nullptr) { 522 static_cast<Derived*>(this)->deallocate(this->_data); 523 } 524 this->_data = newData; 525 } 526 527 template <typename E, typename Derived> 528 void GrowableArrayWithAllocator<E, Derived>::grow(int j) { 529 // grow the array by increasing _capacity to the first power of two larger than the size we need 530 expand_to(next_power_of_2(j)); 531 } 532 533 template <typename E, typename Derived> 534 void GrowableArrayWithAllocator<E, Derived>::reserve(int new_capacity) { 535 if (new_capacity > this->_capacity) { 536 expand_to(new_capacity); 537 } 538 } 539 540 template <typename E, typename Derived> 541 void GrowableArrayWithAllocator<E, Derived>::shrink_to_fit() { 542 int old_capacity = this->_capacity; 543 int len = this->_len; 544 assert(len <= old_capacity, "invariant"); 545 546 // If already at full capacity, nothing to do. 547 if (len == old_capacity) { 548 return; 549 } 550 551 // If not empty, allocate new, smaller, data, and copy old data to it. 552 E* old_data = this->_data; 553 E* new_data = nullptr; 554 this->_capacity = len; // Must preceed allocate(). 555 if (len > 0) { 556 new_data = static_cast<Derived*>(this)->allocate(); 557 for (int i = 0; i < len; ++i) ::new (&new_data[i]) E(old_data[i]); 558 } 559 // Destroy contents of old data, and deallocate it. 560 for (int i = 0; i < old_capacity; ++i) old_data[i].~E(); 561 if (old_data != nullptr) { 562 static_cast<Derived*>(this)->deallocate(old_data); 563 } 564 // Install new data, which might be nullptr. 565 this->_data = new_data; 566 } 567 568 template <typename E, typename Derived> 569 void GrowableArrayWithAllocator<E, Derived>::clear_and_deallocate() { 570 this->clear(); 571 this->shrink_to_fit(); 572 } 573 574 class GrowableArrayResourceAllocator { 575 public: 576 static void* allocate(int max, int element_size); 577 }; 578 579 // Arena allocator 580 class GrowableArrayArenaAllocator { 581 public: 582 static void* allocate(int max, int element_size, Arena* arena); 583 }; 584 585 // CHeap allocator 586 class GrowableArrayCHeapAllocator { 587 public: 588 static void* allocate(int max, int element_size, MEMFLAGS memflags); 589 static void deallocate(void* mem); 590 }; 591 592 #ifdef ASSERT 593 594 // Checks resource allocation nesting 595 class GrowableArrayNestingCheck { 596 // resource area nesting at creation 597 int _nesting; 598 599 public: 600 GrowableArrayNestingCheck(bool on_resource_area); 601 602 void on_resource_area_alloc() const; 603 }; 604 605 #endif // ASSERT 606 607 // Encodes where the backing array is allocated 608 // and performs necessary checks. 609 class GrowableArrayMetadata { 610 uintptr_t _bits; 611 612 // resource area nesting at creation 613 debug_only(GrowableArrayNestingCheck _nesting_check;) 614 615 // Resource allocation 616 static uintptr_t bits() { 617 return 0; 618 } 619 620 // CHeap allocation 621 static uintptr_t bits(MEMFLAGS memflags) { 622 assert(memflags != mtNone, "Must provide a proper MEMFLAGS"); 623 return (uintptr_t(memflags) << 1) | 1; 624 } 625 626 // Arena allocation 627 static uintptr_t bits(Arena* arena) { 628 assert((uintptr_t(arena) & 1) == 0, "Required for on_C_heap() to work"); 629 return uintptr_t(arena); 630 } 631 632 public: 633 // Resource allocation 634 GrowableArrayMetadata() : 635 _bits(bits()) 636 debug_only(COMMA _nesting_check(true)) { 637 } 638 639 // Arena allocation 640 GrowableArrayMetadata(Arena* arena) : 641 _bits(bits(arena)) 642 debug_only(COMMA _nesting_check(false)) { 643 } 644 645 // CHeap allocation 646 GrowableArrayMetadata(MEMFLAGS memflags) : 647 _bits(bits(memflags)) 648 debug_only(COMMA _nesting_check(false)) { 649 } 650 651 #ifdef ASSERT 652 GrowableArrayMetadata(const GrowableArrayMetadata& other) : 653 _bits(other._bits), 654 _nesting_check(other._nesting_check) { 655 assert(!on_C_heap(), "Copying of CHeap arrays not supported"); 656 assert(!other.on_C_heap(), "Copying of CHeap arrays not supported"); 657 } 658 659 GrowableArrayMetadata& operator=(const GrowableArrayMetadata& other) { 660 _bits = other._bits; 661 _nesting_check = other._nesting_check; 662 assert(!on_C_heap(), "Assignment of CHeap arrays not supported"); 663 assert(!other.on_C_heap(), "Assignment of CHeap arrays not supported"); 664 return *this; 665 } 666 667 void init_checks(const GrowableArrayBase* array) const; 668 void on_resource_area_alloc_check() const; 669 #endif // ASSERT 670 671 bool on_C_heap() const { return (_bits & 1) == 1; } 672 bool on_resource_area() const { return _bits == 0; } 673 bool on_arena() const { return (_bits & 1) == 0 && _bits != 0; } 674 675 Arena* arena() const { return (Arena*)_bits; } 676 MEMFLAGS memflags() const { return MEMFLAGS(_bits >> 1); } 677 }; 678 679 // THE GrowableArray. 680 // 681 // Supports multiple allocation strategies: 682 // - Resource stack allocation: if no extra argument is provided 683 // - CHeap allocation: if memflags is provided 684 // - Arena allocation: if an arena is provided 685 // 686 // There are some drawbacks of using GrowableArray, that are removed in some 687 // of the other implementations of GrowableArrayWithAllocator sub-classes: 688 // 689 // Memory overhead: The multiple allocation strategies uses extra metadata 690 // embedded in the instance. 691 // 692 // Strict allocation locations: There are rules about where the GrowableArray 693 // instance is allocated, that depends on where the data array is allocated. 694 // See: init_checks. 695 696 template <typename E> 697 class GrowableArray : public GrowableArrayWithAllocator<E, GrowableArray<E> > { 698 friend class GrowableArrayWithAllocator<E, GrowableArray<E> >; 699 friend class GrowableArrayTest; 700 701 static E* allocate(int max) { 702 return (E*)GrowableArrayResourceAllocator::allocate(max, sizeof(E)); 703 } 704 705 static E* allocate(int max, MEMFLAGS memflags) { 706 return (E*)GrowableArrayCHeapAllocator::allocate(max, sizeof(E), memflags); 707 } 708 709 static E* allocate(int max, Arena* arena) { 710 return (E*)GrowableArrayArenaAllocator::allocate(max, sizeof(E), arena); 711 } 712 713 GrowableArrayMetadata _metadata; 714 715 void init_checks() const { debug_only(_metadata.init_checks(this);) } 716 717 // Where are we going to allocate memory? 718 bool on_C_heap() const { return _metadata.on_C_heap(); } 719 bool on_resource_area() const { return _metadata.on_resource_area(); } 720 bool on_arena() const { return _metadata.on_arena(); } 721 722 E* allocate() { 723 if (on_resource_area()) { 724 debug_only(_metadata.on_resource_area_alloc_check()); 725 return allocate(this->_capacity); 726 } 727 728 if (on_C_heap()) { 729 return allocate(this->_capacity, _metadata.memflags()); 730 } 731 732 assert(on_arena(), "Sanity"); 733 return allocate(this->_capacity, _metadata.arena()); 734 } 735 736 void deallocate(E* mem) { 737 if (on_C_heap()) { 738 GrowableArrayCHeapAllocator::deallocate(mem); 739 } 740 } 741 742 public: 743 GrowableArray() : GrowableArray(2 /* initial_capacity */) {} 744 745 explicit GrowableArray(int initial_capacity) : 746 GrowableArrayWithAllocator<E, GrowableArray<E> >( 747 allocate(initial_capacity), 748 initial_capacity), 749 _metadata() { 750 init_checks(); 751 } 752 753 GrowableArray(int initial_capacity, MEMFLAGS memflags) : 754 GrowableArrayWithAllocator<E, GrowableArray<E> >( 755 allocate(initial_capacity, memflags), 756 initial_capacity), 757 _metadata(memflags) { 758 init_checks(); 759 } 760 761 GrowableArray(int initial_capacity, int initial_len, const E& filler) : 762 GrowableArrayWithAllocator<E, GrowableArray<E> >( 763 allocate(initial_capacity), 764 initial_capacity, initial_len, filler), 765 _metadata() { 766 init_checks(); 767 } 768 769 GrowableArray(int initial_capacity, int initial_len, const E& filler, MEMFLAGS memflags) : 770 GrowableArrayWithAllocator<E, GrowableArray<E> >( 771 allocate(initial_capacity, memflags), 772 initial_capacity, initial_len, filler), 773 _metadata(memflags) { 774 init_checks(); 775 } 776 777 GrowableArray(Arena* arena, int initial_capacity, int initial_len, const E& filler) : 778 GrowableArrayWithAllocator<E, GrowableArray<E> >( 779 allocate(initial_capacity, arena), 780 initial_capacity, initial_len, filler), 781 _metadata(arena) { 782 init_checks(); 783 } 784 785 ~GrowableArray() { 786 if (on_C_heap()) { 787 this->clear_and_deallocate(); 788 } 789 } 790 }; 791 792 // Leaner GrowableArray for CHeap backed data arrays, with compile-time decided MEMFLAGS. 793 template <typename E, MEMFLAGS F> 794 class GrowableArrayCHeap : public GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, F> > { 795 friend class GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, F> >; 796 797 STATIC_ASSERT(F != mtNone); 798 799 static E* allocate(int max, MEMFLAGS flags) { 800 if (max == 0) { 801 return nullptr; 802 } 803 804 return (E*)GrowableArrayCHeapAllocator::allocate(max, sizeof(E), flags); 805 } 806 807 NONCOPYABLE(GrowableArrayCHeap); 808 809 E* allocate() { 810 return allocate(this->_capacity, F); 811 } 812 813 void deallocate(E* mem) { 814 GrowableArrayCHeapAllocator::deallocate(mem); 815 } 816 817 public: 818 GrowableArrayCHeap(int initial_capacity = 0) : 819 GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, F> >( 820 allocate(initial_capacity, F), 821 initial_capacity) {} 822 823 GrowableArrayCHeap(int initial_capacity, int initial_len, const E& filler) : 824 GrowableArrayWithAllocator<E, GrowableArrayCHeap<E, F> >( 825 allocate(initial_capacity, F), 826 initial_capacity, initial_len, filler) {} 827 828 ~GrowableArrayCHeap() { 829 this->clear_and_deallocate(); 830 } 831 832 void* operator new(size_t size) { 833 return AnyObj::operator new(size, F); 834 } 835 836 void* operator new(size_t size, const std::nothrow_t& nothrow_constant) throw() { 837 return AnyObj::operator new(size, nothrow_constant, F); 838 } 839 void operator delete(void *p) { 840 AnyObj::operator delete(p); 841 } 842 }; 843 844 // Custom STL-style iterator to iterate over GrowableArrays 845 // It is constructed by invoking GrowableArray::begin() and GrowableArray::end() 846 template <typename E> 847 class GrowableArrayIterator : public StackObj { 848 friend class GrowableArrayView<E>; 849 template <typename F, typename UnaryPredicate> friend class GrowableArrayFilterIterator; 850 851 private: 852 const GrowableArrayView<E>* _array; // GrowableArray we iterate over 853 int _position; // The current position in the GrowableArray 854 855 // Private constructor used in GrowableArray::begin() and GrowableArray::end() 856 GrowableArrayIterator(const GrowableArrayView<E>* array, int position) : _array(array), _position(position) { 857 assert(0 <= position && position <= _array->length(), "illegal position"); 858 } 859 860 public: 861 GrowableArrayIterator() : _array(nullptr), _position(0) { } 862 GrowableArrayIterator<E>& operator++() { ++_position; return *this; } 863 E operator*() { return _array->at(_position); } 864 865 bool operator==(const GrowableArrayIterator<E>& rhs) { 866 assert(_array == rhs._array, "iterator belongs to different array"); 867 return _position == rhs._position; 868 } 869 870 bool operator!=(const GrowableArrayIterator<E>& rhs) { 871 assert(_array == rhs._array, "iterator belongs to different array"); 872 return _position != rhs._position; 873 } 874 }; 875 876 // Custom STL-style iterator to iterate over elements of a GrowableArray that satisfy a given predicate 877 template <typename E, class UnaryPredicate> 878 class GrowableArrayFilterIterator : public StackObj { 879 friend class GrowableArrayView<E>; 880 881 private: 882 const GrowableArrayView<E>* _array; // GrowableArray we iterate over 883 int _position; // Current position in the GrowableArray 884 UnaryPredicate _predicate; // Unary predicate the elements of the GrowableArray should satisfy 885 886 public: 887 GrowableArrayFilterIterator(const GrowableArrayIterator<E>& begin, UnaryPredicate filter_predicate) : 888 _array(begin._array), _position(begin._position), _predicate(filter_predicate) { 889 // Advance to first element satisfying the predicate 890 while(_position != _array->length() && !_predicate(_array->at(_position))) { 891 ++_position; 892 } 893 } 894 895 GrowableArrayFilterIterator<E, UnaryPredicate>& operator++() { 896 do { 897 // Advance to next element satisfying the predicate 898 ++_position; 899 } while(_position != _array->length() && !_predicate(_array->at(_position))); 900 return *this; 901 } 902 903 E operator*() { return _array->at(_position); } 904 905 bool operator==(const GrowableArrayIterator<E>& rhs) { 906 assert(_array == rhs._array, "iterator belongs to different array"); 907 return _position == rhs._position; 908 } 909 910 bool operator!=(const GrowableArrayIterator<E>& rhs) { 911 assert(_array == rhs._array, "iterator belongs to different array"); 912 return _position != rhs._position; 913 } 914 915 bool operator==(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs) { 916 assert(_array == rhs._array, "iterator belongs to different array"); 917 return _position == rhs._position; 918 } 919 920 bool operator!=(const GrowableArrayFilterIterator<E, UnaryPredicate>& rhs) { 921 assert(_array == rhs._array, "iterator belongs to different array"); 922 return _position != rhs._position; 923 } 924 }; 925 926 // Arrays for basic types 927 typedef GrowableArray<int> intArray; 928 typedef GrowableArray<int> intStack; 929 typedef GrowableArray<bool> boolArray; 930 931 #endif // SHARE_UTILITIES_GROWABLEARRAY_HPP