2022-01-13 21:14:23 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, kleines Filmröllchen <filmroellchen@serenityos.org>.
|
2023-10-06 13:03:54 -03:00
|
|
|
* Copyright (c) 2023, Sam Atkins <atkinssj@serenityos.org>.
|
2022-01-13 21:14:23 -03:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2023-10-06 13:03:54 -03:00
|
|
|
#include <AK/Error.h>
|
2026-04-22 04:15:00 -03:00
|
|
|
#include <AK/OwnPtr.h>
|
2023-01-25 16:19:05 -03:00
|
|
|
#include <AK/Stream.h>
|
|
|
|
|
#include <AK/Vector.h>
|
2022-01-13 21:14:23 -03:00
|
|
|
|
2023-01-25 16:19:05 -03:00
|
|
|
namespace AK {
|
2022-01-13 21:14:23 -03:00
|
|
|
|
2022-12-07 11:47:44 -03:00
|
|
|
/// A stream class that allows for reading/writing on a preallocated memory area
|
|
|
|
|
/// using a single read/write head.
|
2023-09-19 07:01:04 -03:00
|
|
|
class FixedMemoryStream : public SeekableStream {
|
2022-01-13 21:14:23 -03:00
|
|
|
public:
|
2023-11-05 17:31:17 -03:00
|
|
|
enum class Mode {
|
|
|
|
|
ReadOnly,
|
|
|
|
|
ReadWrite,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
explicit FixedMemoryStream(Bytes bytes, Mode mode = Mode::ReadWrite);
|
2023-01-30 07:05:43 -03:00
|
|
|
explicit FixedMemoryStream(ReadonlyBytes bytes);
|
2022-01-13 21:14:23 -03:00
|
|
|
|
2022-12-07 12:22:14 -03:00
|
|
|
virtual bool is_eof() const override;
|
|
|
|
|
virtual bool is_open() const override;
|
|
|
|
|
virtual void close() override;
|
2023-01-29 09:49:42 -03:00
|
|
|
virtual ErrorOr<void> truncate(size_t) override;
|
2025-06-06 09:09:42 -03:00
|
|
|
virtual ErrorOr<Bytes> read_some(Bytes bytes) override
|
|
|
|
|
{
|
|
|
|
|
auto read = m_bytes.slice(m_offset).copy_trimmed_to(bytes);
|
|
|
|
|
m_offset += read;
|
|
|
|
|
return bytes.trim(read);
|
|
|
|
|
}
|
2023-06-06 14:52:56 -03:00
|
|
|
virtual ErrorOr<void> read_until_filled(Bytes bytes) override;
|
2022-11-30 06:03:19 -03:00
|
|
|
|
2023-01-17 10:52:46 -03:00
|
|
|
virtual ErrorOr<size_t> seek(i64 offset, SeekMode seek_mode = SeekMode::SetPosition) override;
|
2022-01-13 21:14:23 -03:00
|
|
|
|
2023-02-24 18:38:01 -03:00
|
|
|
virtual ErrorOr<size_t> write_some(ReadonlyBytes bytes) override;
|
2023-03-01 11:37:45 -03:00
|
|
|
virtual ErrorOr<void> write_until_depleted(ReadonlyBytes bytes) override;
|
2022-01-13 21:14:23 -03:00
|
|
|
|
2022-12-07 12:22:14 -03:00
|
|
|
size_t offset() const;
|
|
|
|
|
size_t remaining() const;
|
2022-11-30 06:03:19 -03:00
|
|
|
|
2023-10-06 13:03:54 -03:00
|
|
|
/// Read a value, but referring to the stream's underlying data instead of copying it.
|
|
|
|
|
/// Of course, only use this if you know the lifetime of the data will exceed the value's.
|
|
|
|
|
// FIXME: Would be nicer to be able to return T& but Variant (and thus ErrorOr) can't hold references.
|
|
|
|
|
template<typename T>
|
|
|
|
|
requires(Traits<T>::is_trivially_serializable())
|
|
|
|
|
ErrorOr<T*> read_in_place()
|
|
|
|
|
{
|
|
|
|
|
if constexpr (!IsConst<T>) {
|
|
|
|
|
if (!m_writing_enabled)
|
2025-05-10 17:12:27 -03:00
|
|
|
return Error::from_string_literal("Tried to obtain a non-const reference from a read-only FixedMemoryStream");
|
2023-10-06 13:03:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T* value = reinterpret_cast<T*>(m_bytes.offset_pointer(m_offset));
|
|
|
|
|
TRY(discard(sizeof(T)));
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Read a span of values, referring to the stream's underlying data instead of copying it.
|
|
|
|
|
/// Of course, only use this if you know the lifetime of the data will exceed the span's.
|
|
|
|
|
template<typename T>
|
|
|
|
|
requires(Traits<T>::is_trivially_serializable())
|
|
|
|
|
ErrorOr<Span<T>> read_in_place(size_t count)
|
|
|
|
|
{
|
|
|
|
|
if constexpr (!IsConst<T>) {
|
|
|
|
|
if (!m_writing_enabled)
|
2025-05-10 17:12:27 -03:00
|
|
|
return Error::from_string_literal("Tried to obtain a non-const span from a read-only FixedMemoryStream");
|
2023-10-06 13:03:54 -03:00
|
|
|
}
|
|
|
|
|
|
2023-11-01 11:56:32 -03:00
|
|
|
Span<T> span { reinterpret_cast<T*>(m_bytes.offset_pointer(m_offset)), count };
|
2023-10-06 13:03:54 -03:00
|
|
|
TRY(discard(sizeof(T) * count));
|
|
|
|
|
return span;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-13 21:14:23 -03:00
|
|
|
private:
|
|
|
|
|
Bytes m_bytes;
|
|
|
|
|
size_t m_offset { 0 };
|
2022-11-30 06:03:19 -03:00
|
|
|
bool m_writing_enabled { true };
|
2022-01-13 21:14:23 -03:00
|
|
|
};
|
|
|
|
|
|
2022-12-07 13:43:31 -03:00
|
|
|
/// A stream class that allows for writing to an automatically allocating memory area
|
|
|
|
|
/// and reading back the written data afterwards.
|
2026-04-22 04:15:00 -03:00
|
|
|
///
|
|
|
|
|
/// Internally a singly-linked list of fixed-size chunks. Writes append to the tail,
|
|
|
|
|
/// reads/discards consume from the head, so both ends are O(1).
|
2023-01-25 16:19:05 -03:00
|
|
|
class AllocatingMemoryStream final : public Stream {
|
2022-12-07 13:43:31 -03:00
|
|
|
public:
|
2023-05-29 06:00:25 -03:00
|
|
|
static constexpr size_t CHUNK_SIZE = 4096;
|
|
|
|
|
|
2026-04-22 04:15:00 -03:00
|
|
|
AllocatingMemoryStream() = default;
|
|
|
|
|
~AllocatingMemoryStream();
|
|
|
|
|
|
2025-04-13 22:17:21 -03:00
|
|
|
void peek_some(Bytes) const;
|
2026-04-21 16:13:41 -03:00
|
|
|
ReadonlyBytes peek_some_contiguous() const;
|
2025-04-13 22:17:21 -03:00
|
|
|
|
2023-02-24 18:38:01 -03:00
|
|
|
virtual ErrorOr<Bytes> read_some(Bytes) override;
|
|
|
|
|
virtual ErrorOr<size_t> write_some(ReadonlyBytes) override;
|
2022-12-07 13:43:31 -03:00
|
|
|
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;
|
|
|
|
|
|
2023-01-13 09:41:14 -03:00
|
|
|
ErrorOr<Optional<size_t>> offset_of(ReadonlyBytes needle) const;
|
|
|
|
|
|
2022-12-07 13:43:31 -03:00
|
|
|
private:
|
2026-04-22 04:15:00 -03:00
|
|
|
struct Chunk {
|
|
|
|
|
// User-provided default ctor so `new Chunk()` does not zero-init the data array.
|
|
|
|
|
Chunk() { }
|
|
|
|
|
|
|
|
|
|
u8 data[CHUNK_SIZE];
|
|
|
|
|
OwnPtr<Chunk> next;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ErrorOr<void> append_new_chunk();
|
|
|
|
|
void pop_head_chunk();
|
|
|
|
|
|
|
|
|
|
OwnPtr<Chunk> m_head;
|
|
|
|
|
Chunk* m_tail { nullptr };
|
|
|
|
|
|
|
|
|
|
// Offset into m_head->data for the next read.
|
|
|
|
|
size_t m_head_read_offset { 0 };
|
2022-12-07 13:43:31 -03:00
|
|
|
|
2026-04-22 04:15:00 -03:00
|
|
|
// Number of bytes written into m_tail->data.
|
|
|
|
|
size_t m_tail_write_offset { 0 };
|
2022-12-07 13:43:31 -03:00
|
|
|
|
2026-04-22 04:15:00 -03:00
|
|
|
size_t m_used_buffer_size { 0 };
|
2022-12-07 13:43:31 -03:00
|
|
|
};
|
|
|
|
|
|
2022-01-13 21:14:23 -03:00
|
|
|
}
|
2023-01-25 16:19:05 -03:00
|
|
|
|
|
|
|
|
#if USING_AK_GLOBALLY
|
|
|
|
|
using AK::AllocatingMemoryStream;
|
|
|
|
|
using AK::FixedMemoryStream;
|
|
|
|
|
#endif
|