Add memory allocation API with templated C++ delegates & minor fixups #1123
Reference in New Issue
Block a user
No description provided.
Delete Branch "main"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This simple pull request adds a memory allocation API to the Yoga C API,
as well as the respective templated C++ delegate functions to allow for the use
of custom memory allocator alongside Yoga.
I also think it might be sensible to remove the YG name prefix from all functions within Yoga.h which are C++-only
and to move them into the yoga namespace.
Updated some things, test-compiled it on Windows x86_64 (under both Clang and MSVC), Linux x86_64 and MacOS x86_64 and ARM64 and everything's looking good.
Found another problem regarding static libraries and relocation on GCC/Clang, fixed that as well.
I think letting Yoga use a custom allocator is a great feature.
I left some comments on the PR wrt implementation.
@@ -0,0 +114,4 @@
})
#else
#define YG_ALLOC_ALIGNED ([](size_t a, size_t s) -> void* { \
return aligned_alloc(a, s); \
nit: why are these macros vs a regular function?
@@ -0,0 +130,4 @@
gAllocatorAllocateFunc = allocFunc;
gAllocatorFreeFunc = freeFunc;
}
If these are global, they will need to be protected by mutex, in case multiple threads are using Yoga at once.
I think it would be cleaner if the allocator was set as part of YGConfig, though it would mean YGConfig itself could not be allocated using the custom allocator. Though, Yoga may otherwise call functions which perform their dynamic allocations, so if the goal is just for the large structures, I think it could still be okay.
@@ -0,0 +132,4 @@
}
YOGA_EXPORT void YGGetAllocationCallbacks(
YGAllocatorAllocateFunc* allocFunc,
@@ -0,0 +150,4 @@
gAllocatorFreeFunc(memory);
}
static inline bool YGDoubleIsUndefined(const double value) {
nit: Do we need to expose allocate and free as part of the public Yoga API? This already has an API for getting the allocators which are set. It doesn't seem like a good code pattern for folks to use Yoga's APIs for their own memory allocation.
This should not be part of the public API, since it is a helper for the internals of Yoga.
Pull request closed