PMDK C++ bindings  1.9.1
This is the C++ bindings documentation for PMDK's libpmemobj.
make_persistent.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2020, Intel Corporation
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
40 #ifndef LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP
41 #define LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP
42 
50 #include <libpmemobj/tx_base.h>
51 
52 #include <new>
53 #include <utility>
54 
55 namespace pmem
56 {
57 
58 namespace obj
59 {
60 
77 template <typename T, typename... Args>
78 typename detail::pp_if_not_array<T>::type
79 make_persistent(allocation_flag flag, Args &&... args)
80 {
81  if (pmemobj_tx_stage() != TX_STAGE_WORK)
83  "refusing to allocate memory outside of transaction scope");
84 
85  persistent_ptr<T> ptr =
86  pmemobj_tx_xalloc(sizeof(T), detail::type_num<T>(), flag.value);
87 
88  if (ptr == nullptr) {
89  if (errno == ENOMEM)
91  "Failed to allocate persistent memory object")
92  .with_pmemobj_errormsg();
93  else
95  "Failed to allocate persistent memory object")
96  .with_pmemobj_errormsg();
97  }
98 
99  detail::create<T, Args...>(ptr.get(), std::forward<Args>(args)...);
100 
101  return ptr;
102 }
103 
119 template <typename T, typename... Args>
120 typename std::enable_if<
121  !detail::is_first_arg_same<allocation_flag, Args...>::value,
122  typename detail::pp_if_not_array<T>::type>::type
123 make_persistent(Args &&... args)
124 {
125  return make_persistent<T>(allocation_flag::none(),
126  std::forward<Args>(args)...);
127 }
128 
146 template <typename T>
147 void
149 {
150  if (pmemobj_tx_stage() != TX_STAGE_WORK)
152  "refusing to free memory outside of transaction scope");
153 
154  if (ptr == nullptr)
155  return;
156 
157  /*
158  * At this point, everything in the object should be tracked
159  * and reverted on transaction abort.
160  */
161  detail::destroy<T>(*ptr);
162 
163  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
165  "failed to delete persistent memory object")
166  .with_pmemobj_errormsg();
167 }
168 
169 } /* namespace obj */
170 
171 } /* namespace pmem */
172 
173 #endif /* LIBPMEMOBJ_CPP_MAKE_PERSISTENT_HPP */
pmem::obj::persistent_ptr::get
element_type * get() const noexcept
Get the direct pointer.
Definition: persistent_ptr.hpp:538
pmem::transaction_free_error
Custom transaction error class.
Definition: pexceptions.hpp:174
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:44
pmem::transaction_out_of_memory
Custom out of memory error class.
Definition: pexceptions.hpp:154
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:148
make_persistent_array.hpp
Persistent_ptr allocation functions for arrays.
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:212
pmem::obj::allocation_flag::none
static allocation_flag none()
Do not change allocator behaviour.
Definition: allocation_flag.hpp:90
life.hpp
Functions for destroying arrays.
pmem::obj::allocation_flag
Type of flag which can be passed to make_persistent.
Definition: allocation_flag.hpp:60
pmem::transaction_scope_error
Custom transaction error class.
Definition: pexceptions.hpp:192
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:79
pmem::transaction_alloc_error
Custom transaction error class.
Definition: pexceptions.hpp:132