2016-06-08 22:25:48 +02:00
|
|
|
/** Memory pool for fixed size objects.
|
2016-01-14 22:59:57 +01:00
|
|
|
*
|
|
|
|
* @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/>.
|
2017-03-03 20:20:13 -04:00
|
|
|
*********************************************************************************/
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2020-07-04 22:39:15 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2017-12-09 02:19:28 +08:00
|
|
|
#include <villas/pool.h>
|
|
|
|
#include <villas/memory.h>
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
using namespace villas;
|
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
int pool_init(struct pool *p, size_t cnt, size_t blocksz, struct memory_type *m)
|
2016-01-14 22:59:57 +01:00
|
|
|
{
|
2016-10-19 01:35:41 -04:00
|
|
|
int ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2016-08-28 23:55:51 -04:00
|
|
|
/* Make sure that we use a block size that is aligned to the size of a cache line */
|
2020-03-04 13:38:40 +01:00
|
|
|
p->alignment = kernel::get_cacheline_size();
|
2016-10-30 23:00:17 -04:00
|
|
|
p->blocksz = p->alignment * CEIL(blocksz, p->alignment);
|
2016-09-22 21:20:21 -04:00
|
|
|
p->len = cnt * p->blocksz;
|
2016-06-08 22:25:48 +02:00
|
|
|
|
2019-10-26 13:07:02 +02:00
|
|
|
void *buffer = memory_alloc_aligned(p->len, p->alignment, m);
|
2017-03-31 10:51:13 +02:00
|
|
|
if (!buffer)
|
2020-07-04 22:39:15 +02:00
|
|
|
throw MemoryAllocationError();
|
|
|
|
|
|
|
|
debug(LOG_POOL | 4, "Allocated %#zx bytes for memory pool", p->len);
|
2018-10-21 12:57:08 +01:00
|
|
|
|
2017-03-31 10:51:13 +02:00
|
|
|
p->buffer_off = (char*) buffer - (char*) p;
|
2016-10-19 01:35:41 -04:00
|
|
|
|
2016-10-30 23:01:14 -04:00
|
|
|
ret = queue_init(&p->queue, LOG2_CEIL(cnt), m);
|
2016-10-19 01:35:41 -04:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
for (unsigned i = 0; i < cnt; i++)
|
2017-03-31 10:51:13 +02:00
|
|
|
queue_push(&p->queue, (char *) buffer + i * p->blocksz);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
p->state = State::INITIALIZED;
|
2016-10-19 01:35:41 -04:00
|
|
|
|
2016-06-08 22:25:48 +02:00
|
|
|
return 0;
|
2016-08-28 23:55:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int pool_destroy(struct pool *p)
|
|
|
|
{
|
2017-03-11 23:50:30 -03:00
|
|
|
int ret;
|
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
if (p->state == State::DESTROYED)
|
2017-04-02 04:56:08 +02:00
|
|
|
return 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2020-09-10 11:11:42 +02:00
|
|
|
ret = queue_destroy(&p->queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-09-22 21:20:21 -04:00
|
|
|
|
2018-07-16 21:16:00 +02:00
|
|
|
void *buffer = (char *) p + p->buffer_off;
|
2018-07-02 19:00:55 +02:00
|
|
|
ret = memory_free(buffer);
|
2017-03-11 23:50:30 -03:00
|
|
|
if (ret == 0)
|
2019-06-23 16:13:23 +02:00
|
|
|
p->state = State::DESTROYED;
|
2017-03-06 13:26:23 -04:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return ret;
|
2017-03-27 13:22:54 +02:00
|
|
|
}
|