PMDK C++ bindings  1.13.0-git23.gf49772ac
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-2020, Intel Corporation */
3 
11 #ifndef LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP
12 #define LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP
13 
21 #include <libpmemobj/tx_base.h>
22 
23 #include <new>
24 #include <utility>
25 
26 namespace pmem
27 {
28 
29 namespace obj
30 {
31 
48 template <typename T, typename... Args>
49 typename detail::pp_if_not_array<T>::type
50 make_persistent(allocation_flag flag, Args &&... args)
51 {
52  if (pmemobj_tx_stage() != TX_STAGE_WORK)
54  "refusing to allocate memory outside of transaction scope");
55 
56  persistent_ptr<T> ptr =
57  pmemobj_tx_xalloc(sizeof(T), detail::type_num<T>(), flag.value);
58 
59  if (ptr == nullptr) {
60  if (errno == ENOMEM)
62  "Failed to allocate persistent memory object")
63  .with_pmemobj_errormsg();
64  else
66  "Failed to allocate persistent memory object")
67  .with_pmemobj_errormsg();
68  }
69 
70  detail::create<T, Args...>(ptr.get(), std::forward<Args>(args)...);
71 
72  return ptr;
73 }
74 
90 template <typename T, typename... Args>
91 typename std::enable_if<
92  !detail::is_first_arg_same<allocation_flag, Args...>::value,
93  typename detail::pp_if_not_array<T>::type>::type
94 make_persistent(Args &&... args)
95 {
96  return make_persistent<T>(allocation_flag::none(),
97  std::forward<Args>(args)...);
98 }
99 
117 template <typename T>
118 void
120 {
121  if (pmemobj_tx_stage() != TX_STAGE_WORK)
123  "refusing to free memory outside of transaction scope");
124 
125  if (ptr == nullptr)
126  return;
127 
128  /*
129  * At this point, everything in the object should be tracked
130  * and reverted on transaction abort.
131  */
132  detail::destroy<T>(*ptr);
133 
134  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
136  "failed to delete persistent memory object")
137  .with_pmemobj_errormsg();
138 }
139 
140 } /* namespace obj */
141 
142 } /* namespace pmem */
143 
144 #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:152
element_type * get() const noexcept
Get the direct pointer.
Definition: persistent_ptr.hpp:478
Custom transaction error class.
Definition: pexceptions.hpp:119
Custom transaction error class.
Definition: pexceptions.hpp:158
Custom out of memory error class.
Definition: pexceptions.hpp:138
Custom transaction error class.
Definition: pexceptions.hpp:176
Commonly used functionality.
Functions for destroying arrays.
Persistent_ptr allocation functions for arrays.
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:119
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:50
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Custom 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.