12template<
typename Next,
typename Owner>
class ChainLink {
18 Owner *
owner()
const {
return reinterpret_cast<Owner *
>(
reinterpret_cast<char *
>(this->
ptr_) -
OWNER_TAG); }
21 static_assert(
alignof(Next) >
OWNER_TAG,
"a node address must leave the owner tag bit free");
25 static_assert(
alignof(Next) >
OWNER_TAG,
"a node address must leave the owner tag bit free");
32 static_assert(
alignof(Owner) >
OWNER_TAG,
"the owner tag needs a free low bit");
33 char *base =
reinterpret_cast<char *
>(
owner);
36 return reinterpret_cast<Next *
>(base +
OWNER_TAG);
40 if (
next !=
nullptr) {
54template<
typename Node,
typename Owner>
class ChainNode {
62 Node *node =
static_cast<Node *
>(
this);
63 while (!node->link_.is_owner())
64 node = node->link_.next();
One pointer per node that holds the next node of a chain, or on the last node the chain's owner.
Next * next() const
The next node, or nullptr on the last one.
static constexpr uintptr_t OWNER_TAG
void set_owner(Owner *owner)
Makes this the last node.
Owner * owner() const
Only valid when is_owner().
void set(Next *next, Owner *owner)
Link to next, or to owner when next is nullptr.
static Next * tagged(Owner *owner)
The value set_owner() stores, for writing through next_slot(). The owner is never null.
void set_next(Next *next)
Next ** next_slot()
Where the next node goes, for appending without a branch; only valid when !is_owner().
Base for a node of a ChainLink chain: holds the link and finds the chain's owner.
ChainLink< Node, Owner > link_
The next node, or the owner on the last node.
Node * chain_last_()
Only for a chain that ends in its owner, never in nullptr.
Owner * chain_owner_()
Walks to the end of the chain on every call.