Skip to content
This repository was archived by the owner on Mar 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions include/libpmemobj++/experimental/inline_string.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */
/* Copyright 2020-2021, Intel Corporation */

/**
* @file
Expand Down Expand Up @@ -30,6 +30,9 @@ namespace experimental
* This class serves similar purpose to pmem::obj::string, but
* keeps the data within the same allocation as inline_string itself.
*
* Unlike other containers, it can be used on pmem and dram. Modifiers (like
* assign()) can only be called if inline string is kept on pmem).
*
* The data is always kept right after the inline_string structure.
* It means that creating an object of inline_string must be done
* as follows:
Expand Down Expand Up @@ -97,50 +100,35 @@ using inline_u32string = basic_inline_string<char32_t>;

/**
* Constructs inline string from a string_view.
*
* @throw pool_error if inline_string doesn't reside on pmem.
*/
template <typename CharT, typename Traits>
basic_inline_string<CharT, Traits>::basic_inline_string(
basic_string_view<CharT, Traits> v)
: size_(v.size()), capacity_(v.size())
{
if (nullptr == pmemobj_pool_by_ptr(this))
throw pmem::pool_error("Invalid pool handle.");

std::copy(v.data(), v.data() + static_cast<ptrdiff_t>(size_), data());

data()[static_cast<ptrdiff_t>(size_)] = '\0';
}

/**
* Constructs empty inline_string with specified capacity.
*
* @throw pool_error if inline_string doesn't reside on pmem.
*/
template <typename CharT, typename Traits>
basic_inline_string<CharT, Traits>::basic_inline_string(size_type capacity)
: size_(0), capacity_(capacity)
{
if (nullptr == pmemobj_pool_by_ptr(this))
throw pmem::pool_error("Invalid pool handle.");

data()[static_cast<ptrdiff_t>(size_)] = '\0';
}

/**
* Copy constructor
*
* @throw pool_error if inline_string doesn't reside on pmem.
*/
template <typename CharT, typename Traits>
basic_inline_string<CharT, Traits>::basic_inline_string(
const basic_inline_string &rhs)
: size_(rhs.size()), capacity_(rhs.capacity())
{
if (nullptr == pmemobj_pool_by_ptr(this))
throw pmem::pool_error("Invalid pool handle.");

std::copy(rhs.data(), rhs.data() + static_cast<ptrdiff_t>(size_),
data());

Expand Down Expand Up @@ -366,12 +354,17 @@ basic_inline_string<CharT, Traits>::snapshotted_data(size_t p, size_t n)
* Transactionally assign content of basic_string_view.
*
* @throw std::out_of_range if rhs is larger than capacity.
* @throw pool_error if inline string is not on pmem.
*/
template <typename CharT, typename Traits>
basic_inline_string<CharT, Traits> &
basic_inline_string<CharT, Traits>::assign(basic_string_view<CharT, Traits> rhs)
{
auto pop = obj::pool_base(pmemobj_pool_by_ptr(this));
auto cpop = pmemobj_pool_by_ptr(this);
if (nullptr == cpop)
throw pmem::pool_error("Invalid pool handle.");

auto pop = pool_base(cpop);

if (rhs.size() > capacity())
throw std::out_of_range("inline_string capacity exceeded.");
Expand Down
70 changes: 27 additions & 43 deletions tests/inline_string/inline_string.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2016-2020, Intel Corporation */
/* Copyright 2016-2021, Intel Corporation */

#include "unittest.hpp"

Expand Down Expand Up @@ -247,59 +247,43 @@ test_inline_string(nvobj::pool<struct root<T>> &pop)
});
}

/* test if inline_string can be placed on dram */
template <typename T>
void
test_ctor_exception_nopmem(nvobj::pool<struct root<T>> &pop)
test_dram(nvobj::pool<struct root<T>> &pop)
{
auto bs1 = std::basic_string<T>(4, static_cast<T>('a'));
nvobj::basic_string_view<T> bsv_test_string1(bs1.data(), bs1.length());
using string_type = nvobj::experimental::basic_inline_string<T>;

try {
std::string example_str("example");
std::basic_string<T> bs(example_str.begin(), example_str.end());
Object<T> o(
1, nvobj::basic_string_view<T>(bs.data(), bs.length()));
UT_ASSERT(0);
} catch (pmem::pool_error &) {
} catch (...) {
UT_ASSERT(0);
}
constexpr size_t string_size = 20;
typename std::aligned_storage<sizeof(string_type) +
(string_size + 1) * sizeof(T),
alignof(string_type)>::type buffer;

auto r = pop.root();
std::basic_string<T> s(string_size, T('a'));

const auto req_capacity = 100;
auto dram_location = reinterpret_cast<string_type *>(&buffer);
new (dram_location)
string_type(nvobj::basic_string_view<T>(s.data(), s.length()));

nvobj::transaction::run(pop, [&] {
nvobj::standard_alloc_policy<void> allocator;
r->o1 = static_cast<nvobj::persistent_ptr<Object<T>>>(
allocator.allocate(sizeof(Object<T>) +
req_capacity * sizeof(T)));
UT_ASSERT(nvobj::basic_string_view<T>(s.data(), s.length()) ==
nvobj::basic_string_view<T>(*dram_location));

new (r->o1.get()) Object<T>(1, bsv_test_string1);
dram_location->~string_type();

try {
Object<T> o(*r->o1);
UT_ASSERT(0);
} catch (pmem::pool_error &) {
} catch (...) {
UT_ASSERT(0);
}
});
}
new (dram_location) string_type(string_size);

template <typename T>
void
test_ctor_exception(void)
{
UT_ASSERTeq(dram_location->capacity(), string_size);
UT_ASSERTeq(dram_location->size(), 0);

/* inline_string cannot be modified on dram. */
try {
int capacity = 10;
nvobjex::basic_inline_string<T>(
static_cast<nvobjex::inline_string::size_type>(
capacity));
UT_ASSERT(0);
s = std::basic_string<T>(string_size / 2, T('b'));
dram_location->assign(s.data());

ASSERT_UNREACHABLE;
} catch (pmem::pool_error &) {
} catch (...) {
UT_ASSERT(0);
ASSERT_UNREACHABLE;
}
}
}
Expand All @@ -324,8 +308,8 @@ test(int argc, char *argv[])
}

test_inline_string<T>(pop);
test_ctor_exception_nopmem<T>(pop);
test_ctor_exception<T>();
test_dram<T>(pop);

pop.close();
}

Expand Down