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 /*
54  * Workaround for missing "is_trivially_copyable" in gcc < 5.0.
55  * Be aware of a difference between __has_trivial_copy and is_trivially_copyable
56  * e.g. for deleted copy constructors __has_trivial_copy(A) returns 1 in clang
57  * and 0 in gcc. It means that for gcc < 5 IS_TRIVIALLY_COPYABLE is more
58  * restrictive than is_trivially_copyable.
59  */
60 #if !defined(__clang__) && defined(__GNUG__) && __GNUC__ < 5
61 #define IS_TRIVIALLY_COPYABLE(T) __has_trivial_copy(T)
62 #else
63 #define IS_TRIVIALLY_COPYABLE(T) std::is_trivially_copyable<T>::value
64 #endif
65 
66 namespace pmem
67 {
68 
69 namespace obj
70 {
71 template <typename T>
73 }
74 
75 namespace detail
76 {
77 
78 /*
79  * Conditionally add 'count' objects to a transaction.
80  *
81  * Adds count objects starting from `that` to the transaction if '*that' is
82  * within a pmemobj pool and there is an active transaction.
83  * Does nothing otherwise.
84  *
85  * @param[in] that pointer to the first object being added to the transaction.
86  * @param[in] count number of elements to be added to the transaction.
87  */
88 template <typename T>
89 inline void
90 conditional_add_to_tx(const T *that, std::size_t count = 1)
91 {
92  if (count == 0)
93  return;
94 
95  if (pmemobj_tx_stage() != TX_STAGE_WORK)
96  return;
97 
98  /* 'that' is not in any open pool */
99  if (!pmemobj_pool_by_ptr(that))
100  return;
101 
102  if (pmemobj_tx_add_range_direct(that, sizeof(*that) * count))
103  throw transaction_error(
104  "Could not add object(s) to the transaction.");
105 }
106 
107 /*
108  * Return type number for given type.
109  */
110 template <typename T>
111 uint64_t
112 type_num()
113 {
114  return typeid(T).hash_code();
115 }
116 
120 inline uint64_t
121 next_pow_2(uint64_t v)
122 {
123  v--;
124  v |= v >> 1;
125  v |= v >> 2;
126  v |= v >> 4;
127  v |= v >> 8;
128  v |= v >> 16;
129  v |= v >> 32;
130  ++v;
131  return v + (v == 0);
132 }
133 
137 inline uint64_t
138 next_pow_2(uint32_t v)
139 {
140  v--;
141  v |= v >> 1;
142  v |= v >> 2;
143  v |= v >> 4;
144  v |= v >> 8;
145  v |= v >> 16;
146  ++v;
147  return v + (v == 0);
148 }
149 
150 } /* namespace detail */
151 
152 } /* namespace pmem */
153 
154 #endif /* LIBPMEMOBJ_CPP_COMMON_HPP */
Persistent pointer class.
Definition: common.hpp:72
uint64_t next_pow_2(uint64_t v)
Round up to the next lowest power of 2.
Definition: common.hpp:121
Custom exceptions.
Custom transaction error class.
Definition: pexceptions.hpp:63
Definition: allocator.hpp:48