PMDK C++ bindings  1.7.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)
100  "failed to allocate persistent memory array");
101 
102  /*
103  * cache raw pointer to data - using persistent_ptr.get() in a loop
104  * is expensive.
105  */
106  auto data = ptr.get();
107 
108  /*
109  * When an exception is thrown from one of the constructors
110  * we don't perform any cleanup - i.e. we don't call destructors
111  * (unlike new[] operator), we only rely on transaction abort.
112  * This approach was taken to ensure consistent behaviour for
113  * case when transaction is aborted after make_persistent completes and
114  * we have no way to call destructors.
115  */
116  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
117  detail::create<I>(data + i);
118 
119  return ptr;
120 }
121 
137 template <typename T>
138 typename detail::pp_if_size_array<T>::type
140 {
141  typedef typename detail::pp_array_type<T>::type I;
142  enum { N = detail::pp_array_elems<T>::elems };
143 
144  if (pmemobj_tx_stage() != TX_STAGE_WORK)
146  "refusing to allocate memory outside of transaction scope");
147 
148  persistent_ptr<T> ptr = pmemobj_tx_xalloc(
149  sizeof(I) * N, detail::type_num<I>(), flag.value);
150 
151  if (ptr == nullptr)
153  "failed to allocate persistent memory array");
154 
155  /*
156  * cache raw pointer to data - using persistent_ptr.get() in a loop
157  * is expensive.
158  */
159  auto data = ptr.get();
160 
161  /*
162  * When an exception is thrown from one of the constructors
163  * we don't perform any cleanup - i.e. we don't call destructors
164  * (unlike new[] operator), we only rely on transaction abort.
165  * This approach was taken to ensure consistent behaviour for
166  * case when transaction is aborted after make_persistent completes and
167  * we have no way to call destructors.
168  */
169  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
170  detail::create<I>(data + i);
171 
172  return ptr;
173 }
174 
190 template <typename T>
191 void
192 delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
193 {
194  typedef typename detail::pp_array_type<T>::type I;
195 
196  if (pmemobj_tx_stage() != TX_STAGE_WORK)
198  "refusing to free memory outside of transaction scope");
199 
200  if (ptr == nullptr)
201  return;
202 
203  /*
204  * cache raw pointer to data - using persistent_ptr.get() in a loop
205  * is expensive.
206  */
207  auto data = ptr.get();
208 
209  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
210  detail::destroy<I>(
211  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
212 
213  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
215  "failed to delete persistent memory object");
216 }
217 
232 template <typename T>
233 void
234 delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
235 {
236  typedef typename detail::pp_array_type<T>::type I;
237  enum { N = detail::pp_array_elems<T>::elems };
238 
239  if (pmemobj_tx_stage() != TX_STAGE_WORK)
241  "refusing to free memory outside of transaction scope");
242 
243  if (ptr == nullptr)
244  return;
245 
246  /*
247  * cache raw pointer to data - using persistent_ptr.get() in a loop
248  * is expensive.
249  */
250  auto data = ptr.get();
251 
252  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
253  detail::destroy<I>(
254  data[static_cast<std::ptrdiff_t>(N) - 1 - i]);
255 
256  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
258  "failed to delete persistent memory object");
259 }
260 
261 } /* namespace obj */
262 
263 } /* namespace pmem */
264 
265 #endif /* LIBPMEMOBJ_CPP_MAKE_PERSISTENT_ARRAY_HPP */
Custom transaction error class.
Definition: pexceptions.hpp:84
Persistent pointer class.
Definition: common.hpp:119
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:138
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
Functions for destroying arrays.
Commonly used functionality.
Custom exceptions.
Compile time type check for make_persistent.
Type of flag which can be passed to make_persistent.
Definition: allocation_flag.hpp:60
Helper functionality for handling variadic templates.
allocation_flag - defines flags which can be passed to make_persistent
Common array traits.
Custom transaction error class.
Definition: pexceptions.hpp:94
Custom transaction error class.
Definition: pexceptions.hpp:104
element_type * get() const noexcept
Get a direct pointer.
Definition: persistent_ptr_base.hpp:286
static allocation_flag none()
Do not change allocator behaviour.
Definition: allocation_flag.hpp:90