PMDK C++ bindings  1.13.0-git23.gf49772ac
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-2021, Intel Corporation */
3 
9 #ifndef LIBPMEMOBJ_CPP_POOL_HPP
10 #define LIBPMEMOBJ_CPP_POOL_HPP
11 
12 #include <cstddef>
13 #include <errno.h>
14 #include <functional>
15 #include <mutex>
16 #include <string>
17 #include <sys/stat.h>
18 #include <typeindex>
19 #include <unordered_map>
20 #include <vector>
21 
25 #include <libpmemobj++/p.hpp>
28 #include <libpmemobj/atomic_base.h>
29 #include <libpmemobj/pool_base.h>
30 
31 namespace pmem
32 {
33 
34 namespace obj
35 {
36 template <typename T>
37 class persistent_ptr;
38 
50 class pool_base {
51 public:
55  pool_base() noexcept : pop(nullptr)
56  {
57  }
58 
66  explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
67  {
68  }
69 
73  pool_base(const pool_base &) noexcept = default;
74 
78  pool_base(pool_base &&) noexcept = default;
79 
83  pool_base &operator=(const pool_base &) noexcept = default;
84 
88  pool_base &operator=(pool_base &&) noexcept = default;
89 
93  virtual ~pool_base() noexcept = default;
94 
107  static pool_base
108  open(const std::string &path, const std::string &layout)
109  {
110 #ifdef _WIN32
111  pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
112 #else
113  pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
114 #endif
115  check_pool(pop, "opening");
116 
117  pmemobj_set_user_data(pop, new detail::pool_data);
118 
119  return pool_base(pop);
120  }
121 
138  static pool_base
139  create(const std::string &path, const std::string &layout,
140  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
141  {
142 #ifdef _WIN32
143  pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
144  size, mode);
145 #else
146  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
147  size, mode);
148 #endif
149  check_pool(pop, "creating");
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
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  check_pool(pop, "opening");
195 
196  pmemobj_set_user_data(pop, new detail::pool_data);
197 
198  return pool_base(pop);
199  }
200 
218  static pool_base
219  create(const std::wstring &path, const std::wstring &layout,
220  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
221  {
222  pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
223  size, mode);
224  check_pool(pop, "creating");
225 
226  pmemobj_set_user_data(pop, new detail::pool_data);
227 
228  return pool_base(pop);
229  }
230 
242  static int
243  check(const std::wstring &path, const std::wstring &layout) noexcept
244  {
245  return pmemobj_checkW(path.c_str(), layout.c_str());
246  }
247 #endif
248 
254  void
256  {
257  if (this->pop == nullptr)
258  throw std::logic_error("Pool already closed");
259 
260  auto *user_data = static_cast<detail::pool_data *>(
261  pmemobj_get_user_data(this->pop));
262 
263  if (user_data->initialized.load())
264  user_data->cleanup();
265 
266  delete user_data;
267 
268  pmemobj_close(this->pop);
269  this->pop = nullptr;
270  }
271 
278  void
279  persist(const void *addr, size_t len) noexcept
280  {
281  pmemobj_persist(this->pop, addr, len);
282  }
283 
289  template <typename Y>
290  void
291  persist(const p<Y> &prop) noexcept
292  {
293  pmemobj_persist(this->pop, &prop, sizeof(Y));
294  }
295 
302  template <typename Y>
303  void
304  persist(const persistent_ptr<Y> &ptr) noexcept
305  {
306  pmemobj_persist(this->pop, &ptr, sizeof(ptr));
307  }
308 
315  void
316  flush(const void *addr, size_t len) noexcept
317  {
318  pmemobj_flush(this->pop, addr, len);
319  }
320 
326  template <typename Y>
327  void
328  flush(const p<Y> &prop) noexcept
329  {
330  pmemobj_flush(this->pop, &prop, sizeof(Y));
331  }
332 
338  template <typename Y>
339  void
340  flush(const persistent_ptr<Y> &ptr) noexcept
341  {
342  pmemobj_flush(this->pop, &ptr, sizeof(ptr));
343  }
344 
348  void
349  drain(void) noexcept
350  {
351  pmemobj_drain(this->pop);
352  }
353 
364  void *
365  memcpy_persist(void *dest, const void *src, size_t len) noexcept
366  {
367  return pmemobj_memcpy_persist(this->pop, dest, src, len);
368  }
369 
380  void *
381  memset_persist(void *dest, int c, size_t len) noexcept
382  {
383  return pmemobj_memset_persist(this->pop, dest, c, len);
384  }
385 
393  PMEMobjpool *
394  handle() noexcept
395  {
396  return this->pop;
397  }
398 
399  POBJ_CPP_DEPRECATED PMEMobjpool *
400  get_handle() noexcept
401  {
402  return pool_base::handle();
403  }
404 
418  pobj_defrag_result
419  defrag(persistent_ptr_base **ptrv, size_t oidcnt)
420  {
421  pobj_defrag_result result;
422  int ret = pmemobj_defrag(this->pop, (PMEMoid **)ptrv, oidcnt,
423  &result);
424 
425  if (ret != 0)
426  throw defrag_error(result, "Defragmentation failed")
427  .with_pmemobj_errormsg();
428 
429  return result;
430  }
431 
432 protected:
433  static void
434  check_pool(pmemobjpool *pop, std::string mode)
435  {
436  if (pop == nullptr) {
437  if (errno == EINVAL || errno == EFBIG ||
438  errno == ENOENT || errno == EEXIST) {
440  "Failed " + mode + " pool")
441  .with_pmemobj_errormsg();
442  } else {
443  throw pmem::pool_error("Failed " + mode +
444  " pool")
445  .with_pmemobj_errormsg();
446  }
447  }
448  }
449 
450  /* The pool opaque handle */
451  PMEMobjpool *pop;
452 
453 #ifndef _WIN32
457  static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
458 #else
462  static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
463 #endif
464 };
465 
481 template <typename T>
482 class pool : public pool_base {
483 public:
487  pool() noexcept = default;
488 
492  pool(const pool &) noexcept = default;
493 
497  pool(pool &&) noexcept = default;
498 
502  pool &operator=(const pool &) noexcept = default;
503 
507  pool &operator=(pool &&) noexcept = default;
508 
512  ~pool() noexcept = default;
513 
517  explicit pool(const pool_base &pb) noexcept : pool_base(pb)
518  {
519  }
520 
524  explicit pool(pool_base &&pb) noexcept : pool_base(pb)
525  {
526  }
527 
538  template <typename M>
539  M
540  ctl_get(const std::string &name)
541  {
542  return ctl_get_detail<M>(pop, name);
543  }
544 
556  template <typename M>
557  M
558  ctl_set(const std::string &name, M arg)
559  {
560  return ctl_set_detail(pop, name, arg);
561  }
562 
574  template <typename M>
575  M
576  ctl_exec(const std::string &name, M arg)
577  {
578  return ctl_exec_detail(pop, name, arg);
579  }
580 
581 #ifdef _WIN32
592  template <typename M>
593  M
594  ctl_get(const std::wstring &name)
595  {
596  return ctl_get_detail<M>(pop, name);
597  }
598 
610  template <typename M>
611  M
612  ctl_set(const std::wstring &name, M arg)
613  {
614  return ctl_set_detail(pop, name, arg);
615  }
616 
628  template <typename M>
629  M
630  ctl_exec(const std::wstring &name, M arg)
631  {
632  return ctl_exec_detail(pop, name, arg);
633  }
634 #endif
635 
645  {
646  if (pop == nullptr)
647  throw pmem::pool_error("Invalid pool handle");
648 
649  persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
650  return root;
651  }
652 
653  POBJ_CPP_DEPRECATED persistent_ptr<T>
654  get_root()
655  {
656  return pool::root();
657  }
658 
671  static pool<T>
672  open(const std::string &path, const std::string &layout)
673  {
674  return pool<T>(pool_base::open(path, layout));
675  }
676 
693  static pool<T>
694  create(const std::string &path, const std::string &layout,
695  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
696  {
697  return pool<T>(pool_base::create(path, layout, size, mode));
698  }
699 
710  static int
711  check(const std::string &path, const std::string &layout)
712  {
713  return pool_base::check(path, layout);
714  }
715 
716 #ifdef _WIN32
730  static pool<T>
731  open(const std::wstring &path, const std::wstring &layout)
732  {
733  return pool<T>(pool_base::open(path, layout));
734  }
735 
753  static pool<T>
754  create(const std::wstring &path, const std::wstring &layout,
755  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
756  {
757  return pool<T>(pool_base::create(path, layout, size, mode));
758  }
759 
771  static int
772  check(const std::wstring &path, const std::wstring &layout)
773  {
774  return pool_base::check(path, layout);
775  }
776 #endif
777 };
778 
789 template <typename T>
790 T
791 ctl_get(const std::string &name)
792 {
793  return ctl_get_detail<T>(nullptr, name);
794 }
795 
807 template <typename T>
808 T
809 ctl_set(const std::string &name, T arg)
810 {
811  return ctl_set_detail(nullptr, name, arg);
812 }
813 
825 template <typename T>
826 T
827 ctl_exec(const std::string &name, T arg)
828 {
829  return ctl_exec_detail(nullptr, name, arg);
830 }
831 
832 #ifdef _WIN32
843 template <typename T>
844 T
845 ctl_get(const std::wstring &name)
846 {
847  return ctl_get_detail<T>(nullptr, name);
848 }
849 
861 template <typename T>
862 T
863 ctl_set(const std::wstring &name, T arg)
864 {
865  return ctl_set_detail(nullptr, name, arg);
866 }
867 
879 template <typename T>
880 T
881 ctl_exec(const std::wstring &name, T arg)
882 {
883  return ctl_exec_detail(nullptr, name, arg);
884 }
885 #endif
886 
887 } /* namespace obj */
888 
889 } /* namespace pmem */
890 
891 #endif /* LIBPMEMOBJ_CPP_POOL_HPP */
Custom defrag error class.
Definition: pexceptions.hpp:225
Resides on pmem class.
Definition: p.hpp:35
Persistent_ptr base (non-template) class.
Definition: persistent_ptr_base.hpp:42
Persistent pointer class.
Definition: persistent_ptr.hpp:152
The non-template pool base class.
Definition: pool.hpp:50
static pool_base open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:108
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:394
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:316
pobj_defrag_result defrag(persistent_ptr_base **ptrv, size_t oidcnt)
Starts defragmentation using selected pointers within this pool.
Definition: pool.hpp:419
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:328
static int check(const std::wstring &path, const std::wstring &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:243
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent pointer.
Definition: pool.hpp:304
pool_base(pool_base &&) noexcept=default
Defaulted move constructor.
void close()
Closes the pool.
Definition: pool.hpp:255
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:291
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:279
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:66
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:340
static const int DEFAULT_MODE
Default create mode.
Definition: pool.hpp:462
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:55
static pool_base open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:191
pool_base(const pool_base &) noexcept=default
Defaulted copy constructor.
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:139
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:381
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:219
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:349
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:365
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:167
PMEMobj pool class.
Definition: pool.hpp:482
static pool< T > open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:731
static pool< T > open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:672
static int check(const std::string &path, const std::string &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:711
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:754
M ctl_set(const std::wstring &name, M arg)
Modify libpmemobj state at pool scope.
Definition: pool.hpp:612
M ctl_set(const std::string &name, M arg)
Modify libpmemobj state at pool scope.
Definition: pool.hpp:558
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:524
M ctl_exec(const std::string &name, M arg)
Execute function at pool scope.
Definition: pool.hpp:576
M ctl_get(const std::string &name)
Query libpmemobj state at pool scope.
Definition: pool.hpp:540
persistent_ptr< T > root()
Retrieves pool's root object.
Definition: pool.hpp:644
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:694
static int check(const std::wstring &path, const std::wstring &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:772
M ctl_get(const std::wstring &name)
Query libpmemobj state at pool scope.
Definition: pool.hpp:594
pool() noexcept=default
Defaulted constructor.
M ctl_exec(const std::wstring &name, M arg)
Execute function at pool scope.
Definition: pool.hpp:630
Custom pool error class.
Definition: pexceptions.hpp:45
Custom pool error class.
Definition: pexceptions.hpp:63
Commonly used functionality.
C++ ctl api.
T ctl_set(const std::string &name, T arg)
Modify libpmemobj state at global scope.
Definition: pool.hpp:809
T ctl_get(const std::string &name)
Query libpmemobj state at global scope.
Definition: pool.hpp:791
T ctl_exec(const std::string &name, T arg)
Execute function at global scope.
Definition: pool.hpp:827
Persistent memory namespace.
Definition: allocation_flag.hpp:15
Resides on pmem property template.
Base class for persistent_ptr.
Custom exceptions.
A volatile data stored along with pmemobjpool.