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