PMDK C++ bindings  1.13.0-git107.g7e59f08f
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 
51 class pool_base {
52 public:
56  pool_base() noexcept : pop(nullptr)
57  {
58  }
59 
67  explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
68  {
69  }
70 
74  pool_base(const pool_base &) noexcept = default;
75 
79  pool_base(pool_base &&) noexcept = default;
80 
84  pool_base &operator=(const pool_base &) noexcept = default;
85 
89  pool_base &operator=(pool_base &&) noexcept = default;
90 
94  virtual ~pool_base() noexcept = default;
95 
108  static pool_base
109  open(const std::string &path, const std::string &layout)
110  {
111 #ifdef _WIN32
112  pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
113 #else
114  pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
115 #endif
116  check_pool(pop, "opening");
117 
118  pmemobj_set_user_data(pop, new detail::pool_data);
119 
120  return pool_base(pop);
121  }
122 
139  static pool_base
140  create(const std::string &path, const std::string &layout,
141  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
142  {
143 #ifdef _WIN32
144  pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
145  size, mode);
146 #else
147  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
148  size, mode);
149 #endif
150  check_pool(pop, "creating");
151 
152  pmemobj_set_user_data(pop, new detail::pool_data);
153 
154  return pool_base(pop);
155  }
156 
167  static int
168  check(const std::string &path, const std::string &layout) noexcept
169  {
170 #ifdef _WIN32
171  return pmemobj_checkU(path.c_str(), layout.c_str());
172 #else
173  return pmemobj_check(path.c_str(), layout.c_str());
174 #endif
175  }
176 
177 #ifdef _WIN32
191  static pool_base
192  open(const std::wstring &path, const std::wstring &layout)
193  {
194  pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
195  check_pool(pop, "opening");
196 
197  pmemobj_set_user_data(pop, new detail::pool_data);
198 
199  return pool_base(pop);
200  }
201 
219  static pool_base
220  create(const std::wstring &path, const std::wstring &layout,
221  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
222  {
223  pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
224  size, mode);
225  check_pool(pop, "creating");
226 
227  pmemobj_set_user_data(pop, new detail::pool_data);
228 
229  return pool_base(pop);
230  }
231 
243  static int
244  check(const std::wstring &path, const std::wstring &layout) noexcept
245  {
246  return pmemobj_checkW(path.c_str(), layout.c_str());
247  }
248 #endif
249 
255  void
257  {
258  if (this->pop == nullptr)
259  throw std::logic_error("Pool already closed");
260 
261  auto *user_data = static_cast<detail::pool_data *>(
262  pmemobj_get_user_data(this->pop));
263 
264  if (user_data->initialized.load())
265  user_data->cleanup();
266 
267  delete user_data;
268 
269  pmemobj_close(this->pop);
270  this->pop = nullptr;
271  }
272 
279  void
280  persist(const void *addr, size_t len) noexcept
281  {
282  pmemobj_persist(this->pop, addr, len);
283  }
284 
290  template <typename Y>
291  void
292  persist(const p<Y> &prop) noexcept
293  {
294  pmemobj_persist(this->pop, &prop, sizeof(Y));
295  }
296 
303  template <typename Y>
304  void
305  persist(const persistent_ptr<Y> &ptr) noexcept
306  {
307  pmemobj_persist(this->pop, &ptr, sizeof(ptr));
308  }
309 
316  void
317  flush(const void *addr, size_t len) noexcept
318  {
319  pmemobj_flush(this->pop, addr, len);
320  }
321 
327  template <typename Y>
328  void
329  flush(const p<Y> &prop) noexcept
330  {
331  pmemobj_flush(this->pop, &prop, sizeof(Y));
332  }
333 
339  template <typename Y>
340  void
341  flush(const persistent_ptr<Y> &ptr) noexcept
342  {
343  pmemobj_flush(this->pop, &ptr, sizeof(ptr));
344  }
345 
349  void
350  drain(void) noexcept
351  {
352  pmemobj_drain(this->pop);
353  }
354 
365  void *
366  memcpy_persist(void *dest, const void *src, size_t len) noexcept
367  {
368  return pmemobj_memcpy_persist(this->pop, dest, src, len);
369  }
370 
381  void *
382  memset_persist(void *dest, int c, size_t len) noexcept
383  {
384  return pmemobj_memset_persist(this->pop, dest, c, len);
385  }
386 
394  PMEMobjpool *
395  handle() noexcept
396  {
397  return this->pop;
398  }
399 
400  POBJ_CPP_DEPRECATED PMEMobjpool *
401  get_handle() noexcept
402  {
403  return pool_base::handle();
404  }
405 
419  pobj_defrag_result
420  defrag(persistent_ptr_base **ptrv, size_t oidcnt)
421  {
422  pobj_defrag_result result;
423  int ret = pmemobj_defrag(this->pop, (PMEMoid **)ptrv, oidcnt,
424  &result);
425 
426  if (ret != 0)
427  throw detail::exception_with_errormsg<defrag_error>(
428  result, "Defragmentation failed");
429  return result;
430  }
431 
432 protected:
433  static void
434  check_pool(pmemobjpool *pop, std::string mode)
435  {
436  if (pop == nullptr) {
437  std::string msg = "Failed " + mode + " pool";
438  if (errno == EINVAL || errno == EFBIG ||
439  errno == ENOENT || errno == EEXIST) {
442  } else {
444  pmem::pool_error>(msg);
445  }
446  }
447  }
448 
449  /* The pool opaque handle */
450  PMEMobjpool *pop;
451 
452 #ifndef _WIN32
456  static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
457 #else
461  static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
462 #endif
463 };
464 
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 */
Resides on pmem class.
Definition: p.hpp:36
Persistent_ptr base (non-template) class.
Definition: persistent_ptr_base.hpp:42
Persistent pointer class.
Definition: persistent_ptr.hpp:153
The non-template pool base class.
Definition: pool.hpp:51
static pool_base open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:109
PMEMobjpool * handle() noexcept
Gets the C style handle to the pool.
Definition: pool.hpp:395
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:317
pobj_defrag_result defrag(persistent_ptr_base **ptrv, size_t oidcnt)
Starts defragmentation using selected pointers within this pool.
Definition: pool.hpp:420
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:329
static int check(const std::wstring &path, const std::wstring &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:244
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent pointer.
Definition: pool.hpp:305
pool_base(pool_base &&) noexcept=default
Defaulted move constructor.
void close()
Closes the pool.
Definition: pool.hpp:256
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:292
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:280
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:67
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:341
static const int DEFAULT_MODE
Default create mode.
Definition: pool.hpp:461
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:56
static pool_base open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:192
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:140
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:382
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:220
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:350
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:366
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:168
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:84
Custom pool error class.
Definition: pexceptions.hpp:97
Commonly used functionality.
C++ ctl API.
basic_string< char > string
The most typical string usage - the char specialization.
Definition: string.hpp:24
basic_string< wchar_t > wstring
The wide char specialization.
Definition: string.hpp:30
ExcT exception_with_errormsg(Args &&... args)
Generic error message decorator for pmemobj-based exceptions.
Definition: pexceptions.hpp:69
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 pmem exceptions.
A volatile data stored along with pmemobjpool.