Skip to content
Permalink
Browse files
8271506: Add ResourceHashtable support for deleting selected entries
Reviewed-by: iklam, stuefe
  • Loading branch information
coleenp committed Aug 3, 2021
1 parent bdb50ca commit f15d6cbcaf191b9718ab51b1e8b72938c0abdd6b
Showing with 56 additions and 1 deletion.
  1. +32 −1 src/hotspot/share/utilities/resourceHash.hpp
  2. +24 −0 test/hotspot/gtest/utilities/test_resourceHash.cpp
@@ -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];
}
@@ -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);
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>
@@ -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 {
@@ -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)));
}
};
};

1 comment on commit f15d6cb

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on f15d6cb Aug 3, 2021

Please sign in to comment.