PMDK C++ bindings  1.7.1
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 LIBPMEMOBJ_CPP_IS_TRIVIALLY_COPYABLE
96  * is more restrictive than is_trivially_copyable.
97  */
98 #if !defined(LIBPMEMOBJ_CPP_USE_HAS_TRIVIAL_COPY)
99 #if !defined(__clang__) && defined(__GNUG__) && __GNUC__ < 5
100 #define LIBPMEMOBJ_CPP_USE_HAS_TRIVIAL_COPY 1
101 #else
102 #define LIBPMEMOBJ_CPP_USE_HAS_TRIVIAL_COPY 0
103 #endif
104 #endif
105 
106 #if LIBPMEMOBJ_CPP_USE_HAS_TRIVIAL_COPY
107 #define LIBPMEMOBJ_CPP_IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
108 #else
109 #define LIBPMEMOBJ_CPP_IS_TRIVIALLY_COPYABLE(T) \
110  std::is_trivially_copyable<T>::value
111 #endif
112 
113 namespace pmem
114 {
115 
116 namespace obj
117 {
118 template <typename T>
120 }
121 
122 namespace detail
123 {
124 
125 /*
126  * Conditionally add 'count' objects to a transaction.
127  *
128  * Adds count objects starting from `that` to the transaction if '*that' is
129  * within a pmemobj pool and there is an active transaction.
130  * Does nothing otherwise.
131  *
132  * @param[in] that pointer to the first object being added to the transaction.
133  * @param[in] count number of elements to be added to the transaction.
134  */
135 template <typename T>
136 inline void
137 conditional_add_to_tx(const T *that, std::size_t count = 1)
138 {
139  if (count == 0)
140  return;
141 
142  if (pmemobj_tx_stage() != TX_STAGE_WORK)
143  return;
144 
145  /* 'that' is not in any open pool */
146  if (!pmemobj_pool_by_ptr(that))
147  return;
148 
149  if (pmemobj_tx_add_range_direct(that, sizeof(*that) * count))
150  throw transaction_error(
151  "Could not add object(s) to the transaction.");
152 }
153 
154 /*
155  * Return type number for given type.
156  */
157 template <typename T>
158 uint64_t
159 type_num()
160 {
161  return typeid(T).hash_code();
162 }
163 
167 inline uint64_t
168 next_pow_2(uint64_t v)
169 {
170  v--;
171  v |= v >> 1;
172  v |= v >> 2;
173  v |= v >> 4;
174  v |= v >> 8;
175  v |= v >> 16;
176  v |= v >> 32;
177  ++v;
178  return v + (v == 0);
179 }
180 
184 inline uint64_t
185 next_pow_2(uint32_t v)
186 {
187  v--;
188  v |= v >> 1;
189  v |= v >> 2;
190  v |= v >> 4;
191  v |= v >> 8;
192  v |= v >> 16;
193  ++v;
194  return v + (v == 0);
195 }
196 
197 } /* namespace detail */
198 
199 } /* namespace pmem */
200 
201 #endif /* LIBPMEMOBJ_CPP_COMMON_HPP */
Persistent pointer class.
Definition: common.hpp:119
uint64_t next_pow_2(uint64_t v)
Round up to the next lowest power of 2.
Definition: common.hpp:168
Custom exceptions.
Custom transaction error class.
Definition: pexceptions.hpp:63