NVML C++ bindings  0.1 experimental
This is the C++ bindings documentation for NVML's libpmemobj.
make_persistent.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016, 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_HPP
41 #define PMEMOBJ_MAKE_PERSISTENT_HPP
42 
46 #include "libpmemobj/tx_base.h"
47 
48 #include <new>
49 
50 namespace nvml
51 {
52 
53 namespace obj
54 {
55 
70 template <typename T, typename... Args>
71 typename detail::pp_if_not_array<T>::type
72 make_persistent(Args &&... args)
73 {
74  if (pmemobj_tx_stage() != TX_STAGE_WORK)
76  "refusing to allocate "
77  "memory outside of transaction scope");
78 
79  persistent_ptr<T> ptr =
80  pmemobj_tx_alloc(sizeof(T), detail::type_num<T>());
81 
82  if (ptr == nullptr)
83  throw transaction_alloc_error("failed to allocate "
84  "persistent memory object");
85  try {
86  new (ptr.get()) T(args...);
87  } catch (...) {
88  pmemobj_tx_free(*ptr.raw_ptr());
89  throw;
90  }
91 
92  return ptr;
93 }
94 
109 template <typename T>
110 void
112 {
113  if (pmemobj_tx_stage() != TX_STAGE_WORK)
115  "refusing to free "
116  "memory outside of transaction scope");
117 
118  if (ptr == nullptr)
119  return;
120 
121  /*
122  * At this point, everything in the object should be tracked
123  * and reverted on transaction abort.
124  */
125  ptr->T::~T();
126 
127  if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
128  throw transaction_alloc_error("failed to delete "
129  "persistent memory object");
130 }
131 
132 } /* namespace obj */
133 
134 } /* namespace nvml */
135 
136 #endif /* PMEMOBJ_MAKE_PERSISTENT_HPP */
Custom transaction error class.
Definition: pexceptions.hpp:94
Persistent pointer class.
Definition: persistent_ptr.hpp:72
PMEMoid * raw_ptr() noexcept
Get pointer to PMEMoid encapsulated by this object.
Definition: persistent_ptr.hpp:289
Commonly used functionality.
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:111
Custom exceptions.
Compile time type check for 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:72
Definition: condition_variable.hpp:48
element_type * get() const noexcept
Get a direct pointer.
Definition: persistent_ptr.hpp:246
Custom transaction error class.
Definition: pexceptions.hpp:84