diff options
Diffstat (limited to 'source/modules/asura-utils/io/data_buffer.cpp')
-rw-r--r-- | source/modules/asura-utils/io/data_buffer.cpp | 116 |
1 files changed, 85 insertions, 31 deletions
diff --git a/source/modules/asura-utils/io/data_buffer.cpp b/source/modules/asura-utils/io/data_buffer.cpp index 5049b38..36e13c7 100644 --- a/source/modules/asura-utils/io/data_buffer.cpp +++ b/source/modules/asura-utils/io/data_buffer.cpp @@ -10,65 +10,109 @@ namespace AsuraEngine { DataBuffer::DataBuffer(DataBuffer& src) + : mSize(0) + , mCapacity(0) + , mBytes(nullptr) { - Load(src); + // ʼ + lock(mMutex) + { + mCapacity = src.mSize; + mBytes = new byte[mCapacity]; + Clear(); + + Load(src); + } } - DataBuffer::DataBuffer(std::size_t size) - : mSize(size) + DataBuffer::DataBuffer(std::size_t capacity) + : mSize(0) + , mCapacity(0) , mBytes(nullptr) { - lock(mMutex); - mBytes = new byte[size]; - memset(mBytes, 0, size); + lock(mMutex) + { + mCapacity = capacity; + mBytes = new byte[mCapacity]; + Clear(); + } } DataBuffer::DataBuffer(const void* data, std::size_t size) - : mSize(size) + : mCapacity(0) + , mSize(0) , mBytes(nullptr) { - Load(data, size); + lock(mMutex) + { + mCapacity = size; + mBytes = new byte[mCapacity]; + Clear(); + + Load(data, size); + } } DataBuffer::~DataBuffer() { - delete[] mBytes; + lock(mMutex) + { + delete[] mBytes; + } } - void DataBuffer::Refactor(size_t size) + void DataBuffer::Refactor(size_t capacity) { - lock(mMutex); - if (!mBytes || mSize != size) + lock(mMutex) { - delete[] mBytes; - mBytes = new byte[size]; - mSize = size; + if (!mBytes || mCapacity != capacity) + { + if(mBytes) + delete[] mBytes; + mCapacity = capacity; + mBytes = new byte[mCapacity]; + mSize = 0; + } + Clear(); } - memset(mBytes, 0, size * sizeof(byte)); } - size_t DataBuffer::Load(DataBuffer& db) + void DataBuffer::Load(DataBuffer& db) { - return Load(db.GetData(), db.GetSize()); + lock(mMutex) + { + Load(db.GetData(), db.GetSize()); + } } - size_t DataBuffer::Load(const void* data, std::size_t size) + void DataBuffer::Load(const void* data, std::size_t size) { - lock(mMutex); - size_t len = mSize > size ? size : mSize; - memcpy(mBytes, data, len); - return len; + lock(mMutex) + { + ASSERT(mCapacity >= size); + memcpy(mBytes, data, size); + mSize = size; + } } void DataBuffer::Move(void* bytes, std::size_t size) { - lock(mMutex); - if (!mBytes) + lock(mMutex) { - delete[] mBytes; + if (mBytes == bytes) + { + // sizeֵڶļʱ + mSize = size; + } + else + { + if (mBytes) + delete[] mBytes; + mBytes = (byte*)bytes; + mSize = size; + mCapacity = size; + } } - mBytes = (byte*)bytes; - mSize = size; } byte* DataBuffer::GetData() @@ -78,9 +122,14 @@ namespace AsuraEngine void DataBuffer::Clear() { - lock(mMutex); - if (mBytes) - memset(mBytes, 0, mSize); + lock(mMutex) + { + if (mBytes) + { + memset(mBytes, 0, mSize); + mSize = 0; + } + } } std::size_t DataBuffer::GetSize() @@ -88,6 +137,11 @@ namespace AsuraEngine return mSize; } + std::size_t DataBuffer::GetCapacity() + { + return mCapacity; + } + void DataBuffer::Lock() { mMutex.Lock(); |