diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-10 20:49:52 +0000 |
commit | 55944e5e40b1be2afc4855d8d2baf4b73d1876b5 (patch) | |
tree | 33f869f55a1b149e9b7c2b7e201867ca5dd52992 /src/test/test-mempool.c | |
parent | Initial commit. (diff) | |
download | systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.tar.xz systemd-55944e5e40b1be2afc4855d8d2baf4b73d1876b5.zip |
Adding upstream version 255.4.upstream/255.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/test/test-mempool.c')
-rw-r--r-- | src/test/test-mempool.c | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/test/test-mempool.c b/src/test/test-mempool.c new file mode 100644 index 0000000..d3bc173 --- /dev/null +++ b/src/test/test-mempool.c @@ -0,0 +1,92 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ + +#include "mempool.h" +#include "random-util.h" +#include "tests.h" + +struct element { + uint64_t value; +}; + +DEFINE_MEMPOOL(test_mempool, struct element, 8); + +TEST(mempool_trim) { + +#define NN 4000 + struct element *a[NN]; + size_t n_freed = 0; + + assert_se(!test_mempool.first_pool); + assert_se(!test_mempool.freelist); + + mempool_trim(&test_mempool); + + for (size_t i = 0; i < NN; i++) { + assert_se(a[i] = mempool_alloc_tile(&test_mempool)); + a[i]->value = i; + } + + mempool_trim(&test_mempool); + + /* free up to one third randomly */ + size_t x = 0; + for (size_t i = 0; i < NN/3; i++) { + x = (x + random_u64()) % ELEMENTSOF(a); + assert_se(!a[x] || a[x]->value == x); + + if (a[x]) + n_freed ++; + + a[x] = mempool_free_tile(&test_mempool, a[x]); + } + + mempool_trim(&test_mempool); + + /* free definitely at least one third */ + for (size_t i = 2; i < NN; i += 3) { + assert_se(!a[i] || a[i]->value == i); + if (a[i]) + n_freed ++; + a[i] = mempool_free_tile(&test_mempool, a[i]); + } + + mempool_trim(&test_mempool); + + /* Allocate another set of tiles, which will fill up the free list and allocate some new tiles */ + struct element *b[NN]; + for (size_t i = 0; i < NN; i++) { + assert_se(b[i] = mempool_alloc_tile(&test_mempool)); + b[i]->value = ~(uint64_t) i; + } + + mempool_trim(&test_mempool); + + /* free everything from the original set*/ + + for (size_t i = 0; i < NN; i += 1) { + assert_se(!a[i] || a[i]->value == i); + if (a[i]) + n_freed ++; + a[i] = mempool_free_tile(&test_mempool, a[i]); + } + + mempool_trim(&test_mempool); + + /* and now everything from the second set too */ + + for (size_t i = 0; i < NN; i += 1) { + assert_se(!b[i] || b[i]->value == ~(uint64_t) i); + if (b[i]) + n_freed ++; + b[i] = mempool_free_tile(&test_mempool, b[i]); + } + + assert_se(n_freed == NN * 2); + + mempool_trim(&test_mempool); + + assert_se(!test_mempool.first_pool); + assert_se(!test_mempool.freelist); +} + +DEFINE_TEST_MAIN(LOG_DEBUG); |