mkql_buffer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #pragma once
  2. #include "defs.h"
  3. #include <yql/essentials/utils/chunked_buffer.h>
  4. #include <util/generic/noncopyable.h>
  5. #include <util/stream/output.h>
  6. #include <util/system/yassert.h>
  7. namespace NKikimr {
  8. namespace NMiniKQL {
  9. class TPagedBuffer;
  10. class TBufferPage : private TNonCopyable {
  11. friend class TPagedBuffer;
  12. static const size_t PageCapacity;
  13. public:
  14. static const size_t PageAllocSize = 128 * 1024;
  15. TBufferPage() = default;
  16. ~TBufferPage() = default;
  17. inline const TBufferPage* Next() const {
  18. return Next_;
  19. }
  20. inline TBufferPage* Next() {
  21. return Next_;
  22. }
  23. inline size_t Size() const {
  24. return Size_;
  25. }
  26. inline char* Data() {
  27. return reinterpret_cast<char*>(this + 1);
  28. }
  29. inline const char* Data() const {
  30. return reinterpret_cast<const char*>(this + 1);
  31. }
  32. inline void Clear() {
  33. Size_ = 0;
  34. }
  35. private:
  36. TBufferPage* Next_ = nullptr;
  37. size_t Size_ = 0;
  38. static TBufferPage* Allocate();
  39. static void Free(TBufferPage* page);
  40. static inline const TBufferPage* GetPage(const char* data) {
  41. Y_DEBUG_ABORT_UNLESS(data);
  42. return reinterpret_cast<const TBufferPage*>(data - sizeof(TBufferPage));
  43. }
  44. static inline TBufferPage* GetPage(char* data) {
  45. Y_DEBUG_ABORT_UNLESS(data);
  46. return reinterpret_cast<TBufferPage*>(data - sizeof(TBufferPage));
  47. }
  48. };
  49. class TPagedBuffer : private TNonCopyable {
  50. public:
  51. using TPtr = std::shared_ptr<TPagedBuffer>;
  52. using TConstPtr = std::shared_ptr<const TPagedBuffer>;
  53. TPagedBuffer() = default;
  54. ~TPagedBuffer() {
  55. if (Head_) {
  56. TBufferPage* curr = TBufferPage::GetPage(Head_);
  57. while (curr) {
  58. auto drop = curr;
  59. curr = curr->Next_;
  60. TBufferPage::Free(drop);
  61. }
  62. }
  63. }
  64. template<typename TFunc>
  65. inline void ForEachPage(TFunc f) const {
  66. if (!Head_) {
  67. return;
  68. }
  69. const TBufferPage* head = TBufferPage::GetPage(Head_);
  70. auto page = head;
  71. auto end = TBufferPage::GetPage(Tail_);
  72. while (page) {
  73. const char* src;
  74. size_t len;
  75. if (page == end) {
  76. src = Tail_;
  77. len = TailSize_;
  78. } else {
  79. src = page->Data();
  80. len = page->Size();
  81. }
  82. if (page == head) {
  83. src += HeadReserve_;
  84. len -= HeadReserve_;
  85. }
  86. if (len) {
  87. f(src, len);
  88. }
  89. page = (page == end) ? nullptr : page->Next();
  90. }
  91. }
  92. inline size_t Size() const {
  93. // + (Tail_ ? TailSize_ : 0);
  94. size_t sizeWithReserve = ClosedPagesSize_ + ((-size_t(Tail_ != nullptr)) & TailSize_);
  95. Y_DEBUG_ABORT_UNLESS(sizeWithReserve >= HeadReserve_);
  96. return sizeWithReserve - HeadReserve_;
  97. }
  98. template<typename TContainer>
  99. inline void CopyTo(TContainer& out) const {
  100. ForEachPage([&out](const char* data, size_t len) {
  101. out.insert(out.end(), data, data + len);
  102. });
  103. }
  104. inline void CopyTo(char* dst) const {
  105. ForEachPage([&dst](const char* data, size_t len) {
  106. std::memcpy(dst, data, len);
  107. dst += len;
  108. });
  109. }
  110. inline void CopyTo(IOutputStream& out) const {
  111. ForEachPage([&out](const char* data, size_t len) {
  112. out.Write(data, len);
  113. });
  114. }
  115. size_t ReservedHeaderSize() const {
  116. return HeadReserve_;
  117. }
  118. inline void ReserveHeader(size_t len) {
  119. Y_DEBUG_ABORT_UNLESS(len > 0);
  120. Y_DEBUG_ABORT_UNLESS(Head_ == Tail_);
  121. Y_DEBUG_ABORT_UNLESS(HeadReserve_ == 0);
  122. Advance(len);
  123. HeadReserve_ = len;
  124. }
  125. char* Header(size_t len) {
  126. if (len > HeadReserve_) {
  127. return nullptr;
  128. }
  129. Y_DEBUG_ABORT_UNLESS(Head_);
  130. HeadReserve_ -= len;
  131. return Head_ + HeadReserve_;
  132. }
  133. // buffer-style operations with last page
  134. inline char* Pos() const {
  135. Y_DEBUG_ABORT_UNLESS(Tail_);
  136. return Tail_ + TailSize_;
  137. }
  138. inline void Clear() {
  139. Tail_ = Head_;
  140. ClosedPagesSize_ = HeadReserve_ = 0;
  141. // = Tail_ ? 0 : TBufferPage::PageAllocSize;
  142. TailSize_ = (-size_t(Tail_ == nullptr)) & TBufferPage::PageCapacity;
  143. }
  144. inline void EraseBack(size_t len) {
  145. Y_DEBUG_ABORT_UNLESS(Tail_ && TailSize_ >= len);
  146. TailSize_ -= len;
  147. }
  148. inline void Advance(size_t len) {
  149. if (Y_LIKELY(TailSize_ + len <= TBufferPage::PageCapacity)) {
  150. TailSize_ += len;
  151. return;
  152. }
  153. MKQL_ENSURE(len <= TBufferPage::PageCapacity, "Advance() size too big");
  154. AppendPage();
  155. TailSize_ = len;
  156. }
  157. inline void Append(char c) {
  158. Advance(1);
  159. *(Pos() - 1) = c;
  160. }
  161. inline void Append(const char* data, size_t size) {
  162. while (size) {
  163. if (TailSize_ == TBufferPage::PageCapacity) {
  164. AppendPage();
  165. }
  166. Y_DEBUG_ABORT_UNLESS(TailSize_ < TBufferPage::PageCapacity);
  167. size_t avail = TBufferPage::PageCapacity - TailSize_;
  168. size_t chunk = std::min(avail, size);
  169. std::memcpy(Pos(), data, chunk);
  170. TailSize_ += chunk;
  171. data += chunk;
  172. size -= chunk;
  173. }
  174. }
  175. static NYql::TChunkedBuffer AsChunkedBuffer(const TConstPtr& buf);
  176. private:
  177. void AppendPage();
  178. char* Head_ = nullptr;
  179. char* Tail_ = nullptr;
  180. // TailSize_ is initialized as if last page is full, this way we can simplifiy check in Advance()
  181. size_t TailSize_ = TBufferPage::PageCapacity;
  182. size_t HeadReserve_ = 0;
  183. size_t ClosedPagesSize_ = 0;
  184. };
  185. } // NMiniKQL
  186. } // NKikimr