mirror of
https://git.rwth-aachen.de/acs/public/villas/node/
synced 2025-03-09 00:00:00 +01:00
refactored memory allocators
This commit is contained in:
parent
4bd461d9e2
commit
cc802859bd
2 changed files with 32 additions and 11 deletions
|
@ -1,12 +1,19 @@
|
|||
/**
|
||||
/** Memory allocators.
|
||||
*
|
||||
* @file
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @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 <stddef.h>
|
||||
|
||||
#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;
|
||||
|
|
25
lib/memory.c
25
lib/memory.c
|
@ -1,5 +1,9 @@
|
|||
/**
|
||||
/** Memory allocators.
|
||||
*
|
||||
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
|
||||
* @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 <stdlib.h>
|
||||
|
@ -10,6 +14,7 @@
|
|||
#include <mach/vm_statistics.h>
|
||||
#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
|
||||
};
|
||||
|
|
Loading…
Add table
Reference in a new issue