Calling C++ destructor directly in resourceHash.hpp
Ioi Lam
ioi.lam at oracle.com
Mon Jun 12 04:19:49 UTC 2017
Yet another "you can do that in C++??" moment.
I am looking at these two functions in "utilities/resourceHash.hpp":
bool put(K const& key, V const& value) {
unsigned hv = HASH(key);
Node** ptr = lookup_node(hv, key);
if (*ptr != NULL) {
(*ptr)->_value = value; // <----------- point (a)
return false;
} else {
*ptr = new (ALLOC_TYPE, MEM_TYPE) Node(hv, key, value);
return true;
}
}
bool remove(K const& key) {
unsigned hv = HASH(key);
Node** ptr = lookup_node(hv, key);
Node* node = *ptr;
if (node != NULL) {
*ptr = node->_next;
node->_value.~V(); // <------------ point (b): add this??
if (ALLOC_TYPE == C_HEAP) {
delete node;
}
return true;
}
return false;
}
At point (a), the assignment could invoke a copying constructor of
V::V(const &V), which could do complex allocation.
Shouldn't we add a corresponding destructor call at point (b), so
V::~V() can be called to perform the appropriate deallocation?
Similarly, in ~ResourceHashtable(), should we also add the V::~V() calls?
... and also call V::~V() at point (a) to destroy the old value?
Thanks
- Ioi
More information about the hotspot-dev
mailing list