Implement STL iterators on ObjectArray and add helpers
Iterating over an ObjectArray is rather cumbersome, requiring manual
for-loops. To improve ergonomics and STL standard-ness this implements
std::iterator's for ObjectArray and converts code to use this (in
simple situations). This should allow us to use standard STL functions
in the future when dealing with ObjectArrays.
Also adds some helpers such as ZipCount and ZipLeft.
Test: ./test.py --host
Change-Id: Ifd515b8321775424b3256a6faf47b2ba970177d3
diff --git a/openjdkjvmti/ti_heap.cc b/openjdkjvmti/ti_heap.cc
index 702a1c4..1d18390 100644
--- a/openjdkjvmti/ti_heap.cc
+++ b/openjdkjvmti/ti_heap.cc
@@ -1166,16 +1166,14 @@
if (array->IsObjectArray()) {
art::ObjPtr<art::mirror::ObjectArray<art::mirror::Object>> obj_array =
array->AsObjectArray<art::mirror::Object>();
- int32_t length = obj_array->GetLength();
- for (int32_t i = 0; i != length; ++i) {
- art::ObjPtr<art::mirror::Object> elem = obj_array->GetWithoutChecks(i);
- if (elem != nullptr) {
+ for (auto elem_pair : art::ZipCount(obj_array->Iterate())) {
+ if (elem_pair.first != nullptr) {
jvmtiHeapReferenceInfo reference_info;
- reference_info.array.index = i;
+ reference_info.array.index = elem_pair.second;
stop_reports_ = !ReportReferenceMaybeEnqueue(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT,
&reference_info,
array,
- elem.Ptr());
+ elem_pair.first.Ptr());
if (stop_reports_) {
break;
}