1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:47:45 +00:00

AK: Move memory streams from LibCore

This commit is contained in:
Tim Schumacher 2023-01-25 20:19:05 +01:00 committed by Andrew Kaster
parent 11550f582b
commit 093cf428a3
46 changed files with 213 additions and 203 deletions

View file

@ -13,7 +13,6 @@ set(SOURCES
IODevice.cpp
LockFile.cpp
MappedFile.cpp
MemoryStream.cpp
MimeData.cpp
NetworkJob.cpp
Notifier.cpp

View file

@ -1,274 +0,0 @@
/*
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
* Copyright (c) 2022, Tim Schumacher <timschumi@gmx.de>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/FixedArray.h>
#include <AK/MemMem.h>
#include <LibCore/MemoryStream.h>
namespace Core::Stream {
FixedMemoryStream::FixedMemoryStream(Bytes bytes)
: m_bytes(bytes)
{
}
FixedMemoryStream::FixedMemoryStream(ReadonlyBytes bytes)
: m_bytes({ const_cast<u8*>(bytes.data()), bytes.size() })
, m_writing_enabled(false)
{
}
ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(Bytes bytes)
{
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
}
ErrorOr<NonnullOwnPtr<FixedMemoryStream>> FixedMemoryStream::construct(ReadonlyBytes bytes)
{
return adopt_nonnull_own_or_enomem<FixedMemoryStream>(new (nothrow) FixedMemoryStream(bytes));
}
bool FixedMemoryStream::is_eof() const
{
return m_offset >= m_bytes.size();
}
bool FixedMemoryStream::is_open() const
{
return true;
}
void FixedMemoryStream::close()
{
// FIXME: It doesn't make sense to close a memory stream. Therefore, we don't do anything here. Is that fine?
}
ErrorOr<void> FixedMemoryStream::truncate(off_t)
{
return Error::from_errno(EBADF);
}
ErrorOr<Bytes> FixedMemoryStream::read(Bytes bytes)
{
auto to_read = min(remaining(), bytes.size());
if (to_read == 0)
return Bytes {};
m_bytes.slice(m_offset, to_read).copy_to(bytes);
m_offset += to_read;
return bytes.trim(to_read);
}
ErrorOr<size_t> FixedMemoryStream::seek(i64 offset, SeekMode seek_mode)
{
switch (seek_mode) {
case SeekMode::SetPosition:
if (offset > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the end of the stream memory");
m_offset = offset;
break;
case SeekMode::FromCurrentPosition:
if (offset + static_cast<i64>(m_offset) > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the end of the stream memory");
m_offset += offset;
break;
case SeekMode::FromEndPosition:
if (offset > static_cast<i64>(m_bytes.size()))
return Error::from_string_literal("Offset past the start of the stream memory");
m_offset = m_bytes.size() - offset;
break;
}
return m_offset;
}
ErrorOr<size_t> FixedMemoryStream::write(ReadonlyBytes bytes)
{
VERIFY(m_writing_enabled);
// FIXME: Can this not error?
auto const nwritten = bytes.copy_trimmed_to(m_bytes.slice(m_offset));
m_offset += nwritten;
return nwritten;
}
ErrorOr<void> FixedMemoryStream::write_entire_buffer(ReadonlyBytes bytes)
{
if (remaining() < bytes.size())
return Error::from_string_literal("Write of entire buffer ends past the memory area");
TRY(write(bytes));
return {};
}
Bytes FixedMemoryStream::bytes()
{
VERIFY(m_writing_enabled);
return m_bytes;
}
ReadonlyBytes FixedMemoryStream::bytes() const
{
return m_bytes;
}
size_t FixedMemoryStream::offset() const
{
return m_offset;
}
size_t FixedMemoryStream::remaining() const
{
return m_bytes.size() - m_offset;
}
ErrorOr<Bytes> AllocatingMemoryStream::read(Bytes bytes)
{
size_t read_bytes = 0;
while (read_bytes < bytes.size()) {
VERIFY(m_write_offset >= m_read_offset);
auto range = TRY(next_read_range());
if (range.size() == 0)
break;
auto copied_bytes = range.copy_trimmed_to(bytes.slice(read_bytes));
read_bytes += copied_bytes;
m_read_offset += copied_bytes;
}
cleanup_unused_chunks();
return bytes.trim(read_bytes);
}
ErrorOr<size_t> AllocatingMemoryStream::write(ReadonlyBytes bytes)
{
size_t written_bytes = 0;
while (written_bytes < bytes.size()) {
VERIFY(m_write_offset >= m_read_offset);
auto range = TRY(next_write_range());
auto copied_bytes = bytes.slice(written_bytes).copy_trimmed_to(range);
written_bytes += copied_bytes;
m_write_offset += copied_bytes;
}
return written_bytes;
}
ErrorOr<void> AllocatingMemoryStream::discard(size_t count)
{
VERIFY(m_write_offset >= m_read_offset);
if (count > used_buffer_size())
return Error::from_string_literal("Number of discarded bytes is higher than the number of allocated bytes");
m_read_offset += count;
cleanup_unused_chunks();
return {};
}
bool AllocatingMemoryStream::is_eof() const
{
return used_buffer_size() == 0;
}
bool AllocatingMemoryStream::is_open() const
{
return true;
}
void AllocatingMemoryStream::close()
{
}
size_t AllocatingMemoryStream::used_buffer_size() const
{
return m_write_offset - m_read_offset;
}
ErrorOr<Optional<size_t>> AllocatingMemoryStream::offset_of(ReadonlyBytes needle) const
{
VERIFY(m_write_offset >= m_read_offset);
if (m_chunks.size() == 0)
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);
auto chunk_count = m_chunks.size();
auto search_spans = TRY(FixedArray<ReadonlyBytes>::create(chunk_count));
for (size_t i = 0; i < chunk_count; i++) {
search_spans[i] = m_chunks[i].span();
}
// 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[0] = search_spans[0].slice(m_read_offset);
return AK::memmem(search_spans.begin(), search_spans.end(), needle);
}
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);
if (read_size == 0)
return ReadonlyBytes { static_cast<u8*>(nullptr), 0 };
VERIFY(chunk_index < m_chunks.size());
return ReadonlyBytes { m_chunks[chunk_index].data() + chunk_offset, read_size };
}
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;
if (chunk_index >= m_chunks.size())
TRY(m_chunks.try_append(TRY(Chunk::create_uninitialized(chunk_size))));
VERIFY(chunk_index < m_chunks.size());
return Bytes { m_chunks[chunk_index].data() + chunk_offset, write_size };
}
void AllocatingMemoryStream::cleanup_unused_chunks()
{
// FIXME: Move these all at once.
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_chunks.append(move(buffer));
}
}
}

View file

@ -1,79 +0,0 @@
/*
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/NonnullOwnPtr.h>
#include <AK/OwnPtr.h>
#include <AK/Span.h>
#include <AK/TypedTransfer.h>
#include <LibCore/Stream.h>
namespace Core::Stream {
/// 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<NonnullOwnPtr<FixedMemoryStream>> construct(Bytes bytes);
static ErrorOr<NonnullOwnPtr<FixedMemoryStream>> construct(ReadonlyBytes bytes);
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;
virtual ErrorOr<void> truncate(off_t) override;
virtual ErrorOr<Bytes> read(Bytes bytes) override;
virtual ErrorOr<size_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override;
virtual ErrorOr<size_t> write(ReadonlyBytes bytes) override;
virtual ErrorOr<void> write_entire_buffer(ReadonlyBytes bytes) override;
Bytes bytes();
ReadonlyBytes bytes() const;
size_t offset() const;
size_t remaining() const;
private:
explicit FixedMemoryStream(Bytes bytes);
explicit FixedMemoryStream(ReadonlyBytes bytes);
Bytes m_bytes;
size_t m_offset { 0 };
bool m_writing_enabled { true };
};
/// A stream class that allows for writing to an automatically allocating memory area
/// and reading back the written data afterwards.
class AllocatingMemoryStream final : public AK::Stream {
public:
virtual ErrorOr<Bytes> read(Bytes) override;
virtual ErrorOr<size_t> write(ReadonlyBytes) override;
virtual ErrorOr<void> discard(size_t) override;
virtual bool is_eof() const override;
virtual bool is_open() const override;
virtual void close() override;
size_t used_buffer_size() const;
ErrorOr<Optional<size_t>> offset_of(ReadonlyBytes needle) const;
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();
void cleanup_unused_chunks();
Vector<Chunk> m_chunks;
size_t m_read_offset = 0;
size_t m_write_offset = 0;
};
}

View file

@ -4,7 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/MemoryStream.h>
#include <AK/MemoryStream.h>
#include <LibCore/SOCKSProxyClient.h>
enum class Method : u8 {
@ -122,7 +122,7 @@ ErrorOr<void> send_version_identifier_and_method_selection_message(Core::Stream:
ErrorOr<Reply> send_connect_request_message(Core::Stream::Socket& socket, Core::SOCKSProxyClient::Version version, Core::SOCKSProxyClient::HostOrIPV4 target, int port, Core::SOCKSProxyClient::Command command)
{
Core::Stream::AllocatingMemoryStream stream;
AllocatingMemoryStream stream;
Socks5ConnectRequestHeader header {
.version_identifier = to_underlying(version),
@ -218,7 +218,7 @@ ErrorOr<Reply> send_connect_request_message(Core::Stream::Socket& socket, Core::
ErrorOr<u8> send_username_password_authentication_message(Core::Stream::Socket& socket, Core::SOCKSProxyClient::UsernamePasswordAuthenticationData const& auth_data)
{
Core::Stream::AllocatingMemoryStream stream;
AllocatingMemoryStream stream;
u8 version = 0x01;
auto size = TRY(stream.write({ &version, sizeof(version) }));