1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-16 00:00:02 +01:00
VILLASnode/lib/pool.c

57 lines
1.5 KiB
C
Raw Normal View History

/** Memory pool for fixed size objects.
*
* @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.
* Unauthorized copying of this file, via any medium is strictly prohibited.
*/
#include <sys/mman.h>
#include "utils.h"
#include "pool.h"
2016-06-08 22:44:44 +02:00
#include "kernel.h"
int pool_init_mmap(struct pool *p, size_t blocksz, size_t cnt)
{
void *addr;
int flags;
size_t len, alignedsz, align;
align = kernel_get_cacheline_size();
alignedsz = blocksz * CEIL(blocksz, align);
len = cnt * alignedsz;
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);
}
int pool_init(struct pool *p, size_t blocksz, size_t align, void *buf, size_t len)
{
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;
}