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>
|
2017-03-03 20:20:13 -04:00
|
|
|
* @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>
|
|
|
|
#endif
|
|
|
|
|
2016-09-19 20:58:41 -04:00
|
|
|
#include "log.h"
|
2016-09-15 21:25:32 -04:00
|
|
|
#include "memory.h"
|
|
|
|
|
|
|
|
void * memory_alloc(const struct memtype *m, size_t len)
|
|
|
|
{
|
2017-02-12 14:12:35 -03:00
|
|
|
debug(LOG_MEM | 2, "Allocating %#zx bytes of %s memory", len, m->name);
|
2016-09-15 21:25:32 -04:00
|
|
|
return m->alloc(len);
|
|
|
|
}
|
|
|
|
|
2016-09-22 21:20:21 -04:00
|
|
|
void * memory_alloc_aligned(const struct memtype *m, size_t len, size_t alignment)
|
|
|
|
{
|
2017-02-12 14:12:35 -03:00
|
|
|
debug(LOG_MEM | 2, "Allocating %#zx bytes of %#zx-byte-aligned %s memory", len, alignment, m->name);
|
2016-10-19 01:34:27 -04:00
|
|
|
warn("%s: not implemented yet!", __FUNCTION__);
|
2016-09-19 20:58:41 -04:00
|
|
|
return memory_alloc(m, len);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return malloc(len);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
|
2016-10-30 16:54:39 -04:00
|
|
|
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
|
|
|
{
|
2016-10-30 19:40:31 -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,
|
2016-10-19 01:34:27 -04:00
|
|
|
.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,
|
2016-10-19 01:34:27 -04:00
|
|
|
.alignment = 12
|
2016-09-15 21:25:32 -04:00
|
|
|
};
|