#include <stdlib.h>
#include "FreeRTOS.h"
#include "list.h"
void vListInitialise( List_t * const pxList )
{
pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );
//初始化链表时,将链表的遍历指针指向链表的尾链表项
pxList->xListEnd.xItemValue = portMAX_DELAY;
//将链表的尾链表项的链表节点值设成portMAX_DELAY,确保其在链表的尾部
pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );
pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );
//尾链表的前后项指针均指向尾链表项
pxList->uxNumberOfItems = ( UBaseType_t ) 0U;//链表中的链表项为0,即没有一个链表项
}
/*-----------------------------------------------------------*/
void vListInitialiseItem( ListItem_t * const pxItem )
{
pxItem->pvContainer = NULL;//初始化链表项仅仅是确认链表项是不属于任何一个链表的
}
/*-----------------------------------------------------------*/
void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem )
{
ListItem_t * const pxIndex = pxList->pxIndex;//获取要插入链表的前一个链表项的指针
pxNewListItem->pxNext = pxIndex;//将新链表的后向指针指向要插入位置的下一个链表项
pxNewListItem->pxPrevious = pxIndex->pxPrevious;//将新链表的前向指针指向要插入链表的前一个链表项,此处即为pxIndex
pxIndex->pxPrevious->pxNext = pxNewListItem;//将要插入链表的前一个链表项(尾链表项)的后向指针指向新链表
pxIndex->pxPrevious = pxNewListItem;//将尾链表的前一个链表变成要插入的链表
pxNewListItem->pvContainer = ( void * ) pxList;//得到链表项插入的链表
( pxList->uxNumberOfItems )++;//插入链表的链表元素个数加一
}
/*-----------------------------------------------------------*/
void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem )
{
ListItem_t *pxIterator;
const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;//获取要插入链表的xItemValue值
if( xValueOfInsertion == portMAX_DELAY )//若新链表的链表节点的值为portMAX_DELAY
{
pxIterator = pxList->xListEnd.pxPrevious;//将待插入的位置变为尾链表位置
}
else//若新链表的链表节点不为protMAX_DELAY
{
//遍历整个链表,寻找合适的位置将新的链表项插入
for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext )
{
}
}
pxNewListItem->pxNext = pxIterator->pxNext;//更新新链表项的后向指针
pxNewListItem->pxNext->pxPrevious = pxNewListItem;//更新新链表的后一个链表的前向指针指向新链表
pxNewListItem->pxPrevious = pxIterator;//更新新链表的前向指针
pxIterator->pxNext = pxNewListItem;//更新新链表的前一个链表的后向指针
pxNewListItem->pvContainer = ( void * ) pxList;//新链表项记住自己所在的链表
( pxList->uxNumberOfItems )++;//链表节点增加
}
/*-----------------------------------------------------------*/
UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
{
List_t * const pxList = ( List_t * ) pxItemToRemove->pvContainer;
//获取链表项所在的链表,pvContainer指向所在的链表
pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
//将pxItemToRemove后一个链表项的前向连接与pxItemToRemove的前一个链表项相连
pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
//将pxItemToRemove前一个链表项的后向连接与pxItemToRemove的后一个链表项相连<table border="1" width="400" cellspacing="1" cellpadding="1"><tbody><tr><td> </td><td> </td></tr><tr><td> </td><td> </td></tr><tr><td> </td><td> </td></tr></tbody></table>
if( pxList->pxIndex == pxItemToRemove )
{
pxList->pxIndex = pxItemToRemove->pxPrevious;//将pxItemToRemove的前一个链表项与链表的遍历指针相连
}
else
{
mtCOVERAGE_TEST_MARKER();
}
pxItemToRemove->pvContainer = NULL;//将pxItemToRemove链表项从所在链表清除(将pvContainer=NULL即将其从链表中清除)
( pxList->uxNumberOfItems )--;//将pxItemToRemove所在链表的链表元素个数减一
return pxList->uxNumberOfItems;//返回链表中剩余链表项的数量
}
/*-----------------------------------------------------------*/