From fe839afa6e5e2dd5e618a5dd880b6d017ea78f1f Mon Sep 17 00:00:00 2001 From: Stone Date: Tue, 7 Jun 2016 09:38:00 +0000 Subject: [PATCH] =?UTF-8?q?=E5=BD=BB=E5=BA=95=E8=A7=A3=E9=99=A4=E5=AF=B9st?= =?UTF-8?q?ring.h=E7=9A=84=E4=BE=9D=E8=B5=96=EF=BC=8C=E9=92=88=E5=AF=B9?= =?UTF-8?q?=E6=B8=85=E9=9B=B6=E5=92=8C=E6=8B=B7=E8=B4=9D=EF=BC=8C=E9=87=8D?= =?UTF-8?q?=E5=86=99=E5=BF=AB=E9=80=9F=E5=AE=9E=E7=8E=B0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Array.cpp | 29 +++++++------ Core/Buffer.cpp | 99 +++++++++++++++++++++++++++++++++++++++--- Core/Buffer.h | 2 + Message/BinaryPair.cpp | 7 +-- Sys.h | 2 - Test/BufferTest.cpp | 6 +-- 6 files changed, 117 insertions(+), 28 deletions(-) diff --git a/Core/Array.cpp b/Core/Array.cpp index b1f4e08b..df4df2a4 100644 --- a/Core/Array.cpp +++ b/Core/Array.cpp @@ -1,8 +1,4 @@ -//#include -//#include -//#include -//#include -#include +//#include #include "_Core.h" @@ -185,16 +181,19 @@ bool Array::SetItem(const void* data, int index, int count) int len2 = index + count; CheckCapacity(len2, index); - byte* buf = (byte*)GetBuffer(); + //byte* buf = (byte*)GetBuffer(); // 如果元素类型大小为1,那么可以直接调用内存设置函数 if(_Size == 1) - memset(&buf[index], *(byte*)data, count); + //memset(&buf[index], *(byte*)data, count); + Set(*(byte*)data, index, count); else { while(count-- > 0) { - memcpy(buf, data, _Size); - buf += _Size; + //memcpy(buf, data, _Size); + //buf += _Size; + Copy(index, data, _Size); + index += _Size; } } @@ -235,7 +234,8 @@ void Array::Clear() assert(_canWrite, "禁止Clear修改"); assert(_Arr, "Clear数据不能为空指针"); - memset(_Arr, 0, _Size * _Length); + //memset(_Arr, 0, _Size * _Length); + Set(0, 0, _Size * _Length); } // 设置指定位置的值,不足时自动扩容 @@ -248,7 +248,8 @@ void Array::SetItemAt(int i, const void* item) if(i >= _Length) _Length = i + 1; - memcpy((byte*)_Arr + _Size * i, item, _Size); + //memcpy((byte*)_Arr + _Size * i, item, _Size); + Copy(_Size * i, item, _Size); } // 重载索引运算符[],返回指定元素的第一个字节 @@ -321,12 +322,14 @@ bool operator==(const Array& bs1, const Array& bs2) { if(bs1.Length() != bs2.Length()) return false; - return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) == 0; + return bs1.CompareTo(bs2) == 0; + //return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) == 0; } bool operator!=(const Array& bs1, const Array& bs2) { if(bs1.Length() != bs2.Length()) return true; - return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) != 0; + return bs1.CompareTo(bs2) != 0; + //return memcmp(bs1._Arr, bs2._Arr, bs1.Length() * bs1._Size) != 0; } diff --git a/Core/Buffer.cpp b/Core/Buffer.cpp index 428a138b..d972339d 100644 --- a/Core/Buffer.cpp +++ b/Core/Buffer.cpp @@ -1,10 +1,13 @@ -#include +//#include #include "_Core.h" #include "Buffer.h" #include "SString.h" +static void memset(byte* ptr, byte item, uint len); +static void memcpy(byte* dst, const byte* src, uint len); + /******************************** Buffer ********************************/ Buffer::Buffer(void* ptr, int len) @@ -134,7 +137,7 @@ int Buffer::Copy(int destIndex, const void* src, int len) if(_Arr == src) return len; // 拷贝数据 - if(len) memmove((byte*)_Arr + destIndex, src, len); + if(len) memcpy((byte*)_Arr + destIndex, (byte*)src, len); return len; } @@ -172,7 +175,7 @@ int Buffer::CopyTo(int srcIndex, void* data, int len) const if(len) { if(data != (byte*)_Arr + srcIndex) - memcpy(data, (byte*)_Arr + srcIndex, len); + memcpy((byte*)data, (byte*)_Arr + srcIndex, len); } return len; @@ -337,13 +340,40 @@ String Buffer::AsString() const return str; } +int Buffer::CompareTo(const Buffer& bs) const +{ + return CompareTo(bs._Arr, bs._Length); +} + +int Buffer::CompareTo(const void* ptr, int len) const +{ + if(len < 0) len = 0xFFFF; + + // 同一个指针,长度决定大小 + if(_Arr == ptr) return _Length - len; + + // 以最短长度来比较 + int count = _Length; + if(count > len) count = len; + + // 遍历每一个字符 + for(cstring p1=_Arr, p2=(cstring)ptr; count>0; count--, p1++, p2++) + { + int n = *p1 - *p2; + if(n) return n; + } + + // 判断剩余长度,以此决定大小 + return _Length - len; +} + bool operator == (const Buffer& bs1, const Buffer& bs2) { if(bs1._Arr == bs2._Arr) return true; if(!bs1._Arr || !bs2._Arr) return false; if(bs1.Length() != bs2.Length()) return false; - return memcmp(bs1._Arr, bs2._Arr, bs1.Length()) == 0; + return bs1.CompareTo(bs2) == 0; } bool operator == (const Buffer& bs1, const void* ptr) @@ -351,7 +381,7 @@ bool operator == (const Buffer& bs1, const void* ptr) if(bs1._Arr == ptr) return true; if(!bs1._Arr || !ptr) return false; - return memcmp(bs1._Arr, ptr, bs1.Length()) == 0; + return bs1.CompareTo(ptr) == 0; } bool operator != (const Buffer& bs1, const Buffer& bs2) @@ -360,7 +390,7 @@ bool operator != (const Buffer& bs1, const Buffer& bs2) if(!bs1._Arr || !bs2._Arr) return true; if(bs1.Length() != bs2.Length()) return true; - return memcmp(bs1._Arr, bs2._Arr, bs1.Length()) != 0; + return bs1.CompareTo(bs2) != 0; } bool operator != (const Buffer& bs1, const void* ptr) @@ -368,5 +398,60 @@ bool operator != (const Buffer& bs1, const void* ptr) if(bs1._Arr == ptr) return false; if(!bs1._Arr || !ptr) return true; - return memcmp(bs1._Arr, ptr, bs1.Length()) != 0; + return bs1.CompareTo(ptr) != 0; +} + +void memset(byte* ptr, byte item, uint len) +{ + // 为了加快速度,分头中尾三部分 + byte* p = ptr; + + // 为了让中间部分凑够4字节对齐 + int n = (uint)p & 0x03; + for(int i=0; i0; i++, len--) + *p++ = item; + + // 中间部分,4字节对齐 + if(len > 4) + { + int v = (item << 24) | (item << 16) | (item << 8) | item; + int* pi = (int*)p; + for(; len>0; len-=4) + *pi++ = v; + p = (byte*)pi; + } + + // 结尾部分 + while(len--) + *p++ = item; +} + +void memcpy(byte* dst, const byte* src, uint len) +{ + // 为了加快速度,分头中尾三部分 + + // 如果两个不能同时对齐,那么无法使用快速拷贝 + int nd = (uint)dst & 0x03; + int ns = (uint)src & 0x03; + if(nd == ns) + { + // 为了让中间部分凑够4字节对齐 + for(int i=0; i0; i++, len--) + *dst++ = *src++; + + // 中间部分,4字节对齐 + if(len > 4) + { + int* pd = (int*)dst; + int* ps = (int*)src; + for(; len>0; len-=4) + *pd++ = *ps++; + dst = (byte*)pd; + src = (byte*)ps; + } + } + + // 结尾部分 + while(len--) + *dst++ = *src++; } diff --git a/Core/Buffer.h b/Core/Buffer.h index f4d8fe7e..ba60063c 100644 --- a/Core/Buffer.h +++ b/Core/Buffer.h @@ -91,6 +91,8 @@ public: explicit operator bool() const { return _Length > 0; } bool operator !() const { return _Length == 0; } + int CompareTo(const Buffer& bs) const; + int CompareTo(const void* ptr, int len = -1) const; friend bool operator == (const Buffer& bs1, const Buffer& bs2); friend bool operator == (const Buffer& bs1, const void* ptr); friend bool operator != (const Buffer& bs1, const Buffer& bs2); diff --git a/Message/BinaryPair.cpp b/Message/BinaryPair.cpp index 003b1f4f..be2d4cd8 100644 --- a/Message/BinaryPair.cpp +++ b/Message/BinaryPair.cpp @@ -1,6 +1,6 @@ #include "BinaryPair.h" -#include +//#include // 初始化消息,各字段为0 /*BinaryPair::BinaryPair(Buffer& bs) @@ -34,7 +34,8 @@ Buffer BinaryPair::Get(cstring name) const // 从当前位置开始向后找,如果找不到,再从头开始找到当前位置。 // 这样子安排,如果是顺序读取,将会提升性能 - int slen = strlen(name); + //int slen = strlen(name); + String sn = name; auto& ms = *_s; uint p = ms.Position(); @@ -53,7 +54,7 @@ Buffer BinaryPair::Get(cstring name) const if(ln2 <0 || ln2 > ms.Remain()) return err; auto dt = ms.ReadBytes(ln2); - if(len == slen && strncmp(name, (cstring)nm, len) == 0) return Buffer(dt, ln2); + if(sn == String((cstring)nm, len)) return Buffer(dt, ln2); } // 从头开始再来一次 diff --git a/Sys.h b/Sys.h index 83ebb0d9..171a922e 100644 --- a/Sys.h +++ b/Sys.h @@ -4,8 +4,6 @@ #include #include #include -/*#include -#include */ #include "Core\Type.h" #include "Core\Buffer.h" diff --git a/Test/BufferTest.cpp b/Test/BufferTest.cpp index 14c2e48a..246ce6dd 100644 --- a/Test/BufferTest.cpp +++ b/Test/BufferTest.cpp @@ -1,6 +1,6 @@ #include "Sys.h" -#include +//#include #if DEBUG static void TestAssign() @@ -97,8 +97,8 @@ void Buffer::Test() // 拷贝数据,默认-1长度表示当前长度 char abc[] = "abcd"; - bs.Copy(5, abc, strlen(abc)); - debug_printf("Copy(5, \"abcd\", %d) => %s\r\n", strlen(abc), cs); + bs.Copy(5, abc, sizeof(abc)); + debug_printf("Copy(5, \"abcd\", %d) => %s\r\n", sizeof(abc), cs); assert(cs[5] == 'a' && cs[8] == 'd', "int Copy(int destIndex, const void* src, int len)"); // 把数据复制到目标缓冲区,默认-1长度表示当前长度