mem.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. #pragma once
  2. #include "zerocopy.h"
  3. #include "zerocopy_output.h"
  4. #include <util/generic/strbuf.h>
  5. /**
  6. * @addtogroup Streams_Memory
  7. * @{
  8. */
  9. /**
  10. * Input stream that reads data from a memory block.
  11. */
  12. class TMemoryInput: public IZeroCopyInputFastReadTo {
  13. public:
  14. TMemoryInput() noexcept;
  15. /**
  16. * Constructs a stream that reads from the provided memory block. It's up
  17. * to the user to make sure that the memory block doesn't get freed while
  18. * this stream is in use.
  19. *
  20. * @param buf Memory block to use.
  21. * @param len Size of the memory block.
  22. */
  23. TMemoryInput(const void* buf, size_t len) noexcept;
  24. TMemoryInput(TString&&) = delete;
  25. explicit TMemoryInput(const TStringBuf buf) noexcept;
  26. explicit TMemoryInput(const char* zstr)
  27. : TMemoryInput(TStringBuf(zstr))
  28. {
  29. }
  30. ~TMemoryInput() override;
  31. TMemoryInput(const TMemoryInput& other) noexcept
  32. : IZeroCopyInputFastReadTo()
  33. , Buf_(other.Buf_)
  34. , Len_(other.Len_)
  35. {
  36. }
  37. TMemoryInput& operator=(const TMemoryInput& other) noexcept {
  38. if (this != &other) {
  39. Buf_ = other.Buf_;
  40. Len_ = other.Len_;
  41. }
  42. return *this;
  43. }
  44. TMemoryInput(TMemoryInput&&) noexcept = default;
  45. TMemoryInput& operator=(TMemoryInput&&) noexcept = default;
  46. /**
  47. * Initializes this stream with a new memory block. It's up to the
  48. * user to make sure that the memory block doesn't get freed while this
  49. * stream is in use.
  50. *
  51. * @param buf New memory block to use.
  52. * @param len Size of the new memory block.
  53. */
  54. void Reset(const void* buf, size_t len) noexcept {
  55. Buf_ = (const char*)buf;
  56. Len_ = len;
  57. }
  58. /**
  59. * @returns Whether there is more data in the stream.
  60. */
  61. bool Exhausted() const noexcept {
  62. return !Avail();
  63. }
  64. /**
  65. * @returns Number of bytes available in the stream.
  66. */
  67. size_t Avail() const noexcept {
  68. return Len_;
  69. }
  70. /**
  71. * @returns Current read position in the memory block
  72. * used by this stream.
  73. */
  74. const char* Buf() const noexcept {
  75. return Buf_;
  76. }
  77. /**
  78. * Initializes this stream with a next chunk extracted from the given zero
  79. * copy stream.
  80. *
  81. * @param stream Zero copy stream to initialize from.
  82. */
  83. void Fill(IZeroCopyInput* stream) {
  84. Len_ = stream->Next(&Buf_);
  85. if (!Len_) {
  86. Reset(nullptr, 0);
  87. }
  88. }
  89. private:
  90. size_t DoNext(const void** ptr, size_t len) override;
  91. void DoUndo(size_t len) override;
  92. private:
  93. const char* Buf_;
  94. size_t Len_;
  95. };
  96. /**
  97. * Output stream that writes data to a memory block.
  98. */
  99. class TMemoryOutput: public IZeroCopyOutput {
  100. public:
  101. /**
  102. * Constructs a stream that writes to the provided memory block. It's up
  103. * to the user to make sure that the memory block doesn't get freed while
  104. * this stream is in use.
  105. *
  106. * @param buf Memory block to use.
  107. * @param len Size of the memory block.
  108. */
  109. TMemoryOutput(void* buf, size_t len) noexcept
  110. : Buf_(static_cast<char*>(buf))
  111. , End_(Buf_ + len)
  112. {
  113. }
  114. ~TMemoryOutput() override;
  115. TMemoryOutput(TMemoryOutput&&) noexcept = default;
  116. TMemoryOutput& operator=(TMemoryOutput&&) noexcept = default;
  117. /**
  118. * Initializes this stream with a new memory block. It's up to the
  119. * user to make sure that the memory block doesn't get freed while this
  120. * stream is in use.
  121. *
  122. * @param buf New memory block to use.
  123. * @param len Size of the new memory block.
  124. */
  125. inline void Reset(void* buf, size_t len) noexcept {
  126. Buf_ = static_cast<char*>(buf);
  127. End_ = Buf_ + len;
  128. }
  129. /**
  130. * @returns Whether there is more space in the
  131. * stream for writing.
  132. */
  133. inline bool Exhausted() const noexcept {
  134. return !Avail();
  135. }
  136. /**
  137. * @returns Number of bytes available for writing
  138. * in the stream.
  139. */
  140. inline size_t Avail() const noexcept {
  141. return End_ - Buf_;
  142. }
  143. /**
  144. * @returns Current write position in the memory block
  145. * used by this stream.
  146. */
  147. inline char* Buf() const noexcept {
  148. return Buf_;
  149. }
  150. /**
  151. * @returns Pointer to the end of the memory block
  152. * used by this stream.
  153. */
  154. char* End() const {
  155. return End_;
  156. }
  157. private:
  158. size_t DoNext(void** ptr) override;
  159. void DoUndo(size_t len) override;
  160. void DoWrite(const void* buf, size_t len) override;
  161. void DoWriteC(char c) override;
  162. protected:
  163. char* Buf_;
  164. char* End_;
  165. };
  166. /**
  167. * Memory output stream that supports changing the position of the
  168. * write pointer.
  169. *
  170. * @see TMemoryOutput
  171. */
  172. class TMemoryWriteBuffer: public TMemoryOutput {
  173. public:
  174. TMemoryWriteBuffer(void* buf, size_t len)
  175. : TMemoryOutput(buf, len)
  176. , Beg_(Buf_)
  177. {
  178. }
  179. void Reset(void* buf, size_t len) {
  180. TMemoryOutput::Reset(buf, len);
  181. Beg_ = Buf_;
  182. }
  183. size_t Len() const {
  184. return Buf() - Beg();
  185. }
  186. size_t Empty() const {
  187. return Buf() == Beg();
  188. }
  189. /**
  190. * @returns Data that has been written into this
  191. * stream as a string.
  192. */
  193. TStringBuf Str() const {
  194. return TStringBuf(Beg(), Buf());
  195. }
  196. char* Beg() const {
  197. return Beg_;
  198. }
  199. /**
  200. * @param ptr New write position for this stream.
  201. * Must be inside the memory block that
  202. * this stream uses.
  203. */
  204. void SetPos(char* ptr) {
  205. Y_ASSERT(Beg_ <= ptr);
  206. SetPosImpl(ptr);
  207. }
  208. /**
  209. * @param pos New write position for this stream,
  210. * relative to the beginning of the memory
  211. * block that this stream uses.
  212. */
  213. void SetPos(size_t pos) {
  214. SetPosImpl(Beg_ + pos);
  215. }
  216. protected:
  217. void SetPosImpl(char* ptr) {
  218. Y_ASSERT(End_ >= ptr);
  219. Buf_ = ptr;
  220. }
  221. protected:
  222. char* Beg_;
  223. };
  224. /** @} */