mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-08-17 01:10:28 +03:00
AK: Expose AllocatingMemoryStream::CHUNK_SIZE
This allows the tests to use that information confidently.
This commit is contained in:
parent
2b8a527478
commit
9a7ae52b31
Notes:
sideshowbarker
2024-07-17 12:02:22 +09:00
Author: https://github.com/timschumi Commit: https://github.com/SerenityOS/serenity/commit/9a7ae52b31 Pull-request: https://github.com/SerenityOS/serenity/pull/19122 Reviewed-by: https://github.com/alimpfard ✅ Reviewed-by: https://github.com/gmta ✅
@ -199,8 +199,8 @@ ErrorOr<Optional<size_t>> AllocatingMemoryStream::offset_of(ReadonlyBytes needle
|
||||
return Optional<size_t> {};
|
||||
|
||||
// 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<ReadonlyBytes>::create(chunk_count));
|
||||
@ -210,7 +210,7 @@ ErrorOr<Optional<size_t>> 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<ReadonlyBytes> 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<u8*>(nullptr), 0 };
|
||||
@ -236,12 +236,12 @@ ErrorOr<Bytes> 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<Bytes> 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));
|
||||
}
|
||||
|
@ -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<Bytes> read_some(Bytes) override;
|
||||
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
|
||||
virtual ErrorOr<void> 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<ReadonlyBytes> next_read_range();
|
||||
ErrorOr<Bytes> next_write_range();
|
||||
|
@ -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()));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user