Memory.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 posix_memalign
  29. return _aligned_malloc(inSize, inAlignment);
  30. #else
  31. void *block = nullptr;
  32. JPH_SUPPRESS_WARNING_PUSH
  33. JPH_GCC_SUPPRESS_WARNING("-Wunused-result")
  34. JPH_CLANG_SUPPRESS_WARNING("-Wunused-result")
  35. posix_memalign(&block, inAlignment, inSize);
  36. JPH_SUPPRESS_WARNING_POP
  37. return block;
  38. #endif
  39. }
  40. JPH_ALLOC_SCOPE void JPH_ALLOC_FN(AlignedFree)(void *inBlock)
  41. {
  42. #if defined(JPH_PLATFORM_WINDOWS)
  43. _aligned_free(inBlock);
  44. #else
  45. free(inBlock);
  46. #endif
  47. }
  48. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  49. AllocateFunction Allocate = nullptr;
  50. FreeFunction Free = nullptr;
  51. AlignedAllocateFunction AlignedAllocate = nullptr;
  52. AlignedFreeFunction AlignedFree = nullptr;
  53. void RegisterDefaultAllocator()
  54. {
  55. Allocate = AllocateImpl;
  56. Free = FreeImpl;
  57. AlignedAllocate = AlignedAllocateImpl;
  58. AlignedFree = AlignedFreeImpl;
  59. }
  60. #endif // JPH_DISABLE_CUSTOM_ALLOCATOR
  61. JPH_NAMESPACE_END