Memory.h 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. /// Note that you can override JPH_DEFAULT_ALLOCATE_ALIGNMENT if your allocator's alignment is different from the alignment as defined by `__STDCPP_DEFAULT_NEW_ALIGNMENT__`.
  9. using AllocateFunction = void *(*)(size_t inSize);
  10. /// Reallocate memory. inBlock can be nullptr in which case it must behave as a memory allocation.
  11. using ReallocateFunction = void *(*)(void *inBlock, size_t inOldSize, size_t inNewSize);
  12. /// Free memory. inBlock can be nullptr in which case it must do nothing.
  13. using FreeFunction = void (*)(void *inBlock);
  14. /// Aligned memory allocation.
  15. using AlignedAllocateFunction = void *(*)(size_t inSize, size_t inAlignment);
  16. /// Free aligned memory. inBlock can be nullptr in which case it must do nothing.
  17. using AlignedFreeFunction = void (*)(void *inBlock);
  18. // User defined allocation / free functions
  19. JPH_EXPORT extern AllocateFunction Allocate;
  20. JPH_EXPORT extern ReallocateFunction Reallocate;
  21. JPH_EXPORT extern FreeFunction Free;
  22. JPH_EXPORT extern AlignedAllocateFunction AlignedAllocate;
  23. JPH_EXPORT extern AlignedFreeFunction AlignedFree;
  24. /// Register platform default allocation / free functions
  25. JPH_EXPORT void RegisterDefaultAllocator();
  26. // 32-bit MinGW g++ doesn't call the correct overload for the new operator when a type is 16 bytes aligned.
  27. // It uses the non-aligned version, which on 32 bit platforms usually returns an 8 byte aligned block.
  28. // We therefore default to 16 byte aligned allocations when the regular new operator is used.
  29. // See: https://github.com/godotengine/godot/issues/105455#issuecomment-2824311547
  30. #if defined(JPH_COMPILER_MINGW) && JPH_CPU_ARCH_BITS == 32
  31. #define JPH_INTERNAL_DEFAULT_ALLOCATE(size) JPH::AlignedAllocate(size, 16)
  32. #define JPH_INTERNAL_DEFAULT_FREE(pointer) JPH::AlignedFree(pointer)
  33. #else
  34. #define JPH_INTERNAL_DEFAULT_ALLOCATE(size) JPH::Allocate(size)
  35. #define JPH_INTERNAL_DEFAULT_FREE(pointer) JPH::Free(pointer)
  36. #endif
  37. /// Macro to override the new and delete functions
  38. #define JPH_OVERRIDE_NEW_DELETE \
  39. JPH_INLINE void *operator new (size_t inCount) { return JPH_INTERNAL_DEFAULT_ALLOCATE(inCount); } \
  40. JPH_INLINE void operator delete (void *inPointer) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
  41. JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] size_t inSize) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
  42. JPH_INLINE void *operator new[] (size_t inCount) { return JPH_INTERNAL_DEFAULT_ALLOCATE(inCount); } \
  43. JPH_INLINE void operator delete[] (void *inPointer) noexcept { JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
  44. JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] size_t inSize) noexcept{ JPH_INTERNAL_DEFAULT_FREE(inPointer); } \
  45. JPH_INLINE void *operator new (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
  46. JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
  47. JPH_INLINE void operator delete (void *inPointer, [[maybe_unused]] size_t inSize, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
  48. JPH_INLINE void *operator new[] (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
  49. JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
  50. JPH_INLINE void operator delete[] (void *inPointer, [[maybe_unused]] size_t inSize, [[maybe_unused]] std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
  51. JPH_INLINE void *operator new ([[maybe_unused]] size_t inCount, void *inPointer) noexcept { return inPointer; } \
  52. JPH_INLINE void operator delete ([[maybe_unused]] void *inPointer, [[maybe_unused]] void *inPlace) noexcept { /* Do nothing */ } \
  53. JPH_INLINE void *operator new[] ([[maybe_unused]] size_t inCount, void *inPointer) noexcept { return inPointer; } \
  54. JPH_INLINE void operator delete[] ([[maybe_unused]] void *inPointer, [[maybe_unused]] void *inPlace) noexcept { /* Do nothing */ }
  55. #else
  56. // Directly define the allocation functions
  57. JPH_EXPORT void *Allocate(size_t inSize);
  58. JPH_EXPORT void *Reallocate(void *inBlock, size_t inOldSize, size_t inNewSize);
  59. JPH_EXPORT void Free(void *inBlock);
  60. JPH_EXPORT void *AlignedAllocate(size_t inSize, size_t inAlignment);
  61. JPH_EXPORT void AlignedFree(void *inBlock);
  62. // Don't implement allocator registering
  63. inline void RegisterDefaultAllocator() { }
  64. // Don't override new/delete
  65. #define JPH_OVERRIDE_NEW_DELETE
  66. #endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
  67. JPH_NAMESPACE_END