2016-06-08 22:25:48 +02:00
|
|
|
/** Memory pool for fixed size objects.
|
2016-01-14 22:59:57 +01:00
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
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>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/pool.hpp>
|
|
|
|
#include <villas/node/memory.hpp>
|
2020-03-04 13:07:20 +01:00
|
|
|
#include <villas/kernel/kernel.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/log.hpp>
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
using namespace villas;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::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;
|
2023-06-23 11:25:20 +02:00
|
|
|
auto logger = logging.get("pool");
|
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 */
|
2021-05-20 06:21:33 -04:00
|
|
|
p->alignment = kernel::getCachelineSize();
|
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
|
|
|
|
2023-06-23 11:25:20 +02:00
|
|
|
logger->debug("New memory pool: alignment={}, blocksz={}, len={}, memory={}", p->alignment, p->blocksz, p->len, m->name);
|
|
|
|
|
2021-08-10 10:12:48 -04: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();
|
|
|
|
|
2021-05-20 05:58:01 -04:00
|
|
|
logger->debug("Allocated {:#x} 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
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
int villas::node::pool_destroy(struct Pool *p)
|
2016-08-28 23:55:51 -04:00
|
|
|
{
|
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;
|
2021-08-10 10:12:48 -04: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
|
|
|
}
|
2022-11-11 03:16:53 -05:00
|
|
|
|
|
|
|
ssize_t villas::node::pool_get_many(struct Pool *p, void *blocks[], size_t cnt)
|
|
|
|
{
|
|
|
|
return queue_pull_many(&p->queue, blocks, cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t villas::node::pool_put_many(struct Pool *p, void *blocks[], size_t cnt)
|
|
|
|
{
|
|
|
|
return queue_push_many(&p->queue, blocks, cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
void * villas::node::pool_get(struct Pool *p)
|
|
|
|
{
|
|
|
|
void *ptr;
|
|
|
|
return queue_pull(&p->queue, &ptr) == 1 ? ptr : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
int villas::node::pool_put(struct Pool *p, void *buf)
|
|
|
|
{
|
|
|
|
return queue_push(&p->queue, buf);
|
|
|
|
}
|