1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/pool.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.3 KiB
C++
Raw Permalink Normal View History

/* Memory pool for fixed size objects.
*
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
*/
#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>
#include <villas/node/memory.hpp>
#include <villas/pool.hpp>
#include <villas/utils.hpp>
2020-03-04 13:38:40 +01:00
using namespace villas;
int villas::node::pool_init(struct Pool *p, size_t cnt, size_t blocksz,
struct memory::Type *m) {
2016-10-19 01:35:41 -04:00
int ret;
auto logger = logging.get("pool");
// 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();
p->blocksz = p->alignment * CEIL(blocksz, p->alignment);
p->len = cnt * p->blocksz;
logger->debug("New memory pool: alignment={}, blocksz={}, len={}, memory={}",
p->alignment, p->blocksz, p->len, m->name);
void *buffer = memory::alloc_aligned(p->len, p->alignment, m);
if (!buffer)
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
p->buffer_off = (char *)buffer - (char *)p;
2016-10-19 01:35:41 -04:00
ret = queue_init(&p->queue, LOG2_CEIL(cnt), m);
2016-10-19 01:35:41 -04:00
if (ret)
return ret;
2019-04-07 15:13:40 +02:00
for (unsigned i = 0; i < cnt; i++)
queue_push(&p->queue, (char *)buffer + i * p->blocksz);
2019-06-23 16:13:23 +02:00
p->state = State::INITIALIZED;
2016-10-19 01:35:41 -04:00
return 0;
}
int villas::node::pool_destroy(struct Pool *p) {
int ret;
2019-06-23 16:13:23 +02:00
if (p->state == State::DESTROYED)
return 0;
ret = queue_destroy(&p->queue);
if (ret)
return ret;
2018-07-16 21:16:00 +02:00
void *buffer = (char *)p + p->buffer_off;
ret = memory::free(buffer);
if (ret == 0)
2019-06-23 16:13:23 +02:00
p->state = State::DESTROYED;
return ret;
2017-03-27 13:22:54 +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);
}
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);
}