blob.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "blob.h"
  2. #include "ref.h"
  3. #include <library/cpp/yt/malloc/malloc.h>
  4. namespace NYT {
  5. ////////////////////////////////////////////////////////////////////////////////
  6. static constexpr size_t InitialBlobCapacity = 16;
  7. static constexpr double BlobCapacityMultiplier = 1.5;
  8. TBlob::TBlob(
  9. TRefCountedTypeCookie tagCookie,
  10. size_t size,
  11. bool initializeStorage,
  12. bool pageAligned)
  13. : PageAligned_(pageAligned)
  14. {
  15. SetTagCookie(tagCookie);
  16. if (size == 0) {
  17. Reset();
  18. } else {
  19. Allocate(std::max(size, InitialBlobCapacity));
  20. Size_ = size;
  21. if (initializeStorage) {
  22. ::memset(Begin_, 0, Size_);
  23. }
  24. }
  25. }
  26. TBlob::TBlob(
  27. TRefCountedTypeCookie tagCookie,
  28. TRef data,
  29. bool pageAligned)
  30. : PageAligned_(pageAligned)
  31. {
  32. SetTagCookie(tagCookie);
  33. Reset();
  34. Append(data);
  35. }
  36. TBlob::TBlob(const TBlob& other)
  37. : PageAligned_(other.PageAligned_)
  38. {
  39. SetTagCookie(other);
  40. if (other.Size_ == 0) {
  41. Reset();
  42. } else {
  43. Allocate(std::max(InitialBlobCapacity, other.Size_));
  44. ::memcpy(Begin_, other.Begin_, other.Size_);
  45. Size_ = other.Size_;
  46. }
  47. }
  48. TBlob::TBlob(TBlob&& other) noexcept
  49. : Begin_(other.Begin_)
  50. , Size_(other.Size_)
  51. , Capacity_(other.Capacity_)
  52. , PageAligned_(other.PageAligned_)
  53. {
  54. SetTagCookie(other);
  55. other.Reset();
  56. }
  57. TBlob::~TBlob()
  58. {
  59. Free();
  60. }
  61. void TBlob::Reserve(size_t newCapacity)
  62. {
  63. if (newCapacity > Capacity_) {
  64. Reallocate(newCapacity);
  65. }
  66. }
  67. void TBlob::Resize(size_t newSize, bool initializeStorage)
  68. {
  69. if (newSize > Size_) {
  70. if (newSize > Capacity_) {
  71. size_t newCapacity;
  72. if (Capacity_ == 0) {
  73. newCapacity = std::max(InitialBlobCapacity, newSize);
  74. } else {
  75. newCapacity = std::max(static_cast<size_t>(Capacity_ * BlobCapacityMultiplier), newSize);
  76. }
  77. Reallocate(newCapacity);
  78. }
  79. if (initializeStorage) {
  80. ::memset(Begin_ + Size_, 0, newSize - Size_);
  81. }
  82. }
  83. Size_ = newSize;
  84. }
  85. TBlob& TBlob::operator = (const TBlob& rhs)
  86. {
  87. if (this != &rhs) {
  88. this->~TBlob();
  89. new(this) TBlob(rhs);
  90. }
  91. return *this;
  92. }
  93. TBlob& TBlob::operator = (TBlob&& rhs) noexcept
  94. {
  95. if (this != &rhs) {
  96. this->~TBlob();
  97. new(this) TBlob(std::move(rhs));
  98. }
  99. return *this;
  100. }
  101. void TBlob::Append(const void* data, size_t size)
  102. {
  103. if (Size_ + size > Capacity_) {
  104. Resize(Size_ + size, false);
  105. ::memcpy(Begin_ + Size_ - size, data, size);
  106. } else {
  107. ::memcpy(Begin_ + Size_, data, size);
  108. Size_ += size;
  109. }
  110. }
  111. void TBlob::Append(TRef ref)
  112. {
  113. Append(ref.Begin(), ref.Size());
  114. }
  115. void TBlob::Append(char ch)
  116. {
  117. if (Size_ + 1 > Capacity_) {
  118. Resize(Size_ + 1, false);
  119. Begin_[Size_ - 1] = ch;
  120. } else {
  121. Begin_[Size_++] = ch;
  122. }
  123. }
  124. void TBlob::Reset()
  125. {
  126. Begin_ = nullptr;
  127. Size_ = Capacity_ = 0;
  128. }
  129. char* TBlob::DoAllocate(size_t size)
  130. {
  131. return static_cast<char*>(PageAligned_
  132. ? ::aligned_malloc(size, GetPageSize())
  133. : ::malloc(size));
  134. }
  135. void TBlob::Allocate(size_t newCapacity)
  136. {
  137. YT_VERIFY(!Begin_);
  138. Begin_ = DoAllocate(newCapacity);
  139. Capacity_ = newCapacity;
  140. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  141. TRefCountedTrackerFacade::AllocateTagInstance(TagCookie_);
  142. TRefCountedTrackerFacade::AllocateSpace(TagCookie_, newCapacity);
  143. #endif
  144. }
  145. void TBlob::Reallocate(size_t newCapacity)
  146. {
  147. if (!Begin_) {
  148. Allocate(newCapacity);
  149. return;
  150. }
  151. char* newBegin = DoAllocate(newCapacity);
  152. ::memcpy(newBegin, Begin_, Size_);
  153. ::free(Begin_);
  154. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  155. TRefCountedTrackerFacade::AllocateSpace(TagCookie_, newCapacity);
  156. TRefCountedTrackerFacade::FreeSpace(TagCookie_, Capacity_);
  157. #endif
  158. Begin_ = newBegin;
  159. Capacity_ = newCapacity;
  160. }
  161. void TBlob::Free()
  162. {
  163. if (!Begin_) {
  164. return;
  165. }
  166. ::free(Begin_);
  167. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  168. TRefCountedTrackerFacade::FreeTagInstance(TagCookie_);
  169. TRefCountedTrackerFacade::FreeSpace(TagCookie_, Capacity_);
  170. #endif
  171. Reset();
  172. }
  173. void TBlob::SetTagCookie(TRefCountedTypeCookie tagCookie)
  174. {
  175. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  176. TagCookie_ = tagCookie;
  177. #endif
  178. }
  179. void TBlob::SetTagCookie(const TBlob& other)
  180. {
  181. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  182. TagCookie_ = other.TagCookie_;
  183. #endif
  184. }
  185. void swap(TBlob& left, TBlob& right)
  186. {
  187. if (&left != &right) {
  188. std::swap(left.Begin_, right.Begin_);
  189. std::swap(left.Size_, right.Size_);
  190. std::swap(left.Capacity_, right.Capacity_);
  191. std::swap(left.PageAligned_, right.PageAligned_);
  192. #ifdef YT_ENABLE_REF_COUNTED_TRACKING
  193. std::swap(left.TagCookie_, right.TagCookie_);
  194. #endif
  195. }
  196. }
  197. ////////////////////////////////////////////////////////////////////////////////
  198. } // namespace NYT