Code Review Asked by Matias Chara on October 27, 2021
This is programmed in Visual Studio 2019 with the C++17 feature set.
The intent with this code is to be able to create memory blocks of the specified alignment and size, so as to have contiguous cache-friendly memory and to only do one malloc and free for the whole block. Is there something wrong or missing from this implementation so that it would effectively fulfill its purpose?
#pragma once
class MemoryBlock
{
private:
void* p_rawMem;
void* p_currentMemLocation;
size_t p_currentMemLocationOffset;
size_t p_totalMemSize;
size_t p_remainingMemSize;
size_t p_alignment;
void *p_lastMemByteLocation;
void p_addCurrentMemLocation(size_t delta);
public:
MemoryBlock(size_t size, size_t alignment);
void* getMemoryWith(size_t size, size_t alignemnt);
~MemoryBlock();
};
#include <memory>
#include <stdexcept>
#include <cmath>
#include <assert.h>
#include "MemoryBlock.h"
void MemoryBlock::p_addCurrentMemLocation(size_t delta)
{
if (delta == 0) {
return;
}
p_currentMemLocationOffset += delta;
assert((p_currentMemLocationOffset) <= p_totalMemSize);
p_remainingMemSize -= delta;
if (p_remainingMemSize == 0) {
p_currentMemLocation = p_lastMemByteLocation;
}
else {
p_currentMemLocation = static_cast<void*>(static_cast<char*>(p_currentMemLocation) + delta);
}
}
MemoryBlock::MemoryBlock(size_t size, size_t alignment):
p_rawMem(_aligned_malloc(size, alignment)),
p_currentMemLocation(p_rawMem),
p_currentMemLocationOffset(0),
p_totalMemSize(size),
p_remainingMemSize(size),
p_alignment(alignment),
p_lastMemByteLocation(static_cast<void*>(static_cast<char*>(p_rawMem) + (size - 1)))
{
}
void* MemoryBlock::getMemoryWith(size_t size, size_t alignment)
{
if (size > p_totalMemSize) {
throw std::bad_alloc();
}
if (size == 0) {
throw std::invalid_argument("size must be greater than 0");
}
if (alignment == 0 || (alignment &(alignment-1))!= 0) {//checks it alignment is power of 2
throw std::invalid_argument("alignment should be a power of 2.");
}
if (alignment > p_alignment) {
throw std::invalid_argument("alignment requirement is greater than the memory block alignment.");
}
if (size > p_remainingMemSize) {
return nullptr;
}
if (p_totalMemSize == p_remainingMemSize) {
p_addCurrentMemLocation(size);
return p_rawMem;
}
p_addCurrentMemLocation((std::ceil(p_currentMemLocationOffset/(double)alignment)*alignment)-p_currentMemLocationOffset);
void* retVal = p_currentMemLocation;
p_addCurrentMemLocation(size);
return retVal;
}
MemoryBlock::~MemoryBlock()
{
_aligned_free(p_rawMem);
}
int main()
{
MemoryBlock memBlock(1024,1024);
int *memBlockIntPtr = new(memBlock.getMemoryWith(sizeof(int),alignof(int))) int(1);
std::cout << *memBlockIntPtr << std::endl;
constexpr size_t intArrSize = 4;
int *memBlockIntArrPtr = static_cast<int*>(memBlock.getMemoryWith(sizeof(int)*intArrSize, alignof(int)));
for (size_t i = 0; i < intArrSize; i++)
{
new (&memBlockIntArrPtr[i]) int(i);
}
for (size_t i = 0; i < intArrSize; i++)
{
std::cout << memBlockIntArrPtr[i] << std::endl;
}
return 0;
}
I also ask you if the usage of the code is correct in the main function, and if I’m handling the void pointers adequately in both single-variable and array cases. If not, of course, please correct me with an explanation. Say if I’m doing main, specifically, correctly or the whole thing- and if not, make it clear why. Also suggest if I should be using an underlying memory buffer of char* instead of void* (perhaps combined with static arrays).
new
or aligned_malloc()
_aligned_malloc()
is a Windows-specific, non-portable function. Luckily, C++17 added the portable aligned_malloc()
without an underscore. It also introduces a variant of new
that allows aligned allocation. The latter would look like so:
#include <new>
...
MemoryBlock::MemoryBlock(size_t size, size_t alignment):
p_rawMem((void *)new(std::align_val_t(alignemnt)) char[size]),
...
{
}
p_remainingMemSize == 0
Why is this a special case in p_addCurrentMemLocation
? Just adding delta
to the current pointer is perfectly fine. A pointer that points just beyond the end of an array or an allocated memory region is still a valid pointer.
delta == 0
An if
-statement is not free, especially not if the branch predictor cannot correctly predict the result of the condition. If delta
is zero, then the rest of the code still works correctly, and it's just two additions and a subtraction.
You should not need to use floating point math to calculate how much to advance p_currentMemLocationOffset
inside getMemoryWith()
. Depending on which processor your code is running on, converting to double
and back to size_t
can be an expensive operation. Furthermore, double
has less precision than size_t
on 64-bits machines, so the result might be wrong if very large memory blocks are used.
Instead of ceil(p_CurrentMemLocationOffset / (double)alignment)
, use the fact that integer division rounds down, and compensate for this by adding alignment - 1
first:
p_addCurrentMemLocation((((p_currentMemLocationOffset + alignment - 1) / alignment) * alignment) - p_currentMemLocationOffset);
Alternatively, use the modulo operation to determine how much to add:
p_addCurrentMemLocation((alignment - p_currentMemLocationOffset % alignment) % alignment);
The latter will be especially fast if the compiler can deduce that alignment
is always a power of two.
char *
vs. void *
It's probably easier to use char *
internally for the pointers, since you are doing arithmetic on them. And if you do that, the only time you need to cast is when returning from getMemoryWith()
.
Answered by G. Sliepen on October 27, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP