freertos 源码分析二 list链表源码

list.c

一、链表初始化

c 复制代码
void vListInitialise( List_t * const pxList )
{                                                                                                                                                  
    pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd ); 
    listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
    pxList->xListEnd.xItemValue = portMAX_DELAY;
    pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );
    pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );
    #if ( configUSE_MINI_LIST_ITEM == 0 ) 
    {   
        pxList->xListEnd.pvOwner = NULL;
        pxList->xListEnd.pxContainer = NULL;
        listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( &( pxList->xListEnd ) );
    }   
    #endif
    pxList->uxNumberOfItems = ( UBaseType_t ) 0U; 
    listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList );
    listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList );
}

分解

pxList->pxIndex 指向结构中的xListEnd结构体

c 复制代码
pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd );

校验

c 复制代码
#if ( configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES == 0 )
    #define listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )
    #define listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )
    #define listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList )
    #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList )
#else
    #define listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )     ( pxItem )->xListItemIntegrityValue1 = pdINTEGRITY_CHECK_VALUE
    #define listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem )    ( pxItem )->xListItemIntegrityValue2 = pdINTEGRITY_CHECK_VALUE
    #define listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList )              ( pxList )->xListIntegrityValue1 = pdINTEGRITY_CHECK_VALUE
    #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList )              ( pxList )->xListIntegrityValue2 = pdINTEGRITY_CHECK_VALUE
#endif

验验数值 5a

c 复制代码
#if ( configUSE_16_BIT_TICKS == 1 ) 
    #define pdINTEGRITY_CHECK_VALUE    0x5a5a
#else
    #define pdINTEGRITY_CHECK_VALUE    0x5a5a5a5aUL
#endif  

portMAX_DELAY数值

c 复制代码
#if( configUSE_16_BIT_TICKS == 1 )
    typedef uint16_t TickType_t;
    #define portMAX_DELAY ( TickType_t ) 0xffff
#else   
    typedef uint32_t TickType_t;
    #define portMAX_DELAY ( TickType_t ) 0xffffffffUL
#endif 

往下

c 复制代码
   pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd );
   pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );

链表结构中xListEnd结构体的前后表项指针匀指向xListEnd

c 复制代码
pxList->uxNumberOfItems = ( UBaseType_t ) 0U; 

表项个数设为零

二、表项初始化

c 复制代码
void vListInitialiseItem( ListItem_t * const pxItem )
{
    pxItem->pxContainer = NULL;
    listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
    listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem );
}

三、表尾插入表项

c 复制代码
void vListInsertEnd( List_t * const pxList,
                     ListItem_t * const pxNewListItem )
{
    ListItem_t * const pxIndex = pxList->pxIndex;
    listTEST_LIST_INTEGRITY( pxList );
    listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
    pxNewListItem->pxNext = pxIndex;
    pxNewListItem->pxPrevious = pxIndex->pxPrevious;
    mtCOVERAGE_TEST_DELAY();
    pxIndex->pxPrevious->pxNext = pxNewListItem;
    pxIndex->pxPrevious = pxNewListItem;
    pxNewListItem->pxContainer = pxList;
    ( pxList->uxNumberOfItems )++;
}  

由于初始化时,索引指向了链表内xListEnd结构体,初次插入表项时,连同链表内表项一同插入链表。

c 复制代码
 ListItem_t * const pxIndex = pxList->pxIndex;

检测校验

c 复制代码
#if ( configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES == 0 ) 
    #define listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList )
    #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList )
    #define listTEST_LIST_ITEM_INTEGRITY( pxItem )
    #define listTEST_LIST_INTEGRITY( pxList )
#else
    #define listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList )  \
            ( pxList )->xListIntegrityValue2 = pdINTEGRITY_CHECK_VALUE
    #define listTEST_LIST_ITEM_INTEGRITY( pxItem )          \
            configASSERT( ( ( pxItem )->xListItemIntegrityValue1 ==                    pdINTEGRITY_CHECK_VALUE ) && ( ( pxItem )->xListItemIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) ) 
    #define listTEST_LIST_INTEGRITY( pxList )               \
            configASSERT( ( ( pxList )->xListIntegrityValue1 ==                        pdINTEGRITY_CHECK_VALUE ) && ( ( pxList )->xListIntegrityValue2 == pdINTEGRITY_CHECK_VALUE ) ) 
#endif

mtCOVERAGE_TEST_DELAY,代码覆盖路径测试 展开为空 检测用

c 复制代码
#ifndef mtCOVERAGE_TEST_DELAY
    #define mtCOVERAGE_TEST_DELAY()
#endif       

指针操作,表项插入在当前链表表项索引之前。

c 复制代码
pxNewListItem->pxNext = pxIndex;                                                                                                               
pxNewListItem->pxPrevious = pxIndex->pxPrevious;
pxIndex->pxPrevious->pxNext = pxNewListItem;                                                                                                   
pxIndex->pxPrevious = pxNewListItem;

设链表为当前表项Container,并增加表项数目。

c 复制代码
pxNewListItem->pxContainer = pxList;
( pxList->uxNumberOfItems )++;   

四、插入表项

依据表项的xItemValue数值,插入表项,若数值相等插入相等值之后。

复制代码
void vListInsert( List_t * const pxList,
                  ListItem_t * const pxNewListItem )
{   
    ListItem_t * pxIterator;
    const TickType_t xValueOfInsertion = pxNewListItem->xItemValue;
    listTEST_LIST_INTEGRITY( pxList );
    listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );
    if( xValueOfInsertion == portMAX_DELAY )
    {
        pxIterator = pxList->xListEnd.pxPrevious;
    }
    else
    {
        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->pxContainer = pxList;
    ( pxList->uxNumberOfItems )++;
}

五、删除表项

c 复制代码
UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove )
{
    List_t * const pxList = pxItemToRemove->pxContainer;
    pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
    pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;
    mtCOVERAGE_TEST_DELAY();
    if( pxList->pxIndex == pxItemToRemove )
    {
        pxList->pxIndex = pxItemToRemove->pxPrevious;
    }
    else
    {
        mtCOVERAGE_TEST_MARKER();
    }
    pxItemToRemove->pxContainer = NULL;
    ( pxList->uxNumberOfItems )--;
    return pxList->uxNumberOfItems;
}

分解

表项pxContainer指针为当前拥有者链表。

c 复制代码
List_t * const pxList = pxItemToRemove->pxContainer;

若移除表项为当前索引,当前索引前移。

c 复制代码
if( pxList->pxIndex == pxItemToRemove )
{
    pxList->pxIndex = pxItemToRemove->pxPrevious;
}

代码覆盖路径测试 检测用

c 复制代码
 mtCOVERAGE_TEST_MARKER();

表项数目减一,返回表项数目。

c 复制代码
( pxList->uxNumberOfItems )--;
return pxList->uxNumberOfItems;
相关推荐
一支闲人1 小时前
C语言相关简单数据结构:双向链表
c语言·数据结构·链表·基础知识·适用于新手小白
cpsvps_net13 小时前
美国服务器环境下Windows容器工作负载智能弹性伸缩
windows
甄超锋14 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cpsvps16 小时前
美国服务器环境下Windows容器工作负载基于指标的自动扩缩
windows
阿巴~阿巴~17 小时前
深入解析C++ STL链表(List)模拟实现
开发语言·c++·链表·stl·list
网硕互联的小客服19 小时前
Apache 如何支持SHTML(SSI)的配置方法
运维·服务器·网络·windows·php
etcix19 小时前
implement copy file content to clipboard on Windows
windows·stm32·单片机
许泽宇的技术分享20 小时前
Windows MCP.Net:基于.NET的Windows桌面自动化MCP服务器深度解析
windows·自动化·.net
非凡ghost21 小时前
AMS PhotoMaster:全方位提升你的照片编辑体验
windows·学习·信息可视化·软件需求
重生之我是Java开发战士1 天前
【数据结构】深入理解单链表与通讯录项目实现
数据结构·链表