Memory.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  6. // Normal memory allocation, must be at least 8 byte aligned on 32 bit platform and 16 byte aligned on 64 bit platform
  7. using AllocateFunction = void *(*)(size_t inSize);
  8. using FreeFunction = void (*)(void *inBlock);
  9. // Aligned memory allocation
  10. using AlignedAllocateFunction = void *(*)(size_t inSize, size_t inAlignment);
  11. using AlignedFreeFunction = void (*)(void *inBlock);
  12. // User defined allocation / free functions
  13. extern AllocateFunction Allocate;
  14. extern FreeFunction Free;
  15. extern AlignedAllocateFunction AlignedAllocate;
  16. extern AlignedFreeFunction AlignedFree;
  17. /// Register platform default allocation / free functions
  18. void RegisterDefaultAllocator();
  19. /// Macro to override the new and delete functions
  20. #define JPH_OVERRIDE_NEW_DELETE \
  21. JPH_INLINE void *operator new (size_t inCount) { return JPH::Allocate(inCount); } \
  22. JPH_INLINE void operator delete (void *inPointer) noexcept { JPH::Free(inPointer); } \
  23. JPH_INLINE void *operator new[] (size_t inCount) { return JPH::Allocate(inCount); } \
  24. JPH_INLINE void operator delete[] (void *inPointer) noexcept { JPH::Free(inPointer); } \
  25. JPH_INLINE void *operator new (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
  26. JPH_INLINE void operator delete (void *inPointer, std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); } \
  27. JPH_INLINE void *operator new[] (size_t inCount, std::align_val_t inAlignment) { return JPH::AlignedAllocate(inCount, static_cast<size_t>(inAlignment)); } \
  28. JPH_INLINE void operator delete[] (void *inPointer, std::align_val_t inAlignment) noexcept { JPH::AlignedFree(inPointer); }
  29. #else
  30. // Directly define the allocation functions
  31. void *Allocate(size_t inSize);
  32. void Free(void *inBlock);
  33. void *AlignedAllocate(size_t inSize, size_t inAlignment);
  34. void AlignedFree(void *inBlock);
  35. // Don't implement allocator registering
  36. inline void RegisterDefaultAllocator() { }
  37. // Don't override new/delete
  38. #define JPH_OVERRIDE_NEW_DELETE
  39. #endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
  40. JPH_NAMESPACE_END