Skip to content

Commit 21a1bfa

Browse files
committed
Add allocators
1 parent 76e79db commit 21a1bfa

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/hotspot/share/utilities/rbTree.hpp

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#define SHARE_UTILITIES_RBTREE_HPP
2727

2828
#include "cppstdlib/type_traits.hpp"
29+
#include "memory/allocation.hpp"
30+
#include "memory/arena.hpp"
31+
#include "memory/resourceArea.hpp"
2932
#include "metaprogramming/enableIf.hpp"
3033
#include "nmt/memTag.hpp"
3134
#include "runtime/os.hpp"
@@ -458,7 +461,8 @@ class RBTree : public AbstractRBTree<K, RBNode<K, V>, COMPARATOR> {
458461
ALLOCATOR _allocator;
459462

460463
public:
461-
RBTree() : BaseType(), _allocator() {}
464+
template<typename... AllocArgs>
465+
RBTree(AllocArgs... alloc_args) : BaseType(), _allocator(alloc_args...) {}
462466
NONCOPYABLE(RBTree);
463467
~RBTree() { remove_all(); }
464468

@@ -580,9 +584,40 @@ class RBTreeCHeapAllocator {
580584
void free(void* ptr) { os::free(ptr); }
581585
};
582586

587+
template <AllocFailType strategy>
588+
class RBTreeArenaAllocator {
589+
Arena* _arena;
590+
public:
591+
RBTreeArenaAllocator(Arena* arena) : _arena(arena) {}
592+
593+
void* allocate(size_t sz) {
594+
return _arena->Amalloc(sz, strategy);
595+
}
596+
void free(void* ptr) { /* NOP */ }
597+
};
598+
599+
template <AllocFailType strategy>
600+
class RBTreeResourceAreaAllocator {
601+
ResourceArea* _rarea;
602+
public:
603+
RBTreeResourceAreaAllocator(ResourceArea* rarea) : _rarea(rarea) {}
604+
void* allocate(size_t sz) {
605+
return _rarea->Amalloc(sz, strategy);
606+
}
607+
void free(void* ptr) { /* NOP */ }
608+
};
609+
610+
611+
583612
template <typename K, typename V, typename COMPARATOR, MemTag mem_tag, AllocFailType strategy = AllocFailStrategy::EXIT_OOM>
584613
using RBTreeCHeap = RBTree<K, V, COMPARATOR, RBTreeCHeapAllocator<mem_tag, strategy>>;
585614

615+
template <typename K, typename V, typename COMPARATOR, AllocFailType strategy = AllocFailStrategy::EXIT_OOM>
616+
using RBTreeArena = RBTree<K, V, COMPARATOR, RBTreeArenaAllocator<strategy>>;
617+
618+
template <typename K, typename V, typename COMPARATOR, AllocFailType strategy = AllocFailStrategy::EXIT_OOM>
619+
using RBTreeResourceArea = RBTree<K, V, COMPARATOR, RBTreeResourceAreaAllocator<strategy>>;
620+
586621
template <typename K, typename COMPARATOR>
587622
using IntrusiveRBTree = AbstractRBTree<K, IntrusiveRBNode, COMPARATOR>;
588623

test/hotspot/gtest/utilities/test_rbtree.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,3 +1252,17 @@ TEST_VM_F(RBTreeTest, AllocatorMayReturnNull) {
12521252
EXPECT_EQ(false, success);
12531253
// The test didn't exit the VM, so it was succesful.
12541254
}
1255+
1256+
TEST_VM_F(RBTreeTest, ArenaAllocator) {
1257+
Arena arena(mtTest);
1258+
RBTreeArena<int, int, Cmp> rbtree(&arena);
1259+
bool success = rbtree.upsert(5, 5);
1260+
ASSERT_EQ(true, success);
1261+
}
1262+
1263+
TEST_VM_F(RBTreeTest, ResourceAreaAllocator) {
1264+
ResourceArea area(mtTest);
1265+
RBTreeResourceArea<int, int, Cmp> rbtree(&area);
1266+
bool success = rbtree.upsert(5, 5);
1267+
ASSERT_EQ(true, success);
1268+
}

0 commit comments

Comments
 (0)