1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00

common/memory: implement freeing for LinearAllocator

This is still very simple. Only really free memory, when all allocation
have been deallocated so we only need to keep track of the current
number of allocations.
This commit is contained in:
Daniel Krebs 2018-05-16 11:25:13 +02:00
parent 929ed2f393
commit c15189b74b
2 changed files with 22 additions and 7 deletions

View file

@ -215,6 +215,7 @@ private:
size_t nextFreeAddress; ///< next chunk will be allocated here
size_t memorySize; ///< total size of managed memory
size_t internalOffset; ///< offset in address space (usually 0)
size_t allocationCount; ///< Number of individual allocations present
};

View file

@ -49,7 +49,8 @@ LinearAllocator::LinearAllocator(MemoryManager::AddressSpaceId memoryAddrSpaceId
BaseAllocator(memoryAddrSpaceId),
nextFreeAddress(0),
memorySize(memorySize),
internalOffset(internalOffset)
internalOffset(internalOffset),
allocationCount(0)
{
// make sure to start at aligned offset, reduce size in case we need padding
if(const size_t paddingBytes = getAlignmentPadding(internalOffset)) {
@ -61,12 +62,20 @@ LinearAllocator::LinearAllocator(MemoryManager::AddressSpaceId memoryAddrSpaceId
// deallocation callback
free = [&](MemoryBlock* mem) {
logger->debug("freeing {:#x} bytes at local addr {:#x} (addr space {})",
mem->getSize(), mem->getOffset(), mem->getAddrSpaceId());
logger->warn("free() not implemented");
logger->debug("available memory: {:#x} bytes", getAvailableMemory());
logger->debug("Deallocating memory block at local addr {:#x} (addr space {})",
mem->getOffset(), mem->getAddrSpaceId());
removeMemoryBlock(*mem);
allocationCount--;
if(allocationCount == 0) {
logger->debug("All allocations are deallocated now, freeing memory");
// all allocations have been deallocated, free all memory
nextFreeAddress = 0;
}
logger->debug("Available memory: {:#x} bytes", getAvailableMemory());
};
}
@ -75,8 +84,11 @@ std::string
LinearAllocator::getName() const
{
std::stringstream name;
name << "LinearAlloc" << getAddrSpaceId()
<< "@0x" << std::hex << internalOffset;
name << "LinearAlloc" << getAddrSpaceId();
if(internalOffset != 0) {
name << "@0x" << std::hex << internalOffset;
}
return name.str();
}
@ -122,6 +134,8 @@ LinearAllocator::allocateBlock(size_t size)
// mount block into the memory graph
insertMemoryBlock(*mem);
// increase the allocation count
allocationCount++;
return mem;
}