diff options
-rw-r--r-- | include/m_system.h | 11 | ||||
-rw-r--r-- | libs/win32/mir_core.lib | bin | 488182 -> 488674 bytes | |||
-rw-r--r-- | libs/win64/mir_core.lib | bin | 493348 -> 493860 bytes | |||
-rw-r--r-- | src/mir_core/src/binbuffer.cpp | 129 | ||||
-rw-r--r-- | src/mir_core/src/mir_core.def | 2 |
5 files changed, 112 insertions, 30 deletions
diff --git a/include/m_system.h b/include/m_system.h index b2abca8722..f961e15e3c 100644 --- a/include/m_system.h +++ b/include/m_system.h @@ -470,16 +470,17 @@ public: class MIR_CORE_EXPORT MBinBuffer { - char *m_buf; - size_t m_len; + char *m_buf = nullptr; public: MBinBuffer(); + MBinBuffer(size_t preAlloc); + MBinBuffer(const MBinBuffer &orig); ~MBinBuffer(); - __forceinline char *data() const { return m_buf; } - __forceinline bool isEmpty() const { return m_len == 0; } - __forceinline size_t length() const { return m_len; } + __forceinline char* data() const { return m_buf; } + __forceinline bool isEmpty() const { return m_buf == nullptr; } + size_t length() const; // adds a buffer to the end void append(const void *pBuf, size_t bufLen); diff --git a/libs/win32/mir_core.lib b/libs/win32/mir_core.lib Binary files differindex 2833d2279c..f367e30c78 100644 --- a/libs/win32/mir_core.lib +++ b/libs/win32/mir_core.lib diff --git a/libs/win64/mir_core.lib b/libs/win64/mir_core.lib Binary files differindex d3b3b804a0..39c8d95b2b 100644 --- a/libs/win64/mir_core.lib +++ b/libs/win64/mir_core.lib diff --git a/src/mir_core/src/binbuffer.cpp b/src/mir_core/src/binbuffer.cpp index e5aef775bd..d558dcc7b7 100644 --- a/src/mir_core/src/binbuffer.cpp +++ b/src/mir_core/src/binbuffer.cpp @@ -17,15 +17,81 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include "stdafx.h" -MBinBuffer::MBinBuffer() : - m_buf(nullptr), - m_len(0) +struct BufImpl { + uint32_t size, lockCount; + + BufImpl* alloc(size_t newSize) + { + bool bEmpty = (this == nullptr); + auto *res = (BufImpl *)mir_realloc(this, newSize + sizeof(BufImpl)); + if (bEmpty) { + res->lockCount = 1; + res->size = 0; + } + return res; + } + + BufImpl* realloc(size_t newSize) + { + bool bEmpty; + newSize += sizeof(BufImpl); + if (this != nullptr) { + newSize += size; + bEmpty = false; + } + else bEmpty = true; + + auto *res = (BufImpl *)mir_realloc(this, newSize); + if (bEmpty) { + res->lockCount = 1; + res->size = 0; + } + return res; + } + + void free() + { + if (this == nullptr) + return; + + if (lockCount == 1) + mir_free(this); + else + lockCount--; + } +}; + +__forceinline BufImpl* ptr2buf(char *p) +{ + return (p == nullptr) ? nullptr : (BufImpl*)p-1; +} + +///////////////////////////////////////////////////////////////////////////////////////// + +MBinBuffer::MBinBuffer() +{} + +MBinBuffer::MBinBuffer(const MBinBuffer &orig) +{ + ptr2buf(m_buf)->free(); + + BufImpl *p = ptr2buf(m_buf = orig.m_buf); + if (p) + p->lockCount++; +} + +MBinBuffer::MBinBuffer(size_t preAlloc) +{ + BufImpl *p = (BufImpl *)mir_alloc(sizeof(BufImpl) + preAlloc); + p->lockCount = 1; + p->size = (unsigned)preAlloc; + m_buf = (char*)(p + 1); } MBinBuffer::~MBinBuffer() { - mir_free(m_buf); + ptr2buf(m_buf)->free(); } void MBinBuffer::append(const void *pBuf, size_t bufLen) @@ -33,12 +99,13 @@ void MBinBuffer::append(const void *pBuf, size_t bufLen) if (pBuf == nullptr || bufLen == 0) return; - m_buf = (char*)mir_realloc(m_buf, bufLen + m_len); - if (m_buf) { - memcpy(m_buf + m_len, pBuf, bufLen); - m_len += bufLen; + BufImpl *p = ptr2buf(m_buf)->realloc(bufLen); + if (p) { + m_buf = (char*)(p + 1); + memcpy(m_buf + p->size, pBuf, bufLen); + p->size += (unsigned)bufLen; } - else m_len = 0; + else m_buf = nullptr; } void MBinBuffer::appendBefore(const void *pBuf, size_t bufLen) @@ -46,13 +113,14 @@ void MBinBuffer::appendBefore(const void *pBuf, size_t bufLen) if (pBuf == nullptr || bufLen == 0) return; - m_buf = (char*)mir_realloc(m_buf, bufLen + m_len); - if (m_buf) { - memmove(m_buf + bufLen, m_buf, m_len); + BufImpl *p = ptr2buf(m_buf)->realloc(bufLen); + if (p) { + m_buf = (char *)(p + 1); + memmove(m_buf + bufLen, m_buf, p->size); memcpy(m_buf, pBuf, bufLen); - m_len += bufLen; + p->size += (unsigned)bufLen; } - else m_len = 0; + else m_buf = nullptr; } void MBinBuffer::assign(const void *pBuf, size_t bufLen) @@ -60,25 +128,36 @@ void MBinBuffer::assign(const void *pBuf, size_t bufLen) if (pBuf == nullptr || bufLen == 0) return; - m_buf = (char *)mir_realloc(m_buf, bufLen); - if (m_buf) { + BufImpl *p = ptr2buf(m_buf)->alloc(bufLen); + if (p) { + p->size = (unsigned)bufLen; + m_buf = (char *)(p + 1); memcpy(m_buf, pBuf, bufLen); - m_len = bufLen; } - else m_len = 0; + else m_buf = nullptr; +} + +size_t MBinBuffer::length() const +{ + BufImpl *p = ptr2buf(m_buf); + return (p) ? p->size : 0; } void MBinBuffer::remove(size_t sz) { - if (sz > m_len) - m_len = sz; + BufImpl *p = ptr2buf(m_buf); + if (!p) + return; + + if (sz > p->size) + sz = p->size; - if (m_len == sz) { - m_len = 0; - mir_free(m_buf); m_buf = nullptr; + if (p->size == sz) { + p->free(); + m_buf = nullptr; } else { - memmove(m_buf, m_buf + sz, m_len - sz); - m_len -= sz; + memmove(m_buf, m_buf + sz, p->size - sz); + p->size -= (unsigned)sz; } } diff --git a/src/mir_core/src/mir_core.def b/src/mir_core/src/mir_core.def index 9409e6d133..619b069b3a 100644 --- a/src/mir_core/src/mir_core.def +++ b/src/mir_core/src/mir_core.def @@ -1541,3 +1541,5 @@ Miranda_WaitOnHandle @1760 Miranda_WaitOnHandleEx @1761
_Utils_CorrectFontSize@4 @1762 NONAME
?OnResize@CDlgBase@@MAEXXZ @1763 NONAME
+??0MBinBuffer@@QAE@ABV0@@Z @1764 NONAME
+??0MBinBuffer@@QAE@I@Z @1765 NONAME
|