Skip to content
Permalink
Browse files

Make absolutely sure allocated frame memory is always 16-byte aligned

  • Loading branch information
dpjudas committed Nov 23, 2019
1 parent bc37d8d commit b7745aaa8b8f84cca70e80ac714c524cdc110877
Showing with 42 additions and 2 deletions.
  1. +40 −0 src/rendering/swrenderer/r_memory.cpp
  2. +2 −2 src/rendering/swrenderer/r_memory.h
@@ -37,6 +37,7 @@
#include "po_man.h"
#include "r_data/colormaps.h"
#include "r_memory.h"
#include <stdlib.h>

void *RenderMemory::AllocBytes(int size)
{
@@ -73,3 +74,42 @@ void RenderMemory::Clear()
FreeBlocks.push_back(std::move(block));
}
}

static void* aligned_alloc(size_t alignment, size_t size)
{
void* ptr;
#if defined _MSC_VER
ptr = _aligned_malloc(size, alignment);
if (!ptr)
throw std::bad_alloc();
#else
// posix_memalign required alignment to be a min of sizeof(void *)
if (alignment < sizeof(void*))
alignment = sizeof(void*);

if (posix_memalign((void**)&ptr, alignment, size))
throw std::bad_alloc();
#endif
return ptr;
}

static void aligned_free(void* ptr)
{
if (ptr)
{
#if defined _MSC_VER
_aligned_free(ptr);
#else
free(ptr);
#endif
}
}

RenderMemory::MemoryBlock::MemoryBlock() : Data(static_cast<uint8_t*>(aligned_alloc(16, BlockSize))), Position(0)
{
}

RenderMemory::MemoryBlock::~MemoryBlock()
{
aligned_free(Data);
}
@@ -29,8 +29,8 @@ class RenderMemory

struct MemoryBlock
{
MemoryBlock() : Data(new uint8_t[BlockSize]), Position(0) { }
~MemoryBlock() { delete[] Data; }
MemoryBlock();
~MemoryBlock();

MemoryBlock(const MemoryBlock &) = delete;
MemoryBlock &operator=(const MemoryBlock &) = delete;

0 comments on commit b7745aa

Please sign in to comment.
You can’t perform that action at this time.