PMDK C++ bindings 1.2.0
This is the C++ bindings documentation for PMDK'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 <string>
43#include <sys/stat.h>
44
46#include "libpmemobj++/p.hpp"
47#include "libpmemobj/pool_base.h"
48
49namespace pmem
50{
51
52namespace obj
53{
54template <typename T>
55class persistent_ptr;
56
65class pool_base {
66public:
70 pool_base() noexcept : pop(nullptr)
71 {
72 }
73
81 explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
82 {
83 }
84
88 pool_base(const pool_base &) noexcept = default;
89
93 pool_base(pool_base &&) noexcept = default;
94
98 pool_base &operator=(const pool_base &) noexcept = default;
99
103 pool_base &operator=(pool_base &&) noexcept = default;
104
108 virtual ~pool_base() noexcept = default;
109
122 static pool_base
123 open(const std::string &path, const std::string &layout)
124 {
125#ifdef _WIN32
126 pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
127#else
128 pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
129#endif
130 if (pop == nullptr)
131 throw pool_error("Failed opening pool");
132
133 return pool_base(pop);
134 }
135
152 static pool_base
153 create(const std::string &path, const std::string &layout,
154 std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
155 {
156#ifdef _WIN32
157 pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
158 size, mode);
159#else
160 pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
161 size, mode);
162#endif
163 if (pop == nullptr)
164 throw pool_error("Failed creating pool");
165
166 return pool_base(pop);
167 }
168
179 static int
180 check(const std::string &path, const std::string &layout) noexcept
181 {
182#ifdef _WIN32
183 return pmemobj_checkU(path.c_str(), layout.c_str());
184#else
185 return pmemobj_check(path.c_str(), layout.c_str());
186#endif
187 }
188
189#ifdef _WIN32
203 static pool_base
204 open(const std::wstring &path, const std::wstring &layout)
205 {
206 pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
207 if (pop == nullptr)
208 throw pool_error("Failed opening pool");
209
210 return pool_base(pop);
211 }
212
230 static pool_base
231 create(const std::wstring &path, const std::wstring &layout,
232 std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
233 {
234 pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
235 size, mode);
236 if (pop == nullptr)
237 throw pool_error("Failed creating pool");
238
239 return pool_base(pop);
240 }
241
253 static int
254 check(const std::wstring &path, const std::wstring &layout) noexcept
255 {
256 return pmemobj_checkW(path.c_str(), layout.c_str());
257 }
258#endif
259
265 void
267 {
268 if (this->pop == nullptr)
269 throw std::logic_error("Pool already closed");
270
271 pmemobj_close(this->pop);
272 this->pop = nullptr;
273 }
274
281 void
282 persist(const void *addr, size_t len) noexcept
283 {
284 pmemobj_persist(this->pop, addr, len);
285 }
286
292 template <typename Y>
293 void
294 persist(const p<Y> &prop) noexcept
295 {
296 pmemobj_persist(this->pop, &prop, sizeof(Y));
297 }
298
304 template <typename Y>
305 void
306 persist(const persistent_ptr<Y> &ptr) noexcept
307 {
308 pmemobj_persist(this->pop, &ptr, sizeof(ptr));
309 }
310
317 void
318 flush(const void *addr, size_t len) noexcept
319 {
320 pmemobj_flush(this->pop, addr, len);
321 }
322
328 template <typename Y>
329 void
330 flush(const p<Y> &prop) noexcept
331 {
332 pmemobj_flush(this->pop, &prop, sizeof(Y));
333 }
334
340 template <typename Y>
341 void
342 flush(const persistent_ptr<Y> &ptr) noexcept
343 {
344 pmemobj_flush(this->pop, &ptr, sizeof(ptr));
345 }
346
350 void
351 drain(void) noexcept
352 {
353 pmemobj_drain(this->pop);
354 }
355
366 void *
367 memcpy_persist(void *dest, const void *src, size_t len) noexcept
368 {
369 return pmemobj_memcpy_persist(this->pop, dest, src, len);
370 }
371
382 void *
383 memset_persist(void *dest, int c, size_t len) noexcept
384 {
385 return pmemobj_memset_persist(this->pop, dest, c, len);
386 }
387
388 /*
389 * Gets the C style handle to the pool.
390 *
391 * Necessary to be able to use the pool with the C API.
392 *
393 * @return pool opaque handle.
394 */
395 PMEMobjpool *
396 get_handle() noexcept
397 {
398 return this->pop;
399 }
400
401protected:
402 /* The pool opaque handle */
403 PMEMobjpool *pop;
404
405#ifndef _WIN32
406 /* Default create mode */
407 static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
408#else
409 /* Default create mode */
410 static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
411#endif
412};
413
422template <typename T>
423class pool : public pool_base {
424public:
428 pool() noexcept = default;
429
433 pool(const pool &) noexcept = default;
434
438 pool(pool &&) noexcept = default;
439
443 pool &operator=(const pool &) noexcept = default;
444
448 pool &operator=(pool &&) noexcept = default;
449
453 ~pool() noexcept = default;
454
458 explicit pool(const pool_base &pb) noexcept : pool_base(pb)
459 {
460 }
461
465 explicit pool(pool_base &&pb) noexcept : pool_base(pb)
466 {
467 }
468
476 {
477 if (pop == nullptr)
478 throw pool_error("Invalid pool handle");
479
480 persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
481 return root;
482 }
483
496 static pool<T>
497 open(const std::string &path, const std::string &layout)
498 {
499 return pool<T>(pool_base::open(path, layout));
500 }
501
518 static pool<T>
519 create(const std::string &path, const std::string &layout,
520 std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
521 {
522 return pool<T>(pool_base::create(path, layout, size, mode));
523 }
524
535 static int
536 check(const std::string &path, const std::string &layout)
537 {
538 return pool_base::check(path, layout);
539 }
540
541#ifdef _WIN32
555 static pool<T>
556 open(const std::wstring &path, const std::wstring &layout)
557 {
558 return pool<T>(pool_base::open(path, layout));
559 }
560
578 static pool<T>
579 create(const std::wstring &path, const std::wstring &layout,
580 std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
581 {
582 return pool<T>(pool_base::create(path, layout, size, mode));
583 }
584
596 static int
597 check(const std::wstring &path, const std::wstring &layout)
598 {
599 return pool_base::check(path, layout);
600 }
601#endif
602};
603
604} /* namespace obj */
605
606} /* namespace pmem */
607
608#endif /* PMEMOBJ_POOL_HPP */
Resides on pmem class.
Definition: p.hpp:64
Persistent pointer class.
Definition: persistent_ptr.hpp:102
The non-template pool base class.
Definition: pool.hpp:65
static pool_base open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:123
void flush(const void *addr, size_t len) noexcept
Performs flush operation on a given chunk of memory.
Definition: pool.hpp:318
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:367
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:330
static int check(const std::wstring &path, const std::wstring &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:254
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent object.
Definition: pool.hpp:306
pool_base(pool_base &&) noexcept=default
Defaulted move constructor.
void close()
Closes the pool.
Definition: pool.hpp:266
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:294
void persist(const void *addr, size_t len) noexcept
Performs persist operation on a given chunk of memory.
Definition: pool.hpp:282
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:81
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:342
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:383
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:70
static pool_base open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:204
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:153
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:231
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:351
static int check(const std::string &path, const std::string &layout) noexcept
Checks if a given pool is consistent.
Definition: pool.hpp:180
PMEMobj pool class.
Definition: pool.hpp:423
persistent_ptr< T > get_root()
Retrieves pool's root object.
Definition: pool.hpp:475
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:519
static int check(const std::string &path, const std::string &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:536
static pool< T > open(const std::string &path, const std::string &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:497
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:465
static int check(const std::wstring &path, const std::wstring &layout)
Checks if a given pool is consistent.
Definition: pool.hpp:597
static pool< T > open(const std::wstring &path, const std::wstring &layout)
Opens an existing object store memory pool.
Definition: pool.hpp:556
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:579
pool() noexcept=default
Defaulted constructor.
Custom pool error class.
Definition: pexceptions.hpp:53
Resides on pmem property template.
Custom exceptions.