rippled
Loading...
Searching...
No Matches
TaggedPointer.h
1#pragma once
2
3#include <xrpl/basics/IntrusivePointer.h>
4#include <xrpl/shamap/SHAMapTreeNode.h>
5
6#include <array>
7#include <cstdint>
8#include <optional>
9
10namespace xrpl {
11
40{
41private:
42 static_assert(
43 alignof(SHAMapHash) >= 4,
44 "Bad alignment: Tag pointer requires low two bits to be zero.");
50 static constexpr std::uintptr_t tagMask = 3;
52 static constexpr std::uintptr_t ptrMask = ~tagMask;
53
55 void
57
59 {
60 };
75
76public:
77 TaggedPointer() = delete;
78 explicit TaggedPointer(std::uint8_t numChildren);
79
94 explicit TaggedPointer(TaggedPointer&& other, std::uint16_t isBranch, std::uint8_t toAllocate);
95
118 TaggedPointer&& other,
119 std::uint16_t srcBranches,
120 std::uint16_t dstBranches,
121 std::uint8_t toAllocate);
122
123 TaggedPointer(TaggedPointer const&) = delete;
124
126
129
131
134 decode() const;
135
137 [[nodiscard]] std::uint8_t
138 capacity() const;
139
145 [[nodiscard]] bool
146 isDense() const;
147
153
155 [[nodiscard]] SHAMapHash*
156 getHashes() const;
157
160 getChildren() const;
161
170 template <class F>
171 void
172 iterChildren(std::uint16_t isBranch, F&& f) const;
173
183 template <class F>
184 void
186
196 getChildIndex(std::uint16_t isBranch, int i) const;
197};
198
199[[nodiscard]] inline int
201{
202#if __cpp_lib_bitops
203 return std::popcount(a);
204#elif defined(__clang__) || defined(__GNUC__)
205 return __builtin_popcount(a);
206#else
207 // fallback to table lookup
208 static auto constexpr const tbl = []() {
210 for (int i = 0; i != 256; ++i)
211 {
212 for (int j = 0; j != 8; ++j)
213 {
214 if (i & (1 << j))
215 ret[i]++;
216 }
217 }
218 return ret;
219 }();
220 return tbl[a & 0xff] + tbl[a >> 8];
221#endif
222}
223
224} // namespace xrpl
A shared intrusive pointer class that supports weak pointers.
TaggedPointer is a combination of a pointer and a mask stored in the lowest two bits.
std::optional< int > getChildIndex(std::uint16_t isBranch, int i) const
Get the child's index inside the hashes or children array (which may or may not be sparse).
void iterChildren(std::uint16_t isBranch, F &&f) const
Call the f callback for all 16 (branchFactor) branches - even if the branch is empty.
TaggedPointer(std::uint8_t numChildren)
std::uint8_t capacity() const
Get the number of elements allocated for each array.
std::tuple< std::uint8_t, SHAMapHash *, intr_ptr::SharedPtr< SHAMapTreeNode > * > getHashesAndChildren() const
Get the number of elements in each array and a pointer to the start of each array.
void destroyHashesAndChildren()
Deallocate memory and run destructors.
TaggedPointer & operator=(TaggedPointer &&)
SHAMapHash * getHashes() const
Get the hashes array.
static constexpr std::uintptr_t ptrMask
bit-and with this mask to get the pointer bits (mask out the tag)
std::pair< std::uint8_t, void * > decode() const
Decode the tagged pointer into its tag and pointer.
TaggedPointer(TaggedPointer &&)
TaggedPointer(TaggedPointer &&other, std::uint16_t srcBranches, std::uint16_t dstBranches, std::uint8_t toAllocate)
Given other with the specified children in srcBranches, create a new TaggedPointer with the allocated...
intr_ptr::SharedPtr< SHAMapTreeNode > * getChildren() const
Get the children array.
bool isDense() const
Check if the arrays have a dense format.
std::uintptr_t tp_
Upper bits are the pointer, lowest two bits are the tag A moved-from object will have a tp_ of zero.
TaggedPointer(TaggedPointer const &)=delete
TaggedPointer(RawAllocateTag, std::uint8_t numChildren)
This constructor allocates space for the hashes and children, but does not run constructors.
TaggedPointer(TaggedPointer &&other, std::uint16_t isBranch, std::uint8_t toAllocate)
Constructor is used change the number of allocated children.
static constexpr std::uintptr_t tagMask
bit-and with this mask to get the tag bits (lowest two bits)
void iterNonEmptyChildIndexes(std::uint16_t isBranch, F &&f) const
Call the f callback for all non-empty branches.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
int popcnt16(std::uint16_t a)
T popcount(T... args)