MemoryStream.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
  3. * Copyright (c) 2022, Tim Schumacher <timschumi@gmx.de>.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/FixedArray.h>
  8. #include <AK/MemMem.h>
  9. #include <LibCore/MemoryStream.h>
  10. namespace Core::Stream {
  11. FixedMemoryStream::FixedMemoryStream(Bytes bytes)
  12. : m_bytes(bytes)
  13. {
  14. }
  15. FixedMemoryStream::FixedMemoryStream(ReadonlyBytes bytes)
  16. : m_bytes({ const_cast<u8*>(bytes.data()), bytes.size() })
  17. , m_writing_enabled(false)
  18. {
  19. }
  20. ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(Bytes bytes)
  21. {
  22. return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
  23. }
  24. ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(ReadonlyBytes bytes)
  25. {
  26. return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
  27. }
  28. bool FixedMemoryStream::is_eof() const
  29. {
  30. return m_offset >= m_bytes.size();
  31. }
  32. bool FixedMemoryStream::is_open() const
  33. {
  34. return true;
  35. }
  36. void FixedMemoryStream::close()
  37. {
  38. // FIXME: It doesn't make sense to close a memory stream. Therefore, we don't do anything here. Is that fine?
  39. }
  40. ErrorOr<void> FixedMemoryStream::truncate(off_t)
  41. {
  42. return Error::from_errno(EBADF);
  43. }
  44. ErrorOr<Bytes> FixedMemoryStream::read(Bytes bytes)
  45. {
  46. auto to_read = min(remaining(), bytes.size());
  47. if (to_read == 0)
  48. return Bytes {};
  49. m_bytes.slice(m_offset, to_read).copy_to(bytes);
  50. m_offset += to_read;
  51. return bytes.trim(to_read);
  52. }
  53. ErrorOr<off_t> FixedMemoryStream::seek(i64 offset, SeekMode seek_mode)
  54. {
  55. switch (seek_mode) {
  56. case SeekMode::SetPosition:
  57. if (offset > static_cast<i64>(m_bytes.size()))
  58. return Error::from_string_literal("Offset past the end of the stream memory");
  59. m_offset = offset;
  60. break;
  61. case SeekMode::FromCurrentPosition:
  62. if (offset + static_cast<i64>(m_offset) > static_cast<i64>(m_bytes.size()))
  63. return Error::from_string_literal("Offset past the end of the stream memory");
  64. m_offset += offset;
  65. break;
  66. case SeekMode::FromEndPosition:
  67. if (offset > static_cast<i64>(m_bytes.size()))
  68. return Error::from_string_literal("Offset past the start of the stream memory");
  69. m_offset = m_bytes.size() - offset;
  70. break;
  71. }
  72. return static_cast<off_t>(m_offset);
  73. }
  74. ErrorOr<size_t> FixedMemoryStream::write(ReadonlyBytes bytes)
  75. {
  76. VERIFY(m_writing_enabled);
  77. // FIXME: Can this not error?
  78. auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
  79. m_offset += nwritten;
  80. return nwritten;
  81. }
  82. ErrorOr<void> FixedMemoryStream::write_entire_buffer(ReadonlyBytes bytes)
  83. {
  84. if (remaining() < bytes.size())
  85. return Error::from_string_literal("Write of entire buffer ends past the memory area");
  86. TRY(write(bytes));
  87. return {};
  88. }
  89. Bytes FixedMemoryStream::bytes()
  90. {
  91. VERIFY(m_writing_enabled);
  92. return m_bytes;
  93. }
  94. ReadonlyBytes FixedMemoryStream::bytes() const
  95. {
  96. return m_bytes;
  97. }
  98. size_t FixedMemoryStream::offset() const
  99. {
  100. return m_offset;
  101. }
  102. size_t FixedMemoryStream::remaining() const
  103. {
  104. return m_bytes.size() - m_offset;
  105. }
  106. ErrorOr<Bytes> AllocatingMemoryStream::read(Bytes bytes)
  107. {
  108. size_t read_bytes = 0;
  109. while (read_bytes < bytes.size()) {
  110. VERIFY(m_write_offset >= m_read_offset);
  111. auto range = TRY(next_read_range());
  112. if (range.size() == 0)
  113. break;
  114. auto copied_bytes = range.copy_trimmed_to(bytes.slice(read_bytes));
  115. read_bytes += copied_bytes;
  116. m_read_offset += copied_bytes;
  117. }
  118. cleanup_unused_chunks();
  119. return bytes.trim(read_bytes);
  120. }
  121. ErrorOr<size_t> AllocatingMemoryStream::write(ReadonlyBytes bytes)
  122. {
  123. size_t written_bytes = 0;
  124. while (written_bytes < bytes.size()) {
  125. VERIFY(m_write_offset >= m_read_offset);
  126. auto range = TRY(next_write_range());
  127. auto copied_bytes = bytes.slice(written_bytes).copy_trimmed_to(range);
  128. written_bytes += copied_bytes;
  129. m_write_offset += copied_bytes;
  130. }
  131. return written_bytes;
  132. }
  133. ErrorOr<void> AllocatingMemoryStream::discard(size_t count)
  134. {
  135. VERIFY(m_write_offset >= m_read_offset);
  136. if (count > used_buffer_size())
  137. return Error::from_string_literal("Number of discarded bytes is higher than the number of allocated bytes");
  138. m_read_offset += count;
  139. cleanup_unused_chunks();
  140. return {};
  141. }
  142. bool AllocatingMemoryStream::is_eof() const
  143. {
  144. return used_buffer_size() == 0;
  145. }
  146. bool AllocatingMemoryStream::is_open() const
  147. {
  148. return true;
  149. }
  150. void AllocatingMemoryStream::close()
  151. {
  152. }
  153. size_t AllocatingMemoryStream::used_buffer_size() const
  154. {
  155. return m_write_offset - m_read_offset;
  156. }
  157. ErrorOr<Optional<size_t>> AllocatingMemoryStream::offset_of(ReadonlyBytes needle) const
  158. {
  159. VERIFY(m_write_offset >= m_read_offset);
  160. if (m_chunks.size() == 0)
  161. return Optional<size_t> {};
  162. // Ensure that we don't have to trim away more than one block.
  163. VERIFY(m_read_offset < chunk_size);
  164. VERIFY(m_chunks.size() * chunk_size - m_write_offset < chunk_size);
  165. auto chunk_count = m_chunks.size();
  166. auto search_spans = TRY(FixedArray<ReadonlyBytes>::try_create(chunk_count));
  167. for (size_t i = 0; i < chunk_count; i++) {
  168. search_spans[i] = m_chunks[i].span();
  169. }
  170. // Trimming is done first to ensure that we don't unintentionally shift around if the first and last chunks are the same.
  171. search_spans[chunk_count - 1] = search_spans[chunk_count - 1].trim(chunk_count * chunk_size - m_write_offset);
  172. search_spans[0] = search_spans[0].slice(m_read_offset);
  173. return AK::memmem(search_spans.begin(), search_spans.end(), needle);
  174. }
  175. ErrorOr<ReadonlyBytes> AllocatingMemoryStream::next_read_range()
  176. {
  177. VERIFY(m_write_offset >= m_read_offset);
  178. size_t const chunk_index = m_read_offset / chunk_size;
  179. size_t const chunk_offset = m_read_offset % chunk_size;
  180. size_t const read_size = min(chunk_size - m_read_offset % chunk_size, m_write_offset - m_read_offset);
  181. if (read_size == 0)
  182. return ReadonlyBytes { static_cast<u8*>(nullptr), 0 };
  183. VERIFY(chunk_index < m_chunks.size());
  184. return ReadonlyBytes { m_chunks[chunk_index].data() + chunk_offset, read_size };
  185. }
  186. ErrorOr<Bytes> AllocatingMemoryStream::next_write_range()
  187. {
  188. VERIFY(m_write_offset >= m_read_offset);
  189. size_t const chunk_index = m_write_offset / chunk_size;
  190. size_t const chunk_offset = m_write_offset % chunk_size;
  191. size_t const write_size = chunk_size - m_write_offset % chunk_size;
  192. if (chunk_index >= m_chunks.size())
  193. TRY(m_chunks.try_append(TRY(Chunk::create_uninitialized(chunk_size))));
  194. VERIFY(chunk_index < m_chunks.size());
  195. return Bytes { m_chunks[chunk_index].data() + chunk_offset, write_size };
  196. }
  197. void AllocatingMemoryStream::cleanup_unused_chunks()
  198. {
  199. // FIXME: Move these all at once.
  200. while (m_read_offset >= chunk_size) {
  201. VERIFY(m_write_offset >= m_read_offset);
  202. auto buffer = m_chunks.take_first();
  203. m_read_offset -= chunk_size;
  204. m_write_offset -= chunk_size;
  205. m_chunks.append(move(buffer));
  206. }
  207. }
  208. }