2016-10-30 19:40:50 -04:00
|
|
|
/** Unit tests for memory pool
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2020-01-20 17:17:00 +01:00
|
|
|
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
|
2017-04-27 12:56:43 +02:00
|
|
|
* @license GNU General Public License (version 3)
|
|
|
|
*
|
|
|
|
* VILLASnode
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* any later version.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2017-05-05 19:24:16 +00:00
|
|
|
*
|
2017-04-27 12:56:43 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-10-30 19:40:50 -04:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <criterion/parameterized.h>
|
|
|
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
2018-03-26 12:50:15 +02:00
|
|
|
#include <villas/pool.h>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2016-10-30 19:40:50 -04:00
|
|
|
|
2018-08-13 14:58:52 +02:00
|
|
|
extern void init_memory();
|
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
struct param {
|
|
|
|
int thread_count;
|
|
|
|
int pool_size;
|
|
|
|
size_t block_size;
|
2019-10-26 13:28:29 +02:00
|
|
|
struct memory_type *mt;
|
2016-10-30 19:40:50 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
ParameterizedTestParameters(pool, basic)
|
|
|
|
{
|
|
|
|
static struct param params[] = {
|
2019-10-26 13:28:29 +02:00
|
|
|
{ 1, 4096, 150, &memory_heap },
|
|
|
|
{ 1, 128, 8, &memory_mmap_hugetlb },
|
|
|
|
{ 1, 4, 8192, &memory_mmap_hugetlb },
|
|
|
|
{ 1, 1 << 13, 4, &memory_mmap_hugetlb }
|
2016-10-30 19:40:50 -04:00
|
|
|
};
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
return cr_make_param_array(struct param, params, ARRAY_LEN(params));
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:57:05 +02:00
|
|
|
// cppcheck-suppress unknownMacro
|
2018-08-13 14:58:52 +02:00
|
|
|
ParameterizedTest(struct param *p, pool, basic, .init = init_memory)
|
2016-10-30 19:40:50 -04:00
|
|
|
{
|
|
|
|
int ret;
|
2020-06-16 02:35:34 +02:00
|
|
|
struct pool pool;
|
2016-10-30 19:40:50 -04:00
|
|
|
|
|
|
|
void *ptr, *ptrs[p->pool_size];
|
|
|
|
|
2019-10-26 13:28:29 +02:00
|
|
|
ret = pool_init(&pool, p->pool_size, p->block_size, p->mt);
|
2016-10-30 19:40:50 -04:00
|
|
|
cr_assert_eq(ret, 0, "Failed to create pool");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
ptr = pool_get(&pool);
|
2018-08-27 11:21:57 +02:00
|
|
|
cr_assert_neq(ptr, nullptr);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
memset(ptr, 1, p->block_size); /* check that we dont get a seg fault */
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:43:22 -04:00
|
|
|
int i;
|
|
|
|
for (i = 1; i < p->pool_size; i++) {
|
2016-10-30 19:40:50 -04:00
|
|
|
ptrs[i] = pool_get(&pool);
|
2016-10-30 19:43:22 -04:00
|
|
|
|
2018-08-27 11:21:57 +02:00
|
|
|
if (ptrs[i] == nullptr)
|
2016-10-30 19:43:22 -04:00
|
|
|
break;
|
2016-10-30 19:40:50 -04:00
|
|
|
}
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:43:22 -04:00
|
|
|
if (i < p->pool_size)
|
2018-08-27 11:21:57 +02:00
|
|
|
cr_assert_neq(ptrs[i], nullptr);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
ptr = pool_get(&pool);
|
2018-08-27 11:21:57 +02:00
|
|
|
cr_assert_eq(ptr, nullptr);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-10-30 19:40:50 -04:00
|
|
|
ret = pool_destroy(&pool);
|
|
|
|
cr_assert_eq(ret, 0, "Failed to destroy pool");
|
|
|
|
|
2017-03-28 10:28:20 +02:00
|
|
|
}
|