From 9a7ae52b310ada54f8c0821bab8974eab211c2a0 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Mon, 29 May 2023 11:00:25 +0200 Subject: [PATCH] AK: Expose `AllocatingMemoryStream::CHUNK_SIZE` This allows the tests to use that information confidently. --- AK/MemoryStream.cpp | 26 +++++++++++++------------- AK/MemoryStream.h | 3 ++- Tests/AK/TestMemoryStream.cpp | 7 +++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/AK/MemoryStream.cpp b/AK/MemoryStream.cpp index 825998f56ec..674c7ec44c3 100644 --- a/AK/MemoryStream.cpp +++ b/AK/MemoryStream.cpp @@ -199,8 +199,8 @@ ErrorOr> AllocatingMemoryStream::offset_of(ReadonlyBytes needle return Optional {}; // Ensure that we don't have to trim away more than one block. - VERIFY(m_read_offset < chunk_size); - VERIFY(m_chunks.size() * chunk_size - m_write_offset < chunk_size); + VERIFY(m_read_offset < CHUNK_SIZE); + VERIFY(m_chunks.size() * CHUNK_SIZE - m_write_offset < CHUNK_SIZE); auto chunk_count = m_chunks.size(); auto search_spans = TRY(FixedArray::create(chunk_count)); @@ -210,7 +210,7 @@ ErrorOr> AllocatingMemoryStream::offset_of(ReadonlyBytes needle } // Trimming is done first to ensure that we don't unintentionally shift around if the first and last chunks are the same. - search_spans[chunk_count - 1] = search_spans[chunk_count - 1].trim(m_write_offset % chunk_size); + search_spans[chunk_count - 1] = search_spans[chunk_count - 1].trim(m_write_offset % CHUNK_SIZE); search_spans[0] = search_spans[0].slice(m_read_offset); return AK::memmem(search_spans.begin(), search_spans.end(), needle); @@ -220,9 +220,9 @@ ErrorOr AllocatingMemoryStream::next_read_range() { VERIFY(m_write_offset >= m_read_offset); - size_t const chunk_index = m_read_offset / chunk_size; - size_t const chunk_offset = m_read_offset % chunk_size; - size_t const read_size = min(chunk_size - m_read_offset % chunk_size, m_write_offset - m_read_offset); + size_t const chunk_index = m_read_offset / CHUNK_SIZE; + size_t const chunk_offset = m_read_offset % CHUNK_SIZE; + size_t const read_size = min(CHUNK_SIZE - m_read_offset % CHUNK_SIZE, m_write_offset - m_read_offset); if (read_size == 0) return ReadonlyBytes { static_cast(nullptr), 0 }; @@ -236,12 +236,12 @@ ErrorOr AllocatingMemoryStream::next_write_range() { VERIFY(m_write_offset >= m_read_offset); - size_t const chunk_index = m_write_offset / chunk_size; - size_t const chunk_offset = m_write_offset % chunk_size; - size_t const write_size = chunk_size - m_write_offset % chunk_size; + size_t const chunk_index = m_write_offset / CHUNK_SIZE; + size_t const chunk_offset = m_write_offset % CHUNK_SIZE; + size_t const write_size = CHUNK_SIZE - m_write_offset % CHUNK_SIZE; if (chunk_index >= m_chunks.size()) - TRY(m_chunks.try_append(TRY(Chunk::create_uninitialized(chunk_size)))); + TRY(m_chunks.try_append(TRY(Chunk::create_uninitialized(CHUNK_SIZE)))); VERIFY(chunk_index < m_chunks.size()); @@ -251,12 +251,12 @@ ErrorOr AllocatingMemoryStream::next_write_range() void AllocatingMemoryStream::cleanup_unused_chunks() { // FIXME: Move these all at once. - while (m_read_offset >= chunk_size) { + while (m_read_offset >= CHUNK_SIZE) { VERIFY(m_write_offset >= m_read_offset); auto buffer = m_chunks.take_first(); - m_read_offset -= chunk_size; - m_write_offset -= chunk_size; + m_read_offset -= CHUNK_SIZE; + m_write_offset -= CHUNK_SIZE; m_chunks.append(move(buffer)); } diff --git a/AK/MemoryStream.h b/AK/MemoryStream.h index 6ebb3d2e0b1..f069b222a34 100644 --- a/AK/MemoryStream.h +++ b/AK/MemoryStream.h @@ -45,6 +45,8 @@ private: /// and reading back the written data afterwards. class AllocatingMemoryStream final : public Stream { public: + static constexpr size_t CHUNK_SIZE = 4096; + virtual ErrorOr read_some(Bytes) override; virtual ErrorOr write_some(ReadonlyBytes) override; virtual ErrorOr discard(size_t) override; @@ -59,7 +61,6 @@ public: private: // Note: We set the inline buffer capacity to zero to make moving chunks as efficient as possible. using Chunk = AK::Detail::ByteBuffer<0>; - static constexpr size_t chunk_size = 4096; ErrorOr next_read_range(); ErrorOr next_write_range(); diff --git a/Tests/AK/TestMemoryStream.cpp b/Tests/AK/TestMemoryStream.cpp index 71a1e081eeb..097c92e4f20 100644 --- a/Tests/AK/TestMemoryStream.cpp +++ b/Tests/AK/TestMemoryStream.cpp @@ -73,16 +73,15 @@ TEST_CASE(allocating_memory_stream_offset_of_oob) AllocatingMemoryStream stream; // NOTE: This test is to make sure that offset_of() doesn't read past the end of the "initialized" data. // So we have to assume some things about the behavior of this class: - // - The chunk size is 4096 bytes. // - A chunk is moved to the end when it's fully read from // - A free chunk is used as-is, no new ones are allocated if one exists. - // First, fill exactly one chunk. - for (size_t i = 0; i < 256; ++i) + // First, fill exactly one chunk (in groups of 16 bytes). + for (size_t i = 0; i < AllocatingMemoryStream::CHUNK_SIZE / 16; ++i) MUST(stream.write_until_depleted("AAAAAAAAAAAAAAAA"sv.bytes())); // Then discard it all. - MUST(stream.discard(4096)); + MUST(stream.discard(AllocatingMemoryStream::CHUNK_SIZE)); // Now we can write into this chunk again, knowing that it's initialized to all 'A's. MUST(stream.write_until_depleted("Well Hello Friends! :^)"sv.bytes()));