From ff1a295814de1abb1fe0ea4bb897b4d19fd6f4bc Mon Sep 17 00:00:00 2001
From: Steffen Vogel <post@steffenvogel.de>
Date: Tue, 21 Aug 2018 13:51:22 +0200
Subject: [PATCH] merge changes from VILLASfpga/feature/hls-rtds2gpu

---
 common/include/villas/memory.hpp | 23 +++++++++++++++++++++++
 common/lib/kernel/vfio.cpp       |  1 +
 2 files changed, 24 insertions(+)

diff --git a/common/include/villas/memory.hpp b/common/include/villas/memory.hpp
index 75311fb2f..2494754e4 100644
--- a/common/include/villas/memory.hpp
+++ b/common/include/villas/memory.hpp
@@ -85,6 +85,8 @@ public:
 	MemoryAccessor(const MemoryBlock& mem) :
 	    translation(MemoryManager::get().getTranslationFromProcess(mem.getAddrSpaceId())) {}
 
+	MemoryAccessor(const MemoryTranslation& translation) :
+	    translation(translation) {}
 
 	T& operator*() const {
 		return *reinterpret_cast<T*>(translation.getLocalAddr(0));
@@ -144,6 +146,12 @@ public:
 		};
 	}
 
+	BaseAllocator(std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn> mem) :
+	    BaseAllocator(mem->getAddrSpaceId())
+	{
+		memoryBlock = std::move(mem);
+	}
+
 	virtual std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn>
 	allocateBlock(size_t size) = 0;
 
@@ -151,6 +159,12 @@ public:
 	MemoryAccessor<T>
 	allocate(size_t num)
 	{
+		if(num == 0) {
+			// doesn't make sense to allocate an empty block
+			logger->error("Trying to allocate empty memory");
+			throw std::bad_alloc();
+		}
+
 		const size_t size = num * sizeof(T);
 		auto mem = allocateBlock(size);
 
@@ -195,6 +209,9 @@ protected:
 	MemoryBlock::deallocator_fn free;
 	SpdLogger logger;
 
+	// optional, if allocator should own the memory block
+	std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn> memoryBlock;
+
 private:
 	MemoryManager::AddressSpaceId memoryAddrSpaceId;
 	DerivedAllocator* derivedAlloc;
@@ -216,6 +233,12 @@ public:
 	                size_t memorySize,
 	                size_t internalOffset = 0);
 
+	LinearAllocator(std::unique_ptr<MemoryBlock, MemoryBlock::deallocator_fn> mem) :
+	    LinearAllocator(mem->getAddrSpaceId(), mem->getSize())
+	{
+		memoryBlock = std::move(mem);
+	}
+
 	size_t getAvailableMemory() const
 	{ return memorySize - nextFreeAddress; }
 
diff --git a/common/lib/kernel/vfio.cpp b/common/lib/kernel/vfio.cpp
index 822cf4b6c..805bacb71 100644
--- a/common/lib/kernel/vfio.cpp
+++ b/common/lib/kernel/vfio.cpp
@@ -754,6 +754,7 @@ VfioGroup::attach(VfioContainer& container, int groupIndex)
 	          << (container.isIommuEnabled() ? "" : "noiommu-")
 	          << groupIndex;
 
+	logger->debug("path: {}", groupPath.str().c_str());
 	group->fd = open(groupPath.str().c_str(), O_RDWR);
 	if (group->fd < 0) {
 		logger->error("Failed to open VFIO group {}", group->index);