PMDK C++ bindings  1.8.1
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-2019, 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 
204 template <typename T>
205 void
206 delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
207 {
208  typedef typename detail::pp_array_type<T>::type I;
209 
210  if (pmemobj_tx_stage() != TX_STAGE_WORK)
212  "refusing to free memory outside of transaction scope");
213 
214  if (ptr == nullptr)
215  return;
216 
217  /*
218  * cache raw pointer to data - using persistent_ptr.get() in a loop
219  * is expensive.
220  */
221  auto data = ptr.get();
222 
223  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
224  detail::destroy<I>(
225  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
226 
227  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
229  "failed to delete persistent memory object")
230  .with_pmemobj_errormsg();
231 }
232 
247 template <typename T>
248 void
249 delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
250 {
251  typedef typename detail::pp_array_type<T>::type I;
252  enum { N = detail::pp_array_elems<T>::elems };
253 
254  if (pmemobj_tx_stage() != TX_STAGE_WORK)
256  "refusing to free memory outside of transaction scope");
257 
258  if (ptr == nullptr)
259  return;
260 
261  /*
262  * cache raw pointer to data - using persistent_ptr.get() in a loop
263  * is expensive.
264  */
265  auto data = ptr.get();
266 
267  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
268  detail::destroy<I>(
269  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
270 
271  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
273  "failed to delete persistent memory object")
274  .with_pmemobj_errormsg();
275 }
276 
277 } /* namespace obj */
278 
279 } /* namespace pmem */
280 
281 #endif /* LIBPMEMOBJ_CPP_MAKE_PERSISTENT_ARRAY_HPP */
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:145
pmem::transaction_free_error
Custom transaction error class.
Definition: pexceptions.hpp:167
pmem
A persistent version of concurrent hash map implementation Ref: https://arxiv.org/abs/1509....
Definition: allocation_flag.hpp:44
pmem::transaction_out_of_memory
Custom out of memory error class.
Definition: pexceptions.hpp:147
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
common.hpp
Commonly used functionality.
pexceptions.hpp
Custom exceptions.
check_persistent_ptr_array.hpp
Compile time type check for make_persistent.
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:132
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:185
variadic.hpp
Helper functionality for handling variadic templates.
allocation_flag.hpp
allocation_flag - defines flags which can be passed to make_persistent
pmem::transaction_alloc_error
Custom transaction error class.
Definition: pexceptions.hpp:128