STLAllocator.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. JPH_NAMESPACE_BEGIN
  6. #ifndef JPH_DISABLE_CUSTOM_ALLOCATOR
  7. /// STL allocator that forwards to our allocation functions
  8. template <typename T>
  9. class STLAllocator
  10. {
  11. public:
  12. using value_type = T;
  13. /// Pointer to type
  14. using pointer = T *;
  15. using const_pointer = const T *;
  16. /// Reference to type.
  17. /// Can be removed in C++20.
  18. using reference = T &;
  19. using const_reference = const T &;
  20. using size_type = size_t;
  21. using difference_type = ptrdiff_t;
  22. /// Constructor
  23. inline STLAllocator() = default;
  24. /// Constructor from other allocator
  25. template <typename T2>
  26. inline STLAllocator(const STLAllocator<T2> &) { }
  27. /// Allocate memory
  28. inline pointer allocate(size_type inN)
  29. {
  30. if constexpr (alignof(T) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16))
  31. return pointer(AlignedAllocate(inN * sizeof(value_type), alignof(T)));
  32. else
  33. return pointer(Allocate(inN * sizeof(value_type)));
  34. }
  35. /// Free memory
  36. inline void deallocate(pointer inPointer, size_type)
  37. {
  38. if constexpr (alignof(T) > (JPH_CPU_ADDRESS_BITS == 32? 8 : 16))
  39. AlignedFree(inPointer);
  40. else
  41. Free(inPointer);
  42. }
  43. /// Allocators are stateless so assumed to be equal
  44. inline bool operator == (const STLAllocator<T> &) const
  45. {
  46. return true;
  47. }
  48. inline bool operator != (const STLAllocator<T> &) const
  49. {
  50. return false;
  51. }
  52. /// Converting to allocator for other type
  53. template <typename T2>
  54. struct rebind
  55. {
  56. using other = STLAllocator<T2>;
  57. };
  58. };
  59. #else
  60. template <typename T> using STLAllocator = std::allocator<T>;
  61. #endif // !JPH_DISABLE_CUSTOM_ALLOCATOR
  62. // Declare STL containers that use our allocator
  63. template <class T> using Array = std::vector<T, STLAllocator<T>>;
  64. using String = std::basic_string<char, std::char_traits<char>, STLAllocator<char>>;
  65. using IStringStream = std::basic_istringstream<char, std::char_traits<char>, STLAllocator<char>>;
  66. JPH_NAMESPACE_END
  67. #if (!defined(JPH_PLATFORM_WINDOWS) || defined(JPH_COMPILER_MINGW)) && !defined(JPH_DISABLE_CUSTOM_ALLOCATOR)
  68. namespace std
  69. {
  70. /// Declare std::hash for String, for some reason on Linux based platforms template deduction takes the wrong variant
  71. template <>
  72. struct hash<JPH::String>
  73. {
  74. inline size_t operator () (const JPH::String &inRHS) const
  75. {
  76. return hash<string_view> { } (inRHS);
  77. }
  78. };
  79. }
  80. #endif // (!JPH_PLATFORM_WINDOWS || JPH_COMPILER_MINGW) && !JPH_DISABLE_CUSTOM_ALLOCATOR