From c6d71ca72757c1d69e608aa101e47818cf9991e1 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Wed, 7 Dec 2022 15:47:44 +0100 Subject: [PATCH] LibCore: Rename `MemoryStream` to `FixedMemoryStream` This is to differentiate between the upcoming `AllocatingMemoryStream`, which automatically allocates memory as needed instead of operating on a static memory area. --- Meta/Lagom/Fuzzers/FuzzBrotli.cpp | 2 +- Meta/Lagom/Fuzzers/FuzzTar.cpp | 2 +- Tests/LibCompress/TestDeflate.cpp | 4 ++-- Userland/Libraries/LibAudio/FlacLoader.cpp | 8 ++++---- Userland/Libraries/LibAudio/MP3Loader.cpp | 2 +- Userland/Libraries/LibAudio/WavLoader.cpp | 4 ++-- Userland/Libraries/LibCompress/Deflate.cpp | 2 +- Userland/Libraries/LibCompress/Gzip.cpp | 2 +- Userland/Libraries/LibCore/MemoryStream.h | 16 +++++++++------- Userland/Libraries/LibHTTP/Job.cpp | 2 +- Userland/Utilities/headless-browser.cpp | 12 ++++++------ 11 files changed, 29 insertions(+), 27 deletions(-) diff --git a/Meta/Lagom/Fuzzers/FuzzBrotli.cpp b/Meta/Lagom/Fuzzers/FuzzBrotli.cpp index 6011e664bd..142bdadc30 100644 --- a/Meta/Lagom/Fuzzers/FuzzBrotli.cpp +++ b/Meta/Lagom/Fuzzers/FuzzBrotli.cpp @@ -10,7 +10,7 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { - auto bufstream_result = Core::Stream::MemoryStream::construct({ data, size }); + auto bufstream_result = Core::Stream::FixedMemoryStream::construct({ data, size }); if (bufstream_result.is_error()) { dbgln("MemoryStream::construct() failed."); return 0; diff --git a/Meta/Lagom/Fuzzers/FuzzTar.cpp b/Meta/Lagom/Fuzzers/FuzzTar.cpp index c508faf3d0..ff529da4c0 100644 --- a/Meta/Lagom/Fuzzers/FuzzTar.cpp +++ b/Meta/Lagom/Fuzzers/FuzzTar.cpp @@ -12,7 +12,7 @@ extern "C" int LLVMFuzzerTestOneInput(uint8_t const* data, size_t size) { - auto input_stream_or_error = Core::Stream::MemoryStream::construct({ data, size }); + auto input_stream_or_error = Core::Stream::FixedMemoryStream::construct({ data, size }); if (input_stream_or_error.is_error()) return 0; diff --git a/Tests/LibCompress/TestDeflate.cpp b/Tests/LibCompress/TestDeflate.cpp index 5f6412eaee..b55ac633e9 100644 --- a/Tests/LibCompress/TestDeflate.cpp +++ b/Tests/LibCompress/TestDeflate.cpp @@ -29,7 +29,7 @@ TEST_CASE(canonical_code_simple) }; auto const huffman = Compress::CanonicalCode::from_bytes(code).value(); - auto memory_stream = MUST(Core::Stream::MemoryStream::construct(input)); + auto memory_stream = MUST(Core::Stream::FixedMemoryStream::construct(input)); auto bit_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(move(memory_stream))); for (size_t idx = 0; idx < 9; ++idx) @@ -49,7 +49,7 @@ TEST_CASE(canonical_code_complex) }; auto const huffman = Compress::CanonicalCode::from_bytes(code).value(); - auto memory_stream = MUST(Core::Stream::MemoryStream::construct(input)); + auto memory_stream = MUST(Core::Stream::FixedMemoryStream::construct(input)); auto bit_stream = MUST(Core::Stream::LittleEndianInputBitStream::construct(move(memory_stream))); for (size_t idx = 0; idx < 12; ++idx) diff --git a/Userland/Libraries/LibAudio/FlacLoader.cpp b/Userland/Libraries/LibAudio/FlacLoader.cpp index b45238aa92..edc9fe7758 100644 --- a/Userland/Libraries/LibAudio/FlacLoader.cpp +++ b/Userland/Libraries/LibAudio/FlacLoader.cpp @@ -42,7 +42,7 @@ Result, LoaderError> FlacLoaderPlugin::try_creat Result, LoaderError> FlacLoaderPlugin::try_create(Bytes buffer) { - auto stream = LOADER_TRY(Core::Stream::MemoryStream::construct(buffer)); + auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(buffer)); auto loader = make(move(stream)); LOADER_TRY(loader->initialize()); @@ -78,7 +78,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header() // Receive the streaminfo block auto streaminfo = TRY(next_meta_block(*bit_input)); FLAC_VERIFY(streaminfo.type == FlacMetadataBlockType::STREAMINFO, LoaderError::Category::Format, "First block must be STREAMINFO"); - auto streaminfo_data_memory = LOADER_TRY(Core::Stream::MemoryStream::construct(streaminfo.data.bytes())); + auto streaminfo_data_memory = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(streaminfo.data.bytes())); auto streaminfo_data = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*streaminfo_data_memory))); // 11.10 METADATA_BLOCK_STREAMINFO @@ -149,7 +149,7 @@ MaybeLoaderError FlacLoaderPlugin::parse_header() // 11.19. METADATA_BLOCK_PICTURE MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block) { - auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes())); + auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes())); auto picture_block_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*memory_stream))); PictureData picture {}; @@ -186,7 +186,7 @@ MaybeLoaderError FlacLoaderPlugin::load_picture(FlacRawMetadataBlock& block) // 11.13. METADATA_BLOCK_SEEKTABLE MaybeLoaderError FlacLoaderPlugin::load_seektable(FlacRawMetadataBlock& block) { - auto memory_stream = LOADER_TRY(Core::Stream::MemoryStream::construct(block.data.bytes())); + auto memory_stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(block.data.bytes())); auto seektable_bytes = LOADER_TRY(BigEndianInputBitStream::construct(Core::Stream::Handle(*memory_stream))); for (size_t i = 0; i < block.length / 18; ++i) { // 11.14. SEEKPOINT diff --git a/Userland/Libraries/LibAudio/MP3Loader.cpp b/Userland/Libraries/LibAudio/MP3Loader.cpp index 229921653c..fd13f8dea8 100644 --- a/Userland/Libraries/LibAudio/MP3Loader.cpp +++ b/Userland/Libraries/LibAudio/MP3Loader.cpp @@ -31,7 +31,7 @@ Result, LoaderError> MP3LoaderPlugin::try_create( Result, LoaderError> MP3LoaderPlugin::try_create(Bytes buffer) { - auto stream = LOADER_TRY(Core::Stream::MemoryStream::construct(buffer)); + auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(buffer)); auto loader = make(move(stream)); LOADER_TRY(loader->initialize()); diff --git a/Userland/Libraries/LibAudio/WavLoader.cpp b/Userland/Libraries/LibAudio/WavLoader.cpp index 07c0e14057..be382c9274 100644 --- a/Userland/Libraries/LibAudio/WavLoader.cpp +++ b/Userland/Libraries/LibAudio/WavLoader.cpp @@ -35,7 +35,7 @@ Result, LoaderError> WavLoaderPlugin::try_create( Result, LoaderError> WavLoaderPlugin::try_create(Bytes buffer) { - auto stream = LOADER_TRY(Core::Stream::MemoryStream::construct(buffer)); + auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(buffer)); auto loader = make(move(stream)); LOADER_TRY(loader->initialize()); @@ -115,7 +115,7 @@ static ErrorOr read_sample(Core::Stream::Stream& stream) LoaderSamples WavLoaderPlugin::samples_from_pcm_data(Bytes const& data, size_t samples_to_read) const { FixedArray samples = LOADER_TRY(FixedArray::try_create(samples_to_read)); - auto stream = LOADER_TRY(Core::Stream::MemoryStream::construct(move(data))); + auto stream = LOADER_TRY(Core::Stream::FixedMemoryStream::construct(move(data))); switch (m_sample_format) { case PcmSampleFormat::Uint8: diff --git a/Userland/Libraries/LibCompress/Deflate.cpp b/Userland/Libraries/LibCompress/Deflate.cpp index 2366d48b00..9bd1ed07db 100644 --- a/Userland/Libraries/LibCompress/Deflate.cpp +++ b/Userland/Libraries/LibCompress/Deflate.cpp @@ -309,7 +309,7 @@ void DeflateDecompressor::close() ErrorOr DeflateDecompressor::decompress_all(ReadonlyBytes bytes) { - auto memory_stream = TRY(Core::Stream::MemoryStream::construct(bytes)); + auto memory_stream = TRY(Core::Stream::FixedMemoryStream::construct(bytes)); DeflateDecompressor deflate_stream { move(memory_stream) }; DuplexMemoryStream output_stream; diff --git a/Userland/Libraries/LibCompress/Gzip.cpp b/Userland/Libraries/LibCompress/Gzip.cpp index 828371ae5b..2aeccf7757 100644 --- a/Userland/Libraries/LibCompress/Gzip.cpp +++ b/Userland/Libraries/LibCompress/Gzip.cpp @@ -153,7 +153,7 @@ Optional GzipDecompressor::describe_header(ReadonlyBytes bytes ErrorOr GzipDecompressor::decompress_all(ReadonlyBytes bytes) { - auto memory_stream = TRY(Core::Stream::MemoryStream::construct(bytes)); + auto memory_stream = TRY(Core::Stream::FixedMemoryStream::construct(bytes)); auto gzip_stream = make(move(memory_stream)); DuplexMemoryStream output_stream; diff --git a/Userland/Libraries/LibCore/MemoryStream.h b/Userland/Libraries/LibCore/MemoryStream.h index e1dace8818..ecaa0e9d53 100644 --- a/Userland/Libraries/LibCore/MemoryStream.h +++ b/Userland/Libraries/LibCore/MemoryStream.h @@ -15,16 +15,18 @@ namespace Core::Stream { -class MemoryStream final : public SeekableStream { +/// A stream class that allows for reading/writing on a preallocated memory area +/// using a single read/write head. +class FixedMemoryStream final : public SeekableStream { public: - static ErrorOr> construct(Bytes bytes) + static ErrorOr> construct(Bytes bytes) { - return adopt_nonnull_own_or_enomem(new (nothrow) MemoryStream(bytes)); + return adopt_nonnull_own_or_enomem(new (nothrow) FixedMemoryStream(bytes)); } - static ErrorOr> construct(ReadonlyBytes bytes) + static ErrorOr> construct(ReadonlyBytes bytes) { - return adopt_nonnull_own_or_enomem(new (nothrow) MemoryStream(bytes)); + return adopt_nonnull_own_or_enomem(new (nothrow) FixedMemoryStream(bytes)); } virtual bool is_eof() const override { return m_offset >= m_bytes.size(); } @@ -98,12 +100,12 @@ public: size_t remaining() const { return m_bytes.size() - m_offset; } protected: - explicit MemoryStream(Bytes bytes) + explicit FixedMemoryStream(Bytes bytes) : m_bytes(bytes) { } - explicit MemoryStream(ReadonlyBytes bytes) + explicit FixedMemoryStream(ReadonlyBytes bytes) : m_bytes({ const_cast(bytes.data()), bytes.size() }) , m_writing_enabled(false) { diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index 678ce97138..839c1387df 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -80,7 +80,7 @@ static Optional handle_content_encoding(ByteBuffer const& buf, Depre } else if (content_encoding == "br") { dbgln_if(JOB_DEBUG, "Job::handle_content_encoding: buf is brotli compressed!"); - auto bufstream_result = Core::Stream::MemoryStream::construct({ buf.data(), buf.size() }); + auto bufstream_result = Core::Stream::FixedMemoryStream::construct({ buf.data(), buf.size() }); if (bufstream_result.is_error()) { dbgln("Job::handle_content_encoding: MemoryStream::construct() failed."); return {}; diff --git a/Userland/Utilities/headless-browser.cpp b/Userland/Utilities/headless-browser.cpp index 2e8d2df8a3..b706e01524 100644 --- a/Userland/Utilities/headless-browser.cpp +++ b/Userland/Utilities/headless-browser.cpp @@ -355,7 +355,7 @@ public: private: HTTPHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr socket, ByteBuffer&& stream_backing_buffer) : m_stream_backing_buffer(move(stream_backing_buffer)) - , m_output_stream(Core::Stream::MemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) + , m_output_stream(Core::Stream::FixedMemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) , m_socket(move(socket)) , m_job(HTTP::Job::construct(move(request), *m_output_stream)) { @@ -381,7 +381,7 @@ public: Optional m_response_code; ByteBuffer m_stream_backing_buffer; - NonnullOwnPtr m_output_stream; + NonnullOwnPtr m_output_stream; NonnullOwnPtr m_socket; NonnullRefPtr m_job; HashMap m_response_headers; @@ -434,7 +434,7 @@ public: private: HTTPSHeadlessRequest(HTTP::HttpRequest&& request, NonnullOwnPtr socket, ByteBuffer&& stream_backing_buffer) : m_stream_backing_buffer(move(stream_backing_buffer)) - , m_output_stream(Core::Stream::MemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) + , m_output_stream(Core::Stream::FixedMemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) , m_socket(move(socket)) , m_job(HTTP::HttpsJob::construct(move(request), *m_output_stream)) { @@ -460,7 +460,7 @@ public: Optional m_response_code; ByteBuffer m_stream_backing_buffer; - NonnullOwnPtr m_output_stream; + NonnullOwnPtr m_output_stream; NonnullOwnPtr m_socket; NonnullRefPtr m_job; HashMap m_response_headers; @@ -503,7 +503,7 @@ public: private: GeminiHeadlessRequest(Gemini::GeminiRequest&& request, NonnullOwnPtr socket, ByteBuffer&& stream_backing_buffer) : m_stream_backing_buffer(move(stream_backing_buffer)) - , m_output_stream(Core::Stream::MemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) + , m_output_stream(Core::Stream::FixedMemoryStream::construct(m_stream_backing_buffer.bytes()).release_value_but_fixme_should_propagate_errors()) , m_socket(move(socket)) , m_job(Gemini::Job::construct(move(request), *m_output_stream)) { @@ -529,7 +529,7 @@ public: Optional m_response_code; ByteBuffer m_stream_backing_buffer; - NonnullOwnPtr m_output_stream; + NonnullOwnPtr m_output_stream; NonnullOwnPtr m_socket; NonnullRefPtr m_job; HashMap m_response_headers;