entt icon indicating copy to clipboard operation
entt copied to clipboard

Components with overriden new operator.

Open jdumas opened this issue 1 year ago • 6 comments

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.

jdumas avatar Mar 26 '24 22:03 jdumas

Have you tried it already in your project? Can you confirm that it works as expected?

skypjack avatar Mar 27 '24 07:03 skypjack

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.

jdumas avatar Mar 27 '24 07:03 jdumas

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!

skypjack avatar Mar 27 '24 07:03 skypjack

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;
}

jdumas avatar Mar 27 '24 20:03 jdumas

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. 👍

skypjack avatar Mar 28 '24 09:03 skypjack

All good, thanks for looking into it!

jdumas avatar Mar 28 '24 14:03 jdumas