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-2017, 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 PMEMOBJ_MAKE_PERSISTENT_ARRAY_HPP
41#define PMEMOBJ_MAKE_PERSISTENT_ARRAY_HPP
42
48#include "libpmemobj/tx_base.h"
49
50namespace pmem
51{
52
53namespace obj
54{
55
70template <typename T>
71typename detail::pp_if_array<T>::type
72make_persistent(std::size_t N)
73{
74 typedef typename detail::pp_array_type<T>::type I;
75
76 if (pmemobj_tx_stage() != TX_STAGE_WORK)
78 "refusing to allocate "
79 "memory outside of transaction scope");
80
82 pmemobj_tx_alloc(sizeof(I) * N, detail::type_num<I>());
83
84 if (ptr == nullptr)
85 throw transaction_alloc_error("failed to allocate "
86 "persistent memory array");
87
88 std::size_t i;
89 try {
90 for (i = 0; i < N; ++i)
91 detail::create<I>(ptr.get() + i);
92 } catch (...) {
93 for (std::size_t j = 1; j <= i; ++j)
94 detail::destroy<I>(ptr[i - j]);
95 pmemobj_tx_free(*ptr.raw_ptr());
96 throw;
97 }
98
99 return ptr;
100}
101
114template <typename T>
115typename detail::pp_if_size_array<T>::type
117{
118 typedef typename detail::pp_array_type<T>::type I;
119 enum { N = detail::pp_array_elems<T>::elems };
120
121 if (pmemobj_tx_stage() != TX_STAGE_WORK)
123 "refusing to allocate "
124 "memory outside of transaction scope");
125
127 pmemobj_tx_alloc(sizeof(I) * N, detail::type_num<I>());
128
129 if (ptr == nullptr)
130 throw transaction_alloc_error("failed to allocate "
131 "persistent memory array");
132
133 std::size_t i;
134 try {
135 for (i = 0; i < N; ++i)
136 detail::create<I>(ptr.get() + i);
137 } catch (...) {
138 for (std::size_t j = 1; j <= i; ++j)
139 detail::destroy<I>(ptr[i - j]);
140 pmemobj_tx_free(*ptr.raw_ptr());
141 throw;
142 }
143
144 return ptr;
145}
146
162template <typename T>
163void
164delete_persistent(typename detail::pp_if_array<T>::type ptr, std::size_t N)
165{
166 typedef typename detail::pp_array_type<T>::type I;
167
168 if (pmemobj_tx_stage() != TX_STAGE_WORK)
170 "refusing to free "
171 "memory outside of transaction scope");
172
173 if (ptr == nullptr)
174 return;
175
176 for (std::size_t i = 0; i < N; ++i)
177 detail::destroy<I>(ptr[N - 1 - i]);
178
179 if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
180 throw transaction_free_error("failed to delete "
181 "persistent memory object");
182}
183
198template <typename T>
199void
200delete_persistent(typename detail::pp_if_size_array<T>::type ptr)
201{
202 typedef typename detail::pp_array_type<T>::type I;
203 enum { N = detail::pp_array_elems<T>::elems };
204
205 if (pmemobj_tx_stage() != TX_STAGE_WORK)
207 "refusing to free "
208 "memory outside of transaction scope");
209
210 if (ptr == nullptr)
211 return;
212
213 for (std::size_t i = 0; i < N; ++i)
214 detail::destroy<I>(ptr[N - 1 - i]);
215
216 if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
217 throw transaction_free_error("failed to delete "
218 "persistent memory object");
219}
220
221} /* namespace obj */
222
223} /* namespace pmem */
224
225#endif /* PMEMOBJ_MAKE_PERSISTENT_ARRAY_HPP */
Common array traits.
Compile time type check for make_persistent.
element_type * get() const noexcept
Get a direct pointer.
Definition: persistent_ptr_base.hpp:278
PMEMoid * raw_ptr() noexcept
Get pointer to PMEMoid encapsulated by this object.
Definition: persistent_ptr_base.hpp:309
Persistent pointer class.
Definition: persistent_ptr.hpp:102
Custom transaction error class.
Definition: pexceptions.hpp:84
Custom transaction error class.
Definition: pexceptions.hpp:94
Custom transaction error class.
Definition: pexceptions.hpp:104
Commonly used functionality.
Functions for destroying arrays.
detail::pp_if_not_array< T >::type make_persistent(Args &&... args)
Transactionally allocate and construct an object of type T.
Definition: make_persistent.hpp:74
void delete_persistent(typename detail::pp_if_not_array< T >::type ptr)
Transactionally free an object of type T held in a persitent_ptr.
Definition: make_persistent.hpp:114
Custom exceptions.