2016-06-08 22:25:48 +02:00
|
|
|
/* Memory pool for fixed size objects.
|
2016-01-14 22:59:57 +01:00
|
|
|
*
|
2022-03-15 09:18:01 -04:00
|
|
|
* Author: Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
|
2022-07-04 18:20:03 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2017-03-03 20:20:13 -04:00
|
|
|
*/
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2020-07-04 22:39:15 +02:00
|
|
|
#include <villas/exceptions.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>
|
2023-09-07 11:46:39 +02:00
|
|
|
#include <villas/node/memory.hpp>
|
|
|
|
#include <villas/pool.hpp>
|
|
|
|
#include <villas/utils.hpp>
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2020-03-04 13:38:40 +01:00
|
|
|
using namespace villas;
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::pool_init(struct Pool *p, size_t cnt, size_t blocksz,
|
|
|
|
struct memory::Type *m) {
|
|
|
|
int ret;
|
2024-07-29 12:35:42 +02:00
|
|
|
auto logger = Log::get("pool");
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
// Make sure that we use a block size that is aligned to the size of a cache line
|
|
|
|
p->alignment = kernel::getCachelineSize();
|
|
|
|
p->blocksz = p->alignment * CEIL(blocksz, p->alignment);
|
|
|
|
p->len = cnt * p->blocksz;
|
2016-06-08 22:25:48 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->debug("New memory pool: alignment={}, blocksz={}, len={}, memory={}",
|
|
|
|
p->alignment, p->blocksz, p->len, m->name);
|
2023-06-23 11:25:20 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
void *buffer = memory::alloc_aligned(p->len, p->alignment, m);
|
|
|
|
if (!buffer)
|
|
|
|
throw MemoryAllocationError();
|
2020-07-04 22:39:15 +02:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
logger->debug("Allocated {:#x} bytes for memory pool", p->len);
|
2018-10-21 12:57:08 +01:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
p->buffer_off = (char *)buffer - (char *)p;
|
2016-10-19 01:35:41 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = queue_init(&p->queue, LOG2_CEIL(cnt), m);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
for (unsigned i = 0; i < cnt; i++)
|
|
|
|
queue_push(&p->queue, (char *)buffer + i * p->blocksz);
|
2017-05-05 19:24:16 +00:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
p->state = State::INITIALIZED;
|
2016-10-19 01:35:41 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return 0;
|
2016-08-28 23:55:51 -04:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::pool_destroy(struct Pool *p) {
|
|
|
|
int ret;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
if (p->state == State::DESTROYED)
|
|
|
|
return 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ret = queue_destroy(&p->queue);
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-09-22 21:20:21 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
void *buffer = (char *)p + p->buffer_off;
|
|
|
|
ret = memory::free(buffer);
|
|
|
|
if (ret == 0)
|
|
|
|
p->state = State::DESTROYED;
|
2017-03-06 13:26:23 -04:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
return ret;
|
2017-03-27 13:22:54 +02:00
|
|
|
}
|
2022-11-11 03:16:53 -05:00
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ssize_t villas::node::pool_get_many(struct Pool *p, void *blocks[],
|
|
|
|
size_t cnt) {
|
|
|
|
return queue_pull_many(&p->queue, blocks, cnt);
|
2022-11-11 03:16:53 -05:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
ssize_t villas::node::pool_put_many(struct Pool *p, void *blocks[],
|
|
|
|
size_t cnt) {
|
|
|
|
return queue_push_many(&p->queue, blocks, cnt);
|
2022-11-11 03:16:53 -05:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
void *villas::node::pool_get(struct Pool *p) {
|
|
|
|
void *ptr;
|
|
|
|
return queue_pull(&p->queue, &ptr) == 1 ? ptr : nullptr;
|
2022-11-11 03:16:53 -05:00
|
|
|
}
|
|
|
|
|
2023-09-07 11:46:39 +02:00
|
|
|
int villas::node::pool_put(struct Pool *p, void *buf) {
|
|
|
|
return queue_push(&p->queue, buf);
|
2022-11-11 03:16:53 -05:00
|
|
|
}
|