rippled
TaggedPointer.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2020 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef RIPPLE_SHAMAP_TAGGEDPOINTER_H_INCLUDED
21 #define RIPPLE_SHAMAP_TAGGEDPOINTER_H_INCLUDED
22 
23 #include <ripple/shamap/SHAMapTreeNode.h>
24 
25 #include <cstdint>
26 #include <optional>
27 
28 namespace ripple {
29 
58 {
59  static_assert(
60  alignof(SHAMapHash) >= 4,
61  "Bad alignment: Tag pointer requires low two bits to be zero.");
67  static constexpr std::uintptr_t tagMask = 3;
69  static constexpr std::uintptr_t ptrMask = ~tagMask;
70 
72  void
74 
76  {
77  };
91  explicit TaggedPointer(RawAllocateTag, std::uint8_t numChildren);
92 
93 public:
94  TaggedPointer() = delete;
95  explicit TaggedPointer(std::uint8_t numChildren);
96 
111  explicit TaggedPointer(
112  TaggedPointer&& other,
113  std::uint16_t isBranch,
114  std::uint8_t toAllocate);
115 
137  explicit TaggedPointer(
138  TaggedPointer&& other,
139  std::uint16_t srcBranches,
140  std::uint16_t dstBranches,
141  std::uint8_t toAllocate);
142 
143  TaggedPointer(TaggedPointer const&) = delete;
144 
146 
149 
150  ~TaggedPointer();
151 
153  [[nodiscard]] std::pair<std::uint8_t, void*>
154  decode() const;
155 
157  [[nodiscard]] std::uint8_t
158  capacity() const;
159 
165  [[nodiscard]] bool
166  isDense() const;
167 
171  [[nodiscard]] std::
172  tuple<std::uint8_t, SHAMapHash*, std::shared_ptr<SHAMapTreeNode>*>
173  getHashesAndChildren() const;
174 
176  [[nodiscard]] SHAMapHash*
177  getHashes() const;
178 
180  [[nodiscard]] std::shared_ptr<SHAMapTreeNode>*
181  getChildren() const;
182 
191  template <class F>
192  void
193  iterChildren(std::uint16_t isBranch, F&& f) const;
194 
204  template <class F>
205  void
206  iterNonEmptyChildIndexes(std::uint16_t isBranch, F&& f) const;
207 
217  getChildIndex(std::uint16_t isBranch, int i) const;
218 };
219 
220 } // namespace ripple
221 
222 #endif
ripple::TaggedPointer::~TaggedPointer
~TaggedPointer()
ripple::TaggedPointer::getChildIndex
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).
std::shared_ptr
STL class.
std::pair
ripple::TaggedPointer::tp_
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.
Definition: TaggedPointer.h:65
ripple::TaggedPointer
TaggedPointer is a combination of a pointer and a mask stored in the lowest two bits.
Definition: TaggedPointer.h:57
ripple::TaggedPointer::operator=
TaggedPointer & operator=(TaggedPointer &&)
ripple::SHAMapHash
Definition: SHAMapHash.h:32
ripple::TaggedPointer::TaggedPointer
TaggedPointer()=delete
ripple::TaggedPointer::getHashes
SHAMapHash * getHashes() const
Get the hashes array.
ripple::TaggedPointer::isDense
bool isDense() const
Check if the arrays have a dense format.
cstdint
ripple::TaggedPointer::decode
std::pair< std::uint8_t, void * > decode() const
Decode the tagged pointer into its tag and pointer.
std::uintptr_t
ripple::TaggedPointer::destroyHashesAndChildren
void destroyHashesAndChildren()
Deallocate memory and run destructors.
ripple::TaggedPointer::iterNonEmptyChildIndexes
void iterNonEmptyChildIndexes(std::uint16_t isBranch, F &&f) const
Call the f callback for all non-empty branches.
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::TaggedPointer::RawAllocateTag
Definition: TaggedPointer.h:75
ripple::TaggedPointer::ptrMask
static constexpr std::uintptr_t ptrMask
bit-and with this mask to get the pointer bits (mask out the tag)
Definition: TaggedPointer.h:69
optional
ripple::TaggedPointer::getChildren
std::shared_ptr< SHAMapTreeNode > * getChildren() const
Get the children array.
ripple::TaggedPointer::iterChildren
void iterChildren(std::uint16_t isBranch, F &&f) const
Call the f callback for all 16 (branchFactor) branches - even if the branch is empty.
ripple::TaggedPointer::getHashesAndChildren
std::tuple< std::uint8_t, SHAMapHash *, std::shared_ptr< SHAMapTreeNode > * > getHashesAndChildren() const
Get the number of elements in each array and a pointer to the start of each array.
ripple::TaggedPointer::capacity
std::uint8_t capacity() const
Get the number of elements allocated for each array.
ripple::TaggedPointer::tagMask
static constexpr std::uintptr_t tagMask
bit-and with this mask to get the tag bits (lowest two bits)
Definition: TaggedPointer.h:67