Memory.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include <Jolt/Jolt.h>
  5. JPH_SUPPRESS_WARNINGS_STD_BEGIN
  6. #include <cstdlib>
  7. JPH_SUPPRESS_WARNINGS_STD_END
  8. #include <stdlib.h>
  9. JPH_NAMESPACE_BEGIN
  10. #ifdef JPH_DISABLE_CUSTOM_ALLOCATOR
  11. #define JPH_ALLOC_FN(x) x
  12. #define JPH_ALLOC_SCOPE
  13. #else
  14. #define JPH_ALLOC_FN(x) x##Impl
  15. #define JPH_ALLOC_SCOPE static
  16. #endif
  17. JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(Allocate)(size_t inSize)
  18. {
  19. return malloc(inSize);
  20. }
  21. JPH_ALLOC_SCOPE void JPH_ALLOC_FN(Free)(void *inBlock)
  22. {
  23. free(inBlock);
  24. }
  25. JPH_ALLOC_SCOPE void *JPH_ALLOC_FN(AlignedAllocate)(size_t inSize, size_t inAlignment)
  26. {
  27. #if defined(JPH_PLATFORM_WINDOWS)
  28. // Microsoft doesn't implement C++17 std::aligned_alloc
  29. return _aligned_malloc(inSize, inAlignment);
  30. #elif defined(JPH_PLATFORM_ANDROID)
  31. return memalign(inAlignment, AlignUp(inSize, inAlignment));
  32. #else
  33. // Using aligned_alloc instead of std::aligned_alloc because the latter is not available on some macOS versions
  34. return aligned_alloc(inAlignment, AlignUp(inSize, inAlignment));
  35. #endif
  36. }
  37. JPH_ALLOC_SCOPE void JPH_ALLOC_FN(AlignedFree)(void *inBlock)
  38. {
  39. #if defined(JPH_PLATFORM_WINDOWS)
  40. _aligned_free(inBlock);
  41. #elif defined(JPH_PLATFORM_ANDROID)
  42. free(inBlock);
  43. #else
  44. free(inBlock);
  45. #endif
  46. }
  47. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  48. AllocateFunction Allocate = nullptr;
  49. FreeFunction Free = nullptr;
  50. AlignedAllocateFunction AlignedAllocate = nullptr;
  51. AlignedFreeFunction AlignedFree = nullptr;
  52. void RegisterDefaultAllocator()
  53. {
  54. Allocate = AllocateImpl;
  55. Free = FreeImpl;
  56. AlignedAllocate = AlignedAllocateImpl;
  57. AlignedFree = AlignedFreeImpl;
  58. }
  59. #endif // JPH_DISABLE_CUSTOM_ALLOCATOR
  60. JPH_NAMESPACE_END