tls_scratch-inl.h 922 B

123456789101112131415161718192021222324252627282930
  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. #include <util/generic/bitops.h>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. template <class T>
  11. YT_PREVENT_TLS_CACHING TMutableRange<T> GetTlsScratchBuffer(size_t size)
  12. {
  13. thread_local std::unique_ptr<T[]> scratchBuffer;
  14. thread_local size_t scratchBufferSize;
  15. if (scratchBufferSize < size) {
  16. scratchBufferSize = FastClp2(size);
  17. scratchBuffer = std::unique_ptr<T[]>(new T[scratchBufferSize]);
  18. }
  19. std::fill(scratchBuffer.get(), scratchBuffer.get() + size, T());
  20. return TMutableRange(scratchBuffer.get(), size);
  21. }
  22. ////////////////////////////////////////////////////////////////////////////////
  23. } // namespace NYT