temp_tls_vector.h 804 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. #include "thread_extra.h"
  3. #include <util/generic/vector.h>
  4. #include <util/system/yassert.h>
  5. template <typename T, typename TTag = void, template <typename, class> class TVectorType = TVector>
  6. class TTempTlsVector {
  7. private:
  8. struct TTagForTls {};
  9. TVectorType<T, std::allocator<T>>* Vector;
  10. public:
  11. TVectorType<T, std::allocator<T>>* GetVector() {
  12. return Vector;
  13. }
  14. TTempTlsVector() {
  15. Vector = FastTlsSingletonWithTag<TVectorType<T, std::allocator<T>>, TTagForTls>();
  16. Y_ASSERT(Vector->empty());
  17. }
  18. ~TTempTlsVector() {
  19. Clear();
  20. }
  21. void Clear() {
  22. Vector->clear();
  23. }
  24. size_t Capacity() const noexcept {
  25. return Vector->capacity();
  26. }
  27. void Shrink() {
  28. Vector->shrink_to_fit();
  29. }
  30. };