Memory.h 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  7. // Normal memory allocation, must be at least 8 byte aligned on 32 bit platform and 16 byte aligned on 64 bit platform
  8. using AllocateFunction = void *(*)(size_t inSize);
  9. using ReallocateFunction = void *(*)(void *inBlock, size_t inOldSize, size_t inNewSize);
  10. using FreeFunction = void (*)(void *inBlock);
  11. // Aligned memory allocation
  12. using AlignedAllocateFunction = void *(*)(size_t inSize, size_t inAlignment);
  13. using AlignedFreeFunction = void (*)(void *inBlock);
  14. // User defined allocation / free functions
  15. JPH_EXPORT extern AllocateFunction Allocate;
  16. JPH_EXPORT extern ReallocateFunction Reallocate;
  17. JPH_EXPORT extern FreeFunction Free;
  18. JPH_EXPORT extern AlignedAllocateFunction AlignedAllocate;
  19. JPH_EXPORT extern AlignedFreeFunction AlignedFree;
  20. /// Register platform default allocation / free functions
  21. JPH_EXPORT void RegisterDefaultAllocator();
  22. // 32-bit MinGW g++ doesn't call the correct overload for the new operator when a type is 16 bytes aligned.
  23. // It uses the non-aligned version, which on 32 bit platforms usually returns an 8 byte aligned block.
  24. // We therefore default to 16 byte aligned allocations when the regular new operator is used.
  25. // See: https://github.com/godotengine/godot/issues/105455#issuecomment-2824311547
  26. #if defined(JPH_COMPILER_MINGW) && JPH_CPU_ADDRESS_BITS == 32
  27. #define JPH_INTERNAL_DEFAULT_ALLOCATE(size) JPH::AlignedAllocate(size, 16)
  28. #define JPH_INTERNAL_DEFAULT_FREE(pointer) JPH::AlignedFree(pointer)
  29. #else
  30. #define JPH_INTERNAL_DEFAULT_ALLOCATE(size) JPH::Allocate(size)
  31. #define JPH_INTERNAL_DEFAULT_FREE(pointer) JPH::Free(pointer)
  32. #endif
  33. /// Macro to override the new and delete functions
  34. #define JPH_OVERRIDE_NEW_DELETE \
  35. JPH_INLINE void *operator new (size_t inCount) { return JPH_INTERNAL_DEFAULT_ALLOCATE(inCount); } \
  36. JPH_INLINE void operator delete (void *inPointer) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
  37. JPH_INLINE void *operator new[] (size_t inCount) { return JPH_INTERNAL_DEFAULT_ALLOCATE(inCount); } \
  38. JPH_INLINE void operator delete[] (void *inPointer) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
  39. JPH_INLINE void *operator new (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
  40. JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
  41. JPH_INLINE void *operator new[] (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
  42. JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
  43. JPH_INLINE void *operator new ([[maybe_unused]] size_t inCount, void *inPointer) noexcept { return inPointer; } \
  44. JPH_INLINE void operator delete ([[maybe_unused]] void *inPointer, [[maybe_unused]] void *inPlace) noexcept { /* Do nothing */ } \
  45. JPH_INLINE void *operator new[] ([[maybe_unused]] size_t inCount, void *inPointer) noexcept { return inPointer; } \
  46. JPH_INLINE void operator delete[] ([[maybe_unused]] void *inPointer, [[maybe_unused]] void *inPlace) noexcept { /* Do nothing */ }
  47. #else
  48. // Directly define the allocation functions
  49. JPH_EXPORT void *Allocate(size_t inSize);
  50. JPH_EXPORT void *Reallocate(void *inBlock, size_t inOldSize, size_t inNewSize);
  51. JPH_EXPORT void Free(void *inBlock);
  52. JPH_EXPORT void *AlignedAllocate(size_t inSize, size_t inAlignment);
  53. JPH_EXPORT void AlignedFree(void *inBlock);
  54. // Don't implement allocator registering
  55. inline void RegisterDefaultAllocator() { }
  56. // Don't override new/delete
  57. #define JPH_OVERRIDE_NEW_DELETE
  58. #endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
  59. JPH_NAMESPACE_END