From df9c53554286346903fa20ba65c00bfc89d4479d Mon Sep 17 00:00:00 2001 From: Katayama Hirofumi MZ Date: Thu, 21 Dec 2023 20:17:35 +0900 Subject: [PATCH] [MSCTFIME][SDK] Improve CicArray (#6217) - Add template type parameter. - Use size_t for indexing instead of INT. - Protect CicArray members. - Adapt msctfime to new CicArray. CORE-19360 --- dll/ime/msctfime/msctfime.cpp | 14 ++--- sdk/include/reactos/cicero/cicarray.h | 82 +++++++++++++++++---------- 2 files changed, 59 insertions(+), 37 deletions(-) diff --git a/dll/ime/msctfime/msctfime.cpp b/dll/ime/msctfime/msctfime.cpp index bfe55ed615f..36dcf4b68f3 100644 --- a/dll/ime/msctfime/msctfime.cpp +++ b/dll/ime/msctfime/msctfime.cpp @@ -348,7 +348,7 @@ typedef INT (CALLBACK *FN_EVENTSINK)(LPVOID, REFGUID); class CCompartmentEventSink : public ITfCompartmentEventSink { - CicArray m_array; + CicArray m_array; LONG m_cRefs; FN_EVENTSINK m_fnEventSink; LPVOID m_pUserData; @@ -373,7 +373,7 @@ public: * @implemented */ CCompartmentEventSink::CCompartmentEventSink(FN_EVENTSINK fnEventSink, LPVOID pUserData) - : m_array(8) + : m_array() , m_cRefs(1) , m_fnEventSink(fnEventSink) , m_pUserData(pUserData) @@ -438,7 +438,7 @@ STDMETHODIMP CCompartmentEventSink::OnChange(REFGUID rguid) HRESULT CCompartmentEventSink::_Advise(IUnknown *pUnknown, REFGUID rguid, BOOL bThread) { - CESMAP *pCesMap = (CESMAP *)m_array.Append(1); + CESMAP *pCesMap = m_array.Append(1); if (!pCesMap) return E_OUTOFMEMORY; @@ -458,7 +458,7 @@ CCompartmentEventSink::_Advise(IUnknown *pUnknown, REFGUID rguid, BOOL bThread) pCesMap->m_pComp->Release(); pCesMap->m_pComp = NULL; } - m_array.Remove(m_array.m_cItems - 1, 1); + m_array.Remove(m_array.size() - 1, 1); } else { @@ -478,11 +478,11 @@ CCompartmentEventSink::_Advise(IUnknown *pUnknown, REFGUID rguid, BOOL bThread) */ HRESULT CCompartmentEventSink::_Unadvise() { - CESMAP *pCesMap = (CESMAP *)m_array.m_pb; - if (!m_array.m_cItems) + CESMAP *pCesMap = m_array.data(); + size_t cItems = m_array.size(); + if (!cItems) return S_OK; - INT cItems = m_array.m_cItems; do { ITfSource *pSource = NULL; diff --git a/sdk/include/reactos/cicero/cicarray.h b/sdk/include/reactos/cicero/cicarray.h index 9ce7cfa6b76..29c2e6e17ab 100644 --- a/sdk/include/reactos/cicero/cicarray.h +++ b/sdk/include/reactos/cicero/cicarray.h @@ -9,57 +9,79 @@ #include "cicbase.h" -class CicArray +class CicArrayBase +{ +protected: + LPBYTE m_pb; + size_t m_cItems, m_cbItem, m_cCapacity; + +public: + CicArrayBase(size_t cbItem); + virtual ~CicArrayBase(); + + BOOL Insert(size_t iItem, size_t cGrow); + LPVOID Append(size_t cGrow); + void Remove(size_t iItem, size_t cRemove); +}; + +template +class CicArray : protected CicArrayBase { public: - LPBYTE m_pb; - INT m_cItems; - INT m_cbItem; - INT m_cCapacity; + CicArray() : CicArrayBase(sizeof(T_ITEM)) { } - CicArray(INT cbItem); - virtual ~CicArray(); + T_ITEM* data() const { return (T_ITEM*)m_pb; } + size_t size() const { return m_cItems; } + bool empty() const { return !size(); } - BOOL Insert(INT iItem, INT cGrow); - LPVOID Append(INT cGrow); - void Remove(INT iItem, INT cRemove); + T_ITEM& operator[](size_t iItem) + { + return *(T_ITEM*)&m_pb[iItem * m_cbItem]; + } + const T_ITEM& operator[](size_t iItem) const + { + return *(const T_ITEM*)&m_pb[iItem * m_cbItem]; + } + + T_ITEM* Append(size_t cGrow) + { + return (T_ITEM*)CicArrayBase::Append(cGrow); + } + + using CicArrayBase::Insert; + using CicArrayBase::Remove; }; /******************************************************************************/ -inline CicArray::CicArray(INT cbItem) +inline CicArrayBase::CicArrayBase(size_t cbItem) { m_cbItem = cbItem; m_pb = NULL; m_cItems = m_cCapacity = 0; } -inline CicArray::~CicArray() +inline CicArrayBase::~CicArrayBase() { cicMemFree(m_pb); } -inline LPVOID CicArray::Append(INT cGrow) +inline LPVOID CicArrayBase::Append(size_t cGrow) { if (!Insert(m_cItems, cGrow)) return NULL; return &m_pb[(m_cItems - cGrow) * m_cbItem]; } -inline BOOL CicArray::Insert(INT iItem, INT cGrow) +inline BOOL CicArrayBase::Insert(size_t iItem, size_t cGrow) { - INT cNewCapacity = m_cItems + cGrow; + size_t cNewCapacity = m_cItems + cGrow; if (m_cCapacity < cNewCapacity) { if (cNewCapacity <= m_cItems + m_cItems / 2) cNewCapacity = m_cItems + m_cItems / 2; - BYTE *pbNew; - if (m_pb) - pbNew = (BYTE *)cicMemReAlloc(m_pb, cNewCapacity * m_cbItem); - else - pbNew = (BYTE *)cicMemAlloc(cNewCapacity * m_cbItem); - + LPBYTE pbNew = (LPBYTE)cicMemReAlloc(m_pb, cNewCapacity * m_cbItem); if (!pbNew) return FALSE; @@ -78,7 +100,7 @@ inline BOOL CicArray::Insert(INT iItem, INT cGrow) return TRUE; } -inline void CicArray::Remove(INT iItem, INT cRemove) +inline void CicArrayBase::Remove(size_t iItem, size_t cRemove) { if (iItem + cRemove < m_cItems) { @@ -89,14 +111,14 @@ inline void CicArray::Remove(INT iItem, INT cRemove) m_cItems -= cRemove; - INT cHalfCapacity = m_cCapacity / 2; - if (cHalfCapacity > m_cItems) + size_t cHalfCapacity = m_cCapacity / 2; + if (cHalfCapacity <= m_cItems) + return; + + LPBYTE pb = (LPBYTE)cicMemReAlloc(m_pb, cHalfCapacity * m_cbItem); + if (pb) { - BYTE *pb = (BYTE *)cicMemReAlloc(m_pb, cHalfCapacity * m_cbItem); - if (pb) - { - m_pb = pb; - m_cCapacity = cHalfCapacity; - } + m_pb = pb; + m_cCapacity = cHalfCapacity; } }