38 #ifndef LIBPMEMOBJ_CPP_PERSISTENT_PTR_HPP 39 #define LIBPMEMOBJ_CPP_PERSISTENT_PTR_HPP 50 #include <libpmemobj.h> 131 template <
typename T>
134 persistent_ptr() =
default;
164 typename pmem::detail::sp_dereference<T>::type
operator*() const
173 typename pmem::detail::sp_member_access<T>::type
operator->() const
184 template <typename = typename std::enable_if<!std::is_void<T>::value>>
185 typename pmem::detail::sp_array_access<T>::type
189 (i < pmem::detail::sp_extent<T>::value ||
190 pmem::detail::sp_extent<T>::value == 0) &&
191 "persistent array index out of bounds");
193 return this->
get()[i];
202 detail::conditional_add_to_tx(
this);
203 this->oid.off +=
sizeof(T);
214 PMEMoid noid = this->oid;
226 detail::conditional_add_to_tx(
this);
227 this->oid.off -=
sizeof(T);
238 PMEMoid noid = this->oid;
250 detail::conditional_add_to_tx(
this);
251 this->oid.off += static_cast<std::uint64_t>(s) *
sizeof(T);
262 detail::conditional_add_to_tx(
this);
263 this->oid.off -= static_cast<std::uint64_t>(s) *
sizeof(T);
288 pmemobjpool *pop = pmemobj_pool_by_oid(this->
raw());
292 "Cannot get pool from persistent pointer");
294 pmemobj_persist(pop, this->
get(),
sizeof(T));
317 pmemobjpool *pop = pmemobj_pool_by_oid(this->
raw());
321 "Cannot get pool from persistent pointer");
323 pmemobj_flush(pop, this->
get(),
sizeof(T));
408 template <
typename T,
typename Y>
412 return OID_EQUALS(lhs.raw(), rhs.raw());
418 template <
typename T,
typename Y>
422 return !(lhs == rhs);
428 template <
typename T>
432 return lhs.get() ==
nullptr;
438 template <
typename T>
442 return lhs.get() ==
nullptr;
448 template <
typename T>
452 return lhs.get() !=
nullptr;
458 template <
typename T>
462 return lhs.get() !=
nullptr;
472 template <
typename T,
typename Y>
476 if (lhs.raw().pool_uuid_lo == rhs.raw().pool_uuid_lo)
477 return lhs.raw().off < rhs.raw().off;
479 return lhs.raw().pool_uuid_lo < rhs.raw().pool_uuid_lo;
487 template <
typename T,
typename Y>
499 template <
typename T,
typename Y>
511 template <
typename T,
typename Y>
523 template <
typename T>
527 return std::less<typename persistent_ptr<T>::element_type *>()(
534 template <
typename T>
538 return std::less<typename persistent_ptr<T>::element_type *>()(
545 template <
typename T>
549 return !(
nullptr < lhs);
555 template <
typename T>
559 return !(rhs <
nullptr);
565 template <
typename T>
569 return nullptr < lhs;
575 template <
typename T>
579 return rhs <
nullptr;
585 template <
typename T>
589 return !(lhs <
nullptr);
595 template <
typename T>
599 return !(
nullptr < rhs);
605 template <
typename T>
606 inline persistent_ptr<T>
610 noid.pool_uuid_lo = lhs.raw().pool_uuid_lo;
611 noid.off = lhs.raw().off + static_cast<std::uint64_t>(s) *
sizeof(T);
619 template <
typename T>
620 inline persistent_ptr<T>
624 noid.pool_uuid_lo = lhs.raw().pool_uuid_lo;
625 noid.off = lhs.raw().off - static_cast<std::uint64_t>(s) *
sizeof(T);
637 template <
typename T,
typename Y,
638 typename =
typename std::enable_if<
639 std::is_same<typename std::remove_cv<T>::type,
640 typename std::remove_cv<Y>::type>::value>>
644 assert(lhs.raw().pool_uuid_lo == rhs.raw().pool_uuid_lo);
645 auto d = static_cast<std::ptrdiff_t>(lhs.raw().off - rhs.raw().off);
647 return d / static_cast<std::ptrdiff_t>(
sizeof(T));
653 template <
typename T>
657 PMEMoid raw_oid = pptr.raw();
658 os << std::hex <<
"0x" << raw_oid.pool_uuid_lo <<
", 0x" << raw_oid.off
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:400
persistent_ptr< T > & operator++()
Prefix increment operator.
Definition: persistent_ptr.hpp:200
Persistent_ptr base class.
Definition: persistent_ptr_base.hpp:68
void persist(void)
Persists what the persistent pointer points to.
Definition: persistent_ptr.hpp:286
Helper template for persistent ptr specialization.
Persistent pointer class.
Definition: common.hpp:119
pmem::detail::sp_member_access< T >::type operator->() const noexcept
Member access operator.
Definition: persistent_ptr.hpp:173
The non-template pool base class.
Definition: pool.hpp:67
Custom pool error class.
Definition: pexceptions.hpp:53
persistent_ptr< T > & operator-=(std::ptrdiff_t s)
Subtraction assignment operator.
Definition: persistent_ptr.hpp:260
persistent_ptr(persistent_ptr< const void > const &rhs) noexcept
Explicit const void specialization of the converting constructor.
Definition: persistent_ptr.hpp:148
persistent_ptr< T > operator++(int)
Postfix increment operator.
Definition: persistent_ptr.hpp:212
void persist(pool_base &pop)
Persists the content of the underlying object.
Definition: persistent_ptr.hpp:274
bool bool_type
The used bool_type.
Definition: persistent_ptr.hpp:358
void flush(pool_base &pop)
Flushes what the persistent pointer points to.
Definition: persistent_ptr.hpp:303
PMEMobj pool class.
Definition: persistent_ptr.hpp:59
persistent_ptr< T > operator--(int)
Postfix decrement operator.
Definition: persistent_ptr.hpp:236
persistent_ptr< T > operator+(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Addition operator for persistent pointers.
Definition: persistent_ptr.hpp:607
Commonly used functionality.
pmem::detail::sp_dereference< T >::type operator *() const noexcept
Dereference operator.
Definition: persistent_ptr.hpp:164
std::random_access_iterator_tag iterator_category
The persistent_ptr iterator category.
Definition: persistent_ptr.hpp:367
persistent_ptr< T > & operator--()
Prefix decrement operator.
Definition: persistent_ptr.hpp:224
persistent_ptr< T > & operator+=(std::ptrdiff_t s)
Addition assignment operator.
Definition: persistent_ptr.hpp:248
void flush(void)
Flushes what the persistent pointer points to.
Definition: persistent_ptr.hpp:315
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:284
pmem::detail::sp_array_access< T >::type operator[](std::ptrdiff_t i) const noexcept
Array access operator.
Definition: persistent_ptr.hpp:186
static persistent_ptr< T > pointer_to(T &ref)
Create a persistent pointer from a given reference.
Definition: persistent_ptr.hpp:339
persistent_ptr< T > operator-(persistent_ptr< T > const &lhs, std::ptrdiff_t s)
Subtraction operator for persistent pointers.
Definition: persistent_ptr.hpp:621
std::ostream & operator<<(std::ostream &os, persistent_ptr< T > const &pptr)
Ostream operator for the persistent pointer.
Definition: persistent_ptr.hpp:655
const PMEMoid & raw() const noexcept
Get PMEMoid encapsulated by this object.
Definition: persistent_ptr_base.hpp:304
Resides on pmem class.
Definition: p.hpp:64
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:516
T value_type
The type of the value pointed to by the persistent_ptr.
Definition: persistent_ptr.hpp:377
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:320
element_type * get() const noexcept
Get a direct pointer.
Definition: persistent_ptr_base.hpp:286
Base class for persistent_ptr.
std::ptrdiff_t difference_type
The persistent_ptr difference type.
Definition: persistent_ptr.hpp:372
persistent_ptr(persistent_ptr< void > const &rhs) noexcept
Explicit void specialization of the converting constructor.
Definition: persistent_ptr.hpp:140
T & reference
The reference type of the value pointed to by the persistent_ptr.
Definition: persistent_ptr.hpp:382