summaryrefslogtreecommitdiff
path: root/source/modules/asura-utils/IO/DataBuffer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'source/modules/asura-utils/IO/DataBuffer.cpp')
-rw-r--r--source/modules/asura-utils/IO/DataBuffer.cpp156
1 files changed, 156 insertions, 0 deletions
diff --git a/source/modules/asura-utils/IO/DataBuffer.cpp b/source/modules/asura-utils/IO/DataBuffer.cpp
new file mode 100644
index 0000000..6660a94
--- /dev/null
+++ b/source/modules/asura-utils/IO/DataBuffer.cpp
@@ -0,0 +1,156 @@
+#include <cstdlib>
+#include <cstring>
+#include "DataBuffer.h"
+
+using namespace AEThreading;
+
+namespace AsuraEngine
+{
+ namespace IO
+ {
+
+ DataBuffer::DataBuffer(DataBuffer& src)
+ : m_Size(0)
+ , m_Capacity(0)
+ , m_Bytes(nullptr)
+ {
+ // ʼ
+ lock(m_Mutex)
+ {
+ m_Capacity = src.m_Size;
+ m_Bytes = new byte[m_Capacity];
+ Clear();
+
+ Load(src);
+ }
+ }
+
+ DataBuffer::DataBuffer(std::size_t capacity)
+ : m_Size(0)
+ , m_Capacity(0)
+ , m_Bytes(nullptr)
+ {
+ lock(m_Mutex)
+ {
+ m_Capacity = capacity;
+ m_Bytes = new byte[m_Capacity];
+ Clear();
+ }
+ }
+
+ DataBuffer::DataBuffer(const void* data, std::size_t size)
+ : m_Capacity(0)
+ , m_Size(0)
+ , m_Bytes(nullptr)
+ {
+ lock(m_Mutex)
+ {
+ m_Capacity = size;
+ m_Bytes = new byte[m_Capacity];
+ Clear();
+
+ Load(data, size);
+ }
+ }
+
+ DataBuffer::~DataBuffer()
+ {
+ lock(m_Mutex)
+ {
+ delete[] m_Bytes;
+ }
+ }
+
+ void DataBuffer::Refactor(size_t capacity)
+ {
+ lock(m_Mutex)
+ {
+ if (!m_Bytes || m_Capacity != capacity)
+ {
+ if(m_Bytes)
+ delete[] m_Bytes;
+ m_Capacity = capacity;
+ m_Bytes = new byte[m_Capacity];
+ m_Size = 0;
+ }
+ Clear();
+ }
+ }
+
+ void DataBuffer::Load(DataBuffer& db)
+ {
+ lock(m_Mutex)
+ {
+ Load(db.GetData(), db.GetSize());
+ }
+ }
+
+ void DataBuffer::Load(const void* data, std::size_t size)
+ {
+ lock(m_Mutex)
+ {
+ ASSERT(m_Capacity >= size);
+ memcpy(m_Bytes, data, size);
+ m_Size = size;
+ }
+ }
+
+ void DataBuffer::Move(void* bytes, std::size_t size)
+ {
+ lock(m_Mutex)
+ {
+ if (m_Bytes == bytes)
+ {
+ // sizeֵڶļʱ
+ m_Size = size;
+ }
+ else
+ {
+ if (m_Bytes)
+ delete[] m_Bytes;
+ m_Bytes = (byte*)bytes;
+ m_Size = size;
+ m_Capacity = size;
+ }
+ }
+ }
+
+ byte* DataBuffer::GetData()
+ {
+ return m_Bytes;
+ }
+
+ void DataBuffer::Clear()
+ {
+ lock(m_Mutex)
+ {
+ if (m_Bytes)
+ {
+ memset(m_Bytes, 0, m_Size);
+ m_Size = 0;
+ }
+ }
+ }
+
+ std::size_t DataBuffer::GetSize()
+ {
+ return m_Size;
+ }
+
+ std::size_t DataBuffer::GetCapacity()
+ {
+ return m_Capacity;
+ }
+
+ void DataBuffer::Lock()
+ {
+ m_Mutex.Lock();
+ }
+
+ void DataBuffer::Unlock()
+ {
+ m_Mutex.Unlock();
+ }
+
+ }
+} \ No newline at end of file