blob.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #pragma once
  2. #include "ref.h"
  3. #include "ref_counted.h"
  4. #include <library/cpp/yt/misc/port.h>
  5. namespace NYT {
  6. ////////////////////////////////////////////////////////////////////////////////
  7. //! Default memory tag for TBlob.
  8. struct TDefaultBlobTag
  9. { };
  10. //! A home-grown optimized replacement for |std::vector<char>| suitable for carrying
  11. //! large chunks of data.
  12. /*!
  13. * Compared to |std::vector<char>|, this class supports uninitialized allocations
  14. * when explicitly requested to.
  15. */
  16. class TBlob
  17. {
  18. public:
  19. //! Constructs a blob with a given size.
  20. explicit TBlob(
  21. TRefCountedTypeCookie tagCookie = GetRefCountedTypeCookie<TDefaultBlobTag>(),
  22. size_t size = 0,
  23. bool initiailizeStorage = true,
  24. bool pageAligned = false);
  25. //! Copies a chunk of memory into a new instance.
  26. TBlob(
  27. TRefCountedTypeCookie tagCookie,
  28. TRef data,
  29. bool pageAligned = false);
  30. //! Copies the data.
  31. TBlob(const TBlob& other);
  32. //! Moves the data (takes the ownership).
  33. TBlob(TBlob&& other) noexcept;
  34. //! Reclaims the memory.
  35. ~TBlob();
  36. //! Ensures that capacity is at least #capacity.
  37. void Reserve(size_t newCapacity);
  38. //! Changes the size to #newSize.
  39. /*!
  40. * If #size exceeds the current capacity,
  41. * we make sure the new capacity grows exponentially.
  42. * Hence calling #Resize N times to increase the size by N only
  43. * takes amortized O(1) time per call.
  44. */
  45. void Resize(size_t newSize, bool initializeStorage = true);
  46. //! Returns the start pointer.
  47. Y_FORCE_INLINE const char* Begin() const
  48. {
  49. return Begin_;
  50. }
  51. //! Returns the start pointer.
  52. Y_FORCE_INLINE char* Begin()
  53. {
  54. return Begin_;
  55. }
  56. //! Returns the end pointer.
  57. Y_FORCE_INLINE const char* End() const
  58. {
  59. return Begin_ + Size_;
  60. }
  61. //! Returns the end pointer.
  62. Y_FORCE_INLINE char* End()
  63. {
  64. return Begin_ + Size_;
  65. }
  66. //! Returns the size.
  67. Y_FORCE_INLINE size_t size() const
  68. {
  69. return Size_;
  70. }
  71. //! Returns the size.
  72. Y_FORCE_INLINE size_t Size() const
  73. {
  74. return Size_;
  75. }
  76. //! Returns the capacity.
  77. Y_FORCE_INLINE size_t Capacity() const
  78. {
  79. return Capacity_;
  80. }
  81. //! Returns the TStringBuf instance for the occupied part of the blob.
  82. Y_FORCE_INLINE TStringBuf ToStringBuf() const
  83. {
  84. return TStringBuf(Begin_, Size_);
  85. }
  86. //! Returns the TRef instance for the occupied part of the blob.
  87. Y_FORCE_INLINE TRef ToRef() const
  88. {
  89. return TRef(Begin_, Size_);
  90. }
  91. //! Provides by-value access to the underlying storage.
  92. Y_FORCE_INLINE char operator [] (size_t index) const
  93. {
  94. return Begin_[index];
  95. }
  96. //! Provides by-ref access to the underlying storage.
  97. Y_FORCE_INLINE char& operator [] (size_t index)
  98. {
  99. return Begin_[index];
  100. }
  101. //! Clears the instance but does not reclaim the memory.
  102. Y_FORCE_INLINE void Clear()
  103. {
  104. Size_ = 0;
  105. }
  106. //! Returns |true| if size is zero.
  107. Y_FORCE_INLINE bool IsEmpty() const
  108. {
  109. return Size_ == 0;
  110. }
  111. //! Overwrites the current instance.
  112. TBlob& operator = (const TBlob& rhs);
  113. //! Takes the ownership.
  114. TBlob& operator = (TBlob&& rhs) noexcept;
  115. //! Appends a chunk of memory to the end.
  116. void Append(const void* data, size_t size);
  117. //! Appends a chunk of memory to the end.
  118. void Append(TRef ref);
  119. //! Appends a single char to the end.
  120. void Append(char ch);
  121. //! Swaps the current and other instances
  122. void Swap(TBlob& other);
  123. friend void swap(TBlob& left, TBlob& right);
  124. private:
  125. char* Begin_ = nullptr;
  126. size_t Size_ = 0;
  127. size_t Capacity_ = 0;
  128. bool PageAligned_ = false;
  129. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  130. TRefCountedTypeCookie TagCookie_ = NullRefCountedTypeCookie;
  131. #endif
  132. char* DoAllocate(size_t newCapacity);
  133. void Allocate(size_t newCapacity);
  134. void Reallocate(size_t newCapacity);
  135. void Free();
  136. void Reset();
  137. void SetTagCookie(TRefCountedTypeCookie tagCookie);
  138. void SetTagCookie(const TBlob& other);
  139. };
  140. void swap(TBlob& left, TBlob& right);
  141. ////////////////////////////////////////////////////////////////////////////////
  142. } // namespace NYT