2
0

Memory.cpp 1.6 KB

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