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
|
|
|
|
2017-03-27 13:22:54 +02:00
|
|
|
int pool_init(struct pool *p, size_t cnt, size_t blocksz, struct memtype *m)
|
2016-01-14 22:59:57 +01:00
|
|
|
{
|
2016-10-19 01:35:41 -04:00
|
|
|
int ret;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2017-03-12 17:04:43 -03:00
|
|
|
assert(p->state == STATE_DESTROYED);
|
2016-10-19 01:35:41 -04: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();
|
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
|
|
|
|
2017-03-31 10:51:13 +02:00
|
|
|
void *buffer = memory_alloc_aligned(m, p->len, p->alignment);
|
|
|
|
if (!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);
|
2017-03-31 10:51:13 +02:00
|
|
|
p->buffer_off = (char*) buffer - (char*) p;
|
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++)
|
2017-03-31 10:51:13 +02:00
|
|
|
queue_push(&p->queue, (char *) buffer + i * p->blocksz);
|
2017-03-06 13:26:23 -04:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
p->state = 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)
|
|
|
|
{
|
2017-03-11 23:50:30 -03:00
|
|
|
int ret;
|
|
|
|
|
2017-04-02 04:56:08 +02:00
|
|
|
if (p->state == STATE_DESTROYED)
|
|
|
|
return 0;
|
2017-03-11 23:50:30 -03:00
|
|
|
|
2016-10-16 02:33:36 -04:00
|
|
|
queue_destroy(&p->queue);
|
2016-09-22 21:20:21 -04:00
|
|
|
|
2017-03-31 10:51:13 +02:00
|
|
|
void *buffer = (char*) p + p->buffer_off;
|
2017-04-06 12:12:56 +02:00
|
|
|
ret = memory_free(p->mem, buffer, p->len);
|
2017-03-11 23:50:30 -03:00
|
|
|
if (ret == 0)
|
|
|
|
p->state = STATE_DESTROYED;
|
2017-03-06 13:26:23 -04:00
|
|
|
|
2017-03-11 23:50:30 -03:00
|
|
|
return ret;
|
2017-03-27 13:22:54 +02:00
|
|
|
}
|