Skip to content

Commit

Permalink
[MSCTFIME][SDK] Improve CicArray (reactos#6217)
Browse files Browse the repository at this point in the history
- Add template type parameter.
- Use size_t for indexing instead of INT.
- Protect CicArray members.
- Adapt msctfime to new CicArray.
CORE-19360
  • Loading branch information
katahiromz authored Dec 21, 2023
1 parent 7d6fc57 commit df9c535
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 37 deletions.
14 changes: 7 additions & 7 deletions dll/ime/msctfime/msctfime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ typedef INT (CALLBACK *FN_EVENTSINK)(LPVOID, REFGUID);

class CCompartmentEventSink : public ITfCompartmentEventSink
{
CicArray m_array;
CicArray<CESMAP> m_array;
LONG m_cRefs;
FN_EVENTSINK m_fnEventSink;
LPVOID m_pUserData;
Expand All @@ -373,7 +373,7 @@ class CCompartmentEventSink : public ITfCompartmentEventSink
* @implemented
*/
CCompartmentEventSink::CCompartmentEventSink(FN_EVENTSINK fnEventSink, LPVOID pUserData)
: m_array(8)
: m_array()
, m_cRefs(1)
, m_fnEventSink(fnEventSink)
, m_pUserData(pUserData)
Expand Down Expand Up @@ -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;

Expand All @@ -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
{
Expand All @@ -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;
Expand Down
82 changes: 52 additions & 30 deletions sdk/include/reactos/cicero/cicarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,79 @@

#include "cicbase.h"

class CicArray
class CicArrayBase
{
public:
protected:
LPBYTE m_pb;
INT m_cItems;
INT m_cbItem;
INT m_cCapacity;
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 <typename T_ITEM>
class CicArray : protected CicArrayBase
{
public:
CicArray() : CicArrayBase(sizeof(T_ITEM)) { }

T_ITEM* data() const { return (T_ITEM*)m_pb; }
size_t size() const { return m_cItems; }
bool empty() const { return !size(); }

CicArray(INT cbItem);
virtual ~CicArray();
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];
}

BOOL Insert(INT iItem, INT cGrow);
LPVOID Append(INT cGrow);
void Remove(INT iItem, INT cRemove);
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;

Expand All @@ -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)
{
Expand All @@ -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;
}
}

0 comments on commit df9c535

Please sign in to comment.