NVML C++ bindings  0.1 experimental
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,
150  mode_t mode = S_IWUSR | S_IRUSR)
151  {
152  pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
153  size, mode);
154  if (pop == nullptr)
155  throw pool_error("Failed creating pool");
156 
157  return pool_base(pop);
158  }
159 
170  static int
171  check(const std::string &path, const std::string &layout) noexcept
172  {
173  return pmemobj_check(path.c_str(), layout.c_str());
174  }
175 
181  void
183  {
184  if (this->pop == nullptr)
185  throw std::logic_error("Pool already closed");
186 
187  pmemobj_close(this->pop);
188  this->pop = nullptr;
189  }
190 
197  void
198  persist(const void *addr, size_t len) noexcept
199  {
200  pmemobj_persist(this->pop, addr, len);
201  }
202 
208  template <typename Y>
209  void
210  persist(const p<Y> &prop) noexcept
211  {
212  pmemobj_persist(this->pop, &prop, sizeof(Y));
213  }
214 
220  template <typename Y>
221  void
222  persist(const persistent_ptr<Y> &ptr) noexcept
223  {
224  pmemobj_persist(this->pop, &ptr, sizeof(ptr));
225  }
226 
233  void
234  flush(const void *addr, size_t len) noexcept
235  {
236  pmemobj_flush(this->pop, addr, len);
237  }
238 
244  template <typename Y>
245  void
246  flush(const p<Y> &prop) noexcept
247  {
248  pmemobj_flush(this->pop, &prop, sizeof(Y));
249  }
250 
256  template <typename Y>
257  void
258  flush(const persistent_ptr<Y> &ptr) noexcept
259  {
260  pmemobj_flush(this->pop, &ptr, sizeof(ptr));
261  }
262 
266  void
267  drain(void) noexcept
268  {
269  pmemobj_drain(this->pop);
270  }
271 
282  void *
283  memcpy_persist(void *dest, const void *src, size_t len) noexcept
284  {
285  return pmemobj_memcpy_persist(this->pop, dest, src, len);
286  }
287 
298  void *
299  memset_persist(void *dest, int c, size_t len) noexcept
300  {
301  return pmemobj_memset_persist(this->pop, dest, c, len);
302  }
303 
304  /*
305  * Gets the C style handle to the pool.
306  *
307  * Necessary to be able to use the pool with the C API.
308  *
309  * @return pool opaque handle.
310  */
311  PMEMobjpool *
312  get_handle() noexcept
313  {
314  return this->pop;
315  }
316 
317 protected:
318  /* The pool opaque handle */
319  PMEMobjpool *pop;
320 };
321 
330 template <typename T>
331 class pool : public pool_base {
332 public:
336  pool() noexcept = default;
337 
341  pool(const pool &) noexcept = default;
342 
346  pool(pool &&) noexcept = default;
347 
351  pool &operator=(const pool &) noexcept = default;
352 
356  pool &operator=(pool &&) noexcept = default;
357 
361  ~pool() noexcept = default;
362 
366  explicit pool(const pool_base &pb) noexcept : pool_base(pb)
367  {
368  }
369 
373  explicit pool(pool_base &&pb) noexcept : pool_base(pb)
374  {
375  }
376 
384  {
385  if (pop == nullptr)
386  throw pool_error("Invalid pool handle");
387 
388  persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
389  return root;
390  }
391 
404  static pool<T>
405  open(const std::string &path, const std::string &layout)
406  {
407  return pool<T>(pool_base::open(path, layout));
408  }
409 
426  static pool<T>
427  create(const std::string &path, const std::string &layout,
428  std::size_t size = PMEMOBJ_MIN_POOL,
429  mode_t mode = S_IWUSR | S_IRUSR)
430  {
431  return pool<T>(pool_base::create(path, layout, size, mode));
432  }
433 
444  static int
445  check(const std::string &path, const std::string &layout)
446  {
447  return pool_base::check(path, layout);
448  }
449 };
450 
451 } /* namespace obj */
452 
453 } /* namespace nvml */
454 
455 #endif /* PMEMOBJ_POOL_HPP */
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:246
virtual ~pool_base() noexcept=default
Default virtual destructor.
static pool_base create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=S_IWUSR|S_IRUSR)
Creates a new transactional object store pool.
Definition: pool.hpp:148
The non-template pool base class.
Definition: pool.hpp:64
pool(const pool_base &pb) noexcept
Defaulted copy constructor.
Definition: pool.hpp:366
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:222
Custom exceptions.
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:373
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:258
static int check(const std::string &path, const std::string &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:445
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:234
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:171
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:198
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:210
persistent_ptr< T > get_root()
Retrieves pool&#39;s root object.
Definition: pool.hpp:383
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:299
Definition: condition_variable.hpp:48
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:267
void close()
Closes the pool.
Definition: pool.hpp:182
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:405
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:283
static pool< T > create(const std::string &path, const std::string &layout, std::size_t size=PMEMOBJ_MIN_POOL, mode_t mode=S_IWUSR|S_IRUSR)
Creates a new transactional object store pool.
Definition: pool.hpp:427
Custom pool error class.
Definition: pexceptions.hpp:53