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;
151 template <
typename T>
156 template <
typename U>
207 template <
typename U,
208 typename =
typename std::enable_if<
209 !std::is_same<T, U>::value &&
210 std::is_same<typename std::remove_cv<T>::type,
216 static_cast<std::uint64_t
>(calculate_offset<U>());
226 typename U,
typename Dummy = void,
227 typename =
typename std::enable_if<
229 typename std::remove_cv<T>::type,
230 typename std::remove_cv<U>::type>::value &&
231 !std::is_void<U>::value,
232 decltype(
static_cast<T *
>(std::declval<U *>()))>::type>
237 static_cast<std::uint64_t
>(calculate_offset<U>());
256 typename pmem::detail::sp_dereference<T>::type
operator*() const
265 typename pmem::detail::sp_member_access<T>::type
operator->() const
276 template <typename = typename std::enable_if<!std::is_void<T>::value>>
277 typename pmem::detail::sp_array_access<T>::type
281 (i < pmem::detail::sp_extent<T>::value ||
282 pmem::detail::sp_extent<T>::value == 0) &&
283 "persistent array index out of bounds");
285 return this->
get()[i];
294 detail::conditional_add_to_tx(
this);
295 this->oid.off +=
sizeof(T);
306 PMEMoid noid = this->oid;
318 detail::conditional_add_to_tx(
this);
319 this->oid.off -=
sizeof(T);
330 PMEMoid noid = this->oid;
342 detail::conditional_add_to_tx(
this);
343 this->oid.off +=
static_cast<std::uint64_t
>(s) *
sizeof(T);
354 detail::conditional_add_to_tx(
this);
355 this->oid.off -=
static_cast<std::uint64_t
>(s) *
sizeof(T);
371 template <
typename Y,
372 typename =
typename std::enable_if<
373 std::is_convertible<Y *, T *>::value>::type>
385 explicit operator bool() const noexcept
387 return get() !=
nullptr;
414 pmemobjpool *pop = pmemobj_pool_by_oid(this->
raw());
418 "Cannot get pool from persistent pointer");
420 pmemobj_persist(pop, this->
get(),
sizeof(T));
443 pmemobjpool *pop = pmemobj_pool_by_oid(this->
raw());
447 "Cannot get pool from persistent pointer");
449 pmemobj_flush(pop, this->
get(),
sizeof(T));
480 if (this->oid.pool_uuid_lo ==
481 std::numeric_limits<decltype(oid.pool_uuid_lo)>::max())
485 pmemobj_direct(this->oid));
540 static_assert(!std::is_polymorphic<element_type>::value,
541 "Polymorphic types are not supported");
553 if (OID_IS_NULL(oid)) {
554 oid.pool_uuid_lo = std::numeric_limits<decltype(
555 oid.pool_uuid_lo)>::max();
556 oid.off =
reinterpret_cast<decltype(oid.off)
>(vptr);
575 template <
typename U>
579 static const ptrdiff_t ptr_offset_magic = 0xF00000000000000;
581 static_assert(ptr_offset_magic %
alignof(U) == 0,
"");
582 static_assert(ptr_offset_magic %
alignof(T) == 0,
"");
584 U *tmp{
reinterpret_cast<U *
>(ptr_offset_magic)};
585 T *diff =
static_cast<T *
>(tmp);
586 return reinterpret_cast<ptrdiff_t
>(diff) -
587 reinterpret_cast<ptrdiff_t
>(tmp);
609 template <
typename T,
typename Y>
613 return OID_EQUALS(lhs.raw(), rhs.raw());
619 template <
typename T,
typename Y>
623 return !(lhs == rhs);
629 template <
typename T>
633 return lhs.get() ==
nullptr;
639 template <
typename T>
643 return lhs.get() ==
nullptr;
649 template <
typename T>
653 return lhs.get() !=
nullptr;
659 template <
typename T>
663 return lhs.get() !=
nullptr;
673 template <
typename T,
typename Y>
677 if (lhs.raw().pool_uuid_lo == rhs.raw().pool_uuid_lo)
678 return lhs.raw().off < rhs.raw().off;
680 return lhs.raw().pool_uuid_lo < rhs.raw().pool_uuid_lo;
688 template <
typename T,
typename Y>
700 template <
typename T,
typename Y>
712 template <
typename T,
typename Y>
724 template <
typename T>
728 return std::less<typename persistent_ptr<T>::element_type *>()(
735 template <
typename T>
739 return std::less<typename persistent_ptr<T>::element_type *>()(
746 template <
typename T>
750 return !(
nullptr < lhs);
756 template <
typename T>
760 return !(rhs <
nullptr);
766 template <
typename T>
770 return nullptr < lhs;
776 template <
typename T>
780 return rhs <
nullptr;
786 template <
typename T>
790 return !(lhs <
nullptr);
796 template <
typename T>
800 return !(
nullptr < rhs);
806 template <
typename T>
807 inline persistent_ptr<T>
811 noid.pool_uuid_lo = lhs.
raw().pool_uuid_lo;
812 noid.off = lhs.
raw().off +
static_cast<std::uint64_t
>(s) *
sizeof(T);
820 template <
typename T>
821 inline persistent_ptr<T>
825 noid.pool_uuid_lo = lhs.
raw().pool_uuid_lo;
826 noid.off = lhs.
raw().off -
static_cast<std::uint64_t
>(s) *
sizeof(T);
838 template <
typename T,
typename Y,
839 typename =
typename std::enable_if<
840 std::is_same<typename std::remove_cv<T>::type,
841 typename std::remove_cv<Y>::type>::value>>
845 assert(lhs.
raw().pool_uuid_lo == rhs.
raw().pool_uuid_lo);
846 auto d =
static_cast<std::ptrdiff_t
>(lhs.
raw().off - rhs.
raw().off);
848 return d /
static_cast<std::ptrdiff_t
>(
sizeof(T));
854 template <
typename T>
858 PMEMoid raw_oid = pptr.
raw();
859 os << std::hex <<
"0x" << raw_oid.pool_uuid_lo <<
", 0x" << raw_oid.off
Resides on pmem class.
Definition: p.hpp:35
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:152
persistent_ptr(persistent_ptr< void > const &rhs) noexcept
Explicit void specialization of the converting constructor.
Definition: persistent_ptr.hpp:175
void persist(pool_base &pop)
Persists the content of the underlying object.
Definition: persistent_ptr.hpp:400
void persist(void)
Persists what the persistent pointer points to.
Definition: persistent_ptr.hpp:412
void flush(pool_base &pop)
Flushes what the persistent pointer points to.
Definition: persistent_ptr.hpp:429
bool bool_type
The used bool_type.
Definition: persistent_ptr.hpp:502
persistent_ptr< T > & operator=(persistent_ptr< Y > const &r)
Converting assignment operator from a different persistent_ptr<>.
Definition: persistent_ptr.hpp:375
persistent_ptr< T > & operator--()
Prefix decrement operator.
Definition: persistent_ptr.hpp:316
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:304
void flush(void)
Flushes what the persistent pointer points to.
Definition: persistent_ptr.hpp:441
pmem::detail::sp_dereference< T >::type operator*() const noexcept
Dereference operator.
Definition: persistent_ptr.hpp:256
persistent_ptr< T > & operator+=(std::ptrdiff_t s)
Addition assignment operator.
Definition: persistent_ptr.hpp:340
pmem::detail::sp_member_access< T >::type operator->() const noexcept
Member access operator.
Definition: persistent_ptr.hpp:265
persistent_ptr(persistent_ptr< const void > const &rhs) noexcept
Explicit const void specialization of the converting constructor.
Definition: persistent_ptr.hpp:183
T & reference
The reference type of the value pointed to by the persistent_ptr.
Definition: persistent_ptr.hpp:526
persistent_ptr< T > & operator-=(std::ptrdiff_t s)
Subtraction assignment operator.
Definition: persistent_ptr.hpp:352
persistent_ptr< T > operator--(int)
Postfix decrement operator.
Definition: persistent_ptr.hpp:328
pmem::detail::sp_array_access< T >::type operator[](std::ptrdiff_t i) const noexcept
Array access operator.
Definition: persistent_ptr.hpp:278
std::random_access_iterator_tag iterator_category
Random access iterator requirements (members)
Definition: persistent_ptr.hpp:511
persistent_ptr(element_type *ptr)
Volatile pointer constructor.
Definition: persistent_ptr.hpp:196
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:163
T value_type
The type of the value pointed to by the persistent_ptr.
Definition: persistent_ptr.hpp:521
persistent_ptr< T > & operator++()
Prefix increment operator.
Definition: persistent_ptr.hpp:292
persistent_ptr(persistent_ptr< U > const &r) noexcept
Copy constructor from a different persistent_ptr<>.
Definition: persistent_ptr.hpp:212
persistent_ptr(persistent_ptr< U > const &r) noexcept
Copy constructor from a different persistent_ptr<>.
Definition: persistent_ptr.hpp:233
ptrdiff_t calculate_offset() const
Calculate in-object offset for structures with inheritance.
Definition: persistent_ptr.hpp:577
element_type * get() const noexcept
Get the direct pointer.
Definition: persistent_ptr.hpp:478
std::ptrdiff_t difference_type
The persistent_ptr difference type.
Definition: persistent_ptr.hpp:516
persistent_ptr(element_type *vptr, int)
Private constructor enabling persistent_ptrs to volatile objects.
Definition: persistent_ptr.hpp:551
void verify_type()
Verify if element_type is not polymorphic.
Definition: persistent_ptr.hpp:538
static persistent_ptr< T > pointer_to(T &ref)
Create a persistent pointer from a given reference.
Definition: persistent_ptr.hpp:465
The non-template pool base class.
Definition: pool.hpp:50
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:316
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:279
Custom pool error class.
Definition: pexceptions.hpp:45
Commonly used functionality.
bool operator<=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less or equal operator.
Definition: array.hpp:779
persistent_ptr< T > operator-(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Subtraction operator for persistent pointers.
Definition: persistent_ptr.hpp:822
bool operator<(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member less than operator.
Definition: array.hpp:748
std::ostream & operator<<(std::ostream &os, persistent_ptr< T > const &pptr)
Ostream operator for the persistent pointer.
Definition: persistent_ptr.hpp:856
persistent_ptr< T > operator+(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Addition operator for persistent pointers.
Definition: persistent_ptr.hpp:808
bool operator>=(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater or equal operator.
Definition: array.hpp:769
bool operator!=(const allocator< T, P, Tr > &lhs, const OtherAllocator &rhs)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:536
bool operator==(standard_alloc_policy< T > const &, standard_alloc_policy< T2 > const &)
Determines if memory from another allocator can be deallocated from this one.
Definition: allocator.hpp:420
bool operator>(const array< T, N > &lhs, const array< T, N > &rhs)
Non-member greater than operator.
Definition: array.hpp:759
void swap(pmem::obj::array< T, N > &lhs, pmem::obj::array< T, N > &rhs)
Non-member swap function.
Definition: array.hpp:909
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Base class for persistent_ptr.
Helper template for persistent ptr specialization.