pool.hpp
Go to the documentation of this file.
/*
* Copyright 2016-2017, Intel Corporation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef PMEMOBJ_POOL_HPP
#define PMEMOBJ_POOL_HPP
#include <stddef.h>
#include <string>
#include <sys/stat.h>
#include "libpmemobj++/detail/pexceptions.hpp"
#include "libpmemobj++/p.hpp"
#include "libpmemobj/pool_base.h"
namespace pmem
{
namespace obj
{
template <typename T>
class persistent_ptr;
class pool_base {
public:
pool_base() noexcept : pop(nullptr)
{
}
explicit pool_base(pmemobjpool *cpop) noexcept : pop(cpop)
{
}
pool_base(const pool_base &) noexcept = default;
pool_base(pool_base &&) noexcept = default;
pool_base &operator=(const pool_base &) noexcept = default;
pool_base &operator=(pool_base &&) noexcept = default;
virtual ~pool_base() noexcept = default;
static pool_base
open(const std::string &path, const std::string &layout)
{
#ifdef _WIN32
pmemobjpool *pop = pmemobj_openU(path.c_str(), layout.c_str());
#else
pmemobjpool *pop = pmemobj_open(path.c_str(), layout.c_str());
#endif
if (pop == nullptr)
throw pool_error("Failed opening pool");
return pool_base(pop);
}
static pool_base
create(const std::string &path, const std::string &layout,
std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
{
#ifdef _WIN32
pmemobjpool *pop = pmemobj_createU(path.c_str(), layout.c_str(),
size, mode);
#else
pmemobjpool *pop = pmemobj_create(path.c_str(), layout.c_str(),
size, mode);
#endif
if (pop == nullptr)
throw pool_error("Failed creating pool");
return pool_base(pop);
}
static int
check(const std::string &path, const std::string &layout) noexcept
{
#ifdef _WIN32
return pmemobj_checkU(path.c_str(), layout.c_str());
#else
return pmemobj_check(path.c_str(), layout.c_str());
#endif
}
#ifdef _WIN32
static pool_base
open(const std::wstring &path, const std::wstring &layout)
{
pmemobjpool *pop = pmemobj_openW(path.c_str(), layout.c_str());
if (pop == nullptr)
throw pool_error("Failed opening pool");
return pool_base(pop);
}
static pool_base
create(const std::wstring &path, const std::wstring &layout,
std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
{
pmemobjpool *pop = pmemobj_createW(path.c_str(), layout.c_str(),
size, mode);
if (pop == nullptr)
throw pool_error("Failed creating pool");
return pool_base(pop);
}
static int
check(const std::wstring &path, const std::wstring &layout) noexcept
{
return pmemobj_checkW(path.c_str(), layout.c_str());
}
#endif
void
close()
{
if (this->pop == nullptr)
throw std::logic_error("Pool already closed");
pmemobj_close(this->pop);
this->pop = nullptr;
}
void
persist(const void *addr, size_t len) noexcept
{
pmemobj_persist(this->pop, addr, len);
}
template <typename Y>
void
persist(const p<Y> &prop) noexcept
{
pmemobj_persist(this->pop, &prop, sizeof(Y));
}
template <typename Y>
void
persist(const persistent_ptr<Y> &ptr) noexcept
{
pmemobj_persist(this->pop, &ptr, sizeof(ptr));
}
void
flush(const void *addr, size_t len) noexcept
{
pmemobj_flush(this->pop, addr, len);
}
template <typename Y>
void
flush(const p<Y> &prop) noexcept
{
pmemobj_flush(this->pop, &prop, sizeof(Y));
}
template <typename Y>
void
flush(const persistent_ptr<Y> &ptr) noexcept
{
pmemobj_flush(this->pop, &ptr, sizeof(ptr));
}
void
drain(void) noexcept
{
pmemobj_drain(this->pop);
}
void *
memcpy_persist(void *dest, const void *src, size_t len) noexcept
{
return pmemobj_memcpy_persist(this->pop, dest, src, len);
}
void *
memset_persist(void *dest, int c, size_t len) noexcept
{
return pmemobj_memset_persist(this->pop, dest, c, len);
}
/*
* Gets the C style handle to the pool.
*
* Necessary to be able to use the pool with the C API.
*
* @return pool opaque handle.
*/
PMEMobjpool *
get_handle() noexcept
{
return this->pop;
}
protected:
/* The pool opaque handle */
PMEMobjpool *pop;
#ifndef _WIN32
/* Default create mode */
static const int DEFAULT_MODE = S_IWUSR | S_IRUSR;
#else
/* Default create mode */
static const int DEFAULT_MODE = S_IWRITE | S_IREAD;
#endif
};
template <typename T>
class pool : public pool_base {
public:
pool() noexcept = default;
pool(const pool &) noexcept = default;
pool(pool &&) noexcept = default;
pool &operator=(const pool &) noexcept = default;
pool &operator=(pool &&) noexcept = default;
~pool() noexcept = default;
explicit pool(const pool_base &pb) noexcept : pool_base(pb)
{
}
explicit pool(pool_base &&pb) noexcept : pool_base(pb)
{
}
persistent_ptr<T>
get_root()
{
if (pop == nullptr)
throw pool_error("Invalid pool handle");
persistent_ptr<T> root = pmemobj_root(this->pop, sizeof(T));
return root;
}
static pool<T>
open(const std::string &path, const std::string &layout)
{
return pool<T>(pool_base::open(path, layout));
}
static pool<T>
create(const std::string &path, const std::string &layout,
std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
{
return pool<T>(pool_base::create(path, layout, size, mode));
}
static int
check(const std::string &path, const std::string &layout)
{
return pool_base::check(path, layout);
}
#ifdef _WIN32
static pool<T>
open(const std::wstring &path, const std::wstring &layout)
{
return pool<T>(pool_base::open(path, layout));
}
static pool<T>
create(const std::wstring &path, const std::wstring &layout,
std::size_t size = PMEMOBJ_MIN_POOL, mode_t mode = DEFAULT_MODE)
{
return pool<T>(pool_base::create(path, layout, size, mode));
}
static int
check(const std::wstring &path, const std::wstring &layout)
{
return pool_base::check(path, layout);
}
#endif
};
} /* namespace obj */
} /* namespace pmem */
#endif /* PMEMOBJ_POOL_HPP */
pmem::obj::pool::pool
pool(const pool_base &pb) noexcept
Defaulted copy constructor.
Definition: pool.hpp:458
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:180
pmem::obj::pool_base::persist
void persist(const persistent_ptr< Y > &ptr) noexcept
Performs persist operation on a given persistent object.
Definition: pool.hpp:306
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: common.hpp:51
pmem::obj::pool_base
The non-template pool base class.
Definition: pool.hpp:65
pmem::obj::pool::pool
pool(pool_base &&pb) noexcept
Defaulted move constructor.
Definition: pool.hpp:465
pmem::obj::pool_base::flush
void flush(const p< Y > &prop) noexcept
Performs flush operation on a given pmem property.
Definition: pool.hpp:330
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:367
pmem::pool_error
Custom pool error class.
Definition: pexceptions.hpp:53
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:204
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:497
p.hpp
Resides on pmem property template.
pmem::obj::pool
PMEMobj pool class.
Definition: persistent_ptr.hpp:59
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:153
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:123
pexceptions.hpp
Custom exceptions.
pmem::obj::pool_base::operator=
pool_base & operator=(const pool_base &) noexcept=default
Defaulted copy assignment operator.
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:282
pmem::obj::pool_base::~pool_base
virtual ~pool_base() noexcept=default
Default virtual destructor.
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:383
pmem::obj::pool::get_root
persistent_ptr< T > get_root()
Retrieves pool&#39;s root object.
Definition: pool.hpp:475
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:597
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:519
pmem::obj::pool_base::flush
void flush(const persistent_ptr< Y > &ptr) noexcept
Performs flush operation on a given persistent object.
Definition: pool.hpp:342
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:254
pmem::obj::pool_base::persist
void persist(const p< Y > &prop) noexcept
Performs persist operation on a given pmem property.
Definition: pool.hpp:294
pmem::obj::pool_base::pool_base
pool_base() noexcept
Defaulted constructor.
Definition: pool.hpp:70
pmem::obj::pool_base::pool_base
pool_base(pmemobjpool *cpop) noexcept
Explicit constructor.
Definition: pool.hpp:81
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:536
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:231
pmem::obj::p
Resides on pmem class.
Definition: p.hpp:64
pmem
Definition: allocator.hpp:48
pmem::obj::pool_base::drain
void drain(void) noexcept
Performs drain operation.
Definition: pool.hpp:351
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:318
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:556
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:579
pmem::obj::pool_base::close
void close()
Closes the pool.
Definition: pool.hpp:266