Skip to main content
fixed compilation.
Source Link
#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);
}
#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_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)
{
}

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);
}
#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);
}
Copy edited (e.g. ref. <https://en.wikipedia.org/wiki/C%2B%2B>, <https://en.wikipedia.org/wiki/Microsoft_Visual_Studio#2019>, and <https://en.wikipedia.org/wiki/C%2B%2B17>).
Source Link

Implementing a very simple memory block for quick allocation and memory alignment. c++C++

This is programmed in visual studio 2019Visual Studio 2019 with c++17the C++17 feature set.

MemoryBlock.h

MemoryBlock.h

MemoryBlock.cpp

MemoryBlock.cpp

main.cpp

main.cpp

I also ask you if the usage of the code is correct in the main function, and if ImI'm handling the void pointers adequately in both single-variable and array cases. If not, of course, please correct me with an explanation. saySay if imI'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).

Implementing a very simple memory block for quick allocation and memory alignment. c++

programmed in visual studio 2019 with c++17 feature set.

MemoryBlock.h

MemoryBlock.cpp

main.cpp

I also ask you if the usage of the code is correct in the main function, and if Im handling the void pointers adequately in both single-variable and array cases. If not, of course, please correct me with an explanation. say if im 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).

Implementing a very simple memory block for quick allocation and memory alignment. C++

This is programmed in Visual Studio 2019 with the C++17 feature set.

MemoryBlock.h

MemoryBlock.cpp

main.cpp

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).

Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1285047058039283712
edited tags
Link
G. Sliepen
  • 61.7k
  • 3
  • 61
  • 152
made sure that p_currentMemLocation never points to an out-of-bounds memory position. Added additional checks for when size is equal to 0 in getMemoryWith()
Source Link
Loading
fixed bug for alignment calculation
Source Link
Loading
fixed throw when theres not enough space- only through when size esceeds the total size of the block.
Source Link
Loading
fixed the assert
Source Link
Loading
Source Link
Loading