Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8271506: Add ResourceHashtable support for deleting selected entries #4938

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/hotspot/share/utilities/resourceHash.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ class ResourceHashtableBase : public STORAGE {
private:
int _number_of_entries;

Node** bucket_at(unsigned index) const {
Node** bucket_at(unsigned index) {
Node** t = table();
return &t[index];
}

const Node* const* bucket_at(unsigned index) const {
Node** t = table();
return &t[index];
}
Expand Down Expand Up @@ -210,6 +215,32 @@ class ResourceHashtableBase : public STORAGE {
++bucket;
}
}

// ITER contains bool do_entry(K const&, V const&), which will be
// called for each entry in the table. If do_entry() returns true,
// the entry is deleted.
template<class ITER>
void unlink(ITER* iter) {
const unsigned sz = table_size();
for (unsigned index = 0; index < sz; index++) {
Node** ptr = bucket_at(index);
while (*ptr != NULL) {
Node* node = *ptr;
// do_entry must clean up the key and value in Node.
bool clean = iter->do_entry(node->_key, node->_value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I miss it, you don't enforce the constness here, no? So, nothing prevents the called hook from modifying key or value. You may do what Ioi did in #4942 and const-cast the parameters for the call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I answered this in email but I don't see it. I want the do_entry function to modify the arguments, like calling their destructor or free operators.

if (clean) {
*ptr = node->_next;
if (ALLOC_TYPE == ResourceObj::C_HEAP) {
delete node;
}
_number_of_entries --;
} else {
ptr = &(node->_next);
}
}
}
}

};

template<unsigned TABLE_SIZE, typename K, typename V>
Expand Down
24 changes: 24 additions & 0 deletions test/hotspot/gtest/utilities/test_resourceHash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ class CommonResourceHashtableTest : public ::testing::Test {
}
};

class DeleterTestIter {
int _val;
public:
DeleterTestIter(int i) : _val(i) {}

bool do_entry(K const& k, V const& v) {
if ((uintptr_t) k == (uintptr_t) _val) {
// Delete me!
return true;
} else {
return false; // continue iteration
}
}
};

};

class SmallResourceHashtableTest : public CommonResourceHashtableTest {
Expand Down Expand Up @@ -233,6 +248,15 @@ class GenericResourceHashtableTest : public CommonResourceHashtableTest {
ASSERT_FALSE(rh.remove(as_K(index)));
}
rh.iterate(&et);

// Add more entries in and then delete one.
for (uintptr_t i = 10; i > 0; --i) {
uintptr_t index = i - 1;
ASSERT_TRUE(rh.put(as_K(index), index));
}
DeleterTestIter dt(5);
rh.unlink(&dt);
ASSERT_FALSE(rh.get(as_K(5)));
}
};
};
Expand Down