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

196 lines
5.7 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
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
2020-01-20 17:17:00 +01:00
* @copyright 2014-2020, Institute for Automation of Complex Power Systems, EONERC
2018-07-02 14:17:50 +02:00
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
#include <cstdlib>
2018-07-02 14:17:50 +02:00
#include <unistd.h>
#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>
#include <villas/log.h>
#include <villas/memory.h>
#include <villas/utils.hpp>
#include <villas/exceptions.hpp>
2018-07-02 14:17:50 +02:00
using namespace villas;
2019-06-04 16:55:38 +02:00
using namespace villas::utils;
2019-10-26 13:07:02 +02:00
static struct memory_allocation * memory_managed_alloc(size_t len, size_t alignment, struct memory_type *m)
2018-07-02 14:17:50 +02:00
{
/* Simple first-fit allocation */
struct memory_block *first = (struct memory_block *) m->_vd;
struct memory_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) {
if (block->used)
2018-07-02 14:17:50 +02:00
continue;
char* cptr = (char *) block + sizeof(struct memory_block);
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) {
if (gap > sizeof(struct memory_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. */
block->length = gap - sizeof(struct memory_block);
struct memory_block *newblock = (struct memory_block *) (cptr - sizeof(struct memory_block));
2018-07-02 14:17:50 +02:00
newblock->prev = block;
newblock->next = block->next;
block->next = newblock;
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. */
block->length = len + gap;
2018-07-02 14:17:50 +02:00
}
if (avail > len + sizeof(struct memory_block)) {
2018-07-02 14:17:50 +02:00
/* Imperfect fit, so create another block for the remaining part */
struct memory_block *newblock = (struct memory_block *) (cptr + len);
2018-07-02 14:17:50 +02:00
newblock->prev = block;
newblock->next = block->next;
block->next = newblock;
2018-07-02 14:17:50 +02:00
if (newblock->next)
newblock->next->prev = newblock;
newblock->used = false;
newblock->length = avail - len - sizeof(struct memory_block);
2018-07-02 14:17:50 +02:00
}
else {
/* If this block was larger than the requested length, but only
* by less than sizeof(struct memory_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 memory_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 */
2019-04-07 15:13:40 +02:00
return nullptr;
2018-07-02 14:17:50 +02:00
}
2019-10-26 13:07:02 +02:00
static int memory_managed_free(struct memory_allocation *ma, struct memory_type *m)
2018-07-02 14:17:50 +02:00
{
struct memory_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 memory_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 memory_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 memory_block);
block->next = block->next->next;
if (block->next)
block->next->prev = block;
2018-07-02 14:17:50 +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
}
struct memory_type * memory_managed(void *ptr, size_t len)
{
2019-04-07 15:13:40 +02:00
struct memory_type *mt = (struct memory_type *) ptr;
struct memory_block *mb;
2019-04-07 15:13:40 +02:00
char *cptr = (char *) ptr;
2018-07-02 14:17:50 +02:00
if (len < sizeof(struct memory_type) + sizeof(struct memory_block)) {
2018-07-02 14:17:50 +02:00
info("memory_managed: passed region too small");
2019-04-07 15:13:40 +02:00
return nullptr;
2018-07-02 14:17:50 +02:00
}
/* Initialize memory_type */
mt->name = "managed";
mt->flags = 0;
mt->alloc = memory_managed_alloc;
mt->free = memory_managed_free;
mt->alignment = 1;
cptr += ALIGN(sizeof(struct memory_type), sizeof(void *));
/* Initialize first free memory block */
mb = (struct memory_block *) cptr;
2019-04-07 15:13:40 +02:00
mb->prev = nullptr;
mb->next = nullptr;
mb->used = false;
2018-07-02 14:17:50 +02:00
cptr += ALIGN(sizeof(struct memory_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;
return mt;
}