summaryrefslogtreecommitdiff
path: root/source/modules/asura-utils/io
diff options
context:
space:
mode:
Diffstat (limited to 'source/modules/asura-utils/io')
-rw-r--r--source/modules/asura-utils/io/binding/_data_buffer.cpp45
-rw-r--r--source/modules/asura-utils/io/data_buffer.cpp116
-rw-r--r--source/modules/asura-utils/io/data_buffer.h35
-rw-r--r--source/modules/asura-utils/io/file.cpp8
-rw-r--r--source/modules/asura-utils/io/io_task.cpp4
5 files changed, 149 insertions, 59 deletions
diff --git a/source/modules/asura-utils/io/binding/_data_buffer.cpp b/source/modules/asura-utils/io/binding/_data_buffer.cpp
index cd73b31..ac240e5 100644
--- a/source/modules/asura-utils/io/binding/_data_buffer.cpp
+++ b/source/modules/asura-utils/io/binding/_data_buffer.cpp
@@ -10,12 +10,13 @@ namespace AsuraEngine
LUAX_REGISTRY(DataBuffer)
{
LUAX_REGISTER_METHODS(state,
- { "New", _New },
- { "GetData", _GetData },
- { "GetSize", _GetSize },
- { "Refactor", _Refactor },
- { "Load", _Load },
- { "Clear", _Clear }
+ { "New", _New },
+ { "GetData", _GetData },
+ { "GetSize", _GetSize },
+ { "GetCapacity", _GetCapacity },
+ { "Refactor", _Refactor },
+ { "Load", _Load },
+ { "Clear", _Clear }
);
}
@@ -24,7 +25,7 @@ namespace AsuraEngine
}
// databuffer = DataBuffer.New(lstring)
- // databuffer = DataBuffer.New(size)
+ // databuffer = DataBuffer.New(capacity)
LUAX_IMPL_METHOD(DataBuffer, _New)
{
LUAX_STATE(L);
@@ -39,8 +40,8 @@ namespace AsuraEngine
}
else if (state.IsType(1, LUA_TNUMBER))
{
- size_t size = lua_tonumber(L, 1);
- DataBuffer* buffer = new DataBuffer(size);
+ size_t capacity = lua_tonumber(L, 1);
+ DataBuffer* buffer = new DataBuffer(capacity);
buffer->PushLuaxUserdata(state);
return 1;
}
@@ -70,13 +71,23 @@ namespace AsuraEngine
return 1;
}
- // databuffer:Refactor(size)
+ // capacity = databuffer:GetCapacity()
+ LUAX_IMPL_METHOD(DataBuffer, _GetCapacity)
+ {
+ LUAX_SETUP(L, "U");
+
+ DataBuffer* self = state.GetUserdata<DataBuffer>(1);
+ lua_pushinteger(L, self->GetCapacity());
+ return 1;
+ }
+
+ // databuffer:Refactor(capacity)
LUAX_IMPL_METHOD(DataBuffer, _Refactor)
{
LUAX_PREPARE(L, DataBuffer);
- size_t size = state.CheckValue<int>(2);
- self->Refactor(size);
+ size_t capacity = state.CheckValue<int>(2);
+ self->Refactor(capacity);
return 0;
}
@@ -92,16 +103,14 @@ namespace AsuraEngine
if (state.IsType(2, LUA_TSTRING))
{
data = lua_tolstring(L, 2, &size);
- size_t len = buffer->Load(data, size);
- state.Push(len);
- return 1;
+ buffer->Load(data, size);
+ return 0;
}
else if(state.IsType(2, LUA_TUSERDATA))
{
DataBuffer* src = state.CheckUserdata<DataBuffer>(2);
- size_t len = buffer->Load(*src);
- state.Push(len);
- return 1;
+ buffer->Load(*src);
+ return 0;
}
else
{
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();
diff --git a/source/modules/asura-utils/io/data_buffer.h b/source/modules/asura-utils/io/data_buffer.h
index 445bdf4..61d158b 100644
--- a/source/modules/asura-utils/io/data_buffer.h
+++ b/source/modules/asura-utils/io/data_buffer.h
@@ -22,17 +22,33 @@ namespace AsuraEngine
LUAX_DECL_FACTORY(DataBuffer);
DataBuffer(DataBuffer& src);
- DataBuffer(std::size_t size);
+ DataBuffer(std::size_t capacity);
DataBuffer(const void* bytes, std::size_t size);
~DataBuffer();
byte* GetData();
size_t GetSize();
-
- void Refactor(size_t size);
- size_t Load(DataBuffer& db);
- size_t Load(const void* bytes, std::size_t size);
+ size_t GetCapacity();
+
+ ///
+ /// ޸
+ ///
+ void Refactor(size_t capacity);
+
+ ///
+ /// Դ˻
+ ///
+ void Load(DataBuffer& db);
+ void Load(const void* bytes, std::size_t size);
+
+ ///
+ /// bytesӵȨcapacityΪsize
+ ///
void Move(void* bytes, std::size_t size);
+
+ ///
+ ///
+ ///
void Clear();
void Lock();
@@ -40,14 +56,23 @@ namespace AsuraEngine
private:
+ ///
+ /// Buffer׵ַݵij
+ ///
byte* mBytes;
size_t mSize;
+ ///
+ /// Buffer
+ ///
+ size_t mCapacity;
+
AEThreading::Mutex mMutex;
LUAX_DECL_METHOD(_New);
LUAX_DECL_METHOD(_GetData);
LUAX_DECL_METHOD(_GetSize);
+ LUAX_DECL_METHOD(_GetCapacity);
LUAX_DECL_METHOD(_Refactor);
LUAX_DECL_METHOD(_Load);
LUAX_DECL_METHOD(_Clear);
diff --git a/source/modules/asura-utils/io/file.cpp b/source/modules/asura-utils/io/file.cpp
index ed95436..9e89c85 100644
--- a/source/modules/asura-utils/io/file.cpp
+++ b/source/modules/asura-utils/io/file.cpp
@@ -122,7 +122,7 @@ namespace AsuraEngine
{
ASSERT(dst);
- if (dst->GetSize() < length)
+ if (dst->GetCapacity() < length)
throw Exception("Data buffer is too small compares to read length.");
if (!mFileHandle || mMode != FILE_MODE_READ)
@@ -149,11 +149,13 @@ namespace AsuraEngine
size_t length = PHYSFS_fileLength(mFileHandle);
- if (dst->GetSize() < length)
+ if (dst->GetCapacity() < length)
throw Exception("Data buffer is too small compares to file length.");
dst->Lock();
- size_t size = PHYSFS_readBytes(mFileHandle, dst->GetData(), length);
+ byte* data = dst->GetData();
+ size_t size = PHYSFS_readBytes(mFileHandle, data, length);
+ dst->Move(data, length);
dst->Unlock();
return size;
}
diff --git a/source/modules/asura-utils/io/io_task.cpp b/source/modules/asura-utils/io/io_task.cpp
index 361b9c5..5f0e1c8 100644
--- a/source/modules/asura-utils/io/io_task.cpp
+++ b/source/modules/asura-utils/io/io_task.cpp
@@ -43,9 +43,9 @@ namespace AsuraEngine
if (mCallback)
{
LuaxScopedState state(invokeThreaad);
- if (PushLuaxMemberRef(state, mCallback))
+ if (this->PushLuaxMemberRef(state, mCallback))
{
- PushLuaxMemberRef(state, mBufferRef);
+ this->PushLuaxMemberRef(state, mBufferRef);
state.Call(1, 0);
}
}