PMDK C++ bindings  1.13.0-git107.g7e59f08f
This is the C++ bindings documentation for PMDK's libpmemobj.
make_persistent.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2016-2021, Intel Corporation */
3 
12 #ifndef LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP
13 #define LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP
14 
22 #include <libpmemobj/tx_base.h>
23 
24 #include <new>
25 #include <utility>
26 
27 namespace pmem
28 {
29 
30 namespace obj
31 {
32 
52 template <typename T, typename... Args>
53 typename detail::pp_if_not_array<T>::type
54 make_persistent(allocation_flag flag, Args &&... args)
55 {
56  if (pmemobj_tx_stage() != TX_STAGE_WORK)
58  "refusing to allocate memory outside of transaction scope");
59 
60  persistent_ptr<T> ptr =
61  pmemobj_tx_xalloc(sizeof(T), detail::type_num<T>(), flag.value);
62 
63  if (ptr == nullptr) {
64  const char *msg = "Failed to allocate persistent memory object";
65  if (errno == ENOMEM)
68  else
71  }
72 
73  detail::create<T, Args...>(ptr.get(), std::forward<Args>(args)...);
74 
75  return ptr;
76 }
77 
96 template <typename T, typename... Args>
97 typename std::enable_if<
98  !detail::is_first_arg_same<allocation_flag, Args...>::value,
99  typename detail::pp_if_not_array<T>::type>::type
100 make_persistent(Args &&... args)
101 {
102  return make_persistent<T>(allocation_flag::none(),
103  std::forward<Args>(args)...);
104 }
105 
124 template <typename T>
125 void
127 {
128  if (pmemobj_tx_stage() != TX_STAGE_WORK)
130  "refusing to free memory outside of transaction scope");
131 
132  if (ptr == nullptr)
133  return;
134 
135  /*
136  * At this point, everything in the object should be tracked
137  * and reverted on transaction abort.
138  */
139  detail::destroy<T>(*ptr);
140 
141  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
144  "failed to delete persistent memory object");
145 }
146 
147 } /* namespace obj */
148 
149 } /* namespace pmem */
150 
151 #endif /* LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP */
allocation_flag - defines flags which can be passed to make_persistent
Compile time type check for make_persistent.
PMEMoid * raw_ptr() noexcept
Get pointer to PMEMoid encapsulated by this object.
Definition: persistent_ptr_base.hpp:164
Persistent pointer class.
Definition: persistent_ptr.hpp:153
element_type * get() const noexcept
Get the direct pointer.
Definition: persistent_ptr.hpp:479
Custom transaction error class.
Definition: pexceptions.hpp:132
Custom transaction error class.
Definition: pexceptions.hpp:156
Custom out of memory error class.
Definition: pexceptions.hpp:144
Custom transaction error class.
Definition: pexceptions.hpp:167
Commonly used functionality.
void delete_persistent(typename detail::pp_if_not_array< T >::type ptr)
Transactionally free an object of type T held in a persistent_ptr.
Definition: make_persistent.hpp:126
detail::pp_if_not_array< T >::type make_persistent(allocation_flag flag, Args &&... args)
Transactionally allocate and construct an object of type T.
Definition: make_persistent.hpp:54
Functions for lifetime management.
persistent_ptr transactional allocation functions for arrays.
ExcT exception_with_errormsg(Args &&... args)
Generic error message decorator for pmemobj-based exceptions.
Definition: pexceptions.hpp:69
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Custom pmem exceptions.
Type of flag which can be passed to make_persistent.
Definition: allocation_flag.hpp:31
static allocation_flag none()
Do not change allocator behaviour.
Definition: allocation_flag.hpp:61
Helper functionality for handling variadic templates.