// include head file
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <vld.h> /* This head file could be omit, it used for check whether memory leak. You must be install visual leak detector plugin for visual */
// Message Node
typedef struct Message
{
uint8_t* strData; // message content
uint32_t uiDataLen; // message length
struct Message* pNext; // next message address
struct Message* pFront; // front message address
}Message_t;
// Message Method
void vInitMessageQueue(void);
void vDeinitMessageQueue(void);
int iAppendMessage(uint8_t* strMsg, uint32_t uiMsgLen);
uint32_t uiMessageQueueSize(void);
Message_t* pGetHeadMessage(void);
int vFreeHeadMessage(void);
static Message_t head, tail;
void vInitMessageQueue(void)
{
head.pNext = &tail;
tail.pFront = &head;
}
void vDeinitMessageQueue(void)
{
while (head.pNext != &tail)
{
Message_t* pTmpMsg = head.pNext;
head.pNext = head.pNext->pNext;
pTmpMsg->pNext->pFront = &head;
free(pTmpMsg->strData);
free(pTmpMsg);
}
}
int iAppendMessage(uint8_t* strMsg, uint32_t uiMsgLen)
{
Message_t* pNewMsg = (Message_t*)malloc(sizeof(Message_t));
if (NULL == pNewMsg)
{
printf("[%s] Malloc message memory failed.\r\n", __FUNCTION__);
return -1;
}
pNewMsg->uiDataLen = uiMsgLen;
pNewMsg->strData = (uint8_t*)malloc(pNewMsg->uiDataLen*sizeof(uint8_t)+1);
if (NULL == pNewMsg->strData)
{
printf("[%s] Malloc message content memory failed.\r\n", __FUNCTION__);
return -2;
}
memset(pNewMsg->strData, 0, pNewMsg->uiDataLen);
memcpy(pNewMsg->strData, strMsg, pNewMsg->uiDataLen);
pNewMsg->strData[pNewMsg->uiDataLen] = '\0';
tail.pFront->pNext = pNewMsg;
pNewMsg->pFront = tail.pFront;
pNewMsg->pNext = &tail;
tail.pFront = pNewMsg;
return 0;
}
uint32_t uiMessageQueueSize(void)
{
uint32_t uiSize = 0;
Message_t* pTmpMsg = NULL;
for (pTmpMsg = &head; pTmpMsg != &tail; pTmpMsg = pTmpMsg->pNext)
{
if (pTmpMsg->pNext != &tail)
{
uiSize++;
}
}
return uiSize;
}
Message_t* pGetHeadMessage(void)
{
return (head.pNext == &tail) ? NULL : head.pNext;
}
int vFreeHeadMessage(void)
{
if (head.pNext != &tail)
{
Message_t* pTmpMsg = head.pNext;
head.pNext = head.pNext->pNext;
pTmpMsg->pNext->pFront = &head;
free(pTmpMsg->strData);
free(pTmpMsg);
return 0;
}
else
{
return -1;
}
}
int main(void)
{
uint8_t strMsg[50];
uint32_t uiMsgLen = 0;
int ret = 0;
memset(strMsg, 0, 50);
vInitMessageQueue();
while (1)
{
printf("Please input message, input 'end' to exit: ");
scanf("%s", strMsg);
if (0 == strcmp(strMsg, "end"))
{
break;
}
uiMsgLen = strlen(strMsg);
strMsg[uiMsgLen] = '\0';
ret = iAppendMessage(strMsg, uiMsgLen);
if (ret < 0)
{
printf("Add message to queue failed.\r\n");
return -1;
}
memset(strMsg, 0, 50);
uiMsgLen = 0;
}
printf("Message queue size is %d.\r\n", uiMessageQueueSize());
printf("Message : ");
while (uiMessageQueueSize())
{
Message_t* pMsg = pGetHeadMessage();
if (NULL == pMsg)
{
printf("Message queue is empty.\r\n");
break;
}
printf("%s ", pMsg->strData);
ret = vFreeHeadMessage();
if (ret < 0)
{
printf("Free message queue head message failed.\r\n");
return -2;
}
}
printf(".\r\n");
vDeinitMessageQueue();
return 0;
}
运行演示: