PMDK C++ bindings  1.5.2
This is the C++ bindings documentation for PMDK's libpmemobj.
pool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2018, 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_POOL_HPP
39 #define LIBPMEMOBJ_CPP_POOL_HPP
40 
41 #include <stddef.h>
42 #include <string>
43 #include <sys/stat.h>
44 
47 #include <libpmemobj++/p.hpp>
48 #include <libpmemobj/pool_base.h>
49 
50 namespace pmem
51 {
52 
53 namespace obj
54 {
55 template <typename T>
56 class persistent_ptr;
57 
66 class pool_base {
67 public:
71  pool_base() noexcept : pop(nullptr)
72  {
73  }
74 
82  explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
83  {
84  }
85 
89  pool_base(const pool_base &) noexcept = default;
90 
94  pool_base(pool_base &&) noexcept = default;
95 
99  pool_base &operator=(const pool_base &) noexcept = default;
100 
104  pool_base &operator=(pool_base &&) noexcept = default;
105 
109  virtual ~pool_base() noexcept = default;
110 
123  static pool_base
124  open(const std::string &path, const std::string &layout)
125  {
126 #ifdef _WIN32
127  pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
128 #else
129  pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
130 #endif
131  if (pop == nullptr)
132  throw pool_error("Failed opening pool");
133 
134  return pool_base(pop);
135  }
136 
153  static pool_base
154  create(const std::string &path, const std::string &layout,
155  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
156  {
157 #ifdef _WIN32
158  pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
159  size, mode);
160 #else
161  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
162  size, mode);
163 #endif
164  if (pop == nullptr)
165  throw pool_error("Failed creating pool");
166 
167  return pool_base(pop);
168  }
169 
180  static int
181  check(const std::string &path, const std::string &layout) noexcept
182  {
183 #ifdef _WIN32
184  return pmemobj_checkU(path.c_str(), layout.c_str());
185 #else
186  return pmemobj_check(path.c_str(), layout.c_str());
187 #endif
188  }
189 
190 #ifdef _WIN32
191 
204  static pool_base
205  open(const std::wstring &path, const std::wstring &layout)
206  {
207  pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
208  if (pop == nullptr)
209  throw pool_error("Failed opening pool");
210 
211  return pool_base(pop);
212  }
213 
231  static pool_base
232  create(const std::wstring &path, const std::wstring &layout,
233  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
234  {
235  pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
236  size, mode);
237  if (pop == nullptr)
238  throw pool_error("Failed creating pool");
239 
240  return pool_base(pop);
241  }
242 
254  static int
255  check(const std::wstring &path, const std::wstring &layout) noexcept
256  {
257  return pmemobj_checkW(path.c_str(), layout.c_str());
258  }
259 #endif
260 
266  void
268  {
269  if (this->pop == nullptr)
270  throw std::logic_error("Pool already closed");
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 
305  template <typename Y>
306  void
307  persist(const persistent_ptr<Y> &ptr) noexcept
308  {
309  pmemobj_persist(this->pop, &ptr, sizeof(ptr));
310  }
311 
318  void
319  flush(const void *addr, size_t len) noexcept
320  {
321  pmemobj_flush(this->pop, addr, len);
322  }
323 
329  template <typename Y>
330  void
331  flush(const p<Y> &prop) noexcept
332  {
333  pmemobj_flush(this->pop, &prop, sizeof(Y));
334  }
335 
341  template <typename Y>
342  void
343  flush(const persistent_ptr<Y> &ptr) noexcept
344  {
345  pmemobj_flush(this->pop, &ptr, sizeof(ptr));
346  }
347 
351  void
352  drain(void) noexcept
353  {
354  pmemobj_drain(this->pop);
355  }
356 
367  void *
368  memcpy_persist(void *dest, const void *src, size_t len) noexcept
369  {
370  return pmemobj_memcpy_persist(this->pop, dest, src, len);
371  }
372 
383  void *
384  memset_persist(void *dest, int c, size_t len) noexcept
385  {
386  return pmemobj_memset_persist(this->pop, dest, c, len);
387  }
388 
389  /*
390  * Gets the C style handle to the pool.
391  *
392  * Necessary to be able to use the pool with the C API.
393  *
394  * @return pool opaque handle.
395  */
396  PMEMobjpool *
397  handle() noexcept
398  {
399  return this->pop;
400  }
401 
402  POBJ_CPP_DEPRECATED PMEMobjpool *
403  get_handle() noexcept
404  {
405  return pool_base::handle();
406  }
407 
408 protected:
409  /* The pool opaque handle */
410  PMEMobjpool *pop;
411 
412 #ifndef _WIN32
413  /* Default create mode */
414  static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
415 #else
416  /* Default create mode */
417  static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
418 #endif
419 };
420 
429 template <typename T>
430 class pool : public pool_base {
431 public:
435  pool() noexcept = default;
436 
440  pool(const pool &) noexcept = default;
441 
445  pool(pool &&) noexcept = default;
446 
450  pool &operator=(const pool &) noexcept = default;
451 
455  pool &operator=(pool &&) noexcept = default;
456 
460  ~pool() noexcept = default;
461 
465  explicit pool(const pool_base &pb) noexcept : pool_base(pb)
466  {
467  }
468 
472  explicit pool(pool_base &&pb) noexcept : pool_base(pb)
473  {
474  }
475 
483  {
484  if (pop == nullptr)
485  throw pool_error("Invalid pool handle");
486 
487  persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
488  return root;
489  }
490 
491  POBJ_CPP_DEPRECATED persistent_ptr<T>
492  get_root()
493  {
494  return pool::root();
495  }
496 
509  static pool<T>
510  open(const std::string &path, const std::string &layout)
511  {
512  return pool<T>(pool_base::open(path, layout));
513  }
514 
531  static pool<T>
532  create(const std::string &path, const std::string &layout,
533  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
534  {
535  return pool<T>(pool_base::create(path, layout, size, mode));
536  }
537 
548  static int
549  check(const std::string &path, const std::string &layout)
550  {
551  return pool_base::check(path, layout);
552  }
553 
554 #ifdef _WIN32
555 
568  static pool<T>
569  open(const std::wstring &path, const std::wstring &layout)
570  {
571  return pool<T>(pool_base::open(path, layout));
572  }
573 
591  static pool<T>
592  create(const std::wstring &path, const std::wstring &layout,
593  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
594  {
595  return pool<T>(pool_base::create(path, layout, size, mode));
596  }
597 
609  static int
610  check(const std::wstring &path, const std::wstring &layout)
611  {
612  return pool_base::check(path, layout);
613  }
614 #endif
615 };
616 
617 } /* namespace obj */
618 
619 } /* namespace pmem */
620 
621 #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:368
pmem::obj::pool_base::persist
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent object.
Definition: pool.hpp:307
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:232
pmem::pool_error
Custom pool error class.
Definition: pexceptions.hpp:53
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:610
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:532
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::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:384
common.hpp
Commonly used functionality.
pmem::obj::pool::pool
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:472
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:181
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:255
pmem::obj::pool_base::drain
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:352
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:64
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:124
pmem::obj::pool_base::close
void close()
Closes the pool.
Definition: pool.hpp:267
pmem::obj::pool::root
persistent_ptr< T > root()
Retrieves pool's root object.
Definition: pool.hpp:482
pmem::obj::pool_base::pool_base
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:71
pmem::obj::pool_base::flush
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:343
pmem::obj::pool::pool
pool() noexcept=default
Defaulted constructor.
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:319
pmem::obj::pool_base::pool_base
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:82
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:154
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: persistent_ptr.hpp:132
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:205
pmem::obj::pool
PMEMobj pool class.
Definition: pool.hpp:430
p.hpp
Resides on pmem property template.
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:510
pmem::obj::pool_base::flush
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:331
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:569
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:592
pmem::obj::pool_base
The non-template pool base class.
Definition: pool.hpp:66
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:549