entt
entt copied to clipboard
Components with overriden new operator.
Hi. I'm trying to use EnTT with JoltPhysics. Jolt by default overrides its new/delete operators, which causes compilation issues with EnTT's usage of placement new operator here:
https://github.com/skypjack/entt/blob/fedcb920ce0068c35ffbc66fd4e84864e6ef71ef/src/entt/core/memory.hpp#L278
As proposed in this discussion, the solution would be to replace new(value) with ::new(value) in EnTT's codebase.
Have you tried it already in your project? Can you confirm that it works as expected?
Yes! But if there are other places where EnTT is using a placement new, those may also need to be updated. Happy to work on a MWE without Jolt if you think that's useful.
The only other class that uses placement new is any afaik.
I don't think a MWE with Jolt is required here actually. A standalone test that doesn't use external libraries would be way better to avoid future regression probably.
Do you have bandwidth to pack something in this sense for the utility in memory.hpp? A rough example would be enough, I can then fix it and throw it in myself but it would definitely save me a lot of time!
Sure. As I said I was planning to provide a MWE without Jolt. So here it is:
#include <entt/entt.hpp>
#include <stdlib.h>
#include <cstdlib>
namespace MyLib {
void* Allocate(size_t inSize)
{
return malloc(inSize);
}
void Free(void* inBlock)
{
free(inBlock);
}
void* AlignedAllocate(size_t inSize, size_t inAlignment)
{
#if defined(_WIN32)
return _aligned_malloc(inSize, inAlignment);
#else
void* block = nullptr;
posix_memalign(&block, inAlignment, inSize);
return block;
#endif
}
void AlignedFree(void* inBlock)
{
#if defined(_WIN32)
_aligned_free(inBlock);
#else
free(inBlock);
#endif
}
class Foo
{
public:
Foo(int x_)
: x(x_)
{}
public:
void* operator new(size_t inCount) { return MyLib::Allocate(inCount); }
void operator delete(void* inPointer) noexcept { MyLib::Free(inPointer); }
void* operator new[](size_t inCount) { return MyLib::Allocate(inCount); }
void operator delete[](void* inPointer) noexcept { MyLib::Free(inPointer); }
void* operator new(size_t inCount, std::align_val_t inAlignment)
{
return MyLib::AlignedAllocate(inCount, static_cast<size_t>(inAlignment));
}
void operator delete(void* inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept
{
MyLib::AlignedFree(inPointer);
}
void* operator new[](size_t inCount, std::align_val_t inAlignment)
{
return MyLib::AlignedAllocate(inCount, static_cast<size_t>(inAlignment));
}
void operator delete[](void* inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept
{
MyLib::AlignedFree(inPointer);
}
protected:
int x;
};
} // namespace MyLib
int main(void)
{
entt::registry r;
auto e = r.create();
r.emplace<MyLib::Foo>(e, 42); // <-- doesn't compile
return 0;
}
As I said I was planning to provide a MWE without Jolt
You're right and I read it wrong. 😅 Thanks for the example, I'll use it as the basis for a non-regression test. 👍
All good, thanks for looking into it!