PMDK C++ bindings  1.9.1
This is the C++ bindings documentation for PMDK's libpmemobj.
defrag.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020, 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_DEFRAG_HPP
39 #define LIBPMEMOBJ_CPP_DEFRAG_HPP
40 
41 #include <type_traits>
42 #include <vector>
43 
46 #include <libpmemobj++/pool.hpp>
47 #include <libpmemobj/atomic_base.h>
48 #include <libpmemobj/base.h>
49 
50 namespace pmem
51 {
52 
53 namespace detail
54 {
55 using for_each_ptr_function = void (*)(obj::persistent_ptr_base &ptr);
56 
57 template <typename T>
58 using t_has_for_each_ptr = typename std::enable_if<
59  std::is_same<decltype(std::declval<T>().for_each_ptr(
60  std::declval<for_each_ptr_function>())),
61  void>::value>::type;
62 
63 template <typename T>
64 using t_is_defragmentable = supports<T, t_has_for_each_ptr>;
65 }
66 
67 namespace obj
68 {
77 template <typename T>
78 static constexpr typename std::enable_if<detail::t_is_defragmentable<T>::value,
79  bool>::type
80 is_defragmentable() noexcept
81 {
82  return true;
83 }
84 
91 template <typename T>
92 static constexpr typename std::enable_if<!detail::t_is_defragmentable<T>::value,
93  bool>::type
94 is_defragmentable() noexcept
95 {
96  return false;
97 }
98 
112 class defrag {
113 public:
123  {
124  this->pop = p;
125  }
126 
139  template <typename T>
140  typename std::enable_if<is_defragmentable<T>(), void>::type
141  add(T &t)
142  {
143  if (pmemobj_pool_by_ptr(&t) != pop.handle())
144  throw std::runtime_error(
145  "object is not from the chosen pool");
146 
147  t.for_each_ptr([&](persistent_ptr_base &ptr) {
148  this->container.push_back(&ptr);
149  });
150  }
151 
167  template <typename T, typename = T>
168  typename std::enable_if<!is_defragmentable<T>(), void>::type
169  add(T &t)
170  {
171  if (pmemobj_pool_by_ptr(&t) != pop.handle())
172  throw std::runtime_error(
173  "object is not from the chosen pool");
174  }
175 
189  template <typename T>
190  void
192  {
193  if (pmemobj_pool_by_oid(ptr.raw()) != pop.handle())
194  throw std::runtime_error(
195  "persistent_ptr does not point to an object from the chosen pool");
196 
197  this->container.push_back(&ptr);
198  /* Calls 'add(T &)' passing the underlying object (T) */
199  this->add<T>(*ptr);
200  }
201 
216  pobj_defrag_result
217  run()
218  {
219  pobj_defrag_result result = this->pop.defrag(
220  this->container.data(), this->container.size());
221 
222  return result;
223  }
224 
225 private:
226  std::vector<persistent_ptr_base *> container;
227  pool_base pop;
228 };
229 
230 } /* namespace obj */
231 
232 } /* namespace pmem */
233 
234 #endif /* LIBPMEMOBJ_CPP_DEFRAG_HPP */
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:44
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:452
pmem::obj::p
Resides on pmem class.
Definition: p.hpp:64
pmem::obj::defrag
Defrag class.
Definition: defrag.hpp:112
pmem::obj::defrag::add
std::enable_if<!is_defragmentable< T >), void >::type add(T &t)
Specialization for non-defragmentable types.
Definition: defrag.hpp:169
pool.hpp
C++ pmemobj pool.
pmem::obj::defrag::defrag
defrag(pool_base p)
The only allowed ctor.
Definition: defrag.hpp:122
pmem::obj::defrag::run
pobj_defrag_result run()
Starts defragmentation with previously stored pointers.
Definition: defrag.hpp:217
pmem::obj::pool_base::handle
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:427
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:212
pmem::obj::persistent_ptr_base
Persistent_ptr base (non-template) class.
Definition: persistent_ptr_base.hpp:71
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:141
persistent_ptr_base.hpp
Base class for persistent_ptr.
pmem::obj::pool_base
The non-template pool base class.
Definition: pool.hpp:75
pmem::obj::defrag::add
void add(persistent_ptr< T > &ptr)
Stores address of a persistent_ptr to the defragmentation queue.
Definition: defrag.hpp:191