PMDK C++ bindings  1.11
This is the C++ bindings documentation for PMDK's libpmemobj.
pool.hpp
Go to the documentation of this file.
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2016-2020, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_POOL_HPP
10 #define LIBPMEMOBJ_CPP_POOL_HPP
11 
12 #include <cstddef>
13 #include <functional>
14 #include <mutex>
15 #include <string>
16 #include <sys/stat.h>
17 #include <typeindex>
18 #include <unordered_map>
19 #include <vector>
20 
24 #include <libpmemobj++/p.hpp>
27 #include <libpmemobj/atomic_base.h>
28 #include <libpmemobj/pool_base.h>
29 
30 namespace pmem
31 {
32 
33 namespace obj
34 {
35 template <typename T>
36 class persistent_ptr;
37 
46 class pool_base {
47 public:
51  pool_base() noexcept : pop(nullptr)
52  {
53  }
54 
62  explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
63  {
64  }
65 
69  pool_base(const pool_base &) noexcept = default;
70 
74  pool_base(pool_base &&) noexcept = default;
75 
79  pool_base &operator=(const pool_base &) noexcept = default;
80 
84  pool_base &operator=(pool_base &&) noexcept = default;
85 
89  virtual ~pool_base() noexcept = default;
90 
103  static pool_base
104  open(const std::string &path, const std::string &layout)
105  {
106 #ifdef _WIN32
107  pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
108 #else
109  pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
110 #endif
111  if (pop == nullptr)
112  throw pmem::pool_error("Failed opening pool")
113  .with_pmemobj_errormsg();
114 
115  pmemobj_set_user_data(pop, new detail::pool_data);
116 
117  return pool_base(pop);
118  }
119 
136  static pool_base
137  create(const std::string &path, const std::string &layout,
138  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
139  {
140 #ifdef _WIN32
141  pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
142  size, mode);
143 #else
144  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
145  size, mode);
146 #endif
147  if (pop == nullptr)
148  throw pmem::pool_error("Failed creating pool")
149  .with_pmemobj_errormsg();
150 
151  pmemobj_set_user_data(pop, new detail::pool_data);
152 
153  return pool_base(pop);
154  }
155 
166  static int
167  check(const std::string &path, const std::string &layout) noexcept
168  {
169 #ifdef _WIN32
170  return pmemobj_checkU(path.c_str(), layout.c_str());
171 #else
172  return pmemobj_check(path.c_str(), layout.c_str());
173 #endif
174  }
175 
176 #ifdef _WIN32
177 
190  static pool_base
191  open(const std::wstring &path, const std::wstring &layout)
192  {
193  pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
194  if (pop == nullptr)
195  throw pmem::pool_error("Failed opening pool")
196  .with_pmemobj_errormsg();
197 
198  pmemobj_set_user_data(pop, new detail::pool_data);
199 
200  return pool_base(pop);
201  }
202 
220  static pool_base
221  create(const std::wstring &path, const std::wstring &layout,
222  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
223  {
224  pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
225  size, mode);
226  if (pop == nullptr)
227  throw pmem::pool_error("Failed creating pool")
228  .with_pmemobj_errormsg();
229 
230  pmemobj_set_user_data(pop, new detail::pool_data);
231 
232  return pool_base(pop);
233  }
234 
246  static int
247  check(const std::wstring &path, const std::wstring &layout) noexcept
248  {
249  return pmemobj_checkW(path.c_str(), layout.c_str());
250  }
251 #endif
252 
258  void
260  {
261  if (this->pop == nullptr)
262  throw std::logic_error("Pool already closed");
263 
264  auto *user_data = static_cast<detail::pool_data *>(
265  pmemobj_get_user_data(this->pop));
266 
267  if (user_data->initialized.load())
268  user_data->cleanup();
269 
270  delete user_data;
271 
272  pmemobj_close(this->pop);
273  this->pop = nullptr;
274  }
275 
282  void
283  persist(const void *addr, size_t len) noexcept
284  {
285  pmemobj_persist(this->pop, addr, len);
286  }
287 
293  template <typename Y>
294  void
295  persist(const p<Y> &prop) noexcept
296  {
297  pmemobj_persist(this->pop, &prop, sizeof(Y));
298  }
299 
306  template <typename Y>
307  void
308  persist(const persistent_ptr<Y> &ptr) noexcept
309  {
310  pmemobj_persist(this->pop, &ptr, sizeof(ptr));
311  }
312 
319  void
320  flush(const void *addr, size_t len) noexcept
321  {
322  pmemobj_flush(this->pop, addr, len);
323  }
324 
330  template <typename Y>
331  void
332  flush(const p<Y> &prop) noexcept
333  {
334  pmemobj_flush(this->pop, &prop, sizeof(Y));
335  }
336 
342  template <typename Y>
343  void
344  flush(const persistent_ptr<Y> &ptr) noexcept
345  {
346  pmemobj_flush(this->pop, &ptr, sizeof(ptr));
347  }
348 
352  void
353  drain(void) noexcept
354  {
355  pmemobj_drain(this->pop);
356  }
357 
368  void *
369  memcpy_persist(void *dest, const void *src, size_t len) noexcept
370  {
371  return pmemobj_memcpy_persist(this->pop, dest, src, len);
372  }
373 
384  void *
385  memset_persist(void *dest, int c, size_t len) noexcept
386  {
387  return pmemobj_memset_persist(this->pop, dest, c, len);
388  }
389 
397  PMEMobjpool *
398  handle() noexcept
399  {
400  return this->pop;
401  }
402 
403  POBJ_CPP_DEPRECATED PMEMobjpool *
404  get_handle() noexcept
405  {
406  return pool_base::handle();
407  }
408 
422  pobj_defrag_result
423  defrag(persistent_ptr_base **ptrv, size_t oidcnt)
424  {
425  pobj_defrag_result result;
426  int ret = pmemobj_defrag(this->pop, (PMEMoid **)ptrv, oidcnt,
427  &result);
428 
429  if (ret != 0)
430  throw defrag_error(result, "Defragmentation failed")
431  .with_pmemobj_errormsg();
432 
433  return result;
434  }
435 
436 protected:
437  /* The pool opaque handle */
438  PMEMobjpool *pop;
439 
440 #ifndef _WIN32
441  /* Default create mode */
442  static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
443 #else
444  /* Default create mode */
445  static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
446 #endif
447 };
448 
461 template <typename T>
462 class pool : public pool_base {
463 public:
467  pool() noexcept = default;
468 
472  pool(const pool &) noexcept = default;
473 
477  pool(pool &&) noexcept = default;
478 
482  pool &operator=(const pool &) noexcept = default;
483 
487  pool &operator=(pool &&) noexcept = default;
488 
492  ~pool() noexcept = default;
493 
497  explicit pool(const pool_base &pb) noexcept : pool_base(pb)
498  {
499  }
500 
504  explicit pool(pool_base &&pb) noexcept : pool_base(pb)
505  {
506  }
507 
518  template <typename M>
519  M
520  ctl_get(const std::string &name)
521  {
522  return ctl_get_detail<M>(pop, name);
523  }
524 
536  template <typename M>
537  M
538  ctl_set(const std::string &name, M arg)
539  {
540  return ctl_set_detail(pop, name, arg);
541  }
542 
554  template <typename M>
555  M
556  ctl_exec(const std::string &name, M arg)
557  {
558  return ctl_exec_detail(pop, name, arg);
559  }
560 
561 #ifdef _WIN32
562 
572  template <typename M>
573  M
574  ctl_get(const std::wstring &name)
575  {
576  return ctl_get_detail<M>(pop, name);
577  }
578 
590  template <typename M>
591  M
592  ctl_set(const std::wstring &name, M arg)
593  {
594  return ctl_set_detail(pop, name, arg);
595  }
596 
608  template <typename M>
609  M
610  ctl_exec(const std::wstring &name, M arg)
611  {
612  return ctl_exec_detail(pop, name, arg);
613  }
614 #endif
615 
625  {
626  if (pop == nullptr)
627  throw pmem::pool_error("Invalid pool handle");
628 
629  persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
630  return root;
631  }
632 
633  POBJ_CPP_DEPRECATED persistent_ptr<T>
634  get_root()
635  {
636  return pool::root();
637  }
638 
651  static pool<T>
652  open(const std::string &path, const std::string &layout)
653  {
654  return pool<T>(pool_base::open(path, layout));
655  }
656 
673  static pool<T>
674  create(const std::string &path, const std::string &layout,
675  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
676  {
677  return pool<T>(pool_base::create(path, layout, size, mode));
678  }
679 
690  static int
691  check(const std::string &path, const std::string &layout)
692  {
693  return pool_base::check(path, layout);
694  }
695 
696 #ifdef _WIN32
697 
710  static pool<T>
711  open(const std::wstring &path, const std::wstring &layout)
712  {
713  return pool<T>(pool_base::open(path, layout));
714  }
715 
733  static pool<T>
734  create(const std::wstring &path, const std::wstring &layout,
735  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
736  {
737  return pool<T>(pool_base::create(path, layout, size, mode));
738  }
739 
751  static int
752  check(const std::wstring &path, const std::wstring &layout)
753  {
754  return pool_base::check(path, layout);
755  }
756 #endif
757 };
758 
769 template <typename T>
770 T
771 ctl_get(const std::string &name)
772 {
773  return ctl_get_detail<T>(nullptr, name);
774 }
775 
787 template <typename T>
788 T
789 ctl_set(const std::string &name, T arg)
790 {
791  return ctl_set_detail(nullptr, name, arg);
792 }
793 
805 template <typename T>
806 T
807 ctl_exec(const std::string &name, T arg)
808 {
809  return ctl_exec_detail(nullptr, name, arg);
810 }
811 
812 #ifdef _WIN32
813 
823 template <typename T>
824 T
825 ctl_get(const std::wstring &name)
826 {
827  return ctl_get_detail<T>(nullptr, name);
828 }
829 
841 template <typename T>
842 T
843 ctl_set(const std::wstring &name, T arg)
844 {
845  return ctl_set_detail(nullptr, name, arg);
846 }
847 
859 template <typename T>
860 T
861 ctl_exec(const std::wstring &name, T arg)
862 {
863  return ctl_exec_detail(nullptr, name, arg);
864 }
865 #endif
866 
867 } /* namespace obj */
868 
869 } /* namespace pmem */
870 
871 #endif /* LIBPMEMOBJ_CPP_POOL_HPP */
pmem::obj::pool_base::memcpy_persist
void * memcpy_persist(void *dest, const void *src, size_t len) noexcept
Performs memcpy and persist operation on a given chunk of memory.
Definition: pool.hpp:369
pmem::obj::ctl_exec
T ctl_exec(const std::string &name, T arg)
Execute function at global scope.
Definition: pool.hpp:807
pmem::obj::pool_base::persist
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent pointer.
Definition: pool.hpp:308
pmem::obj::pool_base::create
static pool_base create(const std::wstring &path, const std::wstring &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:221
pmem::obj::pool::ctl_exec
M ctl_exec(const std::string &name, M arg)
Execute function at pool scope.
Definition: pool.hpp:556
pmem::pool_error
Custom pool error class.
Definition: pexceptions.hpp:45
pmem::obj::pool_base::pool_base
pool_base(const pool_base &) noexcept=default
Defaulted copy constructor.
pmem::obj::pool::check
static int check(const std::wstring &path, const std::wstring &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:752
pmem::obj::pool::create
static pool< T > create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:674
pmem::obj::pool_base::persist
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:283
pmem
Persistent memory namespace.
Definition: allocation_flag.hpp:15
pmem::obj::pool::ctl_get
M ctl_get(const std::string &name)
Query libpmemobj state at pool scope.
Definition: pool.hpp:520
pmem::obj::pool_base::memset_persist
void * memset_persist(void *dest, int c, size_t len) noexcept
Performs memset and persist operation on a given chunk of memory.
Definition: pool.hpp:385
ctl.hpp
C++ ctl api.
common.hpp
Commonly used functionality.
pmem::obj::pool::pool
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:504
pmem::obj::pool::ctl_get
M ctl_get(const std::wstring &name)
Query libpmemobj state at pool scope.
Definition: pool.hpp:574
pmem::obj::pool::ctl_set
M ctl_set(const std::wstring &name, M arg)
Modify libpmemobj state at pool scope.
Definition: pool.hpp:592
pmem::obj::pool_base::check
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:167
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::pool_base::pool_base
pool_base(pool_base &&) noexcept=default
Defaulted move constructor.
pexceptions.hpp
Custom exceptions.
pmem::obj::pool_base::check
static int check(const std::wstring &path, const std::wstring &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:247
pmem::obj::pool_base::drain
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:353
pmem::obj::pool_base::persist
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:295
pmem::obj::p
Resides on pmem class.
Definition: p.hpp:35
pmem::obj::pool_base::open
static pool_base open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:104
pmem::obj::pool_base::close
void close()
Closes the pool.
Definition: pool.hpp:259
pmem::obj::pool::root
persistent_ptr< T > root()
Retrieves pool's root object.
Definition: pool.hpp:624
pmem::obj::pool_base::pool_base
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:51
pmem::obj::pool_base::flush
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:344
pmem::obj::pool::pool
pool() noexcept=default
Defaulted constructor.
pmem::obj::ctl_set
T ctl_set(const std::string &name, T arg)
Modify libpmemobj state at global scope.
Definition: pool.hpp:789
pmem::obj::pool_base::flush
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:320
pmem::obj::pool_base::handle
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:398
pmem::obj::pool_base::pool_base
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:62
pmem::obj::pool_base::create
static pool_base create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:137
pmem::obj::pool::ctl_set
M ctl_set(const std::string &name, M arg)
Modify libpmemobj state at pool scope.
Definition: pool.hpp:538
pmem::defrag_error
Custom defrag error class.
Definition: pexceptions.hpp:207
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:152
pmem::obj::pool_base::open
static pool_base open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:191
pmem::obj::pool
PMEMobj pool class.
Definition: pool.hpp:462
p.hpp
Resides on pmem property template.
pmem::obj::ctl_get
T ctl_get(const std::string &name)
Query libpmemobj state at global scope.
Definition: pool.hpp:771
pmem::obj::pool::open
static pool< T > open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:652
pmem::obj::pool::ctl_exec
M ctl_exec(const std::wstring &name, M arg)
Execute function at pool scope.
Definition: pool.hpp:610
pmem::obj::pool_base::flush
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:332
pool_data.hpp
A volatile data stored along with pmemobjpool.
pmem::obj::persistent_ptr_base
Persistent_ptr base (non-template) class.
Definition: persistent_ptr_base.hpp:42
persistent_ptr_base.hpp
Base class for persistent_ptr.
pmem::obj::pool::open
static pool< T > open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:711
pmem::obj::pool::create
static pool< T > create(const std::wstring &path, const std::wstring &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=DEFAULT_MODE)
Creates a new transactional object store pool.
Definition: pool.hpp:734
pmem::obj::pool_base
The non-template pool base class.
Definition: pool.hpp:46
pmem::obj::pool::check
static int check(const std::string &path, const std::string &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:691