PMDK C++ bindings  1.10.1
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
51 is_defragmentable() noexcept
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
65 is_defragmentable() noexcept
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 */
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
template_helpers.hpp
Commonly used SFINAE helpers.
pmem::obj::pool_base::defrag
pobj_defrag_result defrag(persistent_ptr_base **ptrv, size_t oidcnt)
Starts defragmentation using selected pointers within this pool.
Definition: pool.hpp:423
pmem::obj::p
Resides on pmem class.
Definition: p.hpp:35
pmem::obj::defrag
Defrag class.
Definition: defrag.hpp:83
pmem::obj::defrag::add
std::enable_if<!is_defragmentable< T >), void >::type add(T &t)
Specialization for non-defragmentable types.
Definition: defrag.hpp:140
pool.hpp
C++ pmemobj pool.
pmem::obj::defrag::defrag
defrag(pool_base p)
The only allowed ctor.
Definition: defrag.hpp:93
pmem::obj::defrag::run
pobj_defrag_result run()
Starts defragmentation with previously stored pointers.
Definition: defrag.hpp:188
pmem::obj::pool_base::handle
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:398
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:183
pmem::obj::persistent_ptr_base
Persistent_ptr base (non-template) class.
Definition: persistent_ptr_base.hpp:42
pmem::obj::defrag::add
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
persistent_ptr_base.hpp
Base class for persistent_ptr.
pmem::obj::pool_base
The non-template pool base class.
Definition: pool.hpp:46
pmem::obj::defrag::add
void add(persistent_ptr< T > &ptr)
Stores address of a persistent_ptr to the defragmentation queue.
Definition: defrag.hpp:162