From c15189b74b79f9410a34a16180cd44dd3f7db5fc Mon Sep 17 00:00:00 2001 From: Daniel Krebs Date: Wed, 16 May 2018 11:25:13 +0200 Subject: [PATCH] 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. --- fpga/include/villas/memory.hpp | 1 + fpga/lib/common/memory.cpp | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/fpga/include/villas/memory.hpp b/fpga/include/villas/memory.hpp index b640743fb..a3d5d5dd1 100644 --- a/fpga/include/villas/memory.hpp +++ b/fpga/include/villas/memory.hpp @@ -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 }; diff --git a/fpga/lib/common/memory.cpp b/fpga/lib/common/memory.cpp index 3a44ab63e..fe15e72b1 100644 --- a/fpga/lib/common/memory.cpp +++ b/fpga/lib/common/memory.cpp @@ -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; }