mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 22:07:35 +00:00
LibCore: Add an automatically allocating MemoryStream
Similar to AK::DuplexStream, this writes to the end and reads from the beginning of a dynamically allocated buffer.
This commit is contained in:
parent
8dbc7aa63f
commit
f205f62a44
3 changed files with 200 additions and 0 deletions
|
@ -47,4 +47,31 @@ private:
|
|||
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 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;
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue