nvml-sys 0.0.6

A low-level FFI wrapper around the Persistent Memory Development Kit, PMDK (formerly NVML) and its libraries, including libpmem, libpmemobj and others. Currently tracks master after version 1.3.1.
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include "test/jemalloc_test.h"

#define TEST_POOL_SIZE (16L * 1024L * 1024L)
#define TEST_TOO_SMALL_POOL_SIZE (2L * 1024L * 1024L)
#define TEST_VALUE 123456
#define TEST_MALLOC_FREE_LOOPS 2
#define TEST_MALLOC_SIZE 1024
#define TEST_ALLOCS_SIZE (TEST_POOL_SIZE / 8)
#define TEST_BUFFOR_CMP_SIZE (4L * 1024L * 1024L)

static char mem_pool[TEST_POOL_SIZE];
static char mem_extend_ok[TEST_POOL_SIZE];
static void* allocs[TEST_ALLOCS_SIZE];

static int custom_allocs;

TEST_BEGIN(test_pool_create_errors) {
	pool_t *pool;
	memset(mem_pool, 1, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, 0, 0, 1);
	assert_ptr_null(pool, "pool_create() should return NULL for size 0");

	pool = pool_create(NULL, TEST_POOL_SIZE, 0, 1);
	assert_ptr_null(pool, "pool_create() should return NULL for input addr NULL");
}
TEST_END

TEST_BEGIN(test_pool_create) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);
	assert_ptr_eq(pool, mem_pool, "pool_create() should return addr with valid input");
	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_malloc) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);

	int *test = pool_malloc(pool, sizeof(int));
	assert_ptr_not_null(test, "pool_malloc should return valid ptr");

	*test = TEST_VALUE;
	assert_x_eq(*test, TEST_VALUE, "ptr should be usable");

	assert_lu_gt((uintptr_t)test, (uintptr_t)mem_pool,
		"pool_malloc() should return pointer to memory from pool");
	assert_lu_lt((uintptr_t)test, (uintptr_t)mem_pool+TEST_POOL_SIZE,
		"pool_malloc() should return pointer to memory from pool");

	pool_free(pool, test);

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_free) {
	pool_t *pool;
	int i, j, s = 0, prev_s = 0;
	int allocs = TEST_POOL_SIZE/TEST_MALLOC_SIZE;
	void *arr[allocs];
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);

	for (i = 0; i < TEST_MALLOC_FREE_LOOPS; ++i) {
		for (j = 0; j < allocs; ++j) {
			arr[j] = pool_malloc(pool, TEST_MALLOC_SIZE);
			if (arr[j] != NULL) {
				s++;
			}
		}
		for (j = 0; j < allocs; ++j) {
			if (arr[j] != NULL) {
				pool_free(pool, arr[j]);
			}
		}
		if (prev_s != 0) {
			assert_x_eq(s, prev_s,
				"pool_free() should record back used chunks");
		}

		prev_s = s;
		s = 0;
	}

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_calloc) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 1, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 0, 1);

	int *test = pool_calloc(pool, 1, sizeof(int));
	assert_ptr_not_null(test, "pool_calloc should return valid ptr");

	assert_x_eq(*test, 0, "pool_calloc should return zeroed memory");

	pool_free(pool, test);

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_realloc) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);

	int *test = pool_ralloc(pool, NULL, sizeof(int));
	assert_ptr_not_null(test, "pool_ralloc with NULL addr should return valid ptr");

	int *test2 = pool_ralloc(pool, test, sizeof(int)*2);
	assert_ptr_not_null(test, "pool_ralloc should return valid ptr");
	test2[0] = TEST_VALUE;
	test2[1] = TEST_VALUE;

	assert_x_eq(test[1], TEST_VALUE, "ptr should be usable");

	pool_free(pool, test2);

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_aligned_alloc) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);

	int *test = pool_aligned_alloc(pool, 1024, 1024);
	assert_ptr_not_null(test, "pool_aligned_alloc should return valid ptr");
	assert_x_eq(((uintptr_t)(test) & 1023), 0, "ptr should be aligned");

	assert_lu_gt((uintptr_t)test, (uintptr_t)mem_pool,
		"pool_aligned_alloc() should return pointer to memory from pool");
	assert_lu_lt((uintptr_t)test, (uintptr_t)mem_pool+TEST_POOL_SIZE,
		"pool_aligned_alloc() should return pointer to memory from pool");

	*test = TEST_VALUE;
	assert_x_eq(*test, TEST_VALUE, "ptr should be usable");

	pool_free(pool, test);

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_reuse_pool) {
	pool_t *pool;
	size_t  pool_num = 0;
	custom_allocs = 0;

	/* create and destroy pool multiple times */
	for (; pool_num<100; ++pool_num) {
		pool = pool_create(mem_pool, TEST_POOL_SIZE, 0, 1);
		assert_ptr_not_null(pool, "Can not create pool!!!");
		if (pool == NULL) {
			break;
		}

		void *prev = NULL;
		size_t i = 0;

		/* allocate memory from pool */
		for (; i<100; ++i) {
			void **next = pool_malloc(pool, sizeof (void *));

			assert_lu_gt((uintptr_t)next, (uintptr_t)mem_pool,
				"pool_malloc() should return pointer to memory from pool");
			assert_lu_lt((uintptr_t)next, (uintptr_t)mem_pool+TEST_POOL_SIZE,
				"pool_malloc() should return pointer to memory from pool");

			*next = prev;
			prev = next;
		}

		/* free all allocated memory from pool */
		while (prev != NULL) {
			void **act = prev;
			prev = *act;
			pool_free(pool, act);
		}
		pool_delete(pool);
	}

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_check_memory) {
	pool_t *pool;
	size_t pool_size = POOL_MINIMAL_SIZE;
	assert_lu_lt(POOL_MINIMAL_SIZE, TEST_POOL_SIZE, "Too small pool size");

	size_t object_size;
	size_t size_allocated;
	size_t i;
	size_t j;

	for (object_size = 8; object_size <= TEST_BUFFOR_CMP_SIZE ; object_size *= 2) {
		custom_allocs = 0;
		pool = pool_create(mem_pool, pool_size, 0, 1);
		assert_ptr_not_null(pool, "Can not create pool!!!");
		size_allocated = 0;
		memset(allocs, 0, TEST_ALLOCS_SIZE * sizeof(void *));

		for (i = 0; i < TEST_ALLOCS_SIZE;++i) {
			allocs[i] = pool_malloc(pool, object_size);
			if (allocs[i] == NULL) {
				/* out of memory in pool */
				break;
			}
			assert_lu_gt((uintptr_t)allocs[i], (uintptr_t)mem_pool,
					"pool_malloc() should return pointer to memory from pool");
			assert_lu_lt((uintptr_t)allocs[i], (uintptr_t)mem_pool+pool_size,
				"pool_malloc() should return pointer to memory from pool");

			size_allocated += object_size;

			/* fill each allocation with a unique value */
			memset(allocs[i], (char)i, object_size);
		}

		assert_ptr_not_null(allocs[0], "pool_malloc should return valid ptr");
		assert_lu_lt(i + 1, TEST_ALLOCS_SIZE, "All memory should be used");

		/* check for unexpected modifications of prepare data */
		for (i = 0; i < TEST_ALLOCS_SIZE && allocs[i] != NULL; ++i) {
			char *buffer = allocs[i];
			for (j = 0; j < object_size; ++j)
				if (buffer[j] != (char)i) {
					assert_true(0, "Content of data object was modified unexpectedly"
						" for object size: %zu, id: %zu", object_size, j);
					break;
			}
		}

		pool_delete(pool);

		assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
	}

}
TEST_END

TEST_BEGIN(test_pool_use_all_memory) {
	pool_t *pool;
	size_t size = 0;
	size_t pool_size = POOL_MINIMAL_SIZE;
	assert_lu_lt(POOL_MINIMAL_SIZE, TEST_POOL_SIZE, "Too small pool size");
	custom_allocs = 0;
	pool = pool_create(mem_pool, pool_size, 0, 1);
	assert_ptr_not_null(pool, "Can not create pool!!!");

	void *prev = NULL;
	for (;;) {
		void **next = pool_malloc(pool, sizeof (void *));
		if (next == NULL) {
			/* Out of memory in pool, test end */
			break;
		}
		size += sizeof (void *);

		assert_ptr_not_null(next, "pool_malloc should return valid ptr");

		assert_lu_gt((uintptr_t)next, (uintptr_t)mem_pool,
				"pool_malloc() should return pointer to memory from pool");
		assert_lu_lt((uintptr_t)next, (uintptr_t)mem_pool+pool_size,
			"pool_malloc() should return pointer to memory from pool");

		*next = prev;
		assert_x_eq((uintptr_t)(*next), (uintptr_t)(prev), "ptr should be usable");
		prev = next;

	}

	assert_lu_gt(size, 0, "Can not alloc any memory from pool");

	/* Free all allocated memory from pool */
	while (prev != NULL) {
		void **act = prev;
		prev = *act;
		pool_free(pool, act);
	}

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_extend_errors) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);

	memset(mem_extend_ok, 0, TEST_TOO_SMALL_POOL_SIZE);
	size_t usable_size = pool_extend(pool, mem_extend_ok, TEST_TOO_SMALL_POOL_SIZE, 0);

	assert_zu_eq(usable_size, 0, "pool_extend() should return 0"
		" when provided with memory size smaller then chunksize");

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_extend) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);

	memset(mem_extend_ok, 0, TEST_POOL_SIZE);
	size_t usable_size = pool_extend(pool, mem_extend_ok, TEST_POOL_SIZE, 0);

	assert_zu_ne(usable_size, 0, "pool_extend() should return value"
		" after alignment when provided with enough memory");

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

TEST_BEGIN(test_pool_extend_after_out_of_memory) {
	pool_t *pool;
	custom_allocs = 0;
	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);

	/* use the all memory from pool and from base allocator */
	while (pool_malloc(pool, sizeof (void *)));
	pool->base_next_addr = pool->base_past_addr;

	memset(mem_extend_ok, 0, TEST_POOL_SIZE);
	size_t usable_size = pool_extend(pool, mem_extend_ok, TEST_POOL_SIZE, 0);

	assert_zu_ne(usable_size, 0, "pool_extend() should return value"
		" after alignment when provided with enough memory");

	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");
}
TEST_END

/*
 * print_jemalloc_messages -- custom print function, for jemalloc
 */
static void
print_jemalloc_messages(void* ignore, const char *s)
{

}

TEST_BEGIN(test_pool_check_extend) {
	je_malloc_message = print_jemalloc_messages;
	pool_t *pool;
	custom_allocs = 0;

	pool = pool_create(mem_pool, TEST_POOL_SIZE, 0, 1);
	pool_malloc(pool, 100);
	assert_d_eq(je_pool_check(pool), 1, "je_pool_check() return error");
	pool_delete(pool);
	assert_d_ne(je_pool_check(pool), 1, "je_pool_check() not return error");

	pool = pool_create(mem_pool, TEST_POOL_SIZE, 0, 1);
	assert_d_eq(je_pool_check(pool), 1, "je_pool_check() return error");
	size_t size_extend = pool_extend(pool, mem_extend_ok, TEST_POOL_SIZE, 1);
	assert_zu_ne(size_extend, 0, "pool_extend() should add some free space");
	assert_d_eq(je_pool_check(pool), 1, "je_pool_check() return error");
	pool_malloc(pool, 100);
	pool_delete(pool);
	assert_d_ne(je_pool_check(pool), 1, "je_pool_check() not return error");

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");

	je_malloc_message = NULL;
}
TEST_END

TEST_BEGIN(test_pool_check_memory_out_of_range) {
	je_malloc_message = print_jemalloc_messages;
	pool_t *pool;
	custom_allocs = 0;

	pool = pool_create(mem_pool, TEST_POOL_SIZE, 0, 1);
	assert_d_eq(je_pool_check(pool), 1, "je_pool_check() return error");

	void *usable_addr = (void *)CHUNK_CEILING((uintptr_t)mem_extend_ok);
	size_t usable_size = (TEST_POOL_SIZE - (uintptr_t)(usable_addr -
			(void *)mem_extend_ok)) & ~chunksize_mask;

	chunk_record(pool,
			&pool->chunks_szad_mmap, &pool->chunks_ad_mmap,
			usable_addr, usable_size, 0);

	assert_d_ne(je_pool_check(pool), 1, "je_pool_check() not return error");

	pool_delete(pool);
	assert_d_ne(je_pool_check(pool), 1, "je_pool_check() return error");

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");

	je_malloc_message = NULL;
}
TEST_END

TEST_BEGIN(test_pool_check_memory_overlap) {
	je_malloc_message = print_jemalloc_messages;
	pool_t *pool;
	pool_t *pool2;
	custom_allocs = 0;

	memset(mem_pool, 0, TEST_POOL_SIZE);
	pool = pool_create(mem_pool, TEST_POOL_SIZE, 1, 1);
	size_t size_extend = pool_extend(pool, mem_extend_ok, TEST_POOL_SIZE, 1);
	assert_zu_ne(size_extend, 0, "pool_extend() should add some free space");
	assert_d_eq(je_pool_check(pool), 1, "je_pool_check() return error");

	/* create another pool in the same memory region */
	pool2 = pool_create(mem_extend_ok, TEST_POOL_SIZE, 0, 1);
	assert_d_ne(je_pool_check(pool), 1, "je_pool_check() not return error");
	assert_d_ne(je_pool_check(pool2), 1, "je_pool_check() not return error");
	pool_delete(pool2);
	pool_delete(pool);

	assert_d_eq(custom_allocs, 0, "memory leak when using custom allocator");

	je_malloc_message = NULL;
}
TEST_END


#define	POOL_TEST_CASES\
	test_pool_create_errors,	\
	test_pool_create,	\
	test_pool_malloc,	\
	test_pool_free,	\
	test_pool_calloc,	\
	test_pool_realloc,	\
	test_pool_aligned_alloc,	\
	test_pool_reuse_pool,	\
	test_pool_check_memory,	\
	test_pool_use_all_memory,	\
	test_pool_extend_errors,	\
	test_pool_extend,	\
	test_pool_extend_after_out_of_memory,	\
	test_pool_check_extend,	\
	test_pool_check_memory_out_of_range,	\
	test_pool_check_memory_overlap