xrpld
Loading...
Searching...
No Matches
TaggedPointer.h
1#pragma once
2
3#include <xrpl/basics/SHAMapHash.h>
4#include <xrpl/shamap/SHAMapTreeNode.h>
5
6#include <bit>
7#include <cstdint>
8#include <optional>
9#include <tuple>
10#include <utility>
11#include <version> // IWYU pragma: keep
12
13namespace xrpl {
14
44{
45private:
46 static_assert(
47 alignof(SHAMapHash) >= 4,
48 "Bad alignment: Tag pointer requires low two bits to be zero.");
57 static constexpr std::uintptr_t kTagMask = 3;
61 static constexpr std::uintptr_t kPtrMask = ~kTagMask;
62
66 void
68
70 {
71 };
72
87
88public:
89 TaggedPointer() = delete;
90 explicit TaggedPointer(std::uint8_t numChildren);
91
107 explicit TaggedPointer(TaggedPointer&& other, std::uint16_t isBranch, std::uint8_t toAllocate);
108
132 TaggedPointer&& other,
133 std::uint16_t srcBranches,
134 std::uint16_t dstBranches,
135 std::uint8_t toAllocate);
136
137 TaggedPointer(TaggedPointer const&) = delete;
138
140
143
145
150 decode() const;
151
155 [[nodiscard]] std::uint8_t
156 capacity() const;
157
164 [[nodiscard]] bool
165 isDense() const;
166
173
177 [[nodiscard]] SHAMapHash*
178 getHashes() const;
179
183 [[nodiscard]] SHAMapTreeNodePtr*
184 getChildren() const;
185
195 template <class F>
196 void
197 iterChildren(std::uint16_t isBranch, F&& f) const;
198
209 template <class F>
210 void
212
222 [[nodiscard]] std::optional<int>
223 getChildIndex(std::uint16_t isBranch, int i) const;
224};
225
226[[nodiscard]] inline int
228{
229#if __cpp_lib_bitops
230 return std::popcount(a);
231#elif defined(__clang__) || defined(__GNUC__)
232 return __builtin_popcount(a);
233#else
234 // fallback to table lookup
235 static constexpr auto tbl = []() {
237 for (int i = 0; i != 256; ++i)
238 {
239 for (int j = 0; j != 8; ++j)
240 {
241 if (i & (1 << j))
242 ret[i]++;
243 }
244 }
245 return ret;
246 }();
247 return tbl[a & 0xff] + tbl[a >> 8];
248#endif
249}
250
251} // namespace xrpl
static constexpr std::uintptr_t kTagMask
bit-and with this mask to get the tag bits (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 *, SHAMapTreeNodePtr * > 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.
std::pair< std::uint8_t, void * > decode() const
Decode the tagged pointer into its tag and pointer.
SHAMapTreeNodePtr * getChildren() const
Get the children array.
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...
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.
void iterNonEmptyChildIndexes(std::uint16_t isBranch, F &&f) const
Call the f callback for all non-empty branches.
static constexpr std::uintptr_t kPtrMask
bit-and with this mask to get the pointer bits (mask out the tag)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
intr_ptr::SharedPtr< SHAMapTreeNode > SHAMapTreeNodePtr
int popcnt16(std::uint16_t a)
T popcount(T... args)