NVML C++ bindings  1.0.0
This is the C++ bindings documentation for NVML's libpmemobj.
pool.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016, 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  pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
125  if (pop == nullptr)
126  throw pool_error("Failed opening pool");
127 
128  return pool_base(pop);
129  }
130 
147  static pool_base
148  create(const std::string &path, const std::string &layout,
149  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
150  {
151  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
152  size, mode);
153  if (pop == nullptr)
154  throw pool_error("Failed creating pool");
155 
156  return pool_base(pop);
157  }
158 
169  static int
170  check(const std::string &path, const std::string &layout) noexcept
171  {
172  return pmemobj_check(path.c_str(), layout.c_str());
173  }
174 
180  void
182  {
183  if (this->pop == nullptr)
184  throw std::logic_error("Pool already closed");
185 
186  pmemobj_close(this->pop);
187  this->pop = nullptr;
188  }
189 
196  void
197  persist(const void *addr, size_t len) noexcept
198  {
199  pmemobj_persist(this->pop, addr, len);
200  }
201 
207  template <typename Y>
208  void
209  persist(const p<Y> &prop) noexcept
210  {
211  pmemobj_persist(this->pop, &prop, sizeof(Y));
212  }
213 
219  template <typename Y>
220  void
221  persist(const persistent_ptr<Y> &ptr) noexcept
222  {
223  pmemobj_persist(this->pop, &ptr, sizeof(ptr));
224  }
225 
232  void
233  flush(const void *addr, size_t len) noexcept
234  {
235  pmemobj_flush(this->pop, addr, len);
236  }
237 
243  template <typename Y>
244  void
245  flush(const p<Y> &prop) noexcept
246  {
247  pmemobj_flush(this->pop, &prop, sizeof(Y));
248  }
249 
255  template <typename Y>
256  void
257  flush(const persistent_ptr<Y> &ptr) noexcept
258  {
259  pmemobj_flush(this->pop, &ptr, sizeof(ptr));
260  }
261 
265  void
266  drain(void) noexcept
267  {
268  pmemobj_drain(this->pop);
269  }
270 
281  void *
282  memcpy_persist(void *dest, const void *src, size_t len) noexcept
283  {
284  return pmemobj_memcpy_persist(this->pop, dest, src, len);
285  }
286 
297  void *
298  memset_persist(void *dest, int c, size_t len) noexcept
299  {
300  return pmemobj_memset_persist(this->pop, dest, c, len);
301  }
302 
303  /*
304  * Gets the C style handle to the pool.
305  *
306  * Necessary to be able to use the pool with the C API.
307  *
308  * @return pool opaque handle.
309  */
310  PMEMobjpool *
311  get_handle() noexcept
312  {
313  return this->pop;
314  }
315 
316 protected:
317  /* The pool opaque handle */
318  PMEMobjpool *pop;
319 
320 #ifndef _WIN32
321  /* Default create mode */
322  static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
323 #else
324  /* Default create mode */
325  static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
326 #endif
327 };
328 
337 template <typename T>
338 class pool : public pool_base {
339 public:
343  pool() noexcept = default;
344 
348  pool(const pool &) noexcept = default;
349 
353  pool(pool &&) noexcept = default;
354 
358  pool &operator=(const pool &) noexcept = default;
359 
363  pool &operator=(pool &&) noexcept = default;
364 
368  ~pool() noexcept = default;
369 
373  explicit pool(const pool_base &pb) noexcept : pool_base(pb)
374  {
375  }
376 
380  explicit pool(pool_base &&pb) noexcept : pool_base(pb)
381  {
382  }
383 
391  {
392  if (pop == nullptr)
393  throw pool_error("Invalid pool handle");
394 
395  persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
396  return root;
397  }
398 
411  static pool<T>
412  open(const std::string &path, const std::string &layout)
413  {
414  return pool<T>(pool_base::open(path, layout));
415  }
416 
433  static pool<T>
434  create(const std::string &path, const std::string &layout,
435  std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
436  {
437  return pool<T>(pool_base::create(path, layout, size, mode));
438  }
439 
450  static int
451  check(const std::string &path, const std::string &layout)
452  {
453  return pool_base::check(path, layout);
454  }
455 };
456 
457 } /* namespace obj */
458 
459 } /* namespace nvml */
460 
461 #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:434
Persistent pointer class.
Definition: persistent_ptr.hpp:72
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:245
virtual ~pool_base() noexcept=default
Default virtual destructor.
The non-template pool base class.
Definition: pool.hpp:64
pool(const pool_base &pb) noexcept
Defaulted copy constructor.
Definition: pool.hpp:373
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:221
Custom exceptions.
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:380
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:257
static int check(const std::string &path, const std::string &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:451
PMEMobj pool class.
Definition: persistent_ptr.hpp:55
pool_base & operator=(const pool_base &) noexcept=default
Defaulted copy assignment operator.
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:233
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:170
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:197
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:209
persistent_ptr< T > get_root()
Retrieves pool&#39;s root object.
Definition: pool.hpp:390
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:298
Definition: condition_variable.hpp:48
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:266
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:148
void close()
Closes the pool.
Definition: pool.hpp:181
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:412
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:282
Custom pool error class.
Definition: pexceptions.hpp:53