1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/memory/heap.cpp

60 lines
1.2 KiB
C++
Raw Permalink Normal View History

2018-07-04 17:27:26 +02:00
/** Heap memory allocator.
2018-07-02 14:17:50 +02:00
*
* @author Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
2022-07-04 18:20:03 +02:00
* @license Apache 2.0
2018-07-02 14:17:50 +02:00
*********************************************************************************/
#include <cstdlib>
2018-07-02 14:17:50 +02:00
#include <villas/utils.hpp>
#include <villas/node/memory.hpp>
#include <villas/exceptions.hpp>
2018-07-02 14:17:50 +02:00
using namespace villas;
using namespace villas::node;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
using namespace villas::node::memory;
2019-06-04 16:55:38 +02:00
static
struct Allocation * heap_alloc(size_t len, size_t alignment, struct Type *m)
2018-07-02 14:17:50 +02:00
{
int ret;
auto *ma = new struct Allocation;
if (!ma)
throw MemoryAllocationError();
2018-07-02 14:17:50 +02:00
ma->alignment = alignment;
ma->type = m;
ma->length = len;
2018-07-02 14:17:50 +02:00
if (ma->alignment < sizeof(void *))
ma->alignment = sizeof(void *);
ret = posix_memalign(&ma->address, ma->alignment, ma->length);
if (ret) {
delete ma;
2019-04-07 15:13:40 +02:00
return nullptr;
}
return ma;
2018-07-02 14:17:50 +02:00
}
static
int heap_free(struct Allocation *ma, struct Type *m)
2018-07-02 14:17:50 +02:00
{
::free(ma->address);
2018-07-02 14:17:50 +02:00
return 0;
}
/* List of available memory types */
struct Type villas::node::memory::heap = {
2018-07-02 14:17:50 +02:00
.name = "heap",
.flags = (int) Flags::HEAP,
2019-04-07 15:13:40 +02:00
.alignment = 1,
.alloc = heap_alloc,
.free = heap_free
2018-07-02 14:17:50 +02:00
};