PMDK C++ bindings  1.13.0-git107.g7e59f08f
This is the C++ bindings documentation for PMDK's libpmemobj.
defrag.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2020, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_DEFRAG_HPP
10 #define LIBPMEMOBJ_CPP_DEFRAG_HPP
11 
12 #include <type_traits>
13 #include <vector>
14 
17 #include <libpmemobj++/pool.hpp>
18 #include <libpmemobj/atomic_base.h>
19 #include <libpmemobj/base.h>
20 
21 namespace pmem
22 {
23 
24 namespace detail
25 {
26 using for_each_ptr_function = void (*)(obj::persistent_ptr_base &ptr);
27 
28 template <typename T>
29 using t_has_for_each_ptr = typename std::enable_if<
30  std::is_same<decltype(std::declval<T>().for_each_ptr(
31  std::declval<for_each_ptr_function>())),
32  void>::value>::type;
33 
34 template <typename T>
35 using t_is_defragmentable = supports<T, t_has_for_each_ptr>;
36 }
37 
38 namespace obj
39 {
48 template <typename T>
49 static constexpr typename std::enable_if<detail::t_is_defragmentable<T>::value,
50  bool>::type
52 {
53  return true;
54 }
55 
62 template <typename T>
63 static constexpr typename std::enable_if<!detail::t_is_defragmentable<T>::value,
64  bool>::type
66 {
67  return false;
68 }
69 
83 class defrag {
84 public:
94  {
95  this->pop = p;
96  }
97 
110  template <typename T>
111  typename std::enable_if<is_defragmentable<T>(), void>::type
112  add(T &t)
113  {
114  if (pmemobj_pool_by_ptr(&t) != pop.handle())
115  throw std::runtime_error(
116  "object is not from the chosen pool");
117 
118  t.for_each_ptr([&](persistent_ptr_base &ptr) {
119  this->container.push_back(&ptr);
120  });
121  }
122 
138  template <typename T, typename = T>
139  typename std::enable_if<!is_defragmentable<T>(), void>::type
140  add(T &t)
141  {
142  if (pmemobj_pool_by_ptr(&t) != pop.handle())
143  throw std::runtime_error(
144  "object is not from the chosen pool");
145  }
146 
160  template <typename T>
161  void
163  {
164  if (pmemobj_pool_by_oid(ptr.raw()) != pop.handle())
165  throw std::runtime_error(
166  "persistent_ptr does not point to an object from the chosen pool");
167 
168  this->container.push_back(&ptr);
169  /* Calls 'add(T &)' passing the underlying object (T) */
170  this->add<T>(*ptr);
171  }
172 
187  pobj_defrag_result
188  run()
189  {
190  pobj_defrag_result result = this->pop.defrag(
191  this->container.data(), this->container.size());
192 
193  return result;
194  }
195 
196 private:
197  std::vector<persistent_ptr_base *> container;
198  pool_base pop;
199 };
200 
201 } /* namespace obj */
202 
203 } /* namespace pmem */
204 
205 #endif /* LIBPMEMOBJ_CPP_DEFRAG_HPP */
Defrag class.
Definition: defrag.hpp:83
void add(persistent_ptr< T > &ptr)
Stores address of a persistent_ptr to the defragmentation queue.
Definition: defrag.hpp:162
pobj_defrag_result run()
Starts defragmentation with previously stored pointers.
Definition: defrag.hpp:188
std::enable_if< is_defragmentable< T >), void >::type add(T &t)
Stores address of the referenced object to the defragmentation queue.
Definition: defrag.hpp:112
std::enable_if<!is_defragmentable< T >), void >::type add(T &t)
Specialization for non-defragmentable types.
Definition: defrag.hpp:140
defrag(pool_base p)
The only allowed ctor.
Definition: defrag.hpp:93
Resides on pmem class.
Definition: p.hpp:36
Persistent_ptr base (non-template) class.
Definition: persistent_ptr_base.hpp:42
const PMEMoid & raw() const noexcept
Get PMEMoid encapsulated by this object.
Definition: persistent_ptr_base.hpp:151
Persistent pointer class.
Definition: persistent_ptr.hpp:153
The non-template pool base class.
Definition: pool.hpp:51
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:395
pobj_defrag_result defrag(persistent_ptr_base **ptrv, size_t oidcnt)
Starts defragmentation using selected pointers within this pool.
Definition: pool.hpp:420
static constexpr std::enable_if< detail::t_is_defragmentable< T >::value, bool >::type is_defragmentable() noexcept
Checks if provided T type is defragmentable.
Definition: defrag.hpp:51
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Base class for persistent_ptr.
C++ pmemobj pool.
Commonly used SFINAE helpers.