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>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
|
|
|
|
*********************************************************************************/
|
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-10-19 01:35:41 -04:00
|
|
|
int pool_init(struct pool *p, size_t cnt, size_t blocksz, const struct memtype *m)
|
2016-01-14 22:59:57 +01:00
|
|
|
{
|
2016-10-19 01:35:41 -04:00
|
|
|
int ret;
|
|
|
|
|
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();
|
2016-10-30 23:00:17 -04:00
|
|
|
p->blocksz = p->alignment * CEIL(blocksz, p->alignment);
|
2016-09-22 21:20:21 -04:00
|
|
|
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
|
2017-02-12 14:12:35 -03:00
|
|
|
debug(LOG_POOL | 4, "Allocated %#zx bytes for memory pool", p->len);
|
2016-10-19 01:35:41 -04:00
|
|
|
|
2016-10-30 23:01:14 -04:00
|
|
|
ret = queue_init(&p->queue, LOG2_CEIL(cnt), m);
|
2016-10-19 01:35:41 -04:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2016-06-08 22:25:48 +02:00
|
|
|
|
|
|
|
for (int i = 0; i < cnt; i++)
|
2016-10-16 02:33:36 -04:00
|
|
|
queue_push(&p->queue, (char *) p->buffer + i * p->blocksz);
|
2017-03-06 13:26:23 -04:00
|
|
|
|
|
|
|
p->state = POOL_STATE_INITIALIZED;
|
2016-10-19 01:35:41 -04:00
|
|
|
|
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-10-16 02:33:36 -04:00
|
|
|
queue_destroy(&p->queue);
|
2016-09-22 21:20:21 -04:00
|
|
|
|
2017-03-06 13:26:23 -04:00
|
|
|
if (p->state == POOL_STATE_INITIALIZED)
|
|
|
|
return memory_free(p->mem, p->buffer, p->len);
|
2017-02-18 10:43:01 -05:00
|
|
|
|
2017-03-06 13:26:23 -04:00
|
|
|
p->state = POOL_STATE_DESTROYED;
|
|
|
|
|
2017-02-18 10:43:01 -05:00
|
|
|
return 0;
|
2016-06-08 22:25:48 +02:00
|
|
|
}
|