tls_scratch-inl.h 820 B

123456789101112131415161718192021222324252627
  1. #ifndef TLS_SCRATH_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include tls_scratch.h"
  3. // For the sake of sane code completion.
  4. #include "tls_scratch.h"
  5. #endif
  6. #include <library/cpp/yt/misc/tls.h>
  7. namespace NYT {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. template <class T>
  10. YT_PREVENT_TLS_CACHING TMutableRange<T> GetTlsScratchBuffer(size_t size)
  11. {
  12. // This is a workround for std::vector<bool>.
  13. using TBoxed = std::array<T, 1>;
  14. thread_local std::vector<TBoxed> tlsVector;
  15. tlsVector.reserve(size);
  16. auto range = TMutableRange(reinterpret_cast<T*>(tlsVector.data()), size);
  17. std::fill(range.begin(), range.end(), T());
  18. return range;
  19. }
  20. ////////////////////////////////////////////////////////////////////////////////
  21. } // namespace NYT