blob.cpp 5.2 KB

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