PMDK C++ bindings  1.9
This is the C++ bindings documentation for PMDK's libpmemobj.
make_persistent_array.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_ARRAY_HPP
41 #define LIBPMEMOBJ_CPP_MAKE_PERSISTENT_ARRAY_HPP
42 
50 #include <libpmemobj/tx_base.h>
51 
52 #include <cassert>
53 #include <limits>
54 
55 namespace pmem
56 {
57 
58 namespace obj
59 {
60 
77 template <typename T>
78 typename detail::pp_if_array<T>::type
80 {
81  typedef typename detail::pp_array_type<T>::type I;
82 
83  /*
84  * Allowing N greater than ptrdiff_t max value would cause problems
85  * with accessing array and calculating address difference between two
86  * elements placed further apart than ptrdiff_t max value
87  */
88  assert(N <=
89  static_cast<std::size_t>(std::numeric_limits<ptrdiff_t>::max()));
90 
91  if (pmemobj_tx_stage() != TX_STAGE_WORK)
93  "refusing to allocate memory outside of transaction scope");
94 
95  persistent_ptr<T> ptr = pmemobj_tx_xalloc(
96  sizeof(I) * N, detail::type_num<I>(), flag.value);
97 
98  if (ptr == nullptr) {
99  if (errno == ENOMEM)
101  "Failed to allocate persistent memory array")
102  .with_pmemobj_errormsg();
103  else
105  "Failed to allocate persistent memory array")
106  .with_pmemobj_errormsg();
107  }
108 
109  /*
110  * cache raw pointer to data - using persistent_ptr.get() in a loop
111  * is expensive.
112  */
113  auto data = ptr.get();
114 
115  /*
116  * When an exception is thrown from one of the constructors
117  * we don't perform any cleanup - i.e. we don't call destructors
118  * (unlike new[] operator), we only rely on transaction abort.
119  * This approach was taken to ensure consistent behaviour for
120  * case when transaction is aborted after make_persistent completes and
121  * we have no way to call destructors.
122  */
123  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
124  detail::create<I>(data + i);
125 
126  return ptr;
127 }
128 
144 template <typename T>
145 typename detail::pp_if_size_array<T>::type
147 {
148  typedef typename detail::pp_array_type<T>::type I;
149  enum { N = detail::pp_array_elems<T>::elems };
150 
151  if (pmemobj_tx_stage() != TX_STAGE_WORK)
153  "refusing to allocate memory outside of transaction scope");
154 
155  persistent_ptr<T> ptr = pmemobj_tx_xalloc(
156  sizeof(I) * N, detail::type_num<I>(), flag.value);
157 
158  if (ptr == nullptr) {
159  if (errno == ENOMEM)
161  "Failed to allocate persistent memory array")
162  .with_pmemobj_errormsg();
163  else
165  "Failed to allocate persistent memory array")
166  .with_pmemobj_errormsg();
167  }
168 
169  /*
170  * cache raw pointer to data - using persistent_ptr.get() in a loop
171  * is expensive.
172  */
173  auto data = ptr.get();
174 
175  /*
176  * When an exception is thrown from one of the constructors
177  * we don't perform any cleanup - i.e. we don't call destructors
178  * (unlike new[] operator), we only rely on transaction abort.
179  * This approach was taken to ensure consistent behaviour for
180  * case when transaction is aborted after make_persistent completes and
181  * we have no way to call destructors.
182  */
183  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
184  detail::create<I>(data + i);
185 
186  return ptr;
187 }
188 
207 template <typename T>
208 void
209 delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
210 {
211  typedef typename detail::pp_array_type<T>::type I;
212 
213  if (pmemobj_tx_stage() != TX_STAGE_WORK)
215  "refusing to free memory outside of transaction scope");
216 
217  if (ptr == nullptr)
218  return;
219 
220  /*
221  * cache raw pointer to data - using persistent_ptr.get() in a loop
222  * is expensive.
223  */
224  auto data = ptr.get();
225 
226  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
227  detail::destroy<I>(
228  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
229 
230  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
232  "failed to delete persistent memory object")
233  .with_pmemobj_errormsg();
234 }
235 
253 template <typename T>
254 void
255 delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
256 {
257  typedef typename detail::pp_array_type<T>::type I;
258  enum { N = detail::pp_array_elems<T>::elems };
259 
260  if (pmemobj_tx_stage() != TX_STAGE_WORK)
262  "refusing to free memory outside of transaction scope");
263 
264  if (ptr == nullptr)
265  return;
266 
267  /*
268  * cache raw pointer to data - using persistent_ptr.get() in a loop
269  * is expensive.
270  */
271  auto data = ptr.get();
272 
273  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
274  detail::destroy<I>(
275  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
276 
277  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
279  "failed to delete persistent memory object")
280  .with_pmemobj_errormsg();
281 }
282 
283 } /* namespace obj */
284 
285 } /* namespace pmem */
286 
287 #endif /* LIBPMEMOBJ_CPP_MAKE_PERSISTENT_ARRAY_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:169
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:44
pmem::transaction_out_of_memory
Custom out of memory error class.
Definition: pexceptions.hpp:149
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
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
array_traits.hpp
Common array traits.
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:187
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:130