PMDK C++ bindings  1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
 All Classes Files Functions Variables Typedefs Friends Pages
make_persistent_array.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2018, 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)
89  throw transaction_scope_error(
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  * When an exception is thrown from one of the constructors
102  * we don't perform any cleanup - i.e. we don't call destructors
103  * (unlike new[] operator), we only rely on transaction abort.
104  * This approach was taken to ensure consistent behaviour for
105  * case when transaction is aborted after make_persistent completes and
106  * we have no way to call destructors.
107  */
108  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
109  detail::create<I>(ptr.get() + i);
110 
111  return ptr;
112 }
113 
127 template <typename T>
128 typename detail::pp_if_size_array<T>::type
129 make_persistent()
130 {
131  typedef typename detail::pp_array_type<T>::type I;
132  enum { N = detail::pp_array_elems<T>::elems };
133 
134  if (pmemobj_tx_stage() != TX_STAGE_WORK)
135  throw transaction_scope_error(
136  "refusing to allocate "
137  "memory outside of transaction scope");
138 
139  persistent_ptr<T> ptr =
140  pmemobj_tx_alloc(sizeof(I) * N, detail::type_num<I>());
141 
142  if (ptr == nullptr)
143  throw transaction_alloc_error("failed to allocate "
144  "persistent memory array");
145 
146  /*
147  * When an exception is thrown from one of the constructors
148  * we don't perform any cleanup - i.e. we don't call destructors
149  * (unlike new[] operator), we only rely on transaction abort.
150  * This approach was taken to ensure consistent behaviour for
151  * case when transaction is aborted after make_persistent completes and
152  * we have no way to call destructors.
153  */
154  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
155  detail::create<I>(ptr.get() + i);
156 
157  return ptr;
158 }
159 
175 template <typename T>
176 void
177 delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
178 {
179  typedef typename detail::pp_array_type<T>::type I;
180 
181  if (pmemobj_tx_stage() != TX_STAGE_WORK)
182  throw transaction_scope_error(
183  "refusing to free "
184  "memory outside of transaction scope");
185 
186  if (ptr == nullptr)
187  return;
188 
189  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
190  detail::destroy<I>(ptr[static_cast<std::ptrdiff_t>(N) - 1 - i]);
191 
192  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
193  throw transaction_free_error("failed to delete "
194  "persistent memory object");
195 }
196 
211 template <typename T>
212 void
213 delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
214 {
215  typedef typename detail::pp_array_type<T>::type I;
216  enum { N = detail::pp_array_elems<T>::elems };
217 
218  if (pmemobj_tx_stage() != TX_STAGE_WORK)
219  throw transaction_scope_error(
220  "refusing to free "
221  "memory outside of transaction scope");
222 
223  if (ptr == nullptr)
224  return;
225 
226  for (std::ptrdiff_t i = 0; i < static_cast<std::ptrdiff_t>(N); ++i)
227  detail::destroy<I>(ptr[static_cast<std::ptrdiff_t>(N) - 1 - i]);
228 
229  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
230  throw transaction_free_error("failed to delete "
231  "persistent memory object");
232 }
233 
234 } /* namespace obj */
235 
236 } /* namespace pmem */
237 
238 #endif /* LIBPMEMOBJ_CPP_MAKE_PERSISTENT_ARRAY_HPP */
Functions for destroying arrays.
Commonly used functionality.
Custom exceptions.
Compile time type check for make_persistent.
Common array traits.
Definition: allocator.hpp:48