Add memory allocation API with templated C++ delegates & minor fixups #1123
@@ -11,7 +11,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <memory>
|
#include <malloc.h>
|
||||||
#include "Utils.h"
|
#include "Utils.h"
|
||||||
#include "YGNode.h"
|
#include "YGNode.h"
|
||||||
#include "YGNodePrint.h"
|
#include "YGNodePrint.h"
|
||||||
@@ -106,8 +106,24 @@ static int YGDefaultLog(
|
|||||||
#undef YG_UNUSED
|
#undef YG_UNUSED
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static YGAllocatorAllocateFunc gAllocatorAllocateFunc = &malloc;
|
#ifdef _MSC_VER
|
||||||
static YGAllocatorFreeFunc gAllocatorFreeFunc = &free;
|
#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(
|
YOGA_EXPORT void YGSetAllocationCallbacks(
|
||||||
YGAllocatorAllocateFunc allocFunc,
|
YGAllocatorAllocateFunc allocFunc,
|
||||||
@@ -127,8 +143,8 @@ YOGA_EXPORT void YGGetAllocationCallbacks(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
YOGA_EXPORT void* YGMemoryAllocate(size_t size) {
|
YOGA_EXPORT void* YGMemoryAllocate(size_t alignment, size_t size) {
|
||||||
return gAllocatorAllocateFunc(size);
|
return gAllocatorAllocateFunc(alignment, size);
|
||||||
}
|
}
|
||||||
|
|
||||||
YOGA_EXPORT void YGMemoryFree(void* memory) {
|
YOGA_EXPORT void YGMemoryFree(void* memory) {
|
||||||
|
@@ -29,7 +29,7 @@ typedef struct YGSize {
|
|||||||
float height;
|
float height;
|
||||||
} YGSize;
|
} YGSize;
|
||||||
|
|
||||||
typedef void* (*YGAllocatorAllocateFunc)(size_t size);
|
typedef void* (*YGAllocatorAllocateFunc)(size_t alignment, size_t size);
|
||||||
typedef void (*YGAllocatorFreeFunc)(void* memory);
|
typedef void (*YGAllocatorFreeFunc)(void* memory);
|
||||||
|
|
||||||
typedef struct YGConfig* YGConfigRef;
|
typedef struct YGConfig* YGConfigRef;
|
||||||
@@ -60,7 +60,7 @@ typedef YGNodeRef (
|
|||||||
WIN_EXPORT void YGSetAllocationCallbacks(YGAllocatorAllocateFunc allocFunc, YGAllocatorFreeFunc freeFunc);
|
WIN_EXPORT void YGSetAllocationCallbacks(YGAllocatorAllocateFunc allocFunc, YGAllocatorFreeFunc freeFunc);
|
||||||
WIN_EXPORT void YGGetAllocationCallbacks(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);
|
WIN_EXPORT void YGMemoryFree(void* memory);
|
||||||
|
|
||||||
// YGNode
|
// YGNode
|
||||||
@@ -378,7 +378,7 @@ YG_EXTERN_C_END
|
|||||||
// cast nor pass in the size of the allocated chunk of memory explicitly.
|
// cast nor pass in the size of the allocated chunk of memory explicitly.
|
||||||
template<typename T, typename... A>
|
template<typename T, typename... A>
|
||||||
T* YGAllocate(A&&... arguments) {
|
T* YGAllocate(A&&... arguments) {
|
||||||
auto* memory = reinterpret_cast<T*>(YGMemoryAllocate(sizeof(T)));
|
auto* memory = reinterpret_cast<T*>(YGMemoryAllocate(sizeof(T), alignof(T)));
|
||||||
new(memory) T(std::forward<A>(arguments)...);
|
new(memory) T(std::forward<A>(arguments)...);
|
||||||
return memory;
|
return memory;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user
nit: why are these macros vs a regular function?