前言
FreeRTOS列表與列表項其實就是鏈表和節點,在list.c
和list.h
實現
列表項數據結構
//列表項數據結構
typedef struct xLIST_ITEM
{
TickType_t xItemValue; //輔助值,用作節點做順序排序
struct xLIST_ITEM * pxNext;//後繼指針
struct xLIST_ITEM * pxPrevious;//前驅指針
void * pvOwner; //指向擁有該列表項的內核對象,通常是TCB
void * pvContainer; //指向該列表項所在的列表
}ListItem_t;
列表項初始化
void vListInitialiseItem(ListItem_t * const pxItem)
{
//初始化該列表項所在列表指針指向NULL,表示還沒插入任何列表
pxItem->pvContainer = NULL;
}
列表數據結構
typedef struct xLIST
{
UBaseType_t uxNumberOfItems; //指示這條列表上有多少個列表項
ListItem_t * pxIndex; //列表項索引指針
MiniListItem_t xListEnd; //列表最後一個列表項
}List_t;
其中MiniListItem_t
數據結構如下
typedef struct xMINI_LIST_ITEM
{
TickType_t xItemValue;//輔助值,用作節點做順序排序
struct xLIST_ITEM * pxNext;//後繼指針
struct xLIST_ITEM * pxPrevious;//前驅指針
}MiniListItem_t;
可以看到是列表項數據結構去掉了pvOwner
和pvContainer
成員
列表初始化
void vListInitialise(List_t * const pxList)
{
//列表索引指向最後一個節點
pxList->pxIndex = (ListItem_t *) &(pxList->xListEnd);
//將列表最後一個節點輔助值設置爲最大,確保該節點是最後節點
pxList->xListEnd.xItemValue = portMAX_DELAY;
//將最後一個節點的前驅和後繼指針指向自己,表示列表此時爲空
pxList->xListEnd.pxNext = (ListItem_t*) &(pxList->xListEnd);
pxList->xListEnd.pxPrevious = (ListItem_t*) &(pxList->xListEnd);
//表示此時列表中有0個列表項
pxList->uxNumberOfItems = (UBaseType_t)0U;
}
如下圖
將列表項插入列表尾部
void vListInsertEnd(List_t * const pxList,ListItem_t * const pxNewListItem)
{
//取列表項索引指針,此時該指針指向最後一個節點
ListItem_t * const pxIndex = pxList->pxIndex;
//新插入的列表項前驅指針指向最後一個節點
pxNewListItem->pxNext = pxIndex;
//新插入的列表項的後繼指針也指向最後一個節點
pxNewListItem->pxPrevious = pxIndex->pxPrevious;
//列表的最後一個節點的後繼指針指向新插入的列表項
pxNewListItem->pxPrevious->pxNext = pxNewListItem;
//列表的最後一個節點的前驅指針也指向新插入的列表項
pxIndex->pxPrevious = pxNewListItem;
//新插入的列表項是輸入該列表的
pxNewListItem->pvContainer = (void*) pxList;
//該列表中列表項數目+1
(pxList->uxNumberOfItems)++;
}
如下
將列表項按照升序排列插入到列表
假如向現有的2個列表項序輔助值分別是 1 和 3的列表插入輔助值是2個列表項
void vListInsert(List_t * const pxList,ListItem_t * const pxNewListItem)
{
ListItem_t *pxIterator;
//獲取新插入列表項的輔助值
const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
if(xValueOfInsertion == portMAX_DELAY)
{
pxIterator = pxList->xListEnd.pxPrevious;
}
else
{
for(pxIterator = (ListItem_t*) &(pxList->xListEnd);
pxIterator->pxNext->xItemValue <=xValueOfInsertion ;
pxIterator = pxIterator->pxNext)
{
}
}
//對於下圖此時pxIterator指向List_item1
//此時pxIterator->pxNext是List_item3地址
pxNewListItem->pxNext = pxIterator->pxNext;//1
//pxNewListItem->pxNext->pxPrevious是item3的前驅指針指向新列表項item2
pxNewListItem->pxNext->pxPrevious = pxNewListItem;
//新列表項item2的前驅指針指向pxIterator即item1
pxNewListItem->pxPrevious = pxIterator;
//pxIterator即item1的後繼指向新列表項item2
pxIterator->pxNext = pxNewListItem;
pxNewListItem->pvContainer = (void*) pxList;
(pxList->uxNumberOfItems)++;
}
如圖,注意2、3步的指針箭頭,野火的pdf畫的有誤
將列表項從列表刪除
UBaseType_t uxListRemove(ListItem_t * const pxItemToRemove)
{
//獲取列表項所在列表
List_t * const pxList = (List_t*)pxItemToRemove->pvContainer;
//把要刪除的列表項從列表中摘出來
pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
//
if(pxList->pxIndex == pxItemToRemove)
{
pxList->pxIndex = pxItemToRemove->pxPrevious;
}
//該列表項所有者置空
pxItemToRemove->pvContainer = NULL;
(pxList->uxNumberOfItems)--;
return pxList->uxNumberOfItems;
}
下面是測試代碼即仿真結果
List_t List_Test;
ListItem_t List_Item1;
ListItem_t List_Item2;
ListItem_t List_Item3;
int main(void)
{
//初始化列表
vListInitialise(&List_Test);
//初始化列表項
vListInitialiseItem(&List_Item1);
List_Item1.xItemValue = 1;
vListInitialiseItem(&List_Item2);
List_Item2.xItemValue = 2;
vListInitialiseItem(&List_Item3);
List_Item3.xItemValue = 3;
vListInsert(&List_Test,&List_Item1);
vListInsert(&List_Test,&List_Item3);
vListInsert(&List_Test,&List_Item2);
while(1);
}