STLAllocator.h 2.4 KB

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