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/BaseAllocator: test allocated memory for accessibility

Write to and read-verify allocated memory block when using allocate()
wrapper.
This commit is contained in:
Daniel Krebs 2018-05-15 13:26:59 +02:00
parent 2477ed4b4b
commit 6b7d694103

View file

@ -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<volatile uint8_t> byteAccessor(*mem);
size_t idx = 0;
for(int i = 0; idx < mem->getSize(); i++, idx = (1 << i)) {
auto val = static_cast<uint8_t>(i);
byteAccessor[idx] = val;
if(byteAccessor[idx] != val) {
logger->error("Cannot access allocated memory");
throw std::bad_alloc();
}
}
return MemoryAccessor<T>(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<MemoryBlock, MemoryBlock::deallocator_fn>