File: make_persistent_atomic.cpp

package info (click to toggle)
libpmemobj-cpp 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,388 kB
  • sloc: cpp: 136,076; sh: 1,022; perl: 381; ansic: 163; makefile: 13
file content (313 lines) | stat: -rw-r--r-- 6,575 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2016-2020, Intel Corporation */

/*
 * obj_cpp_make_persistent_atomic.cpp -- cpp make_persistent_atomic test for
 * objects
 */

#include "unittest.hpp"

#include <libpmemobj++/make_persistent_atomic.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj/ctl.h>

#define LAYOUT "cpp"

namespace nvobj = pmem::obj;

namespace
{

const int TEST_ARR_SIZE = 10;

struct force_throw {
};

class foo {
public:
	foo() : bar(1)
	{
		for (int i = 0; i < TEST_ARR_SIZE; ++i)
			this->arr[i] = 1;
	}

	foo(const int &val) : bar(val)
	{
		for (int i = 0; i < TEST_ARR_SIZE; ++i)
			this->arr[i] = static_cast<char>(val);
	}

	foo(const int &val, char arr_val) : bar(val)
	{
		for (int i = 0; i < TEST_ARR_SIZE; ++i)
			this->arr[i] = arr_val;
	}

	explicit foo(struct force_throw &val)
	{
		throw std::bad_alloc();
	}

	/*
	 * Assert values of foo.
	 */
	void
	check_foo(int val, char arr_val)
	{
		UT_ASSERTeq(val, this->bar);
		for (int i = 0; i < TEST_ARR_SIZE; ++i)
			UT_ASSERTeq(arr_val, this->arr[i]);
	}

	nvobj::p<int> bar;
	nvobj::p<char> arr[TEST_ARR_SIZE];
};

static int var_bar_copy_ctors_called = 0;
static int var_bar_move_ctors_called = 0;

struct var_bar {

	/* Expects 'a' to be rvalue ref and 'b' and 'c' to be lvalue ref. */
	template <typename A, typename B, typename C>
	var_bar(A &&a, B &&b, C &&c)
	{
		static_assert(std::is_rvalue_reference<decltype(a)>::value, "");
		static_assert(std::is_lvalue_reference<decltype(b)>::value, "");
		static_assert(std::is_lvalue_reference<decltype(c)>::value, "");
	}

	var_bar(const var_bar &a)
	{
		var_bar_copy_ctors_called++;
	}

	var_bar(var_bar &&a)
	{
		var_bar_move_ctors_called++;
	}
};

struct root {
	nvobj::persistent_ptr<foo> pfoo;
	nvobj::persistent_ptr<var_bar> pvbar;
};

/*
 * test_make_no_args -- (internal) test make_persistent without arguments
 */
void
test_make_no_args(nvobj::pool<struct root> &pop)
{
	nvobj::persistent_ptr<root> r = pop.root();

	UT_ASSERT(r->pfoo == nullptr);

	nvobj::make_persistent_atomic<foo>(pop, r->pfoo);
	r->pfoo->check_foo(1, 1);

	nvobj::delete_persistent_atomic<foo>(r->pfoo);
}

/*
 * test_make_args -- (internal) test make_persistent with arguments
 */
void
test_make_args(nvobj::pool<struct root> &pop)
{
	nvobj::persistent_ptr<root> r = pop.root();
	UT_ASSERT(r->pfoo == nullptr);

	nvobj::make_persistent_atomic<foo>(pop, r->pfoo, 2);
	r->pfoo->check_foo(2, 2);

	nvobj::delete_persistent_atomic<foo>(r->pfoo);

	nvobj::make_persistent_atomic<foo>(pop, r->pfoo, 3, 4);
	r->pfoo->check_foo(3, 4);

	nvobj::delete_persistent_atomic<foo>(r->pfoo);
}

/*
 * test_throw -- (internal) test if make_persistent_atomic rethrows constructor
 * exception
 */
void
test_throw(nvobj::pool<struct root> &pop)
{
	nvobj::persistent_ptr<root> r = pop.root();
	UT_ASSERT(r->pfoo == nullptr);

	bool exception_thrown = false;
	force_throw val;
	try {
		nvobj::make_persistent_atomic<foo>(pop, r->pfoo, val);
	} catch (std::bad_alloc &) {
		exception_thrown = true;
	} catch (std::exception &e) {
		UT_FATALexc(e);
	}

	UT_ASSERT(exception_thrown);
	UT_ASSERT(r->pfoo == nullptr);
}

/*
 * test_delete_null -- (internal) test atomic delete nullptr
 */
void
test_delete_null(nvobj::pool<struct root> &pop)
{
	nvobj::persistent_ptr<foo> pfoo;

	UT_ASSERT(pfoo == nullptr);

	try {
		nvobj::delete_persistent_atomic<foo>(pfoo);
	} catch (...) {
		UT_ASSERT(0);
	}
}

/*
 * test_flags -- (internal) test proper handling of flags
 */
void
test_flags(nvobj::pool<struct root> &pop)
{
	nvobj::persistent_ptr<root> r = pop.root();

	auto alloc_class = pop.ctl_set<struct pobj_alloc_class_desc>(
		"heap.alloc_class.new.desc",
		{sizeof(foo) + 16, 0, 200, POBJ_HEADER_COMPACT, 0});

	try {
		nvobj::make_persistent_atomic<foo>(
			pop, r->pfoo,
			nvobj::allocation_flag_atomic::class_id(
				alloc_class.class_id));
	} catch (std::exception &e) {
		UT_FATALexc(e);
	}

	UT_ASSERTeq(pmemobj_alloc_usable_size(r->pfoo.raw()), sizeof(foo));

	try {
		nvobj::delete_persistent_atomic<foo>(r->pfoo);
	} catch (std::exception &e) {
		UT_FATALexc(e);
	}

	try {
		nvobj::make_persistent_atomic<foo>(
			pop, r->pfoo,
			nvobj::allocation_flag_atomic::class_id(
				alloc_class.class_id),
			1, 2);
	} catch (std::exception &e) {
		UT_FATALexc(e);
	}

	r->pfoo->check_foo(1, 2);

	UT_ASSERTeq(pmemobj_alloc_usable_size(r->pfoo.raw()), sizeof(foo));

	try {
		nvobj::delete_persistent_atomic<foo>(r->pfoo);
	} catch (std::exception &e) {
		UT_FATALexc(e);
	}
}

/*
 * test_rlvalue_parameters -- (internal) test proper forwarding of arguments
 * to the constructor (maintaining rvalue and lvalue reference types)
 */
void
test_rlvalue_parameters(nvobj::pool<struct root> &pop)
{
	auto r = pop.root();

	int a = 1, b = 2, c = 3;
	nvobj::make_persistent_atomic<var_bar>(pop, r->pvbar, std::move(a), b,
					       c);

	nvobj::persistent_ptr<var_bar> vbar1;
	nvobj::persistent_ptr<var_bar> vbar2;
	nvobj::persistent_ptr<var_bar> vbar3;

	nvobj::make_persistent_atomic<var_bar>(pop, vbar1, *r->pvbar);
	UT_ASSERT(var_bar_copy_ctors_called == 1);
	UT_ASSERT(var_bar_move_ctors_called == 0);

	nvobj::make_persistent_atomic<var_bar>(pop, vbar2, *r->pvbar);
	UT_ASSERT(var_bar_copy_ctors_called == 2);
	UT_ASSERT(var_bar_move_ctors_called == 0);

	nvobj::make_persistent_atomic<var_bar>(pop, vbar3,
					       std::move(*r->pvbar));

	UT_ASSERT(var_bar_copy_ctors_called == 2);
	UT_ASSERT(var_bar_move_ctors_called == 1);
}

/*
 * test_make_invalid -- (internal) test failure of atomic make_persistent
 */
void
test_make_invalid(nvobj::pool<struct root> &pop)
{
	nvobj::persistent_ptr<foo> pfoo;

	bool thrown = false;
	try {
		nvobj::make_persistent_atomic<foo>(
			pop, pfoo,
			nvobj::allocation_flag_atomic::class_id(254));
	} catch (std::bad_alloc &e) {
		thrown = true;
	} catch (std::exception &e) {
		UT_FATALexc(e);
	}

	UT_ASSERT(thrown);
}
}

static void
test(int argc, char *argv[])
{
	if (argc != 2)
		UT_FATAL("usage: %s file-name", argv[0]);

	const char *path = argv[1];

	nvobj::pool<struct root> pop;

	try {
		pop = nvobj::pool<struct root>::create(
			path, LAYOUT, PMEMOBJ_MIN_POOL, S_IWUSR | S_IRUSR);
	} catch (pmem::pool_error &pe) {
		UT_FATAL("!pool::create: %s %s", pe.what(), path);
	}

	test_make_no_args(pop);
	test_make_args(pop);
	test_throw(pop);
	test_delete_null(pop);
	test_flags(pop);
	test_rlvalue_parameters(pop);
	test_make_invalid(pop);

	pop.close();
}

int
main(int argc, char *argv[])
{
	return run_test([&] { test(argc, argv); });
}