STLAlignedAllocator.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. /// STL allocator that takes care that memory is aligned to N bytes
  7. template <typename T, size_t N>
  8. class STLAlignedAllocator
  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. /// The allocator is stateless
  22. using is_always_equal = std::true_type;
  23. /// Allocator supports moving
  24. using propagate_on_container_move_assignment = std::true_type;
  25. /// Constructor
  26. inline STLAlignedAllocator() = default;
  27. /// Constructor from other allocator
  28. template <typename T2>
  29. inline explicit STLAlignedAllocator(const STLAlignedAllocator<T2, N> &) { }
  30. /// Allocate memory
  31. inline pointer allocate(size_type inN)
  32. {
  33. return (pointer)AlignedAllocate(inN * sizeof(value_type), N);
  34. }
  35. /// Free memory
  36. inline void deallocate(pointer inPointer, size_type)
  37. {
  38. AlignedFree(inPointer);
  39. }
  40. /// Allocators are stateless so assumed to be equal
  41. inline bool operator == (const STLAlignedAllocator<T, N> &) const
  42. {
  43. return true;
  44. }
  45. inline bool operator != (const STLAlignedAllocator<T, N> &) const
  46. {
  47. return false;
  48. }
  49. /// Converting to allocator for other type
  50. template <typename T2>
  51. struct rebind
  52. {
  53. using other = STLAlignedAllocator<T2, N>;
  54. };
  55. };
  56. JPH_NAMESPACE_END