PMDK C++ bindings  1.2.0
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-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)
90  "refusing to allocate memory outside of transaction scope");
91 
92  persistent_ptr<T> ptr =
93  pmemobj_tx_alloc(sizeof(I) * N, detail::type_num<I>());
94 
95  if (ptr == nullptr)
97  "failed to allocate persistent memory array");
98 
99  /*
100  * When an exception is thrown from one of the constructors
101  * we don't perform any cleanup - i.e. we don't call destructors
102  * (unlike new[] operator), we only rely on transaction abort.
103  * This approach was taken to ensure consistent behaviour for
104  * case when transaction is aborted after make_persistent completes and
105  * we have no way to call destructors.
106  */
107  for (std::size_t i = 0; i < N; ++i)
108  detail::create<I>(ptr.get() + i);
109 
110  return ptr;
111 }
112 
126 template <typename T>
127 typename detail::pp_if_size_array<T>::type
129 {
130  typedef typename detail::pp_array_type<T>::type I;
131  enum { N = detail::pp_array_elems<T>::elems };
132 
133  if (pmemobj_tx_stage() != TX_STAGE_WORK)
135  "refusing to allocate memory outside of transaction scope");
136 
137  persistent_ptr<T> ptr =
138  pmemobj_tx_alloc(sizeof(I) * N, detail::type_num<I>());
139 
140  if (ptr == nullptr)
142  "failed to allocate persistent memory array");
143 
144  /*
145  * When an exception is thrown from one of the constructors
146  * we don't perform any cleanup - i.e. we don't call destructors
147  * (unlike new[] operator), we only rely on transaction abort.
148  * This approach was taken to ensure consistent behaviour for
149  * case when transaction is aborted after make_persistent completes and
150  * we have no way to call destructors.
151  */
152  for (std::size_t i = 0; i < N; ++i)
153  detail::create<I>(ptr.get() + i);
154 
155  return ptr;
156 }
157 
173 template <typename T>
174 void
175 delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
176 {
177  typedef typename detail::pp_array_type<T>::type I;
178 
179  if (pmemobj_tx_stage() != TX_STAGE_WORK)
181  "refusing to free memory outside of transaction scope");
182 
183  if (ptr == nullptr)
184  return;
185 
186  for (std::size_t i = 0; i < N; ++i)
187  detail::destroy<I>(ptr[N - 1 - i]);
188 
189  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
191  "failed to delete persistent memory object");
192 }
193 
208 template <typename T>
209 void
210 delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
211 {
212  typedef typename detail::pp_array_type<T>::type I;
213  enum { N = detail::pp_array_elems<T>::elems };
214 
215  if (pmemobj_tx_stage() != TX_STAGE_WORK)
217  "refusing to free memory outside of transaction scope");
218 
219  if (ptr == nullptr)
220  return;
221 
222  for (std::size_t i = 0; i < N; ++i)
223  detail::destroy<I>(ptr[N - 1 - i]);
224 
225  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
227  "failed to delete persistent memory object");
228 }
229 
230 } /* namespace obj */
231 
232 } /* namespace pmem */
233 
234 #endif /* LIBPMEMOBJ_CPP_MAKE_PERSISTENT_ARRAY_HPP */
Custom transaction error class.
Definition: pexceptions.hpp:84
Persistent pointer class.
Definition: common.hpp:72
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:109
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
Functions for destroying arrays.
Commonly used functionality.
Custom exceptions.
Compile time type check for make_persistent.
Common array traits.
Custom transaction error class.
Definition: pexceptions.hpp:94
Custom transaction error class.
Definition: pexceptions.hpp:104
Definition: allocator.hpp:48
element_type * get() const noexcept
Get a direct pointer.
Definition: persistent_ptr_base.hpp:286