1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-23 00:00:01 +01:00
VILLASnode/lib/pool.c
Georg Reinke 08a60dcaca make pointers in pool and queue relative
This is necessary for the new shared memory node, since these structs
may be stored in the shared memory area which is in general mapped to
different virtual addresses in the different processes.
2017-03-31 10:51:13 +02:00

44 lines
1.1 KiB
C

/** Memory pool for fixed size objects.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2016, Institute for Automation of Complex Power Systems, EONERC
*/
#include "utils.h"
#include "pool.h"
#include "memory.h"
#include "kernel/kernel.h"
int pool_init(struct pool *p, size_t cnt, size_t blocksz, struct memtype *m)
{
int ret;
/* 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 = p->alignment * CEIL(blocksz, p->alignment);
p->len = cnt * p->blocksz;
p->mem = m;
void *buffer = memory_alloc_aligned(m, p->len, p->alignment);
if (!buffer)
serror("Failed to allocate memory for memory pool");
p->buffer_off = (char*) buffer - (char*) p;
ret = queue_init(&p->queue, LOG2_CEIL(cnt), m);
if (ret)
return ret;
for (int i = 0; i < cnt; i++)
queue_push(&p->queue, (char *) buffer + i * p->blocksz);
return 0;
}
int pool_destroy(struct pool *p)
{
queue_destroy(&p->queue);
void *buffer = (char*) p + p->buffer_off;
return memory_free(p->mem, buffer, p->len);
}