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/managed.cpp

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

177 lines
5.1 KiB
C++
Raw Permalink Normal View History

2018-07-04 17:27:26 +02:00
/* Managed 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 <cerrno>
#include <cstdlib>
2018-07-02 14:17:50 +02:00
#include <strings.h>
#include <unistd.h>
2018-07-02 14:17:50 +02:00
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/time.h>
2018-07-02 14:17:50 +02:00
#include <sys/types.h>
#include <villas/exceptions.hpp>
2021-02-16 14:15:14 +01:00
#include <villas/log.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 *managed_alloc(size_t len, size_t alignment,
struct Type *m) {
// Simple first-fit allocation
struct Block *first = (struct Block *)m->_vd;
struct Block *block;
2018-07-02 14:17:50 +02:00
for (block = first; block != nullptr; block = block->next) {
if (block->used)
continue;
2018-07-02 14:17:50 +02:00
char *cptr = (char *)block + sizeof(struct Block);
size_t avail = block->length;
uintptr_t uptr = (uintptr_t)cptr;
2018-07-02 14:17:50 +02:00
/* Check alignment first; leave a gap at start of block to assure
2018-07-02 14:17:50 +02:00
* alignment if necessary */
uintptr_t rem = uptr % alignment;
uintptr_t gap = 0;
if (rem != 0) {
gap = alignment - rem;
if (gap > avail)
continue; // Next aligned address isn't in this block anymore
cptr += gap;
avail -= gap;
}
if (avail >= len) {
if (gap > sizeof(struct Block)) {
/* The alignment gap is big enough to fit another block.
2018-07-02 14:17:50 +02:00
* The original block descriptor is already at the correct
* position, so we just change its len and create a new block
* descriptor for the actual block we're handling. */
block->length = gap - sizeof(struct Block);
struct Block *newblock = (struct Block *)(cptr - sizeof(struct Block));
newblock->prev = block;
newblock->next = block->next;
block->next = newblock;
newblock->used = false;
newblock->length = len;
block = newblock;
} else {
/* The gap is too small to fit another block descriptor, so we
2018-07-02 14:17:50 +02:00
* must account for the gap length in the block length. */
block->length = len + gap;
}
if (avail > len + sizeof(struct Block)) {
// Imperfect fit, so create another block for the remaining part
struct Block *newblock = (struct Block *)(cptr + len);
newblock->prev = block;
newblock->next = block->next;
block->next = newblock;
if (newblock->next)
newblock->next->prev = newblock;
newblock->used = false;
newblock->length = avail - len - sizeof(struct Block);
} else {
/* If this block was larger than the requested length, but only
* by less than sizeof(struct Block), we may have wasted
* memory by previous assignments to block->length. */
block->length = avail;
}
2018-07-02 14:17:50 +02:00
block->used = true;
auto *ma = new struct Allocation;
if (!ma)
throw MemoryAllocationError();
2018-07-02 14:17:50 +02:00
ma->address = cptr;
ma->type = m;
ma->alignment = alignment;
ma->length = len;
ma->managed.block = block;
return ma;
}
}
2018-07-02 14:17:50 +02:00
// No suitable block found
return nullptr;
2018-07-02 14:17:50 +02:00
}
static int managed_free(struct Allocation *ma, struct Type *m) {
struct Block *block = ma->managed.block;
// Try to merge it with neighbouring free blocks
if (block->prev && !block->prev->used && block->next && !block->next->used) {
// Special case first: both previous and next block are unused
block->prev->length +=
block->length + block->next->length + 2 * sizeof(struct Block);
block->prev->next = block->next->next;
if (block->next->next)
block->next->next->prev = block->prev;
} else if (block->prev && !block->prev->used) {
block->prev->length += block->length + sizeof(struct Block);
block->prev->next = block->next;
if (block->next)
block->next->prev = block->prev;
} else if (block->next && !block->next->used) {
block->length += block->next->length + sizeof(struct Block);
block->next = block->next->next;
if (block->next)
block->next->prev = block;
} else {
// no neighbouring free block, so just mark it as free
block->used = false;
}
return 0;
2018-07-02 14:17:50 +02:00
}
struct Type *villas::node::memory::managed(void *ptr, size_t len) {
struct Type *mt = (struct Type *)ptr;
struct Block *mb;
char *cptr = (char *)ptr;
2018-07-02 14:17:50 +02:00
if (len < sizeof(struct Type) + sizeof(struct Block)) {
auto logger = logging.get("memory:managed");
logger->info("Passed region is too small");
return nullptr;
}
2018-07-02 14:17:50 +02:00
// Initialize type
mt->name = "managed";
mt->flags = 0;
mt->alloc = managed_alloc;
mt->free = managed_free;
mt->alignment = 1;
2018-07-02 14:17:50 +02:00
cptr += ALIGN(sizeof(struct Type), sizeof(void *));
2018-07-02 14:17:50 +02:00
// Initialize first free memory block
mb = (struct Block *)cptr;
mb->prev = nullptr;
mb->next = nullptr;
mb->used = false;
2018-07-02 14:17:50 +02:00
cptr += ALIGN(sizeof(struct Block), sizeof(void *));
2018-07-02 14:17:50 +02:00
mb->length = len - (cptr - (char *)ptr);
2018-07-02 14:17:50 +02:00
mt->_vd = (void *)mb;
2018-07-02 14:17:50 +02:00
return mt;
2018-07-02 14:17:50 +02:00
}