diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index 52a4a76c..bc20ed5c 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include "Utils.h" #include "YGNode.h" #include "YGNodePrint.h" @@ -106,8 +106,24 @@ static int YGDefaultLog( #undef YG_UNUSED #endif -static YGAllocatorAllocateFunc gAllocatorAllocateFunc = &malloc; -static YGAllocatorFreeFunc gAllocatorFreeFunc = &free; +#ifdef _MSC_VER +#define YG_ALLOC_ALIGNED ([](size_t a, size_t s) -> void* { \ + return _aligned_malloc(s, a); \ +}) +#define YG_FREE_ALIGNED ([](void* m) -> void { \ + _aligned_free(m); \ +}) +#else +#define YG_ALLOC_ALIGNED ([](size_t a, size_t s) -> void* { \ + return aligned_alloc(a, s); \ +}) +#define YG_FREE_ALIGNED ([](void* m) -> void { \ + free(m); \ +}) +#endif + +static YGAllocatorAllocateFunc gAllocatorAllocateFunc = YG_ALLOC_ALIGNED; +static YGAllocatorFreeFunc gAllocatorFreeFunc = YG_FREE_ALIGNED; YOGA_EXPORT void YGSetAllocationCallbacks( YGAllocatorAllocateFunc allocFunc, @@ -127,8 +143,8 @@ YOGA_EXPORT void YGGetAllocationCallbacks( } } -YOGA_EXPORT void* YGMemoryAllocate(size_t size) { - return gAllocatorAllocateFunc(size); +YOGA_EXPORT void* YGMemoryAllocate(size_t alignment, size_t size) { + return gAllocatorAllocateFunc(alignment, size); } YOGA_EXPORT void YGMemoryFree(void* memory) { diff --git a/yoga/Yoga.h b/yoga/Yoga.h index 9e0c5f25..f54e5f9e 100644 --- a/yoga/Yoga.h +++ b/yoga/Yoga.h @@ -29,7 +29,7 @@ typedef struct YGSize { float height; } YGSize; -typedef void* (*YGAllocatorAllocateFunc)(size_t size); +typedef void* (*YGAllocatorAllocateFunc)(size_t alignment, size_t size); typedef void (*YGAllocatorFreeFunc)(void* memory); typedef struct YGConfig* YGConfigRef; @@ -60,7 +60,7 @@ typedef YGNodeRef ( WIN_EXPORT void YGSetAllocationCallbacks(YGAllocatorAllocateFunc allocFunc, YGAllocatorFreeFunc freeFunc); WIN_EXPORT void YGGetAllocationCallbacks(YGAllocatorAllocateFunc* allocFunc, YGAllocatorFreeFunc* freeFunc); -WIN_EXPORT void* YGMemoryAllocate(size_t size); +WIN_EXPORT void* YGMemoryAllocate(size_t alignment, size_t size); WIN_EXPORT void YGMemoryFree(void* memory); // YGNode @@ -378,7 +378,7 @@ YG_EXTERN_C_END // cast nor pass in the size of the allocated chunk of memory explicitly. template T* YGAllocate(A&&... arguments) { - auto* memory = reinterpret_cast(YGMemoryAllocate(sizeof(T))); + auto* memory = reinterpret_cast(YGMemoryAllocate(sizeof(T), alignof(T))); new(memory) T(std::forward(arguments)...); return memory; }