Memory.cpp 1.6 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 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. posix_memalign(&block, inAlignment, inSize);
  35. JPH_SUPPRESS_WARNING_POP
  36. return block;
  37. #endif
  38. }
  39. JPH_ALLOC_SCOPE void JPH_ALLOC_FN(AlignedFree)(void *inBlock)
  40. {
  41. #if defined(JPH_PLATFORM_WINDOWS)
  42. _aligned_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