From 6b7d6941035157d88d07c4ceba72c8fb443ea9f0 Mon Sep 17 00:00:00 2001 From: Daniel Krebs Date: Tue, 15 May 2018 13:26:59 +0200 Subject: [PATCH] common/BaseAllocator: test allocated memory for accessibility Write to and read-verify allocated memory block when using allocate() wrapper. --- fpga/include/villas/memory.hpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/fpga/include/villas/memory.hpp b/fpga/include/villas/memory.hpp index 12779bf01..38b801c45 100644 --- a/fpga/include/villas/memory.hpp +++ b/fpga/include/villas/memory.hpp @@ -130,6 +130,21 @@ public: { const size_t size = num * sizeof(T); auto mem = allocateBlock(size); + + // Check if the allocated memory is really accessible by writing to the + // allocated memory and reading back. Exponentially increase offset to + // speed up testing. + MemoryAccessor byteAccessor(*mem); + size_t idx = 0; + for(int i = 0; idx < mem->getSize(); i++, idx = (1 << i)) { + auto val = static_cast(i); + byteAccessor[idx] = val; + if(byteAccessor[idx] != val) { + logger->error("Cannot access allocated memory"); + throw std::bad_alloc(); + } + } + return MemoryAccessor(std::move(mem)); } @@ -181,6 +196,9 @@ public: size_t getAvailableMemory() const { return memorySize - nextFreeAddress; } + size_t getSize() const + { return memorySize; } + std::string getName() const; std::unique_ptr