2016-06-08 22:25:48 +02:00
|
|
|
/** Memory pool for fixed size objects.
|
2016-01-14 22:59:57 +01:00
|
|
|
*
|
|
|
|
* @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.
|
2016-06-08 22:25:48 +02:00
|
|
|
* Unauthorized copying of this file, via any medium is strictly prohibited.
|
2016-01-14 22:59:57 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utils.h"
|
2016-06-08 22:25:48 +02:00
|
|
|
|
2016-01-14 22:59:57 +01:00
|
|
|
#include "pool.h"
|
2016-08-28 23:55:51 -04:00
|
|
|
#include "memory.h"
|
2016-06-14 01:17:58 +02:00
|
|
|
#include "kernel/kernel.h"
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-08-28 23:55:51 -04:00
|
|
|
int pool_init(struct pool *p, size_t blocksz, size_t cnt, const struct memtype *m)
|
2016-01-14 22:59:57 +01:00
|
|
|
{
|
2016-08-28 23:55:51 -04:00
|
|
|
/* Make sure that we use a block size that is aligned to the size of a cache line */
|
2016-09-22 21:20:21 -04:00
|
|
|
p->alignment = kernel_get_cacheline_size();
|
|
|
|
p->blocksz = blocksz * CEIL(blocksz, p->alignment);
|
|
|
|
p->len = cnt * p->blocksz;
|
|
|
|
p->mem = m;
|
2016-06-08 22:25:48 +02:00
|
|
|
|
2016-09-22 21:20:21 -04:00
|
|
|
p->buffer = memory_alloc_aligned(m, p->len, p->alignment);
|
|
|
|
if (!p->buffer)
|
2016-08-28 23:55:51 -04:00
|
|
|
serror("Failed to allocate memory for memory pool");
|
|
|
|
else
|
2016-09-22 21:20:21 -04:00
|
|
|
debug(DBG_POOL | 4, "Allocated %#zx bytes for memory pool", p->len);
|
2016-06-08 22:25:48 +02:00
|
|
|
|
2016-08-28 23:55:51 -04:00
|
|
|
mpmc_queue_init(&p->queue, cnt, m);
|
2016-06-08 22:25:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < cnt; i++)
|
2016-09-22 21:20:21 -04:00
|
|
|
mpmc_queue_push(&p->queue, (char *) p->buffer + i * p->blocksz);
|
2016-06-08 22:25:48 +02:00
|
|
|
|
|
|
|
return 0;
|
2016-08-28 23:55:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int pool_destroy(struct pool *p)
|
|
|
|
{
|
2016-09-22 21:20:21 -04:00
|
|
|
mpmc_queue_destroy(&p->queue);
|
|
|
|
|
|
|
|
return memory_free(p->mem, p->buffer, p->len);
|
2016-06-08 22:25:48 +02:00
|
|
|
}
|