Skip to content

Commit 14557e7

Browse files
author
Afshin Zafari
committedNov 23, 2023
8314502: Change the comparator taking version of GrowableArray::find to be a template method
Reviewed-by: jsjolen, sspitsyn, stefank
1 parent 2802643 commit 14557e7

13 files changed

+123
-57
lines changed
 

‎src/hotspot/share/gc/parallel/mutableNUMASpace.cpp

+9-4
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ size_t MutableNUMASpace::free_in_words() const {
142142
return s;
143143
}
144144

145+
int MutableNUMASpace::lgrp_space_index(int lgrp_id) const {
146+
return lgrp_spaces()->find_if([&](LGRPSpace* space) {
147+
return space->lgrp_id() == checked_cast<uint>(lgrp_id);
148+
});
149+
}
145150

146151
size_t MutableNUMASpace::tlab_capacity(Thread *thr) const {
147152
guarantee(thr != nullptr, "No thread");
@@ -160,7 +165,7 @@ size_t MutableNUMASpace::tlab_capacity(Thread *thr) const {
160165
}
161166
}
162167
// That's the normal case, where we know the locality group of the thread.
163-
int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
168+
int i = lgrp_space_index(lgrp_id);
164169
if (i == -1) {
165170
return 0;
166171
}
@@ -179,7 +184,7 @@ size_t MutableNUMASpace::tlab_used(Thread *thr) const {
179184
return 0;
180185
}
181186
}
182-
int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
187+
int i = lgrp_space_index(lgrp_id);
183188
if (i == -1) {
184189
return 0;
185190
}
@@ -199,7 +204,7 @@ size_t MutableNUMASpace::unsafe_max_tlab_alloc(Thread *thr) const {
199204
return 0;
200205
}
201206
}
202-
int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
207+
int i = lgrp_space_index(lgrp_id);
203208
if (i == -1) {
204209
return 0;
205210
}
@@ -569,7 +574,7 @@ HeapWord* MutableNUMASpace::cas_allocate(size_t size) {
569574
thr->set_lgrp_id(lgrp_id);
570575
}
571576

572-
int i = lgrp_spaces()->find(&lgrp_id, LGRPSpace::equals);
577+
int i = lgrp_space_index(lgrp_id);
573578
// It is possible that a new CPU has been hotplugged and
574579
// we haven't reshaped the space accordingly.
575580
if (i == -1) {

‎src/hotspot/share/gc/parallel/mutableNUMASpace.hpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,6 @@ class MutableNUMASpace : public MutableSpace {
9393
delete _alloc_rate;
9494
}
9595

96-
static bool equals(void* lgrp_id_value, LGRPSpace* p) {
97-
return *(uint*)lgrp_id_value == p->lgrp_id();
98-
}
99-
10096
// Report a failed allocation.
10197
void set_allocation_failed() { _allocation_failed = true; }
10298

@@ -158,6 +154,8 @@ class MutableNUMASpace : public MutableSpace {
158154
void select_tails(MemRegion new_region, MemRegion intersection,
159155
MemRegion* bottom_region, MemRegion *top_region);
160156

157+
int lgrp_space_index(int lgrp_id) const;
158+
161159
public:
162160
GrowableArray<LGRPSpace*>* lgrp_spaces() const { return _lgrp_spaces; }
163161
MutableNUMASpace(size_t alignment);

‎src/hotspot/share/prims/jvmtiImpl.cpp

+3-11
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,6 @@ void GrowableCache::recache() {
119119
_listener_fun(_this_obj,_cache);
120120
}
121121

122-
bool GrowableCache::equals(void* v, GrowableElement *e2) {
123-
GrowableElement *e1 = (GrowableElement *) v;
124-
assert(e1 != nullptr, "e1 != nullptr");
125-
assert(e2 != nullptr, "e2 != nullptr");
126-
127-
return e1->equals(e2);
128-
}
129-
130122
//
131123
// class GrowableCache - public methods
132124
//
@@ -163,8 +155,8 @@ GrowableElement* GrowableCache::at(int index) {
163155
return e;
164156
}
165157

166-
int GrowableCache::find(GrowableElement* e) {
167-
return _elements->find(e, GrowableCache::equals);
158+
int GrowableCache::find(const GrowableElement* e) const {
159+
return _elements->find_if([&](const GrowableElement* other_e) { return e->equals(other_e); });
168160
}
169161

170162
// append a copy of the element to the end of the collection
@@ -216,7 +208,7 @@ void JvmtiBreakpoint::copy(JvmtiBreakpoint& bp) {
216208
_class_holder = OopHandle(JvmtiExport::jvmti_oop_storage(), bp._class_holder.resolve());
217209
}
218210

219-
bool JvmtiBreakpoint::equals(JvmtiBreakpoint& bp) {
211+
bool JvmtiBreakpoint::equals(const JvmtiBreakpoint& bp) const {
220212
return _method == bp._method
221213
&& _bci == bp._bci;
222214
}

‎src/hotspot/share/prims/jvmtiImpl.hpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ class JvmtiBreakpoints;
6666
class GrowableElement : public CHeapObj<mtInternal> {
6767
public:
6868
virtual ~GrowableElement() {}
69-
virtual address getCacheValue() =0;
70-
virtual bool equals(GrowableElement* e) =0;
71-
virtual GrowableElement *clone() =0;
69+
virtual address getCacheValue() =0;
70+
virtual bool equals(const GrowableElement* e) const =0;
71+
virtual GrowableElement* clone() =0;
7272
};
7373

7474
class GrowableCache {
@@ -88,8 +88,6 @@ class GrowableCache {
8888
// (but NOT when cached elements are recomputed).
8989
void (*_listener_fun)(void *, address*);
9090

91-
static bool equals(void *, GrowableElement *);
92-
9391
// recache all elements after size change, notify listener
9492
void recache();
9593

@@ -104,7 +102,7 @@ class GrowableCache {
104102
// get the value of the index element in the collection
105103
GrowableElement* at(int index);
106104
// find the index of the element, -1 if it doesn't exist
107-
int find(GrowableElement* e);
105+
int find(const GrowableElement* e) const;
108106
// append a copy of the element to the end of the collection, notify listener
109107
void append(GrowableElement* e);
110108
// remove the element at index, notify listener
@@ -165,7 +163,7 @@ class JvmtiBreakpoint : public GrowableElement {
165163
JvmtiBreakpoint() : _method(nullptr), _bci(0) {}
166164
JvmtiBreakpoint(Method* m_method, jlocation location);
167165
virtual ~JvmtiBreakpoint();
168-
bool equals(JvmtiBreakpoint& bp);
166+
bool equals(const JvmtiBreakpoint& bp) const;
169167
void copy(JvmtiBreakpoint& bp);
170168
address getBcp() const;
171169
void each_method_version_do(method_action meth_act);
@@ -177,7 +175,7 @@ class JvmtiBreakpoint : public GrowableElement {
177175

178176
// GrowableElement implementation
179177
address getCacheValue() { return getBcp(); }
180-
bool equals(GrowableElement* e) { return equals((JvmtiBreakpoint&) *e); }
178+
bool equals(const GrowableElement* e) const { return equals((const JvmtiBreakpoint&) *e); }
181179

182180
GrowableElement *clone() {
183181
JvmtiBreakpoint *bp = new JvmtiBreakpoint();

‎src/hotspot/share/runtime/perfData.cpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ void PerfData::create_entry(BasicType dtype, size_t dsize, size_t vlen) {
185185
PerfMemory::mark_updated();
186186
}
187187

188+
bool PerfData::name_equals(const char* name) const {
189+
return strcmp(name, this->name()) == 0;
190+
}
191+
188192
PerfLong::PerfLong(CounterNS ns, const char* namep, Units u, Variability v)
189193
: PerfData(ns, namep, u, v) {
190194

@@ -501,17 +505,9 @@ PerfDataList::~PerfDataList() {
501505

502506
}
503507

504-
bool PerfDataList::by_name(void* name, PerfData* pd) {
505-
506-
if (pd == nullptr)
507-
return false;
508-
509-
return strcmp((const char*)name, pd->name()) == 0;
510-
}
511-
512508
PerfData* PerfDataList::find_by_name(const char* name) {
513509

514-
int i = _set->find((void*)name, PerfDataList::by_name);
510+
int i = _set->find_if([&](PerfData* pd) { return pd->name_equals(name); });
515511

516512
if (i >= 0 && i <= _set->length())
517513
return _set->at(i);

‎src/hotspot/share/runtime/perfData.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ class PerfData : public CHeapObj<mtInternal> {
319319
// PerfData memory region. This redundancy is maintained for
320320
// security reasons as the PerfMemory region may be in shared
321321
// memory.
322-
const char* name() { return _name; }
322+
const char* name() const { return _name; }
323+
bool name_equals(const char* name) const;
323324

324325
// returns the variability classification associated with this item
325326
Variability variability() { return _v; }
@@ -576,7 +577,7 @@ class PerfDataList : public CHeapObj<mtInternal> {
576577
PerfDataArray* _set;
577578

578579
// method to search for a instrumentation object by name
579-
static bool by_name(void* name, PerfData* pd);
580+
static bool by_name(const char* name, PerfData* pd);
580581

581582
protected:
582583
// we expose the implementation here to facilitate the clone

‎src/hotspot/share/runtime/unhandledOops.cpp

+6-7
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,16 @@ void UnhandledOops::register_unhandled_oop(oop* op) {
7171
_oop_list->push(entry);
7272
}
7373

74-
75-
bool match_oop_entry(void *op, UnhandledOopEntry e) {
76-
return (e.oop_ptr() == op);
77-
}
78-
7974
// Mark unhandled oop as okay for GC - the containing struct has an oops_do and
8075
// for some reason the oop has to be on the stack.
8176
// May not be called for the current thread, as in the case of
8277
// VM_GetOrSetLocal in jvmti.
8378
void UnhandledOops::allow_unhandled_oop(oop* op) {
8479
assert (CheckUnhandledOops, "should only be called with checking option");
8580

86-
int i = _oop_list->find_from_end(op, match_oop_entry);
81+
int i = _oop_list->find_from_end_if([&](const UnhandledOopEntry& e) {
82+
return e.match_oop_entry(op);
83+
});
8784
assert(i!=-1, "safe for gc oop not in unhandled_oop_list");
8885

8986
UnhandledOopEntry entry = _oop_list->at(i);
@@ -105,7 +102,9 @@ void UnhandledOops::unregister_unhandled_oop(oop* op) {
105102
}
106103
_level--;
107104

108-
int i = _oop_list->find_from_end(op, match_oop_entry);
105+
int i = _oop_list->find_from_end_if([&](const UnhandledOopEntry& e) {
106+
return e.match_oop_entry(op);
107+
});
109108
assert(i!=-1, "oop not in unhandled_oop_list");
110109
_oop_list->remove_at(i);
111110
}

‎src/hotspot/share/runtime/unhandledOops.hpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,17 @@ class UnhandledOopEntry : public CHeapObj<mtThread> {
5353
private:
5454
oop* _oop_ptr;
5555
bool _ok_for_gc;
56+
57+
bool match_oop_entry(oop* op) const {
58+
return _oop_ptr == op;
59+
}
60+
5661
public:
57-
oop* oop_ptr() { return _oop_ptr; }
5862
UnhandledOopEntry() : _oop_ptr(nullptr), _ok_for_gc(false) {}
5963
UnhandledOopEntry(oop* op) :
6064
_oop_ptr(op), _ok_for_gc(false) {}
6165
};
6266

63-
6467
class UnhandledOops : public CHeapObj<mtThread> {
6568
friend class Thread;
6669
private:

‎src/hotspot/share/services/diagnosticFramework.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,8 @@ bool DCmdArgIter::next(TRAPS) {
144144
return _key_len != 0;
145145
}
146146

147-
bool DCmdInfo::by_name(void* cmd_name, DCmdInfo* info) {
148-
if (info == nullptr) return false;
149-
return strcmp((const char*)cmd_name, info->name()) == 0;
147+
bool DCmdInfo::name_equals(const char* name) const {
148+
return strcmp(name, this->name()) == 0;
150149
}
151150

152151
void DCmdParser::add_dcmd_option(GenDCmdArgument* arg) {

‎src/hotspot/share/services/diagnosticFramework.hpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,12 @@ class DCmdInfo : public ResourceObj {
140140
: _name(name), _description(description), _impact(impact), _permission(permission),
141141
_num_arguments(num_arguments), _is_enabled(enabled) {}
142142
const char* name() const { return _name; }
143+
bool name_equals(const char* cmd_name) const;
143144
const char* description() const { return _description; }
144145
const char* impact() const { return _impact; }
145146
const JavaPermission& permission() const { return _permission; }
146147
int num_arguments() const { return _num_arguments; }
147148
bool is_enabled() const { return _is_enabled; }
148-
149-
static bool by_name(void* name, DCmdInfo* info);
150149
};
151150

152151
// A DCmdArgumentInfo instance provides a description of a diagnostic command

‎src/hotspot/share/services/management.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -2004,7 +2004,9 @@ JVM_ENTRY(void, jmm_GetDiagnosticCommandInfo(JNIEnv *env, jobjectArray cmds,
20042004
THROW_MSG(vmSymbols::java_lang_NullPointerException(),
20052005
"Command name cannot be null.");
20062006
}
2007-
int pos = info_list->find((void*)cmd_name,DCmdInfo::by_name);
2007+
int pos = info_list->find_if([&](DCmdInfo* info) {
2008+
return info->name_equals(cmd_name);
2009+
});
20082010
if (pos == -1) {
20092011
THROW_MSG(vmSymbols::java_lang_IllegalArgumentException(),
20102012
"Unknown diagnostic command");

‎src/hotspot/share/utilities/growableArray.hpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,29 @@ class GrowableArrayView : public GrowableArrayBase {
209209
return -1;
210210
}
211211

212-
int find(void* token, bool f(void*, E)) const {
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 {
213219
for (int i = 0; i < _len; i++) {
214-
if (f(token, _data[i])) return i;
220+
if (predicate(_data[i])) return i;
215221
}
216222
return -1;
217223
}
218224

219-
int find_from_end(void* token, bool f(void*, E)) const {
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 {
220232
// start at the end of the array
221233
for (int i = _len-1; i >= 0; i--) {
222-
if (f(token, _data[i])) return i;
234+
if (predicate(_data[i])) return i;
223235
}
224236
return -1;
225237
}

‎test/hotspot/gtest/utilities/test_growableArray.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -601,3 +601,65 @@ TEST(GrowableArrayCHeap, sanity) {
601601
delete a;
602602
}
603603
}
604+
605+
TEST(GrowableArrayCHeap, find_if) {
606+
struct Element {
607+
int value;
608+
};
609+
GrowableArrayCHeap<Element, mtTest> array;
610+
array.push({1});
611+
array.push({2});
612+
array.push({3});
613+
614+
{
615+
int index = array.find_if([&](const Element& elem) {
616+
return elem.value == 1;
617+
});
618+
ASSERT_EQ(index, 0);
619+
}
620+
621+
{
622+
int index = array.find_if([&](const Element& elem) {
623+
return elem.value > 1;
624+
});
625+
ASSERT_EQ(index, 1);
626+
}
627+
628+
{
629+
int index = array.find_if([&](const Element& elem) {
630+
return elem.value == 4;
631+
});
632+
ASSERT_EQ(index, -1);
633+
}
634+
}
635+
636+
TEST(GrowableArrayCHeap, find_from_end_if) {
637+
struct Element {
638+
int value;
639+
};
640+
GrowableArrayCHeap<Element, mtTest> array;
641+
array.push({1});
642+
array.push({2});
643+
array.push({3});
644+
645+
{
646+
int index = array.find_from_end_if([&](const Element& elem) {
647+
return elem.value == 1;
648+
});
649+
ASSERT_EQ(index, 0);
650+
}
651+
652+
{
653+
int index = array.find_from_end_if([&](const Element& elem) {
654+
return elem.value > 1;
655+
});
656+
ASSERT_EQ(index, 2);
657+
}
658+
659+
{
660+
int index = array.find_from_end_if([&](const Element& elem) {
661+
return elem.value == 4;
662+
});
663+
ASSERT_EQ(index, -1);
664+
}
665+
}

0 commit comments

Comments
 (0)
Please sign in to comment.