STLAlignedAllocator.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /// Constructor
  22. inline STLAlignedAllocator() = default;
  23. /// Constructor from other allocator
  24. template <typename T2>
  25. inline explicit STLAlignedAllocator(const STLAlignedAllocator<T2, N> &) { }
  26. /// Allocate memory
  27. inline pointer allocate(size_type inN)
  28. {
  29. return (pointer)AlignedAllocate(inN * sizeof(value_type), N);
  30. }
  31. /// Free memory
  32. inline void deallocate(pointer inPointer, size_type)
  33. {
  34. AlignedFree(inPointer);
  35. }
  36. /// Allocators are stateless so assumed to be equal
  37. inline bool operator == (const STLAlignedAllocator<T, N> &) const
  38. {
  39. return true;
  40. }
  41. inline bool operator != (const STLAlignedAllocator<T, N> &) const
  42. {
  43. return false;
  44. }
  45. /// Converting to allocator for other type
  46. template <typename T2>
  47. struct rebind
  48. {
  49. using other = STLAlignedAllocator<T2, N>;
  50. };
  51. };
  52. JPH_NAMESPACE_END