PMDK C++ bindings  1.12-git53.g67ba2be4
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 */
pmem::obj::persistent_ptr::get
element_type * get() const noexcept
Get the direct pointer.
Definition: persistent_ptr.hpp:478
pmem::transaction_free_error
Custom transaction error class.
Definition: pexceptions.hpp:140
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::transaction_out_of_memory
Custom out of memory error class.
Definition: pexceptions.hpp:120
common.hpp
Commonly used functionality.
pexceptions.hpp
Custom exceptions.
check_persistent_ptr_array.hpp
Compile time type check for make_persistent.
pmem::obj::delete_persistent
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
make_persistent_array.hpp
Persistent_ptr allocation functions for arrays.
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:152
pmem::obj::allocation_flag::none
static allocation_flag none()
Do not change allocator behaviour.
Definition: allocation_flag.hpp:61
life.hpp
Functions for destroying arrays.
pmem::obj::allocation_flag
Type of flag which can be passed to make_persistent.
Definition: allocation_flag.hpp:31
pmem::transaction_scope_error
Custom transaction error class.
Definition: pexceptions.hpp:158
variadic.hpp
Helper functionality for handling variadic templates.
allocation_flag.hpp
allocation_flag - defines flags which can be passed to make_persistent
pmem::obj::make_persistent
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
pmem::transaction_alloc_error
Custom transaction error class.
Definition: pexceptions.hpp:101