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

2018-07-04 17:27:26 +02:00
/* Heap memory allocator.
2018-07-02 14:17:50 +02:00
*
2022-03-15 09:18:01 -04:00
* Author: Steffen Vogel <post@steffenvogel.de>
2022-03-15 09:28:57 -04:00
* SPDX-FileCopyrightText: 2014-2023 Institute for Automation of Complex Power Systems, RWTH Aachen University
2022-07-04 18:20:03 +02:00
* 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) {
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) {
::free(ma->address);
2018-07-02 14:17:50 +02:00
return 0;
}
// List of available memory types
struct Type villas::node::memory::heap = {.name = "heap",
.flags = (int)Flags::HEAP,
2019-04-07 15:13:40 +02:00
.alignment = 1,
.alloc = heap_alloc,
.free = heap_free};