From cc802859bd9e8dc8158c20562208487d105b3d5e Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Mon, 19 Sep 2016 20:58:41 -0400 Subject: [PATCH] refactored memory allocators --- include/villas/memory.h | 18 ++++++++++++++---- lib/memory.c | 25 ++++++++++++++++++------- 2 files changed, 32 insertions(+), 11 deletions(-) diff --git a/include/villas/memory.h b/include/villas/memory.h index 19711f7a0..753c189c9 100644 --- a/include/villas/memory.h +++ b/include/villas/memory.h @@ -1,12 +1,19 @@ -/** +/** Memory allocators. * + * @file + * @author Steffen Vogel + * @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. */ +#include + #ifndef _MEMORY_H_ #define _MEMORY_H_ -typedef void *(*memzone_allocator_t)(size_t); -typedef int (*memzone_deallocator_t)(void *, size_t); +typedef void *(*memzone_allocator_t)(size_t len); +typedef int (*memzone_deallocator_t)(void *ptr, size_t len); enum memtype_flags { MEMORY_MMAP = (1 << 0), @@ -22,7 +29,7 @@ struct memtype { size_t alignment; memzone_allocator_t alloc; - memzone_deallocator_t dealloc; + memzone_deallocator_t free; }; /** @todo Unused for now */ @@ -35,6 +42,9 @@ struct memzone { }; void * memory_alloc(const struct memtype *m, size_t len); + +void * memory_aligned_alloc(const struct memtype *m, size_t len, size_t alignment); + int memory_free(const struct memtype *m, void *ptr, size_t len); extern const struct memtype memtype_heap; diff --git a/lib/memory.c b/lib/memory.c index e7c01e38f..c91c86d1d 100644 --- a/lib/memory.c +++ b/lib/memory.c @@ -1,5 +1,9 @@ -/** +/** Memory allocators. * + * @author Steffen Vogel + * @copyright 2014-2016, Institute for Automation of Complex Power Systems, EONERC + * This file is part of VILLASnode. All Rights Reserved. Proprietary and confidential. + * Unauthorized copying of this file, via any medium is strictly prohibited. */ #include @@ -10,6 +14,7 @@ #include #endif +#include "log.h" #include "memory.h" void * memory_alloc(const struct memtype *m, size_t len) @@ -17,9 +22,15 @@ void * memory_alloc(const struct memtype *m, size_t len) return m->alloc(len); } +void * memory_aligned_alloc(const struct memtype *m, size_t len, size_t align) +{ + warn("memory_aligned_alloc: not implemented yet. Falling back to unaligned version."); + return memory_alloc(m, len); +} + int memory_free(const struct memtype *m, void *ptr, size_t len) { - return m->dealloc(ptr, len); + return m->free(ptr, len); } static void * memory_heap_alloc(size_t len) @@ -27,7 +38,7 @@ static void * memory_heap_alloc(size_t len) return malloc(len); } -int memory_heap_dealloc(void *ptr, size_t len) +int memory_heap_free(void *ptr, size_t len) { free(ptr); @@ -49,7 +60,7 @@ static void * memory_hugepage_alloc(size_t len) return mmap(NULL, len, prot, flags, -1, 0); } -static int memory_hugepage_dealloc(void *ptr, size_t len) +static int memory_hugepage_free(void *ptr, size_t len) { return munmap(ptr, len); } @@ -59,7 +70,7 @@ const struct memtype memtype_heap = { .name = "heap", .flags = MEMORY_HEAP, .alloc = memory_heap_alloc, - .dealloc = memory_heap_dealloc, + .free = memory_heap_free, .alignment = 1 }; @@ -67,7 +78,7 @@ const struct memtype memtype_hugepage = { .name = "mmap_hugepages", .flags = MEMORY_MMAP | MEMORY_HUGEPAGE, .alloc = memory_hugepage_alloc, - .dealloc = memory_hugepage_dealloc, + .free = memory_hugepage_free, .alignment = 1 << 21 /* 2 MiB hugepage */ }; @@ -75,6 +86,6 @@ const struct memtype memtype_hugepage = { const struct memtype memtype_dma = { .name = "dma", .flags = MEMORY_DMA | MEMORY_MMAP, - .alloc = NULL, .dealloc = NULL, + .alloc = NULL, .free = NULL, .alignment = 1 << 12 };