Skip to content

Commit

Permalink
8271506: Add ResourceHashtable support for deleting selected entries
Browse files Browse the repository at this point in the history
Reviewed-by: mbaesken
Backport-of: 0b51fe2
  • Loading branch information
GoeLin committed Jan 4, 2023
1 parent d2c8326 commit d8450ef
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/hotspot/share/utilities/resourceHash.hpp
Expand Up @@ -173,6 +173,32 @@ class ResourceHashtable : public ResourceObj {
static size_t node_size() {
return sizeof(Node);
}

// 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) {
Node** bucket = const_cast<Node**>(_table);
while (bucket < &_table[SIZE]) {
Node** ptr = bucket;
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;
}
} else {
ptr = &(node->_next);
}
}
++bucket;
}
}

};


Expand Down
25 changes: 25 additions & 0 deletions test/hotspot/gtest/utilities/test_resourceHash.cpp
Expand Up @@ -58,6 +58,22 @@ 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 @@ -194,6 +210,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

1 comment on commit d8450ef

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.