Skip to content

Commit

Permalink
slightly tweak implementation of empty collection nodes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Dwight Guth committed Oct 15, 2021
1 parent 2eea220 commit 198c2ae
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 4 additions & 3 deletions immer/detail/hamts/champ.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ struct champ
node_t* root;
size_t size;

static node_t* empty()
static node_t*& empty()
{
static const auto node = node_t::make_inner_n(0);
return node->inc();
static auto node = node_t::make_inner_n(0);
node->inc();
return node;
}

champ(node_t* r, size_t sz = 0)
Expand Down
14 changes: 8 additions & 6 deletions immer/detail/rbts/rrbtree.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@ struct rrbtree
ipow((size_t{1} << B) - 2, (S - BL) / B);
}

static node_t* empty_root()
static node_t*& empty_root()
{
static const auto empty_ = node_t::make_inner_n(0u);
return empty_->inc();
static auto empty_ = node_t::make_inner_n(0u);
empty_->inc();
return empty_;
}

static node_t* empty_tail()
static node_t*& empty_tail()
{
static const auto empty_ = node_t::make_leaf_n(0u);
return empty_->inc();
static auto empty_ = node_t::make_leaf_n(0u);
empty_->inc();
return empty_;
}

template <typename U>
Expand Down

0 comments on commit 198c2ae

Please sign in to comment.