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
|
|
|
*/
|
|
|
|
|
2016-06-08 22:25:48 +02:00
|
|
|
#include <sys/mman.h>
|
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-06-08 22:44:44 +02:00
|
|
|
#include "kernel.h"
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:25:48 +02:00
|
|
|
int pool_init_mmap(struct pool *p, size_t blocksz, size_t cnt)
|
2016-01-14 22:59:57 +01:00
|
|
|
{
|
2016-06-08 22:25:48 +02:00
|
|
|
void *addr;
|
|
|
|
int flags;
|
|
|
|
size_t len, alignedsz, align;
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:25:48 +02:00
|
|
|
align = kernel_get_cacheline_size();
|
|
|
|
alignedsz = blocksz * CEIL(blocksz, align);
|
|
|
|
len = cnt * alignedsz;
|
2016-01-14 22:59:57 +01:00
|
|
|
|
2016-06-08 22:25:48 +02:00
|
|
|
debug(DBG_POOL | 4, "Allocating %#zx bytes for memory pool", len);
|
|
|
|
|
|
|
|
flags = MAP_LOCKED | MAP_PRIVATE | MAP_ANONYMOUS; // MAP_HUGETLB
|
|
|
|
/** @todo Use hugepages */
|
|
|
|
|
|
|
|
/* addr is allways aligned to pagesize boundary */
|
|
|
|
addr = mmap(NULL, len, PROT_READ | PROT_WRITE, flags, -1, 0);
|
|
|
|
if (addr == MAP_FAILED)
|
|
|
|
serror("Failed to allocate memory for sample pool");
|
|
|
|
|
|
|
|
return pool_init(p, blocksz, align, addr, len);
|
2016-01-14 22:59:57 +01:00
|
|
|
}
|
|
|
|
|
2016-06-08 22:25:48 +02:00
|
|
|
int pool_init(struct pool *p, size_t blocksz, size_t align, void *buf, size_t len)
|
2016-01-14 22:59:57 +01:00
|
|
|
{
|
2016-06-08 22:25:48 +02:00
|
|
|
size_t alignedsz, cnt;
|
|
|
|
|
|
|
|
assert(IS_ALIGNED(buf, align)); /* buf has to be aligned */
|
|
|
|
|
|
|
|
p->blocksz = blocksz;
|
|
|
|
p->alignment = align;
|
|
|
|
|
|
|
|
alignedsz = blocksz * CEIL(blocksz, align);
|
|
|
|
cnt = len / alignedsz;
|
|
|
|
|
|
|
|
lstack_init(&p->stack, cnt);
|
|
|
|
|
|
|
|
for (int i = 0; i < cnt; i++)
|
|
|
|
lstack_push(&p->stack, buf + i * alignedsz);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|