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

80 lines
2.2 KiB
C++
Raw Permalink Normal View History

/** Memory pool for fixed size objects.
*
* @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-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-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/>.
*********************************************************************************/
#include <villas/utils.hpp>
#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>
2021-02-16 14:15:14 +01:00
#include <villas/log.hpp>
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-10-19 01:35:41 -04:00
int ret;
/* 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;
2019-10-26 13:07:02 +02:00
void *buffer = memory_alloc_aligned(p->len, p->alignment, m);
if (!buffer)
throw MemoryAllocationError();
2021-02-16 14:15:14 +01:00
auto logger = logging.get("pool");
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 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
}