STLTempAllocator.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #pragma once
  5. #include <Jolt/Core/TempAllocator.h>
  6. JPH_NAMESPACE_BEGIN
  7. /// STL allocator that wraps around TempAllocator
  8. template <typename T>
  9. class STLTempAllocator
  10. {
  11. public:
  12. using value_type = T;
  13. /// Pointer to type
  14. using pointer = T *;
  15. using const_pointer = const T *;
  16. /// Reference to type.
  17. /// Can be removed in C++20.
  18. using reference = T &;
  19. using const_reference = const T &;
  20. using size_type = size_t;
  21. using difference_type = ptrdiff_t;
  22. /// Constructor
  23. inline STLTempAllocator(TempAllocator &inAllocator) : mAllocator(inAllocator) { }
  24. /// Constructor from other allocator
  25. template <typename T2>
  26. inline explicit STLTempAllocator(const STLTempAllocator<T2> &inRHS) : mAllocator(inRHS.GetAllocator()) { }
  27. /// Allocate memory
  28. inline pointer allocate(size_type inN)
  29. {
  30. return pointer(mAllocator.Allocate(uint(inN * sizeof(value_type))));
  31. }
  32. /// Free memory
  33. inline void deallocate(pointer inPointer, size_type inN)
  34. {
  35. mAllocator.Free(inPointer, uint(inN * sizeof(value_type)));
  36. }
  37. /// Allocators are stateless so assumed to be equal
  38. inline bool operator == (const STLTempAllocator<T> &) const
  39. {
  40. return true;
  41. }
  42. inline bool operator != (const STLTempAllocator<T> &) const
  43. {
  44. return false;
  45. }
  46. /// Converting to allocator for other type
  47. template <typename T2>
  48. struct rebind
  49. {
  50. using other = STLTempAllocator<T2>;
  51. };
  52. /// Get our temp allocator
  53. TempAllocator & GetAllocator() const
  54. {
  55. return mAllocator;
  56. }
  57. private:
  58. TempAllocator & mAllocator;
  59. };
  60. JPH_NAMESPACE_END