NVML C++ bindings  1.1.0
This is the C++ bindings documentation for NVML's libpmemobj.
pool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017, 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 PMEMOBJ_POOL_HPP
39 #define PMEMOBJ_POOL_HPP
40 
41 #include <stddef.h>
42 #include <sys/stat.h>
43 
45 #include "libpmemobj++/p.hpp"
46 #include "libpmemobj/pool_base.h"
47 
48 namespace nvml
49 {
50 
51 namespace obj
52 {
53 template <typename T>
54 class persistent_ptr;
55 
64 class pool_base {
65 public:
69  pool_base() noexcept : pop(nullptr)
70  {
71  }
72 
80  explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
81  {
82  }
83 
87  pool_base(const pool_base &) noexcept = default;
88 
92  pool_base(pool_base &&) noexcept = default;
93 
97  pool_base &operator=(const pool_base &) noexcept = default;
98 
102  pool_base &operator=(pool_base &&) noexcept = default;
103 
107  virtual ~pool_base() noexcept = default;
108 
121  static pool_base
122  open(const std::string &path, const std::string &layout)
123  {
124 #ifdef _WIN32
125  pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
126 #else
127  pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
128 #endif
129  if (pop == nullptr)
130  throw pool_error("Failed opening pool");
131 
132  return pool_base(pop);
133  }
134 
151  static pool_base
152  create(const std::string &path, const std::string &layout,
153  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
154  {
155 #ifdef _WIN32
156  pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
157  size, mode);
158 #else
159  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
160  size, mode);
161 #endif
162  if (pop == nullptr)
163  throw pool_error("Failed creating pool");
164 
165  return pool_base(pop);
166  }
167 
178  static int
179  check(const std::string &path, const std::string &layout) noexcept
180  {
181 #ifdef _WIN32
182  return pmemobj_checkU(path.c_str(), layout.c_str());
183 #else
184  return pmemobj_check(path.c_str(), layout.c_str());
185 #endif
186  }
187 
188 #ifdef _WIN32
189 
202  static pool_base
203  open(const std::wstring &path, const std::wstring &layout)
204  {
205  pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
206  if (pop == nullptr)
207  throw pool_error("Failed opening pool");
208 
209  return pool_base(pop);
210  }
211 
229  static pool_base
230  create(const std::wstring &path, const std::wstring &layout,
231  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
232  {
233  pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
234  size, mode);
235  if (pop == nullptr)
236  throw pool_error("Failed creating pool");
237 
238  return pool_base(pop);
239  }
240 
252  static int
253  check(const std::wstring &path, const std::wstring &layout) noexcept
254  {
255  return pmemobj_checkW(path.c_str(), layout.c_str());
256  }
257 #endif
258 
264  void
266  {
267  if (this->pop == nullptr)
268  throw std::logic_error("Pool already closed");
269 
270  pmemobj_close(this->pop);
271  this->pop = nullptr;
272  }
273 
280  void
281  persist(const void *addr, size_t len) noexcept
282  {
283  pmemobj_persist(this->pop, addr, len);
284  }
285 
291  template <typename Y>
292  void
293  persist(const p<Y> &prop) noexcept
294  {
295  pmemobj_persist(this->pop, &prop, sizeof(Y));
296  }
297 
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 
387  /*
388  * Gets the C style handle to the pool.
389  *
390  * Necessary to be able to use the pool with the C API.
391  *
392  * @return pool opaque handle.
393  */
394  PMEMobjpool *
395  get_handle() noexcept
396  {
397  return this->pop;
398  }
399 
400 protected:
401  /* The pool opaque handle */
402  PMEMobjpool *pop;
403 
404 #ifndef _WIN32
405  /* Default create mode */
406  static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
407 #else
408  /* Default create mode */
409  static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
410 #endif
411 };
412 
421 template <typename T>
422 class pool : public pool_base {
423 public:
427  pool() noexcept = default;
428 
432  pool(const pool &) noexcept = default;
433 
437  pool(pool &&) noexcept = default;
438 
442  pool &operator=(const pool &) noexcept = default;
443 
447  pool &operator=(pool &&) noexcept = default;
448 
452  ~pool() noexcept = default;
453 
457  explicit pool(const pool_base &pb) noexcept : pool_base(pb)
458  {
459  }
460 
464  explicit pool(pool_base &&pb) noexcept : pool_base(pb)
465  {
466  }
467 
475  {
476  if (pop == nullptr)
477  throw pool_error("Invalid pool handle");
478 
479  persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
480  return root;
481  }
482 
495  static pool<T>
496  open(const std::string &path, const std::string &layout)
497  {
498  return pool<T>(pool_base::open(path, layout));
499  }
500 
517  static pool<T>
518  create(const std::string &path, const std::string &layout,
519  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
520  {
521  return pool<T>(pool_base::create(path, layout, size, mode));
522  }
523 
534  static int
535  check(const std::string &path, const std::string &layout)
536  {
537  return pool_base::check(path, layout);
538  }
539 
540 #ifdef _WIN32
541 
554  static pool<T>
555  open(const std::wstring &path, const std::wstring &layout)
556  {
557  return pool<T>(pool_base::open(path, layout));
558  }
559 
577  static pool<T>
578  create(const std::wstring &path, const std::wstring &layout,
579  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
580  {
581  return pool<T>(pool_base::create(path, layout, size, mode));
582  }
583 
595  static int
596  check(const std::wstring &path, const std::wstring &layout)
597  {
598  return pool_base::check(path, layout);
599  }
600 #endif
601 };
602 
603 } /* namespace obj */
604 
605 } /* namespace nvml */
606 
607 #endif /* PMEMOBJ_POOL_HPP */
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:518
Persistent pointer class.
Definition: common.hpp:51
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:329
static pool_base open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:203
Definition: pext.hpp:338
static int check(const std::wstring &path, const std::wstring &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:596
The non-template pool base class.
Definition: pool.hpp:64
Resides on pmem property template.
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:69
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent object.
Definition: pool.hpp:305
Custom exceptions.
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:578
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:464
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:341
static int check(const std::wstring &path, const std::wstring &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:253
pool() noexcept=default
Defaulted constructor.
static int check(const std::string &path, const std::string &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:535
PMEMobj pool class.
Definition: persistent_ptr.hpp:59
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:317
static pool< T > open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:555
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:179
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:281
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:293
persistent_ptr< T > get_root()
Retrieves pool&#39;s root object.
Definition: pool.hpp:474
static pool_base open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:122
Resides on pmem class.
Definition: p.hpp:64
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
Definition: allocator.hpp:48
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:230
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:350
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:152
void close()
Closes the pool.
Definition: pool.hpp:265
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:80
static pool< T > open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:496
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
Custom pool error class.
Definition: pexceptions.hpp:53