STLAlignedAllocator.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. JPH_NAMESPACE_BEGIN
  5. /// STL allocator that takes care that memory is aligned to N bytes
  6. template <typename T, size_t N>
  7. class STLAlignedAllocator
  8. {
  9. public:
  10. using value_type = T;
  11. /// Pointer to type
  12. using pointer = T *;
  13. using const_pointer = const T *;
  14. /// Reference to type.
  15. /// Can be removed in C++20.
  16. using reference = T &;
  17. using const_reference = const T &;
  18. using size_type = size_t;
  19. using difference_type = ptrdiff_t;
  20. /// Constructor
  21. inline STLAlignedAllocator() = default;
  22. /// Constructor from other allocator
  23. template <typename T2>
  24. inline explicit STLAlignedAllocator(const STLAlignedAllocator<T2, N> &) { }
  25. /// Allocate memory
  26. inline pointer allocate(size_type inN)
  27. {
  28. return (pointer)AlignedAllocate(inN * sizeof(value_type), N);
  29. }
  30. /// Free memory
  31. inline void deallocate(pointer inPointer, size_type)
  32. {
  33. AlignedFree(inPointer);
  34. }
  35. /// Allocators are stateless so assumed to be equal
  36. inline bool operator == (const STLAlignedAllocator<T, N> &) const
  37. {
  38. return true;
  39. }
  40. inline bool operator != (const STLAlignedAllocator<T, N> &) const
  41. {
  42. return false;
  43. }
  44. /// Converting to allocator for other type
  45. template <typename T2>
  46. struct rebind
  47. {
  48. using other = STLAlignedAllocator<T2, N>;
  49. };
  50. };
  51. JPH_NAMESPACE_END