2016-10-30 18:39:22 -04:00
|
|
|
/** Unit tests for memory management
|
|
|
|
*
|
|
|
|
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, 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 18:39:22 -04:00
|
|
|
*********************************************************************************/
|
|
|
|
|
|
|
|
#include <criterion/criterion.h>
|
|
|
|
#include <criterion/theories.h>
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
2018-03-26 12:50:15 +02:00
|
|
|
#include <villas/memory.h>
|
|
|
|
#include <villas/utils.h>
|
2016-10-30 18:39:22 -04:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
#define HUGEPAGESIZE (1 << 22)
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2016-10-30 18:39:22 -04:00
|
|
|
TheoryDataPoints(memory, aligned) = {
|
2016-11-20 03:02:19 -05:00
|
|
|
DataPoints(size_t, 1, 32, 55, 1 << 10, 1 << 20),
|
2016-10-30 18:39:22 -04:00
|
|
|
DataPoints(size_t, 1, 8, 1 << 12),
|
2018-07-02 14:17:50 +02:00
|
|
|
DataPoints(struct memory_type *, &memory_type_heap, &memory_hugepage)
|
2016-10-30 18:39:22 -04:00
|
|
|
};
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
Theory((size_t len, size_t align, struct memory_type *m), memory, aligned) {
|
2016-10-30 18:39:22 -04:00
|
|
|
int ret;
|
|
|
|
void *ptr;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_init(100);
|
|
|
|
cr_assert(!ret);
|
|
|
|
|
2016-10-30 18:39:22 -04:00
|
|
|
ptr = memory_alloc_aligned(m, len, align);
|
|
|
|
cr_assert_neq(ptr, NULL, "Failed to allocate memory");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-11-20 03:02:19 -05:00
|
|
|
cr_assert(IS_ALIGNED(ptr, align));
|
2016-10-30 19:40:31 -04:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
if (m == &memory_hugepage) {
|
2016-10-30 18:39:22 -04:00
|
|
|
cr_assert(IS_ALIGNED(ptr, HUGEPAGESIZE));
|
|
|
|
}
|
2016-10-30 19:40:31 -04:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(ptr);
|
2016-10-30 18:39:22 -04:00
|
|
|
cr_assert_eq(ret, 0, "Failed to release memory: ret=%d, ptr=%p, len=%zu: %s", ret, ptr, len, strerror(errno));
|
2017-03-28 10:28:20 +02:00
|
|
|
}
|
2017-03-28 12:06:10 +02:00
|
|
|
|
|
|
|
Test(memory, manager) {
|
2017-03-31 18:28:51 +02:00
|
|
|
size_t total_size;
|
|
|
|
size_t max_block;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2017-03-31 18:28:51 +02:00
|
|
|
int ret;
|
|
|
|
void *p, *p1, *p2, *p3;
|
2018-07-02 14:17:50 +02:00
|
|
|
struct memory_type *m;
|
2017-03-31 18:28:51 +02:00
|
|
|
|
|
|
|
total_size = 1 << 10;
|
2018-07-02 19:00:55 +02:00
|
|
|
max_block = total_size - sizeof(struct memory_type) - sizeof(struct memory_block);
|
|
|
|
|
|
|
|
ret = memory_init(0);
|
|
|
|
cr_assert(!ret);
|
2017-03-31 18:28:51 +02:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
p = memory_alloc(&memory_type_heap, total_size);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(p);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
m = memory_managed(p, total_size);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert_not_null(m);
|
|
|
|
|
|
|
|
p1 = memory_alloc(m, 16);
|
|
|
|
cr_assert_not_null(p1);
|
|
|
|
|
|
|
|
p2 = memory_alloc(m, 32);
|
|
|
|
cr_assert_not_null(p2);
|
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(p1);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2017-03-31 18:28:51 +02:00
|
|
|
p1 = memory_alloc_aligned(m, 128, 128);
|
|
|
|
cr_assert_not_null(p1);
|
|
|
|
cr_assert(IS_ALIGNED(p1, 128));
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2017-03-31 18:28:51 +02:00
|
|
|
p3 = memory_alloc_aligned(m, 128, 256);
|
|
|
|
cr_assert(p3);
|
|
|
|
cr_assert(IS_ALIGNED(p3, 256));
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(p2);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(p1);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(p3);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2017-03-31 18:28:51 +02:00
|
|
|
p1 = memory_alloc(m, max_block);
|
|
|
|
cr_assert_not_null(p1);
|
2017-03-28 12:06:10 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(p1);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 13:10:33 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(p);
|
2017-03-31 18:28:51 +02:00
|
|
|
cr_assert(ret == 0);
|
2017-03-28 12:06:10 +02:00
|
|
|
}
|