From 1cc86a2c31757a4cd87f483ff58797131798f046 Mon Sep 17 00:00:00 2001 From: Dwight Guth Date: Fri, 15 Oct 2021 13:59:09 -0500 Subject: [PATCH] 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. --- immer/detail/hamts/champ.hpp | 7 ++++--- immer/detail/rbts/rrbtree.hpp | 14 ++++++++------ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/immer/detail/hamts/champ.hpp b/immer/detail/hamts/champ.hpp index 77cf7e47..30075d6b 100644 --- a/immer/detail/hamts/champ.hpp +++ b/immer/detail/hamts/champ.hpp @@ -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) diff --git a/immer/detail/rbts/rrbtree.hpp b/immer/detail/rbts/rrbtree.hpp index 2f7c77c7..4075e0c9 100644 --- a/immer/detail/rbts/rrbtree.hpp +++ b/immer/detail/rbts/rrbtree.hpp @@ -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