[+] added AU_THROW_STRING

[*] extend ByteBuffer
[*] minor reformatting
This commit is contained in:
Reece Wilson 2022-01-18 16:49:50 +00:00
parent 29bd8dca0c
commit 09d1db69f2
5 changed files with 100 additions and 23 deletions

View File

@ -14,11 +14,11 @@ namespace Aurora::Locale
{
struct LocalizationInfo
{
LocalizationInfo(const AuString& language, const AuString& country, const AuString& codeset, const ECodePage codePage) : language(language), country(country), codeset(codeset), codepage(codePage) {}
LocalizationInfo(const AuString &language, const AuString &country, const AuString &codeset, const ECodePage codePage) : language(language), country(country), codeset(codeset), codepage(codePage) {}
const AuString& language; /// ISO 639
const AuString& country; /// ISO 3166
const AuString& codeset; ///
const AuString &language; /// ISO 639
const AuString &country; /// ISO 3166
const AuString &codeset; ///
const ECodePage codepage; /// Potentially eSysUnk; noting that eSysUnk is valid and handlable by the internal backend
};

View File

@ -9,7 +9,9 @@
namespace Aurora::Memory
{
static const auto kBufferPageSize = 512;
static const auto kBufferPageSize = 512;
//static const auto kBufferBasePower = 8;
static const auto kBufferInitialPower = 9;// -kBufferBasePower; // 4-bit integer
/***
* A bytebuffer object represents a linear, partially-linear resizable, buffer **or** a ring buffer.
@ -59,19 +61,22 @@ namespace Aurora::Memory
// Stable ByteBuffer ABI Header; u32 flags //
///////////////////////////////////////////////////////////////////////
/// Is ring buffer?
AuUInt32 flagCircular : 1;
AuUInt8 flagCircular : 1;
/// Should resize linear buffer to accommodate additional writes
AuUInt32 flagExpandable : 1;
AuUInt8 flagExpandable : 1;
AuUInt32 flagReadError : 1;
AuUInt32 flagWriteError : 1;
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
///////////////////////////////////////////////////////////////////////
ByteBuffer(const ByteBuffer &buffer, bool preservePointers = true)
{
this->base = ZAlloc<AuUInt8 *>(buffer.length);
this->base = FAlloc<AuUInt8 *>(buffer.length);
if (!this->base) AU_THROW_STRING("memory error");
this->length = buffer.length;
this->allocSize = buffer.length;
if (preservePointers)
@ -87,35 +92,42 @@ namespace Aurora::Memory
std::memcpy(this->base, buffer.base, this->length);
this->flagCircular = buffer.flagCircular;
this->flagExpandable = buffer.flagExpandable;
this->scaleSize = kBufferInitialPower;
}
ByteBuffer(const void *in, AuUInt length, bool circular = false, bool expandable = false) : flagCircular(circular), flagExpandable(expandable), flagReadError(0), flagWriteError(0)
{
this->base = ZAlloc<AuUInt8 *>(length);
this->base = FAlloc<AuUInt8 *>(length);
if (!this->base) AU_THROW_STRING("memory error");
this->length = length;
this->allocSize = length;
this->readPtr = this->base;
this->writePtr = this->readPtr + this->length;
std::memcpy(this->base, in, this->length);
this->scaleSize = kBufferInitialPower;
}
ByteBuffer(const AuList<AuUInt8> &vector, bool circular = false, bool expandable = false) : flagCircular(circular), flagExpandable(expandable), flagReadError(0), flagWriteError(0)
{
this->base = ZAlloc<AuUInt8 *>(length);
this->base = FAlloc<AuUInt8 *>(vector.size());
if (!this->base) AU_THROW_STRING("memory error");
this->length = vector.size();
this->allocSize = vector.size();
this->readPtr = this->base;
this->writePtr = this->readPtr + this->length;
std::memcpy(this->base, vector.data(), this->length);
this->scaleSize = kBufferInitialPower;
}
ByteBuffer(AuUInt length, bool circular = false, bool expandable = false) : flagCircular(circular), flagExpandable(expandable), flagReadError(0), flagWriteError(0)
{
this->base = ZAlloc<AuUInt8 *>(length);
if (!this->base) AU_THROW_STRING("memory error");
this->length = length;
this->allocSize = length;
this->readPtr = this->base;
this->writePtr = this->base;
this->scaleSize = kBufferInitialPower;
}
ByteBuffer() : flagCircular(0), flagExpandable(0), flagReadError(0), flagWriteError(0)
@ -125,6 +137,7 @@ namespace Aurora::Memory
this->allocSize = {};
this->readPtr = {};
this->writePtr = {};
this->scaleSize = kBufferInitialPower;
}
~ByteBuffer()
@ -144,11 +157,11 @@ namespace Aurora::Memory
}
// Iterator
AUKN_SYM AuUInt8 *begin();
AUKN_SYM AuUInt8 *end();
AUKN_SYM AuUInt8 *begin() const;
AUKN_SYM AuUInt8 *end() const;
// To alternative types
AUKN_SYM AuList<AuUInt8> ToVector();
AUKN_SYM AuList<AuUInt8> ToVector() const;
// Seek
AUKN_SYM bool ReaderTryGoForward(AuUInt32 offset);
@ -159,6 +172,56 @@ namespace Aurora::Memory
AUKN_SYM AuOptional<AuUInt8 *> WriterTryGetWriteHeadFor(AuUInt32 nBytes);
inline AuUInt32 GetAllocationPower() const
{
return AuUInt32(1) << (AuUInt32(this->scaleSize));
}
inline operator AuList<AuUInt8>() const
{
return ToVector();
}
inline operator MemoryViewRead() const
{
return MemoryViewRead(begin(), end());
}
inline bool Allocate(AuUInt length, bool fast = true)
{
if (this->base)
{
Free(this->base);
this->base = nullptr;
}
this->base = fast ? FAlloc<AuUInt8 *>(length) : ZAlloc<AuUInt8 *>(length);
if (!this->base)
{
return false;
}
this->length = length;
this->allocSize = length;
this->readPtr = this->base;
this->writePtr = this->readPtr + this->length;
return true;
}
inline bool SetBuffer(const void *in, AuUInt length)
{
if (!Allocate(length))
{
return false;
}
std::memcpy(this->base, in, this->length);
return true;
}
inline bool SetBuffer(const AuList<AuUInt8> &buffer)
{
return SetBuffer(buffer.data(), buffer.size());
}
// Template implementation
// TODO: move to .inl
@ -244,6 +307,16 @@ namespace Aurora::Memory
return true;
}
inline void GC()
{
if (this->allocSize == this->length) return;
auto temp = Memory::FRealloc(this->base, this->length);
if (!temp) return;
this->base = temp;
this->length = this->length;
this->allocSize = this->length;
}
inline bool Resize(AuUInt length)
{
AuUInt oldWriteIdx, oldReadIdx, oldLength, newLength;
@ -263,8 +336,9 @@ namespace Aurora::Memory
}
else
{
auto scale = GetAllocationPower();
oldLength = this->length;
newLength = std::max(AuUInt(length), AuUInt(((this->allocSize / kBufferPageSize) + 1) * kBufferPageSize));
newLength = std::max(AuUInt(length), AuUInt(((this->allocSize / scale) + 1) * scale));
nextPtr = ZRealloc(this->base, newLength);
if (!nextPtr)
@ -448,7 +522,7 @@ namespace Aurora::Memory
}
}
inline AuList<AuUInt8> RemainingBytesToVector(bool endAtWrite = true)
inline AuList<AuUInt8> RemainingBytesToVector(bool endAtWrite = true) const
{
AuList<AuUInt8> vec;
@ -502,7 +576,7 @@ namespace Aurora::Memory
return true;
}
inline AuUInt GetReadOffset()
inline AuUInt GetReadOffset() const
{
if (flagCircular)
{
@ -514,7 +588,7 @@ namespace Aurora::Memory
}
}
inline AuUInt GetWriteOffset()
inline AuUInt GetWriteOffset() const
{
if (flagCircular)
{

View File

@ -131,6 +131,10 @@
#define AU_FWD(var) std::forward<decltype(var)>(var)
#endif
#if !defined(AU_THROW_STRING)
#define AU_THROW_STRING(var) throw std::string(var)
#endif
#define AU_ITERATE_ARRAY(index, arry) AuUInt index = 0; index < AuArraySize(arry); index++
#define AU_ITERATE_N(index, n) AuUInt index = 0; index < n; index++
#define AU_ITERATE_N_TO_X(index, n, x) AuUInt index = n; index < x; index++

View File

@ -10,7 +10,7 @@
namespace Aurora::Memory
{
AuList<AuUInt8> ByteBuffer::ToVector()
AuList<AuUInt8> ByteBuffer::ToVector() const
{
AuList<AuUInt8> vec;
vec.resize(length);
@ -129,13 +129,13 @@ namespace Aurora::Memory
}
}
AuUInt8 *ByteBuffer::begin()
AuUInt8 *ByteBuffer::begin() const
{
SysAssert(!flagCircular, "::begin is only available for linear buffers");
return base;
}
AuUInt8 *ByteBuffer::end()
AuUInt8 *ByteBuffer::end() const
{
SysAssert(!flagCircular, "::end is only available for linear buffers");
return base + length;

View File

@ -330,7 +330,6 @@ namespace Aurora::Threading::Threads
return 0;
};
#if defined(AURORA_PLATFORM_WIN32)
BOOL a {};
if (AuxUlibIsDLLSynchronizationHeld(&a))