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
|
|
|
*/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstdlib>
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node/memory.hpp>
|
2019-04-23 13:09:50 +02:00
|
|
|
#include <villas/utils.hpp>
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2020-07-04 16:22:10 +02:00
|
|
|
using namespace villas;
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node;
|
2019-06-04 16:55:38 +02:00
|
|
|
using namespace villas::utils;
|
2021-08-10 10:12:48 -04:00
|
|
|
using namespace villas::node::memory;
|
2019-06-04 16:55:38 +02:00
|
|
|
|
2021-08-10 10:12:48 -04: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;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *ma = new struct Allocation;
|
2018-07-02 19:00:55 +02:00
|
|
|
if (!ma)
|
2020-07-04 16:22:10 +02:00
|
|
|
throw MemoryAllocationError();
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
ma->alignment = alignment;
|
|
|
|
ma->type = m;
|
|
|
|
ma->length = len;
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
if (ma->alignment < sizeof(void *))
|
|
|
|
ma->alignment = sizeof(void *);
|
|
|
|
|
|
|
|
ret = posix_memalign(&ma->address, ma->alignment, ma->length);
|
|
|
|
if (ret) {
|
2020-01-21 16:26:51 +01:00
|
|
|
delete ma;
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2018-07-02 19:00:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ma;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04: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
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Type villas::node::memory::heap = {.name = "heap",
|
|
|
|
.flags = (int)Flags::HEAP,
|
2019-04-07 15:13:40 +02:00
|
|
|
.alignment = 1,
|
2021-08-10 10:12:48 -04:00
|
|
|
.alloc = heap_alloc,
|
|
|
|
.free = heap_free};
|