Skip to content

Commit

Permalink
feat(CSerialPort): reducing receive memory fragmentation
Browse files Browse the repository at this point in the history
Signed-off-by: itas109 <[email protected]>
  • Loading branch information
itas109 committed Dec 20, 2024
1 parent f660702 commit ea79d47
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
20 changes: 17 additions & 3 deletions src/SerialPortUnixBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,8 @@ void *CSerialPortUnixBase::commThreadMonitor(void *pParam)

if (p_base)
{
int isNew = 0;
char dataArray[4096];
for (; p_base->isThreadRunning();)
{
int readbytes = 0;
Expand All @@ -314,7 +316,16 @@ void *CSerialPortUnixBase::commThreadMonitor(void *pParam)
if (readbytes >= p_base->getMinByteReadNotify()) // 设定字符数,默认为1
{
char *data = NULL;
data = new char[readbytes];
if (readbytes <= 4096)
{
data = dataArray;
isNew = 0;
}
else
{
data = new char[readbytes];
isNew = 1;
}
if (data)
{
if (p_base->p_buffer)
Expand Down Expand Up @@ -357,8 +368,11 @@ void *CSerialPortUnixBase::commThreadMonitor(void *pParam)
}
}

delete[] data;
data = NULL;
if (isNew)
{
delete[] data;
data = NULL;
}
}
}
else
Expand Down
21 changes: 18 additions & 3 deletions src/SerialPortWinBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,8 @@ unsigned int __stdcall CSerialPortWinBase::commThreadMonitor(LPVOID pParam)
HANDLE m_mainHandle = p_base->getMainHandle();
OVERLAPPED m_overlapMonitor = p_base->getOverlapMonitor();

int isNew = 0;
char dataArray[4096];
for (; p_base->isThreadRunning();)
{
eventMask = 0;
Expand All @@ -344,7 +346,17 @@ unsigned int __stdcall CSerialPortWinBase::commThreadMonitor(LPVOID pParam)
if (comstat.cbInQue >= p_base->getMinByteReadNotify()) // 设定字符数,默认为1
{
char *data = NULL;
data = new char[comstat.cbInQue];
if (comstat.cbInQue <= 4096)
{
data = dataArray;
isNew = 0;
}
else
{
data = new char[comstat.cbInQue];
isNew = 1;
}

if (data)
{
if (p_base->p_buffer)
Expand Down Expand Up @@ -387,8 +399,11 @@ unsigned int __stdcall CSerialPortWinBase::commThreadMonitor(LPVOID pParam)
}
}

delete[] data;
data = NULL;
if (isNew)
{
delete[] data;
data = NULL;
}
}
}
}
Expand Down

0 comments on commit ea79d47

Please sign in to comment.