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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

55 lines
1.4 KiB
C++
Raw Permalink Normal View History

/* Heap memory allocator.
2018-07-02 14:17:50 +02:00
*
* Author: Steffen Vogel <post@steffenvogel.de>
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
* SPDX-License-Identifier: Apache-2.0
*/
2018-07-02 14:17:50 +02:00
#include <cstdlib>
2018-07-02 14:17:50 +02:00
#include <villas/exceptions.hpp>
#include <villas/node/memory.hpp>
#include <villas/utils.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) {
int ret;
2018-07-02 14:17:50 +02:00
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;
return nullptr;
}
return ma;
2018-07-02 14:17:50 +02:00
}
static int heap_free(struct Allocation *ma, struct Type *m) {
::free(ma->address);
2018-07-02 14:17:50 +02:00
return 0;
2018-07-02 14:17:50 +02:00
}
// List of available memory types
struct Type villas::node::memory::heap = {.name = "heap",
.flags = (int)Flags::HEAP,
.alignment = 1,
.alloc = heap_alloc,
.free = heap_free};