File: ptr.hpp

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 (308 lines) | stat: -rw-r--r-- 6,786 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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2015-2020, Intel Corporation */

#include "unittest.hpp"

#include <libpmemobj++/experimental/self_relative_ptr.hpp>
#include <libpmemobj++/make_persistent.hpp>
#include <libpmemobj++/make_persistent_array_atomic.hpp>
#include <libpmemobj++/make_persistent_atomic.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/persistent_ptr_base.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>

#include <sstream>

namespace nvobj = pmem::obj;
namespace nvobjexp = nvobj::experimental;

template <template <typename U> class pointer>
void
assert_if_oid_is_null(pointer<int> &f)
{
	UT_ASSERT(OID_IS_NULL(f.raw()));
}

template <>
void
assert_if_oid_is_null(nvobjexp::self_relative_ptr<int> &f)
{
}

/*
 * test_null_ptr -- verifies if the pointer correctly behaves like a
 * nullptr-value
 */
template <template <typename U> class pointer>
void
test_null_ptr(pointer<int> &f)
{
	assert_if_oid_is_null(f);
	UT_ASSERT((bool)f == false);
	UT_ASSERT(!f);
	UT_ASSERTeq(f.get(), nullptr);
	UT_ASSERT(f == nullptr);
}

/*
 * get_temp -- returns a temporary persistent_ptr
 */
template <template <typename U> class pointer>
pointer<int>
get_temp()
{
	pointer<int> int_null = nullptr;

	return int_null;
}

/*
 * test_ptr_operators_null -- verifies various operations on nullptr pointers
 */
template <template <typename U> class pointer>
void
test_ptr_operators_null()
{
	pointer<int> int_default_null;
	test_null_ptr(int_default_null);

	pointer<int> int_explicit_ptr_null = nullptr;
	test_null_ptr(int_explicit_ptr_null);

	pointer<int> int_explicit_oid_null = OID_NULL;
	test_null_ptr(int_explicit_oid_null);

	pointer<int> int_base = nullptr;
	pointer<int> int_same = int_base;
	int_same = int_base;
	test_null_ptr(int_same);

	swap(int_base, int_same);

	auto temp_ptr = get_temp<pointer>();
	test_null_ptr(temp_ptr);
}

constexpr int TEST_INT = 10;
constexpr int TEST_ARR_SIZE = 10;
constexpr char TEST_CHAR = 'a';

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

template <template <typename U> class pointer>
struct nested {
	pointer<foo> inner;
};

template <template <typename U> class pointer, class pointer_base>
struct templated_root {
	pointer<foo> pfoo;
	pointer<nvobj::p<int>[(long unsigned) TEST_ARR_SIZE]> parr;
	pointer_base arr[3];

	/* This variable is unused, but it's here to check if the persistent_ptr
	 * does not violate its own restrictions.
	 */
	pointer<nested<pointer>> outer;
};

bool
base_is_null(const nvobjexp::self_relative_ptr_base &ptr)
{
	return ptr.is_null();
}

bool
base_is_null(const nvobj::persistent_ptr_base &ptr)
{
	auto pmemoid = ptr.raw();
	return pmemoid.off == 0 && pmemoid.pool_uuid_lo == 0;
}

/*
 * test_root_pointer -- verifies pointers at the root are nullptr
 */
template <template <typename U> class pointer, class pointer_base>
void
test_root_pointers(templated_root<pointer, pointer_base> &root)
{
	UT_ASSERT(root.pfoo == nullptr);
	UT_ASSERT(root.parr == nullptr);
	for (auto it = std::begin(root.arr); it != std::end(root.arr); ++it) {
		UT_ASSERT(base_is_null(*it));
	}
}

/*
 * test_ptr_array -- verifies the array specialization behavior
 */
template <template <typename U> class pointer, class pointer_base>
void
test_ptr_array(nvobj::pool<templated_root<pointer, pointer_base>> &pop)
{
	pointer<nvobj::p<int>[]> parr_vsize;

	try {
		nvobj::persistent_ptr<nvobj::p<int>[]> local_ptr;
		nvobj::make_persistent_atomic<nvobj::p<int>[]>(pop, local_ptr,
							       TEST_ARR_SIZE);
		parr_vsize = local_ptr;
	} catch (...) {
		UT_ASSERT(0);
	}

	{
		nvobj::transaction::manual tx(pop);

		for (int i = 0; i < TEST_ARR_SIZE; ++i)
			parr_vsize[i] = i;
		nvobj::transaction::commit();
	}

	for (int i = 0; i < TEST_ARR_SIZE; ++i)
		UT_ASSERTeq(parr_vsize[i], i);

	auto r = pop.root();

	try {
		nvobj::transaction::run(pop, [&] {
			r->parr = pmemobj_tx_zalloc(sizeof(int) * TEST_ARR_SIZE,
						    0);
		});
	} catch (...) {
		UT_ASSERT(0);
	}

	UT_ASSERT(r->parr != nullptr);

	bool exception_thrown = false;
	try {
		nvobj::transaction::run(pop, [&] {
			for (int i = 0; i < TEST_ARR_SIZE; ++i)
				r->parr[i] = TEST_INT;

			nvobj::transaction::abort(-1);
		});
	} catch (pmem::manual_tx_abort &) {
		exception_thrown = true;
	} catch (...) {
		UT_ASSERT(0);
	}

	UT_ASSERT(exception_thrown);

	exception_thrown = false;
	try {
		nvobj::transaction::run(pop, [&] {
			for (int i = 0; i < TEST_ARR_SIZE; ++i)
				r->parr[i] = TEST_INT;

			nvobj::transaction::abort(-1);
		});
	} catch (pmem::manual_tx_abort &) {
		exception_thrown = true;
	} catch (...) {
		UT_ASSERT(0);
	}

	UT_ASSERT(exception_thrown);

	for (int i = 0; i < TEST_ARR_SIZE; ++i)
		UT_ASSERTeq(r->parr[i], 0);
}

/*
 * test_ptr_transactional -- verifies the persistent ptr with the tx C API
 */
template <template <typename U> class pointer, class pointer_base>
void
test_ptr_transactional(nvobj::pool<templated_root<pointer, pointer_base>> &pop)
{
	auto r = pop.root();
	pointer<foo> to_swap;
	try {
		nvobj::transaction::run(pop, [&] {
			UT_ASSERT(r->pfoo == nullptr);

			r->pfoo = nvobj::make_persistent<foo>();

			/* allocate for future swap test */
			to_swap = nvobj::make_persistent<foo>();
		});
	} catch (...) {
		UT_ASSERT(0);
	}

	auto pfoo = r->pfoo;

	try {
		nvobj::transaction::run(pop, [&] {
			pfoo->bar = TEST_INT;
			/* raw memory access requires extra care */
			pmem::detail::conditional_add_to_tx(&pfoo->arr);
			memset(&pfoo->arr, TEST_CHAR, sizeof(pfoo->arr));

			/* do the swap test */
			pointer<foo> foo_ptr = pfoo;
			pointer<foo> swap_ptr = to_swap;

			std::stringstream stream;
			stream << "Before swap: " << r->pfoo << " " << to_swap
			       << std::endl;

			to_swap.swap(r->pfoo);

			stream << "After swap: " << r->pfoo << " " << to_swap
			       << std::endl;
			UT_OUT("%s\n", stream.str().c_str());

			UT_ASSERT(to_swap == foo_ptr);
			UT_ASSERT(r->pfoo == swap_ptr);

			swap(r->pfoo, to_swap);
			UT_ASSERT(to_swap == swap_ptr);
			UT_ASSERT(r->pfoo == foo_ptr);

			nvobj::delete_persistent<foo>(to_swap);
		});
	} catch (...) {
		UT_ASSERT(0);
	}

	UT_ASSERTeq(pfoo->bar, TEST_INT);
	for (auto c : pfoo->arr) {
		UT_ASSERTeq(c, TEST_CHAR);
	}

	bool exception_thrown = false;
	try {
		nvobj::transaction::run(pop, [&] {
			pfoo->bar = 0;
			nvobj::transaction::abort(-1);
		});
	} catch (pmem::manual_tx_abort &) {
		exception_thrown = true;
	} catch (...) {
		UT_ASSERT(0);
	}

	UT_ASSERT(exception_thrown);
	UT_ASSERTeq(pfoo->bar, TEST_INT);

	try {
		nvobj::transaction::run(pop, [&] {
			nvobj::delete_persistent<foo>(r->pfoo);
			r->pfoo = nullptr;
		});
	} catch (...) {
		UT_ASSERT(0);
	}

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