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(alignof(SHAMapHash) >= 4, "Bad alignment: Tag pointer requires low two bits to be zero.");
48 static constexpr std::uintptr_t tagMask = 3;
50 static constexpr std::uintptr_t ptrMask = ~tagMask;
51
53 void
55
57 {
58 };
73
74public:
75 TaggedPointer() = delete;
76 explicit TaggedPointer(std::uint8_t numChildren);
77
92 explicit TaggedPointer(TaggedPointer&& other, std::uint16_t isBranch, std::uint8_t toAllocate);
93
116 TaggedPointer&& other,
117 std::uint16_t srcBranches,
118 std::uint16_t dstBranches,
119 std::uint8_t toAllocate);
120
121 TaggedPointer(TaggedPointer const&) = delete;
122
124
127
129
132 decode() const;
133
135 [[nodiscard]] std::uint8_t
136 capacity() const;
137
143 [[nodiscard]] bool
144 isDense() const;
145
151
153 [[nodiscard]] SHAMapHash*
154 getHashes() const;
155
158 getChildren() const;
159
168 template <class F>
169 void
170 iterChildren(std::uint16_t isBranch, F&& f) const;
171
181 template <class F>
182 void
184
194 getChildIndex(std::uint16_t isBranch, int i) const;
195};
196
197[[nodiscard]] inline int
199{
200#if __cpp_lib_bitops
201 return std::popcount(a);
202#elif defined(__clang__) || defined(__GNUC__)
203 return __builtin_popcount(a);
204#else
205 // fallback to table lookup
206 static auto constexpr const tbl = []() {
208 for (int i = 0; i != 256; ++i)
209 {
210 for (int j = 0; j != 8; ++j)
211 {
212 if (i & (1 << j))
213 ret[i]++;
214 }
215 }
216 return ret;
217 }();
218 return tbl[a & 0xff] + tbl[a >> 8];
219#endif
220}
221
222} // 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)