PMDK C++ bindings  1.5.2
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 
48 #include <libpmemobj/tx_base.h>
49 
50 #include <cassert>
51 #include <limits>
52 
53 namespace pmem
54 {
55 
56 namespace obj
57 {
58 
74 template <typename T>
75 typename detail::pp_if_array<T>::type
76 make_persistent(std::size_t N)
77 {
78  typedef typename detail::pp_array_type<T>::type I;
79 
80  /*
81  * Allowing N greater than ptrdiff_t max value would cause problems
82  * with accessing array and calculating address difference between two
83  * elements placed further apart than ptrdiff_t max value
84  */
85  assert(N <=
86  static_cast<std::size_t>(std::numeric_limits<ptrdiff_t>::max()));
87 
88  if (pmemobj_tx_stage() != TX_STAGE_WORK)
90  "refusing to allocate "
91  "memory outside of transaction scope");
92 
93  persistent_ptr<T> ptr =
94  pmemobj_tx_alloc(sizeof(I) * N, detail::type_num<I>());
95 
96  if (ptr == nullptr)
97  throw transaction_alloc_error("failed to allocate "
98  "persistent memory array");
99 
100  /*
101  * cache raw pointer to data - using persistent_ptr.get() in a loop
102  * is expensive.
103  */
104  auto data = ptr.get();
105 
106  /*
107  * When an exception is thrown from one of the constructors
108  * we don't perform any cleanup - i.e. we don't call destructors
109  * (unlike new[] operator), we only rely on transaction abort.
110  * This approach was taken to ensure consistent behaviour for
111  * case when transaction is aborted after make_persistent completes and
112  * we have no way to call destructors.
113  */
114  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
115  detail::create<I>(data + i);
116 
117  return ptr;
118 }
119 
133 template <typename T>
134 typename detail::pp_if_size_array<T>::type
136 {
137  typedef typename detail::pp_array_type<T>::type I;
138  enum { N = detail::pp_array_elems<T>::elems };
139 
140  if (pmemobj_tx_stage() != TX_STAGE_WORK)
142  "refusing to allocate "
143  "memory outside of transaction scope");
144 
145  persistent_ptr<T> ptr =
146  pmemobj_tx_alloc(sizeof(I) * N, detail::type_num<I>());
147 
148  if (ptr == nullptr)
149  throw transaction_alloc_error("failed to allocate "
150  "persistent memory array");
151 
152  /*
153  * cache raw pointer to data - using persistent_ptr.get() in a loop
154  * is expensive.
155  */
156  auto data = ptr.get();
157 
158  /*
159  * When an exception is thrown from one of the constructors
160  * we don't perform any cleanup - i.e. we don't call destructors
161  * (unlike new[] operator), we only rely on transaction abort.
162  * This approach was taken to ensure consistent behaviour for
163  * case when transaction is aborted after make_persistent completes and
164  * we have no way to call destructors.
165  */
166  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
167  detail::create<I>(data + i);
168 
169  return ptr;
170 }
171 
187 template <typename T>
188 void
189 delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
190 {
191  typedef typename detail::pp_array_type<T>::type I;
192 
193  if (pmemobj_tx_stage() != TX_STAGE_WORK)
195  "refusing to free "
196  "memory outside of transaction scope");
197 
198  if (ptr == nullptr)
199  return;
200 
201  /*
202  * cache raw pointer to data - using persistent_ptr.get() in a loop
203  * is expensive.
204  */
205  auto data = ptr.get();
206 
207  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
208  detail::destroy<I>(
209  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
210 
211  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
212  throw transaction_free_error("failed to delete "
213  "persistent memory object");
214 }
215 
230 template <typename T>
231 void
232 delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
233 {
234  typedef typename detail::pp_array_type<T>::type I;
235  enum { N = detail::pp_array_elems<T>::elems };
236 
237  if (pmemobj_tx_stage() != TX_STAGE_WORK)
239  "refusing to free "
240  "memory outside of transaction scope");
241 
242  if (ptr == nullptr)
243  return;
244 
245  /*
246  * cache raw pointer to data - using persistent_ptr.get() in a loop
247  * is expensive.
248  */
249  auto data = ptr.get();
250 
251  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
252  detail::destroy<I>(
253  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
254 
255  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
256  throw transaction_free_error("failed to delete "
257  "persistent memory object");
258 }
259 
260 } /* namespace obj */
261 
262 } /* namespace pmem */
263 
264 #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:110
pmem::transaction_free_error
Custom transaction error class.
Definition: pexceptions.hpp:94
pmem::obj::make_persistent
detail::pp_if_not_array< T >::type make_persistent(Args &&... args)
Transactionally allocate and construct an object of type T.
Definition: make_persistent.hpp:75
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
array_traits.hpp
Common array traits.
life.hpp
Functions for destroying arrays.
pmem::transaction_scope_error
Custom transaction error class.
Definition: pexceptions.hpp:104
pmem::transaction_alloc_error
Custom transaction error class.
Definition: pexceptions.hpp:84