2018-07-04 17:27:26 +02:00
|
|
|
/** Managed memory allocator.
|
2018-07-02 14:17:50 +02:00
|
|
|
*
|
2022-12-14 17:41:58 +01: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
|
|
|
*********************************************************************************/
|
|
|
|
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstdlib>
|
2018-07-02 14:17:50 +02:00
|
|
|
#include <unistd.h>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cerrno>
|
2018-07-02 14:17:50 +02:00
|
|
|
#include <strings.h>
|
|
|
|
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
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>
|
2020-07-04 16:22:10 +02:00
|
|
|
#include <villas/exceptions.hpp>
|
2021-02-16 14:15:14 +01:00
|
|
|
#include <villas/log.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 * managed_alloc(size_t len, size_t alignment, struct Type *m)
|
2018-07-02 14:17:50 +02:00
|
|
|
{
|
|
|
|
/* Simple first-fit allocation */
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Block *first = (struct Block *) m->_vd;
|
|
|
|
struct Block *block;
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2019-04-07 15:13:40 +02:00
|
|
|
for (block = first; block != nullptr; block = block->next) {
|
2018-07-02 19:00:55 +02:00
|
|
|
if (block->used)
|
2018-07-02 14:17:50 +02:00
|
|
|
continue;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
char* cptr = (char *) block + sizeof(struct Block);
|
2018-07-02 19:00:55 +02:00
|
|
|
size_t avail = block->length;
|
2018-07-02 14:17:50 +02:00
|
|
|
uintptr_t uptr = (uintptr_t) cptr;
|
|
|
|
|
|
|
|
/* Check alignment first; leave a gap at start of block to assure
|
|
|
|
* 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) {
|
2021-08-10 10:12:48 -04:00
|
|
|
if (gap > sizeof(struct Block)) {
|
2018-07-02 14:17:50 +02:00
|
|
|
/* The alignment gap is big enough to fit another block.
|
|
|
|
* 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. */
|
2021-08-10 10:12:48 -04:00
|
|
|
block->length = gap - sizeof(struct Block);
|
|
|
|
struct Block *newblock = (struct Block *) (cptr - sizeof(struct Block));
|
2018-07-02 14:17:50 +02:00
|
|
|
newblock->prev = block;
|
|
|
|
newblock->next = block->next;
|
|
|
|
block->next = newblock;
|
2018-07-02 19:00:55 +02:00
|
|
|
newblock->used = false;
|
|
|
|
newblock->length = len;
|
2018-07-02 14:17:50 +02:00
|
|
|
block = newblock;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* The gap is too small to fit another block descriptor, so we
|
|
|
|
* must account for the gap length in the block length. */
|
2018-07-02 19:00:55 +02:00
|
|
|
block->length = len + gap;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (avail > len + sizeof(struct Block)) {
|
2018-07-02 14:17:50 +02:00
|
|
|
/* Imperfect fit, so create another block for the remaining part */
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Block *newblock = (struct Block *) (cptr + len);
|
2018-07-02 14:17:50 +02:00
|
|
|
newblock->prev = block;
|
|
|
|
newblock->next = block->next;
|
|
|
|
block->next = newblock;
|
2018-07-02 19:00:55 +02:00
|
|
|
|
2018-07-02 14:17:50 +02:00
|
|
|
if (newblock->next)
|
|
|
|
newblock->next->prev = newblock;
|
2018-07-02 19:00:55 +02:00
|
|
|
|
|
|
|
newblock->used = false;
|
2021-08-10 10:12:48 -04:00
|
|
|
newblock->length = avail - len - sizeof(struct Block);
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* If this block was larger than the requested length, but only
|
2021-08-10 10:12:48 -04:00
|
|
|
* by less than sizeof(struct Block), we may have wasted
|
2018-07-02 19:00:55 +02:00
|
|
|
* memory by previous assignments to block->length. */
|
|
|
|
block->length = avail;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
block->used = true;
|
|
|
|
|
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->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 */
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
static
|
|
|
|
int managed_free(struct Allocation *ma, struct Type *m)
|
2018-07-02 14:17:50 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Block *block = ma->managed.block;
|
2018-07-02 19:00:55 +02:00
|
|
|
|
|
|
|
/* 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 */
|
2021-08-10 10:12:48 -04:00
|
|
|
block->prev->length += block->length + block->next->length + 2 * sizeof(struct Block);
|
2018-07-02 19:00:55 +02:00
|
|
|
block->prev->next = block->next->next;
|
|
|
|
if (block->next->next)
|
|
|
|
block->next->next->prev = block->prev;
|
|
|
|
}
|
|
|
|
else if (block->prev && !block->prev->used) {
|
2021-08-10 10:12:48 -04:00
|
|
|
block->prev->length += block->length + sizeof(struct Block);
|
2018-07-02 19:00:55 +02:00
|
|
|
block->prev->next = block->next;
|
|
|
|
if (block->next)
|
|
|
|
block->next->prev = block->prev;
|
|
|
|
}
|
|
|
|
else if (block->next && !block->next->used) {
|
2021-08-10 10:12:48 -04:00
|
|
|
block->length += block->next->length + sizeof(struct Block);
|
2018-07-02 19:00:55 +02:00
|
|
|
block->next = block->next->next;
|
|
|
|
if (block->next)
|
|
|
|
block->next->prev = block;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
2018-07-02 19:00:55 +02:00
|
|
|
else {
|
|
|
|
/* no neighbouring free block, so just mark it as free */
|
|
|
|
block->used = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Type * villas::node::memory::managed(void *ptr, size_t len)
|
2018-07-02 14:17:50 +02:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
struct Type *mt = (struct Type *) ptr;
|
|
|
|
struct Block *mb;
|
2019-04-07 15:13:40 +02:00
|
|
|
char *cptr = (char *) ptr;
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (len < sizeof(struct Type) + sizeof(struct Block)) {
|
2021-02-16 14:15:14 +01:00
|
|
|
auto logger = logging.get("memory:managed");
|
|
|
|
logger->info("Passed region is too small");
|
2019-04-07 15:13:40 +02:00
|
|
|
return nullptr;
|
2018-07-02 14:17:50 +02:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
/* Initialize type */
|
2018-07-02 14:17:50 +02:00
|
|
|
mt->name = "managed";
|
|
|
|
mt->flags = 0;
|
2021-08-10 10:12:48 -04:00
|
|
|
mt->alloc = managed_alloc;
|
|
|
|
mt->free = managed_free;
|
2018-07-02 14:17:50 +02:00
|
|
|
mt->alignment = 1;
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
cptr += ALIGN(sizeof(struct Type), sizeof(void *));
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
/* Initialize first free memory block */
|
2021-08-10 10:12:48 -04:00
|
|
|
mb = (struct Block *) cptr;
|
2019-04-07 15:13:40 +02:00
|
|
|
mb->prev = nullptr;
|
|
|
|
mb->next = nullptr;
|
2018-07-02 19:00:55 +02:00
|
|
|
mb->used = false;
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
cptr += ALIGN(sizeof(struct Block), sizeof(void *));
|
2018-07-02 14:17:50 +02:00
|
|
|
|
2018-07-02 19:00:55 +02:00
|
|
|
mb->length = len - (cptr - (char *) ptr);
|
2018-07-02 14:17:50 +02:00
|
|
|
|
|
|
|
mt->_vd = (void *) mb;
|
|
|
|
|
|
|
|
return mt;
|
|
|
|
}
|