Memory.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. return std::aligned_alloc(inAlignment, AlignUp(inSize, inAlignment));
  34. #endif
  35. }
  36. JPH_ALLOC_SCOPE void JPH_ALLOC_FN(AlignedFree)(void *inBlock)
  37. {
  38. #if defined(JPH_PLATFORM_WINDOWS)
  39. _aligned_free(inBlock);
  40. #elif defined(JPH_PLATFORM_ANDROID)
  41. free(inBlock);
  42. #else
  43. std::free(inBlock);
  44. #endif
  45. }
  46. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  47. AllocateFunction Allocate = nullptr;
  48. FreeFunction Free = nullptr;
  49. AlignedAllocateFunction AlignedAllocate = nullptr;
  50. AlignedFreeFunction AlignedFree = nullptr;
  51. void RegisterDefaultAllocator()
  52. {
  53. Allocate = AllocateImpl;
  54. Free = FreeImpl;
  55. AlignedAllocate = AlignedAllocateImpl;
  56. AlignedFree = AlignedFreeImpl;
  57. }
  58. #endif // JPH_DISABLE_CUSTOM_ALLOCATOR
  59. JPH_NAMESPACE_END