PMDK C++ bindings  1.2.0
This is the C++ bindings documentation for PMDK's libpmemobj.
common.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 
38 #ifndef LIBPMEMOBJ_CPP_COMMON_HPP
39 #define LIBPMEMOBJ_CPP_COMMON_HPP
40 
42 #include <libpmemobj/tx_base.h>
43 #include <typeinfo>
44 
45 #if defined(__GNUC__) || defined(__clang__)
46 #define POBJ_CPP_DEPRECATED __attribute__((deprecated))
47 #elif defined(_MSC_VER)
48 #define POBJ_CPP_DEPRECATED __declspec(deprecated)
49 #else
50 #define POBJ_CPP_DEPRECATED
51 #endif
52 
53 #if LIBPMEMOBJ_CPP_VG_ENABLED
54 #undef LIBPMEMOBJ_CPP_VG_PMEMCHECK_ENABLED
55 #undef LIBPMEMOBJ_CPP_VG_MEMCHECK_ENABLED
56 #undef LIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED
57 #undef LIBPMEMOBJ_CPP_VG_DRD_ENABLED
58 
59 #define LIBPMEMOBJ_CPP_VG_PMEMCHECK_ENABLED 1
60 #define LIBPMEMOBJ_CPP_VG_MEMCHECK_ENABLED 1
61 #define LIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED 1
62 #define LIBPMEMOBJ_CPP_VG_DRD_ENABLED 1
63 #endif
64 
65 #if LIBPMEMOBJ_CPP_VG_PMEMCHECK_ENABLED || \
66  LIBPMEMOBJ_CPP_VG_MEMCHECK_ENABLED || \
67  LIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED || LIBPMEMOBJ_CPP_VG_DRD_ENABLED
68 #define LIBPMEMOBJ_CPP_ANY_VG_TOOL_ENABLED 1
69 #endif
70 
71 #if LIBPMEMOBJ_CPP_ANY_VG_TOOL_ENABLED
72 #include <valgrind.h>
73 #endif
74 
75 #if LIBPMEMOBJ_CPP_VG_PMEMCHECK_ENABLED
76 #include <pmemcheck.h>
77 #endif
78 
79 #if LIBPMEMOBJ_CPP_VG_MEMCHECK_ENABLED
80 #include <memcheck.h>
81 #endif
82 
83 #if LIBPMEMOBJ_CPP_VG_HELGRIND_ENABLED
84 #include <helgrind.h>
85 #endif
86 
87 #if LIBPMEMOBJ_CPP_VG_DRD_ENABLED
88 #include <drd.h>
89 #endif
90 
91 /*
92  * Workaround for missing "is_trivially_copyable" in gcc < 5.0.
93  * Be aware of a difference between __has_trivial_copy and is_trivially_copyable
94  * e.g. for deleted copy constructors __has_trivial_copy(A) returns 1 in clang
95  * and 0 in gcc. It means that for gcc < 5 IS_TRIVIALLY_COPYABLE is more
96  * restrictive than is_trivially_copyable.
97  */
98 #if !defined(__clang__) && defined(__GNUG__) && __GNUC__ < 5
99 #define IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
100 #else
101 #define IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
102 #endif
103 
104 namespace pmem
105 {
106 
107 namespace obj
108 {
109 template <typename T>
111 }
112 
113 namespace detail
114 {
115 
116 /*
117  * Conditionally add 'count' objects to a transaction.
118  *
119  * Adds count objects starting from `that` to the transaction if '*that' is
120  * within a pmemobj pool and there is an active transaction.
121  * Does nothing otherwise.
122  *
123  * @param[in] that pointer to the first object being added to the transaction.
124  * @param[in] count number of elements to be added to the transaction.
125  */
126 template <typename T>
127 inline void
128 conditional_add_to_tx(const T *that, std::size_t count = 1)
129 {
130  if (count == 0)
131  return;
132 
133  if (pmemobj_tx_stage() != TX_STAGE_WORK)
134  return;
135 
136  /* 'that' is not in any open pool */
137  if (!pmemobj_pool_by_ptr(that))
138  return;
139 
140  if (pmemobj_tx_add_range_direct(that, sizeof(*that) * count))
141  throw transaction_error(
142  "Could not add object(s) to the transaction.");
143 }
144 
145 /*
146  * Return type number for given type.
147  */
148 template <typename T>
149 uint64_t
150 type_num()
151 {
152  return typeid(T).hash_code();
153 }
154 
158 inline uint64_t
159 next_pow_2(uint64_t v)
160 {
161  v--;
162  v |= v >> 1;
163  v |= v >> 2;
164  v |= v >> 4;
165  v |= v >> 8;
166  v |= v >> 16;
167  v |= v >> 32;
168  ++v;
169  return v + (v == 0);
170 }
171 
175 inline uint64_t
176 next_pow_2(uint32_t v)
177 {
178  v--;
179  v |= v >> 1;
180  v |= v >> 2;
181  v |= v >> 4;
182  v |= v >> 8;
183  v |= v >> 16;
184  ++v;
185  return v + (v == 0);
186 }
187 
188 } /* namespace detail */
189 
190 } /* namespace pmem */
191 
192 #endif /* LIBPMEMOBJ_CPP_COMMON_HPP */
Persistent pointer class.
Definition: common.hpp:110
uint64_t next_pow_2(uint64_t v)
Round up to the next lowest power of 2.
Definition: common.hpp:159
Custom exceptions.
Custom transaction error class.
Definition: pexceptions.hpp:63
Definition: allocation_flag.hpp:43