mem.h 6.5 KB

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