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/memory.c

133 lines
3 KiB
C
Raw Normal View History

2016-09-19 20:58:41 -04:00
/** Memory allocators.
2016-09-15 21:25:32 -04:00
*
2016-09-19 20:58:41 -04:00
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2017, Institute for Automation of Complex Power Systems, EONERC
*********************************************************************************/
2016-09-15 21:25:32 -04:00
#include <stdlib.h>
#include <sys/mman.h>
/* Required to allocate hugepages on Apple OS X */
#ifdef __MACH__
#include <mach/vm_statistics.h>
#elif defined(__linux__)
#include "kernel/kernel.h"
2016-09-15 21:25:32 -04:00
#endif
2016-09-19 20:58:41 -04:00
#include "log.h"
2016-09-15 21:25:32 -04:00
#include "memory.h"
2017-03-11 23:39:00 -03:00
#include "utils.h"
2016-09-15 21:25:32 -04:00
2017-03-11 23:39:00 -03:00
int memory_init(int hugepages)
{
#ifdef __linux__
info("Initialize memory sub-system");
int nr = kernel_get_nr_hugepages();
2017-03-11 23:39:00 -03:00
if (nr < hugepages) { INDENT
kernel_set_nr_hugepages(hugepages);
debug(LOG_MEM | 2, "Reserved %d hugepages (was %d)", hugepages, nr);
}
#endif
return 0;
}
2016-09-15 21:25:32 -04:00
void * memory_alloc(const struct memtype *m, size_t len)
{
void *ptr = m->alloc(len, sizeof(void *));
debug(LOG_MEM | 2, "Allocated %#zx bytes of %s memory: %p", len, m->name, ptr);
return ptr;
2016-09-15 21:25:32 -04:00
}
void * memory_alloc_aligned(const struct memtype *m, size_t len, size_t alignment)
{
void *ptr = m->alloc(len, alignment);
debug(LOG_MEM | 2, "Allocated %#zx bytes of %#zx-byte-aligned %s memory: %p", len, alignment, m->name, ptr);
return ptr;
2016-09-19 20:58:41 -04:00
}
2016-09-15 21:25:32 -04:00
int memory_free(const struct memtype *m, void *ptr, size_t len)
{
2017-02-12 14:12:35 -03:00
debug(LOG_MEM | 2, "Releasing %#zx bytes of %s memory", len, m->name);
2016-09-19 20:58:41 -04:00
return m->free(ptr, len);
2016-09-15 21:25:32 -04:00
}
static void * memory_heap_alloc(size_t len, size_t alignment)
2016-09-15 21:25:32 -04:00
{
void *ptr;
int ret;
if (alignment < sizeof(void *))
alignment = sizeof(void *);
ret = posix_memalign(&ptr, alignment, len);
return ret ? NULL : ptr;
2016-09-15 21:25:32 -04:00
}
2016-09-19 20:58:41 -04:00
int memory_heap_free(void *ptr, size_t len)
2016-09-15 21:25:32 -04:00
{
free(ptr);
return 0;
}
/** Allocate memory backed by hugepages with malloc() like interface */
static void * memory_hugepage_alloc(size_t len, size_t alignment)
2016-09-15 21:25:32 -04:00
{
int prot = PROT_READ | PROT_WRITE;
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
#ifdef __MACH__
flags |= VM_FLAGS_SUPERPAGE_SIZE_2MB;
#elif defined(__linux__)
flags |= MAP_HUGETLB | MAP_LOCKED;
#endif
void *ret = mmap(NULL, len, prot, flags, -1, 0);
if (ret == MAP_FAILED) {
info("Failed to allocate huge pages: Check https://www.kernel.org/doc/Documentation/vm/hugetlbpage.txt");
return NULL;
}
return ret;
2016-09-15 21:25:32 -04:00
}
2016-09-19 20:58:41 -04:00
static int memory_hugepage_free(void *ptr, size_t len)
2016-09-15 21:25:32 -04:00
{
len = ALIGN(len, HUGEPAGESIZE); /* ugly see: https://lkml.org/lkml/2015/3/27/171 */
2016-09-15 21:25:32 -04:00
return munmap(ptr, len);
}
/* List of available memory types */
const struct memtype memtype_heap = {
.name = "heap",
.flags = MEMORY_HEAP,
.alloc = memory_heap_alloc,
2016-09-19 20:58:41 -04:00
.free = memory_heap_free,
2016-09-15 21:25:32 -04:00
.alignment = 1
};
const struct memtype memtype_hugepage = {
.name = "mmap_hugepages",
.flags = MEMORY_MMAP | MEMORY_HUGEPAGE,
.alloc = memory_hugepage_alloc,
2016-09-19 20:58:41 -04:00
.free = memory_hugepage_free,
.alignment = 21 /* 2 MiB hugepage */
2016-09-15 21:25:32 -04:00
};
/** @todo */
const struct memtype memtype_dma = {
.name = "dma",
.flags = MEMORY_DMA | MEMORY_MMAP,
2016-09-19 20:58:41 -04:00
.alloc = NULL, .free = NULL,
.alignment = 12
2016-09-15 21:25:32 -04:00
};