Skip to content

Commit 198c2ae

Browse files
author
Dwight Guth
committed
slightly tweak implementation of empty collection nodes
This change removes the `const` qualifier from the static objects containing empty rrbtree and champ nodes, and also modifies the methods slightly so that they return a reference. This change makes it possible for these types to be used as part of a garbage-collected runtime which makes use of a copying collector, because if the user specifies a heap_poicy which allocates these nodes into a heap that needs to be relocated during garbage collection, the garbage collector will need to update the static pointer inside these methods in order to correctly relocate these nodes. This change removes the `const` qualifier from the static objects containing empty rrbtrees and champ
1 parent 2eea220 commit 198c2ae

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

immer/detail/hamts/champ.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ struct champ
3434
node_t* root;
3535
size_t size;
3636

37-
static node_t* empty()
37+
static node_t*& empty()
3838
{
39-
static const auto node = node_t::make_inner_n(0);
40-
return node->inc();
39+
static auto node = node_t::make_inner_n(0);
40+
node->inc();
41+
return node;
4142
}
4243

4344
champ(node_t* r, size_t sz = 0)

immer/detail/rbts/rrbtree.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,18 @@ struct rrbtree
4747
ipow((size_t{1} << B) - 2, (S - BL) / B);
4848
}
4949

50-
static node_t* empty_root()
50+
static node_t*& empty_root()
5151
{
52-
static const auto empty_ = node_t::make_inner_n(0u);
53-
return empty_->inc();
52+
static auto empty_ = node_t::make_inner_n(0u);
53+
empty_->inc();
54+
return empty_;
5455
}
5556

56-
static node_t* empty_tail()
57+
static node_t*& empty_tail()
5758
{
58-
static const auto empty_ = node_t::make_leaf_n(0u);
59-
return empty_->inc();
59+
static auto empty_ = node_t::make_leaf_n(0u);
60+
empty_->inc();
61+
return empty_;
6062
}
6163

6264
template <typename U>

0 commit comments

Comments
 (0)