9 #ifndef LIBPMEMOBJ_CPP_PERSISTENT_PTR_HPP
10 #define LIBPMEMOBJ_CPP_PERSISTENT_PTR_HPP
21 #include <libpmemobj/base.h>
56 if (this->oid.pool_uuid_lo ==
57 std::numeric_limits<decltype(oid.pool_uuid_lo)>::max())
58 return reinterpret_cast<element_type *
>(oid.off);
60 return static_cast<element_type *
>(
61 pmemobj_direct(this->oid));
65 typename =
typename std::enable_if<
66 std::is_convertible<Y *, void *>::value>::type>
75 explicit operator bool()
const noexcept
77 return get() !=
nullptr;
102 if (this->oid.pool_uuid_lo ==
103 std::numeric_limits<decltype(oid.pool_uuid_lo)>::max())
104 return reinterpret_cast<element_type *
>(oid.off);
106 return static_cast<element_type *
>(
107 pmemobj_direct(this->oid));
110 template <
typename Y,
111 typename =
typename std::enable_if<
112 std::is_convertible<Y *, const void *>::value>::type>
121 explicit operator bool()
const noexcept
123 return get() !=
nullptr;
152 template <
typename T>
157 template <
typename U>
208 template <
typename U,
209 typename =
typename std::enable_if<
210 !std::is_same<T, U>::value &&
211 std::is_same<typename std::remove_cv<T>::type,
217 static_cast<std::uint64_t
>(calculate_offset<U>());
227 typename U,
typename Dummy = void,
228 typename =
typename std::enable_if<
230 typename std::remove_cv<T>::type,
231 typename std::remove_cv<U>::type>::value &&
232 !std::is_void<U>::value,
233 decltype(
static_cast<T *
>(std::declval<U *>()))>::type>
238 static_cast<std::uint64_t
>(calculate_offset<U>());
257 typename pmem::detail::sp_dereference<T>::type
operator*() const
266 typename pmem::detail::sp_member_access<T>::type
operator->() const
277 template <typename = typename std::enable_if<!std::is_void<T>::value>>
278 typename pmem::detail::sp_array_access<T>::type
282 (i < pmem::detail::sp_extent<T>::value ||
283 pmem::detail::sp_extent<T>::value == 0) &&
284 "persistent array index out of bounds");
286 return this->
get()[i];
296 this->oid.off +=
sizeof(T);
307 PMEMoid noid = this->oid;
320 this->oid.off -=
sizeof(T);
331 PMEMoid noid = this->oid;
344 this->oid.off +=
static_cast<std::uint64_t
>(s) *
sizeof(T);
356 this->oid.off -=
static_cast<std::uint64_t
>(s) *
sizeof(T);
372 template <
typename Y,
373 typename =
typename std::enable_if<
374 std::is_convertible<Y *, T *>::value>::type>
378 this_type(r).swap(*
this);
386 explicit operator bool() const noexcept
388 return get() !=
nullptr;
415 pmemobjpool *pop = pmemobj_pool_by_oid(this->
raw());
419 "Cannot get pool from persistent pointer");
421 pmemobj_persist(pop, this->
get(),
sizeof(T));
444 pmemobjpool *pop = pmemobj_pool_by_oid(this->
raw());
448 "Cannot get pool from persistent pointer");
450 pmemobj_flush(pop, this->
get(),
sizeof(T));
481 if (this->oid.pool_uuid_lo ==
482 std::numeric_limits<decltype(oid.pool_uuid_lo)>::max())
486 pmemobj_direct(this->oid));
541 static_assert(!std::is_polymorphic<element_type>::value,
542 "Polymorphic types are not supported");
554 if (OID_IS_NULL(oid)) {
555 oid.pool_uuid_lo = std::numeric_limits<decltype(
556 oid.pool_uuid_lo)>::max();
557 oid.off =
reinterpret_cast<decltype(oid.off)
>(vptr);
576 template <
typename U>
580 static const ptrdiff_t ptr_offset_magic = 0xF00000000000000;
582 static_assert(ptr_offset_magic %
alignof(U) == 0,
"");
583 static_assert(ptr_offset_magic %
alignof(T) == 0,
"");
585 U *tmp{
reinterpret_cast<U *
>(ptr_offset_magic)};
586 T *diff =
static_cast<T *
>(tmp);
587 return reinterpret_cast<ptrdiff_t
>(diff) -
588 reinterpret_cast<ptrdiff_t
>(tmp);
612 template <
typename T,
typename Y>
616 return OID_EQUALS(lhs.raw(), rhs.raw());
623 template <
typename T,
typename Y>
627 return !(lhs == rhs);
634 template <
typename T>
638 return lhs.get() ==
nullptr;
645 template <
typename T>
649 return lhs.get() ==
nullptr;
656 template <
typename T>
660 return lhs.get() !=
nullptr;
667 template <
typename T>
671 return lhs.get() !=
nullptr;
682 template <
typename T,
typename Y>
686 if (lhs.raw().pool_uuid_lo == rhs.raw().pool_uuid_lo)
687 return lhs.raw().off < rhs.raw().off;
689 return lhs.raw().pool_uuid_lo < rhs.raw().pool_uuid_lo;
698 template <
typename T,
typename Y>
711 template <
typename T,
typename Y>
724 template <
typename T,
typename Y>
737 template <
typename T>
741 return std::less<typename persistent_ptr<T>::element_type *>()(
749 template <
typename T>
753 return std::less<typename persistent_ptr<T>::element_type *>()(
761 template <
typename T>
765 return !(
nullptr < lhs);
772 template <
typename T>
776 return !(rhs <
nullptr);
783 template <
typename T>
787 return nullptr < lhs;
794 template <
typename T>
798 return rhs <
nullptr;
805 template <
typename T>
809 return !(lhs <
nullptr);
816 template <
typename T>
820 return !(
nullptr < rhs);
827 template <
typename T>
832 noid.pool_uuid_lo = lhs.
raw().pool_uuid_lo;
833 noid.off = lhs.
raw().off +
static_cast<std::uint64_t
>(s) *
sizeof(T);
842 template <
typename T>
847 noid.pool_uuid_lo = lhs.
raw().pool_uuid_lo;
848 noid.off = lhs.
raw().off -
static_cast<std::uint64_t
>(s) *
sizeof(T);
861 template <
typename T,
typename Y,
862 typename =
typename std::enable_if<
863 std::is_same<typename std::remove_cv<T>::type,
864 typename std::remove_cv<Y>::type>::value>>
868 assert(lhs.
raw().pool_uuid_lo == rhs.
raw().pool_uuid_lo);
869 auto d =
static_cast<std::ptrdiff_t
>(lhs.
raw().off - rhs.
raw().off);
871 return d /
static_cast<std::ptrdiff_t
>(
sizeof(T));
878 template <
typename T>
882 PMEMoid raw_oid = pptr.
raw();
883 os << std::hex <<
"0x" << raw_oid.pool_uuid_lo <<
", 0x" << raw_oid.off
Resides on pmem class.
Definition: p.hpp:36
persistent_ptr const void specialization.
Definition: persistent_ptr.hpp:88
persistent_ptr void specialization.
Definition: persistent_ptr.hpp:42
Persistent_ptr base (non-template) class.
Definition: persistent_ptr_base.hpp:42
void swap(persistent_ptr_base &other)
Swaps two persistent_ptr objects of the same type.
Definition: persistent_ptr_base.hpp:136
persistent_ptr_base() noexcept
Default constructor, zeroes the PMEMoid.
Definition: persistent_ptr_base.hpp:47
const PMEMoid & raw() const noexcept
Get PMEMoid encapsulated by this object.
Definition: persistent_ptr_base.hpp:151
Persistent pointer class.
Definition: persistent_ptr.hpp:153
bool operator==(persistent_ptr< T > const &lhs, std::nullptr_t) noexcept
Equality operator with nullptr.
Definition: persistent_ptr.hpp:636
persistent_ptr(persistent_ptr< void > const &rhs) noexcept
Explicit void specialization of the converting constructor.
Definition: persistent_ptr.hpp:176
void persist(pool_base &pop)
Persists the content of the underlying object.
Definition: persistent_ptr.hpp:401
bool operator>(persistent_ptr< T > const &lhs, std::nullptr_t) noexcept
Compare a persistent_ptr with a null pointer.
Definition: persistent_ptr.hpp:785
bool operator>=(persistent_ptr< T > const &lhs, persistent_ptr< Y > const &rhs) noexcept
Greater or equal than operator.
Definition: persistent_ptr.hpp:726
void persist(void)
Persists what the persistent pointer points to.
Definition: persistent_ptr.hpp:413
void flush(pool_base &pop)
Flushes what the persistent pointer points to.
Definition: persistent_ptr.hpp:430
bool operator!=(std::nullptr_t, persistent_ptr< T > const &lhs) noexcept
Inequality operator with nullptr.
Definition: persistent_ptr.hpp:669
bool bool_type
The used bool_type.
Definition: persistent_ptr.hpp:503
persistent_ptr< T > & operator=(persistent_ptr< Y > const &r)
Converting assignment operator from a different persistent_ptr<>.
Definition: persistent_ptr.hpp:376
persistent_ptr< T > & operator--()
Prefix decrement operator.
Definition: persistent_ptr.hpp:317
bool operator>(std::nullptr_t, persistent_ptr< T > const &rhs) noexcept
Compare a persistent_ptr with a null pointer.
Definition: persistent_ptr.hpp:796
persistent_ptr_base() noexcept
Default constructor, zeroes the PMEMoid.
Definition: persistent_ptr_base.hpp:47
persistent_ptr< T > operator++(int)
Postfix increment operator.
Definition: persistent_ptr.hpp:305
void flush(void)
Flushes what the persistent pointer points to.
Definition: persistent_ptr.hpp:442
pmem::detail::sp_dereference< T >::type operator*() const noexcept
Dereference operator.
Definition: persistent_ptr.hpp:257
persistent_ptr< T > & operator+=(std::ptrdiff_t s)
Addition assignment operator.
Definition: persistent_ptr.hpp:341
pmem::detail::sp_member_access< T >::type operator->() const noexcept
Member access operator.
Definition: persistent_ptr.hpp:266
persistent_ptr(persistent_ptr< const void > const &rhs) noexcept
Explicit const void specialization of the converting constructor.
Definition: persistent_ptr.hpp:184
T & reference
The reference type of the value pointed to by the persistent_ptr.
Definition: persistent_ptr.hpp:527
persistent_ptr< T > & operator-=(std::ptrdiff_t s)
Subtraction assignment operator.
Definition: persistent_ptr.hpp:353
persistent_ptr< T > operator--(int)
Postfix decrement operator.
Definition: persistent_ptr.hpp:329
bool operator==(persistent_ptr< T > const &lhs, persistent_ptr< Y > const &rhs) noexcept
Equality operator.
Definition: persistent_ptr.hpp:614
pmem::detail::sp_array_access< T >::type operator[](std::ptrdiff_t i) const noexcept
Array access operator.
Definition: persistent_ptr.hpp:279
bool operator==(std::nullptr_t, persistent_ptr< T > const &lhs) noexcept
Equality operator with nullptr.
Definition: persistent_ptr.hpp:647
std::random_access_iterator_tag iterator_category
Random access iterator requirements (members)
Definition: persistent_ptr.hpp:512
bool operator!=(persistent_ptr< T > const &lhs, persistent_ptr< Y > const &rhs) noexcept
Inequality operator.
Definition: persistent_ptr.hpp:625
persistent_ptr(element_type *ptr)
Volatile pointer constructor.
Definition: persistent_ptr.hpp:197
pmem::detail::sp_element< T >::type element_type
Type of the actual object with all qualifiers removed, used for easy underlying type access.
Definition: persistent_ptr.hpp:164
T value_type
The type of the value pointed to by the persistent_ptr.
Definition: persistent_ptr.hpp:522
bool operator>=(std::nullptr_t, persistent_ptr< T > const &rhs) noexcept
Compare a persistent_ptr with a null pointer.
Definition: persistent_ptr.hpp:818
bool operator>(persistent_ptr< T > const &lhs, persistent_ptr< Y > const &rhs) noexcept
Greater than operator.
Definition: persistent_ptr.hpp:713
persistent_ptr< T > & operator++()
Prefix increment operator.
Definition: persistent_ptr.hpp:293
void swap(persistent_ptr< T > &a, persistent_ptr< T > &b)
Swaps two persistent_ptr objects of the same type.
Definition: persistent_ptr.hpp:601
persistent_ptr(persistent_ptr< U > const &r) noexcept
Copy constructor from a different persistent_ptr<>.
Definition: persistent_ptr.hpp:213
persistent_ptr(persistent_ptr< U > const &r) noexcept
Copy constructor from a different persistent_ptr<>.
Definition: persistent_ptr.hpp:234
ptrdiff_t calculate_offset() const
Calculate in-object offset for structures with inheritance.
Definition: persistent_ptr.hpp:578
element_type * get() const noexcept
Get the direct pointer.
Definition: persistent_ptr.hpp:479
persistent_ptr< T > operator-(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Subtraction operator for persistent pointers.
Definition: persistent_ptr.hpp:844
std::ptrdiff_t difference_type
The persistent_ptr difference type.
Definition: persistent_ptr.hpp:517
persistent_ptr< T > pointer
The pointer type.
Definition: persistent_ptr.hpp:532
bool operator!=(persistent_ptr< T > const &lhs, std::nullptr_t) noexcept
Inequality operator with nullptr.
Definition: persistent_ptr.hpp:658
persistent_ptr(element_type *vptr, int)
Private constructor enabling persistent_ptrs to volatile objects.
Definition: persistent_ptr.hpp:552
ptrdiff_t operator-(persistent_ptr< T > const &lhs, persistent_ptr< Y > const &rhs)
Subtraction operator for persistent pointers of identical type.
Definition: persistent_ptr.hpp:866
void verify_type()
Verify if element_type is not polymorphic.
Definition: persistent_ptr.hpp:539
persistent_ptr< T > operator+(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Addition operator for persistent pointers.
Definition: persistent_ptr.hpp:829
bool operator>=(persistent_ptr< T > const &lhs, std::nullptr_t) noexcept
Compare a persistent_ptr with a null pointer.
Definition: persistent_ptr.hpp:807
static persistent_ptr< T > pointer_to(T &ref)
Create a persistent pointer from a given reference.
Definition: persistent_ptr.hpp:466
The non-template pool base class.
Definition: pool.hpp:51
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:317
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:280
Custom pool error class.
Definition: pexceptions.hpp:84
Commonly used functionality.
void conditional_add_to_tx(const T *that, std::size_t count=1, uint64_t flags=0)
Conditionally add 'count' objects to a transaction.
Definition: common.hpp:176
std::ostream & operator<<(std::ostream &os, const radix_tree< K, V, BV, MtMode > &tree)
Prints tree in DOT format.
Definition: radix_tree.hpp:2843
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Base class for persistent_ptr.
Helper template for persistent ptr specialization.