From d026ddde822efed49c5fd4e28f67403782da7f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 15 Dec 2021 11:16:46 +0100 Subject: [PATCH] Add unit test of aligned isc_mem functions Add unit test that checks whether all the aligned functions work and that allocators return memory aligned at the specified boundary. --- lib/isc/tests/mem_test.c | 47 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) diff --git a/lib/isc/tests/mem_test.c b/lib/isc/tests/mem_test.c index 9e35af3fc7..b61ed7cb01 100644 --- a/lib/isc/tests/mem_test.c +++ b/lib/isc/tests/mem_test.c @@ -12,6 +12,7 @@ #if HAVE_CMOCKA #include +#include #include /* IWYU pragma: keep */ #include #include @@ -148,6 +149,45 @@ isc_mem_test(void **state) { isc_mempool_destroy(&mp1); } +#if defined(HAVE_MALLOC_NP_H) || defined(HAVE_JEMALLOC) +/* aligned memory system tests */ +static void +isc_mem_aligned_test(void **state) { + isc_mem_t *mctx2 = NULL; + void *ptr; + size_t alignment; + uintptr_t aligned; + + UNUSED(state); + + /* Check different alignment sizes up to the page size */ + for (alignment = sizeof(void *); alignment <= 4096; alignment *= 2) { + size_t size = alignment / 2 - 1; + ptr = isc_mem_get_aligned(test_mctx, size, alignment); + + /* Check if the pointer is properly aligned */ + aligned = (((uintptr_t)ptr / alignment) * alignment); + assert_ptr_equal(aligned, (uintptr_t)ptr); + + /* Check if we can resize to range */ + ptr = isc_mem_reget_aligned(test_mctx, ptr, size, + size * 2 + alignment, alignment); + + /* Check if the pointer is still properly aligned */ + aligned = (((uintptr_t)ptr / alignment) * alignment); + assert_ptr_equal(aligned, (uintptr_t)ptr); + + isc_mem_put_aligned(test_mctx, ptr, size * 2 + alignment, + alignment); + + /* Check whether isc_mem_putanddetach_detach() also works */ + isc_mem_create(&mctx2); + ptr = isc_mem_get_aligned(mctx2, size, alignment); + isc_mem_putanddetach_aligned(&mctx2, ptr, size, alignment); + } +} +#endif /* defined(HAVE_MALLOC_NP_H) || defined(HAVE_JEMALLOC) */ + /* test TotalUse calculation */ static void isc_mem_total_test(void **state) { @@ -474,7 +514,8 @@ isc_mem_benchmark(void **state) { t = isc_time_microdiff(&ts2, &ts1); printf("[ TIME ] isc_mem_benchmark: " - "%d isc_mem_{get,put} calls, %f seconds, %f calls/second\n", + "%d isc_mem_{get,put} calls, %f seconds, %f " + "calls/second\n", nthreads * ITERS * NUM_ITEMS, t / 1000000.0, (nthreads * ITERS * NUM_ITEMS) / (t / 1000000.0)); } @@ -490,6 +531,10 @@ main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test_setup_teardown(isc_mem_test, _setup, _teardown), +#if defined(HAVE_MALLOC_NP_H) || defined(HAVE_JEMALLOC) + cmocka_unit_test_setup_teardown(isc_mem_aligned_test, _setup, + _teardown), +#endif /* defined(HAVE_MALLOC_NP_H) || defined(HAVE_JEMALLOC) */ cmocka_unit_test_setup_teardown(isc_mem_total_test, _setup, _teardown), cmocka_unit_test_setup_teardown(isc_mem_inuse_test, _setup,