Skip to content

Commit 17a408c

Browse files
committed
8348169: Destruct values on free in Treap
Reviewed-by: cnorrbin, gziemski
1 parent 25bb698 commit 17a408c

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/hotspot/share/nmt/nmtTreap.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
3131
#include "utilities/growableArray.hpp"
3232
#include "utilities/macros.hpp"
3333
#include "utilities/powerOfTwo.hpp"
34+
#include <type_traits>
3435

3536
// A Treap is a self-balanced binary tree where each node is equipped with a
3637
// priority. It adds the invariant that the priority of a parent P is strictly larger
@@ -228,7 +229,9 @@ class Treap {
228229
: _allocator(),
229230
_root(nullptr),
230231
_prng_seed(_initial_seed),
231-
_node_count(0) {}
232+
_node_count(0) {
233+
static_assert(std::is_trivially_destructible<K>::value, "must be");
234+
}
232235

233236
~Treap() {
234237
this->remove_all();
@@ -266,6 +269,7 @@ class Treap {
266269
if (second_split.right != nullptr) {
267270
// The key k existed, we delete it.
268271
_node_count--;
272+
second_split.right->_value.~V();
269273
_allocator.free(second_split.right);
270274
}
271275
// Merge together everything
@@ -283,6 +287,7 @@ class Treap {
283287
if (head == nullptr) continue;
284288
to_delete.push(head->_left);
285289
to_delete.push(head->_right);
290+
head->_value.~V();
286291
_allocator.free(head);
287292
}
288293
_root = nullptr;

test/hotspot/gtest/nmt/test_nmt_treap.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,5 +325,28 @@ TEST_VM_F(NMTTreapTest, VerifyItThroughStressTest) {
325325
verify_it(treap);
326326
}
327327
}
328+
struct NTD {
329+
static bool has_run_destructor;
330+
~NTD() {
331+
has_run_destructor = true;
332+
}
333+
};
334+
335+
bool NTD::has_run_destructor = false;
336+
337+
TEST_VM_F(NMTTreapTest, ValueDestructorsAreRun) {
338+
TreapCHeap<int, NTD, Cmp> treap;
339+
NTD ntd;
340+
treap.upsert(0, ntd);
341+
treap.remove(0);
342+
EXPECT_TRUE(NTD::has_run_destructor);
343+
NTD::has_run_destructor = false;
344+
{
345+
TreapCHeap<int, NTD, Cmp> treap;
346+
NTD ntd;
347+
treap.upsert(0, ntd);
348+
}
349+
EXPECT_TRUE(NTD::has_run_destructor);
350+
}
328351

329352
#endif // ASSERT

0 commit comments

Comments
 (0)