2021-09-06 10:58:08 +00:00
|
|
|
/***
|
|
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
|
|
|
|
File: ByteBuffer.hpp
|
|
|
|
Date: 2021-8-5
|
|
|
|
Author: Reece
|
|
|
|
***/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
namespace Aurora::Memory
|
|
|
|
{
|
2022-01-18 16:49:50 +00:00
|
|
|
static const auto kBufferPageSize = 512;
|
|
|
|
//static const auto kBufferBasePower = 8;
|
|
|
|
static const auto kBufferInitialPower = 9;// -kBufferBasePower; // 4-bit integer
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
* A bytebuffer object represents a linear, partially-linear resizable, buffer **or** a ring buffer.
|
|
|
|
*
|
2021-10-03 12:44:09 +00:00
|
|
|
* Use cases for a ring buffer include wrapping streams for a use case in which the consumer may
|
|
|
|
* expect arbitrary stream seeks of an otherwise limited consume-once stream
|
|
|
|
*
|
2021-09-06 10:58:08 +00:00
|
|
|
* IE;
|
2021-11-22 16:16:02 +00:00
|
|
|
* -> Peeking a header in a datagram, or tcp stream; where instead of freeing the datagram or double
|
2021-10-03 12:44:09 +00:00
|
|
|
* buffering the network stack when required, a ring buffer is used to prevent reallocation on each frame
|
2021-11-22 16:16:02 +00:00
|
|
|
* -> Peeking, or seeking back after, compression read. A compression api could be fed on-Sdemand or ad hoc,
|
2021-10-03 12:44:09 +00:00
|
|
|
* writing to its write head pointer, while never running out of space so long as the decompressed ring
|
|
|
|
* read head continues moving
|
2021-09-06 10:58:08 +00:00
|
|
|
*
|
|
|
|
* Small, linear, write/read-once [de]serialization use cases may elect to allocate a buffer and
|
|
|
|
* follow the linear fast paths; perhaps even enabling flagExpandable for hopefully-smarter-than-stdvec-scaling
|
|
|
|
*
|
|
|
|
* Ring buffers scale from the write head, to the read head, potentially going-around in the process
|
|
|
|
*
|
|
|
|
* Linear flagExpandable buffers scale from [0, length]; reallocating at end of buffer if flagExpandable is enabled
|
|
|
|
* if expanding is enabled,
|
|
|
|
* realloc(max(size + offset, (offset / kBufferPageSize + 1) * kBufferPageSize))
|
|
|
|
*
|
|
|
|
* Deprecates INetworkStream, fixes allocation issues around compression backends
|
|
|
|
* Superseeds abuse of AuList<AuUInt8> for binary blobs, alongside Memory::Array
|
|
|
|
*/
|
|
|
|
struct ByteBuffer
|
|
|
|
{
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Stable ByteBuffer ABI Header; length and read/write head pointers //
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/// Internal capacity to mitigate trivial reallocs
|
|
|
|
AuUInt allocSize;
|
|
|
|
|
|
|
|
/// Abstract size
|
|
|
|
AuUInt length;
|
|
|
|
|
|
|
|
/// Buffer pointer
|
|
|
|
AuUInt8 *base;
|
|
|
|
/// Stream pointer
|
|
|
|
AuUInt8 *readPtr;
|
|
|
|
/// Stream pointer
|
|
|
|
AuUInt8 *writePtr;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Stable ByteBuffer ABI Header; u32 flags //
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/// Is ring buffer?
|
2022-01-18 16:49:50 +00:00
|
|
|
AuUInt8 flagCircular : 1;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
/// Should resize linear buffer to accommodate additional writes
|
2022-01-18 16:49:50 +00:00
|
|
|
AuUInt8 flagExpandable : 1;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 16:49:50 +00:00
|
|
|
AuUInt8 flagReadError : 1;
|
|
|
|
AuUInt8 flagWriteError : 1;
|
|
|
|
// - implicit padding
|
|
|
|
AuUInt8 scaleSize;// : 4; screw it.... we should just take 6 * (4/8) up to 32/64, we wont go up a slab allocation bucket, whatever you want to call it
|
2021-09-06 10:58:08 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
2022-01-18 16:56:54 +00:00
|
|
|
ByteBuffer(ByteBuffer &&buffer)
|
|
|
|
{
|
|
|
|
this->base = buffer.base;
|
|
|
|
this->length = buffer.length;
|
|
|
|
this->allocSize = buffer.length;
|
|
|
|
this->writePtr = this->base + (buffer.writePtr - buffer.base);
|
|
|
|
this->readPtr = this->base + (buffer.readPtr - buffer.base);
|
|
|
|
this->flagCircular = buffer.flagCircular;
|
|
|
|
this->flagExpandable = buffer.flagExpandable;
|
|
|
|
this->scaleSize = buffer.scaleSize;
|
|
|
|
buffer.base = {};
|
|
|
|
buffer.length = {};
|
|
|
|
buffer.allocSize = {};
|
|
|
|
buffer.writePtr = {};
|
|
|
|
buffer.readPtr = {};
|
|
|
|
buffer.flagCircular = {};
|
|
|
|
buffer.flagExpandable = {};
|
|
|
|
buffer.scaleSize = {};
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
ByteBuffer(const ByteBuffer &buffer, bool preservePointers = true)
|
|
|
|
{
|
2022-01-18 16:49:50 +00:00
|
|
|
this->base = FAlloc<AuUInt8 *>(buffer.length);
|
|
|
|
if (!this->base) AU_THROW_STRING("memory error");
|
2021-09-06 10:58:08 +00:00
|
|
|
this->length = buffer.length;
|
|
|
|
this->allocSize = buffer.length;
|
|
|
|
if (preservePointers)
|
|
|
|
{
|
|
|
|
this->writePtr = this->base + (buffer.writePtr - buffer.base);
|
|
|
|
this->readPtr = this->base + (buffer.readPtr - buffer.base);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->writePtr = this->base;
|
|
|
|
this->readPtr = this->base;
|
|
|
|
}
|
2022-01-19 17:08:13 +00:00
|
|
|
AuMemcpy(this->base, buffer.base, this->length);
|
2021-09-06 10:58:08 +00:00
|
|
|
this->flagCircular = buffer.flagCircular;
|
|
|
|
this->flagExpandable = buffer.flagExpandable;
|
2022-01-18 16:56:54 +00:00
|
|
|
this->scaleSize = buffer.scaleSize;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer(const void *in, AuUInt length, bool circular = false, bool expandable = false) : flagCircular(circular), flagExpandable(expandable), flagReadError(0), flagWriteError(0)
|
|
|
|
{
|
2022-01-18 16:49:50 +00:00
|
|
|
this->base = FAlloc<AuUInt8 *>(length);
|
|
|
|
if (!this->base) AU_THROW_STRING("memory error");
|
2021-09-06 10:58:08 +00:00
|
|
|
this->length = length;
|
|
|
|
this->allocSize = length;
|
|
|
|
this->readPtr = this->base;
|
|
|
|
this->writePtr = this->readPtr + this->length;
|
2022-01-19 17:08:13 +00:00
|
|
|
AuMemcpy(this->base, in, this->length);
|
2022-01-18 16:49:50 +00:00
|
|
|
this->scaleSize = kBufferInitialPower;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer(const AuList<AuUInt8> &vector, bool circular = false, bool expandable = false) : flagCircular(circular), flagExpandable(expandable), flagReadError(0), flagWriteError(0)
|
|
|
|
{
|
2022-01-18 16:49:50 +00:00
|
|
|
this->base = FAlloc<AuUInt8 *>(vector.size());
|
|
|
|
if (!this->base) AU_THROW_STRING("memory error");
|
2021-09-06 10:58:08 +00:00
|
|
|
this->length = vector.size();
|
|
|
|
this->allocSize = vector.size();
|
|
|
|
this->readPtr = this->base;
|
|
|
|
this->writePtr = this->readPtr + this->length;
|
2022-01-19 17:08:13 +00:00
|
|
|
AuMemcpy(this->base, vector.data(), this->length);
|
2022-01-18 16:49:50 +00:00
|
|
|
this->scaleSize = kBufferInitialPower;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer(AuUInt length, bool circular = false, bool expandable = false) : flagCircular(circular), flagExpandable(expandable), flagReadError(0), flagWriteError(0)
|
|
|
|
{
|
|
|
|
this->base = ZAlloc<AuUInt8 *>(length);
|
2022-01-18 16:49:50 +00:00
|
|
|
if (!this->base) AU_THROW_STRING("memory error");
|
2021-09-06 10:58:08 +00:00
|
|
|
this->length = length;
|
|
|
|
this->allocSize = length;
|
|
|
|
this->readPtr = this->base;
|
|
|
|
this->writePtr = this->base;
|
2022-01-18 16:49:50 +00:00
|
|
|
this->scaleSize = kBufferInitialPower;
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 23:56:26 +00:00
|
|
|
ByteBuffer() : flagCircular(0), flagExpandable(0), flagReadError(0), flagWriteError(0)
|
|
|
|
{
|
|
|
|
this->base = {};
|
|
|
|
this->length = {};
|
|
|
|
this->allocSize = {};
|
|
|
|
this->readPtr = {};
|
|
|
|
this->writePtr = {};
|
2022-01-18 16:49:50 +00:00
|
|
|
this->scaleSize = kBufferInitialPower;
|
2021-09-14 23:56:26 +00:00
|
|
|
}
|
|
|
|
|
2021-09-06 10:58:08 +00:00
|
|
|
~ByteBuffer()
|
|
|
|
{
|
2021-09-14 23:56:26 +00:00
|
|
|
if (this->base)
|
|
|
|
{
|
|
|
|
Free(this->base);
|
|
|
|
}
|
2021-09-06 10:58:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void ResetPositions()
|
|
|
|
{
|
|
|
|
this->flagReadError = 0;
|
|
|
|
this->flagWriteError = 0;
|
|
|
|
this->readPtr = base;
|
|
|
|
this->writePtr = base;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Iterator
|
2022-01-18 18:59:19 +00:00
|
|
|
inline auline AuUInt8 * begin() const;
|
|
|
|
inline auline AuUInt8 * end() const;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
// Utils To alternative types
|
|
|
|
inline auline AuList<AuUInt8> ToVector() const;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline AuUInt32 GetAllocationPower() const;
|
|
|
|
inline operator AuList<AuUInt8>() const;
|
|
|
|
inline operator MemoryViewRead() const;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline AuList<AuUInt8> RemainingBytesToVector(bool endAtWrite = true) const;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
// Seek / Position
|
|
|
|
inline auline bool ReaderTryGoForward(AuUInt32 offset);
|
|
|
|
inline auline bool ReaderTryGoBack(AuUInt32 offset);
|
|
|
|
|
|
|
|
inline auline bool WriterTryGoForward(AuUInt32 offset);
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline auline AuUInt RemainingWrite(bool endAtRead = true);
|
|
|
|
inline auline AuUInt RemainingBytes(bool endAtWrite = true);
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline auline bool Skip(AuUInt count);
|
|
|
|
inline auline AuUInt GetReadOffset() const;
|
|
|
|
inline auline AuUInt GetWriteOffset() const;
|
2021-09-06 10:58:08 +00:00
|
|
|
|
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline AuOptional<AuUInt8 *> WriterTryGetWriteHeadFor(AuUInt32 nBytes);
|
2021-09-06 13:57:33 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
// Memory operations
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline auline bool Allocate(AuUInt length, bool fast = true);
|
|
|
|
inline auline bool SetBuffer(const void *in, AuUInt length);
|
|
|
|
inline auline bool SetBuffer(const AuList<AuUInt8> &buffer);
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline auline void GC();
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline auline bool Resize(AuUInt length);
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
// Basic Read Write
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
inline auline AuUInt Write(const void *buffer, AuUInt requestLength);
|
|
|
|
inline auline AuUInt Read(void *out, AuUInt requestedLength, bool peek = false);
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
// Typed read/write
|
|
|
|
template<typename T>
|
|
|
|
T Read();
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
template<typename T>
|
|
|
|
bool Write(const T &in);
|
2021-09-06 10:58:08 +00:00
|
|
|
|
2022-01-18 18:59:19 +00:00
|
|
|
template<typename T>
|
|
|
|
bool Read(T &out);
|
2021-09-06 10:58:08 +00:00
|
|
|
};
|
|
|
|
}
|