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

42 lines
1.2 KiB
C
Raw Normal View History

/** Memory pool for fixed size objects.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2016-02-09 05:33:19 +01:00
* @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC
2016-06-08 23:21:42 +02:00
* This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
#include "utils.h"
#include "pool.h"
#include "memory.h"
#include "kernel/kernel.h"
int pool_init(struct pool *p, size_t blocksz, size_t cnt, const struct memtype *m)
{
/* Make sure that we use a block size that is aligned to the size of a cache line */
p->alignment = kernel_get_cacheline_size();
p->blocksz = blocksz * CEIL(blocksz, p->alignment);
p->len = cnt * p->blocksz;
p->mem = m;
p->buffer = memory_alloc_aligned(m, p->len, p->alignment);
if (!p->buffer)
serror("Failed to allocate memory for memory pool");
else
debug(DBG_POOL | 4, "Allocated %#zx bytes for memory pool", p->len);
mpmc_queue_init(&p->queue, cnt, m);
for (int i = 0; i < cnt; i++)
mpmc_queue_push(&p->queue, (char *) p->buffer + i * p->blocksz);
return 0;
}
int pool_destroy(struct pool *p)
{
mpmc_queue_destroy(&p->queue);
return memory_free(p->mem, p->buffer, p->len);
}