Improve alignment handling for the alignment allocator

This commit is contained in:
Chris Robinson 2019-06-03 22:58:56 -07:00
parent c76fb714cc
commit f0bc9d8a9b
4 changed files with 8 additions and 4 deletions

View File

@ -66,7 +66,7 @@ struct HrtfHandle {
std::unique_ptr<HrtfHandle> HrtfHandle::Create(size_t fname_len) std::unique_ptr<HrtfHandle> HrtfHandle::Create(size_t fname_len)
{ {
void *ptr{al_calloc(DEF_ALIGN, HrtfHandle::Sizeof(fname_len))}; void *ptr{al_calloc(alignof(HrtfHandle), HrtfHandle::Sizeof(fname_len))};
return std::unique_ptr<HrtfHandle>{new (ptr) HrtfHandle{fname_len}}; return std::unique_ptr<HrtfHandle>{new (ptr) HrtfHandle{fname_len}};
} }

View File

@ -246,7 +246,7 @@ ALeffectslotArray *ALeffectslot::CreatePtrArray(size_t count) noexcept
/* Allocate space for twice as many pointers, so the mixer has scratch /* Allocate space for twice as many pointers, so the mixer has scratch
* space to store a sorted list during mixing. * space to store a sorted list during mixing.
*/ */
void *ptr{al_calloc(DEF_ALIGN, ALeffectslotArray::Sizeof(count*2))}; void *ptr{al_calloc(alignof(ALeffectslotArray), ALeffectslotArray::Sizeof(count*2))};
return new (ptr) ALeffectslotArray{count}; return new (ptr) ALeffectslotArray{count};
} }

View File

@ -3,6 +3,7 @@
#include "almalloc.h" #include "almalloc.h"
#include <cassert>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#ifdef HAVE_MALLOC_H #ifdef HAVE_MALLOC_H
@ -26,6 +27,9 @@
void *al_malloc(size_t alignment, size_t size) void *al_malloc(size_t alignment, size_t size)
{ {
assert((alignment & (alignment-1)) == 0);
alignment = std::max(alignment, sizeof(void*));
#if defined(HAVE_ALIGNED_ALLOC) #if defined(HAVE_ALIGNED_ALLOC)
size = (size+(alignment-1))&~(alignment-1); size = (size+(alignment-1))&~(alignment-1);
return aligned_alloc(alignment, size); return aligned_alloc(alignment, size);

View File

@ -39,7 +39,7 @@ int al_is_sane_alignment_allocator(void) noexcept;
namespace al { namespace al {
template<typename T, size_t alignment=DEF_ALIGN> template<typename T, size_t alignment=alignof(T)>
struct allocator : public std::allocator<T> { struct allocator : public std::allocator<T> {
using size_type = size_t; using size_type = size_t;
using pointer = T*; using pointer = T*;
@ -97,7 +97,7 @@ std::unique_ptr<T> make_unique(ArgsT&&...args)
* struct, with placement new, to have a run-time-sized array that's embedded * struct, with placement new, to have a run-time-sized array that's embedded
* with its size. * with its size.
*/ */
template<typename T,size_t alignment=DEF_ALIGN> template<typename T, size_t alignment=alignof(T)>
struct FlexArray { struct FlexArray {
const size_t mSize; const size_t mSize;
alignas(alignment) T mArray[]; alignas(alignment) T mArray[];