STLTempAllocator.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Core/TempAllocator.h>
  5. JPH_NAMESPACE_BEGIN
  6. /// STL allocator that wraps around TempAllocator
  7. template <typename T>
  8. class STLTempAllocator
  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 STLTempAllocator(TempAllocator &inAllocator) : mAllocator(inAllocator) { }
  23. /// Constructor from other allocator
  24. template <typename T2>
  25. inline explicit STLTempAllocator(const STLTempAllocator<T2> &inRHS) : mAllocator(inRHS.GetAllocator()) { }
  26. /// Allocate memory
  27. inline pointer allocate(size_type inN)
  28. {
  29. return (pointer)mAllocator.Allocate(uint(inN * sizeof(value_type)));
  30. }
  31. /// Free memory
  32. inline void deallocate(pointer inPointer, size_type inN)
  33. {
  34. mAllocator.Free(inPointer, uint(inN * sizeof(value_type)));
  35. }
  36. /// Allocators are stateless so assumed to be equal
  37. inline bool operator == (const STLTempAllocator<T> &) const
  38. {
  39. return true;
  40. }
  41. inline bool operator != (const STLTempAllocator<T> &) const
  42. {
  43. return false;
  44. }
  45. /// Converting to allocator for other type
  46. template <typename T2>
  47. struct rebind
  48. {
  49. using other = STLTempAllocator<T2>;
  50. };
  51. /// Get our temp allocator
  52. TempAllocator & GetAllocator() const
  53. {
  54. return mAllocator;
  55. }
  56. private:
  57. TempAllocator & mAllocator;
  58. };
  59. JPH_NAMESPACE_END