make_persistent.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_MAKE_PERSISTENT_HPP
#define PMEMOBJ_MAKE_PERSISTENT_HPP
#include "libpmemobj++/detail/check_persistent_ptr_array.hpp"
#include "libpmemobj++/detail/common.hpp"
#include "libpmemobj++/detail/life.hpp"
#include "libpmemobj++/detail/pexceptions.hpp"
#include "libpmemobj/tx_base.h"
#include <new>
#include <utility>
namespace pmem
{
namespace obj
{
template <typename T, typename... Args>
typename detail::pp_if_not_array<T>::type
make_persistent(Args &&... args)
{
if (pmemobj_tx_stage() != TX_STAGE_WORK)
throw transaction_scope_error(
"refusing to allocate "
"memory outside of transaction scope");
persistent_ptr<T> ptr =
pmemobj_tx_alloc(sizeof(T), detail::type_num<T>());
if (ptr == nullptr)
throw transaction_alloc_error("failed to allocate "
"persistent memory object");
try {
detail::create<T, Args...>(ptr.get(),
std::forward<Args>(args)...);
} catch (...) {
pmemobj_tx_free(*ptr.raw_ptr());
throw;
}
return ptr;
}
template <typename T>
void
delete_persistent(typename detail::pp_if_not_array<T>::type ptr)
{
if (pmemobj_tx_stage() != TX_STAGE_WORK)
throw transaction_scope_error(
"refusing to free "
"memory outside of transaction scope");
if (ptr == nullptr)
return;
/*
* At this point, everything in the object should be tracked
* and reverted on transaction abort.
*/
detail::destroy<T>(*ptr);
if (pmemobj_tx_free(*ptr.raw_ptr()) != 0)
throw transaction_free_error("failed to delete "
"persistent memory object");
}
} /* namespace obj */
} /* namespace pmem */
#endif /* PMEMOBJ_MAKE_PERSISTENT_HPP */
pmem::transaction_alloc_error
Custom transaction error class.
Definition: pexceptions.hpp:84
pmem::obj::persistent_ptr
Persistent pointer class.
Definition: common.hpp:51
pmem::obj::delete_persistent
void delete_persistent(typename detail::pp_if_not_array< T >::type ptr)
Transactionally free an object of type T held in a persitent_ptr.
Definition: make_persistent.hpp:114
pmem::obj::make_persistent
detail::pp_if_not_array< T >::type make_persistent(Args &&... args)
Transactionally allocate and construct an object of type T.
Definition: make_persistent.hpp:74
life.hpp
Functions for destroying arrays.
common.hpp
Commonly used functionality.
pexceptions.hpp
Custom exceptions.
check_persistent_ptr_array.hpp
Compile time type check for make_persistent.
pmem::transaction_free_error
Custom transaction error class.
Definition: pexceptions.hpp:94
pmem::detail::persistent_ptr_base::raw_ptr
PMEMoid * raw_ptr() noexcept
Get pointer to PMEMoid encapsulated by this object.
Definition: persistent_ptr_base.hpp:309
pmem::transaction_scope_error
Custom transaction error class.
Definition: pexceptions.hpp:104
pmem
Definition: allocator.hpp:48
pmem::detail::persistent_ptr_base::get
element_type * get() const noexcept
Get a direct pointer.
Definition: persistent_ptr_base.hpp:278