Freertos代码之链表

芯    片: STM32F427VITx    

指 令 集: ARMV7、Thumb2
编译环境: arm gcc

系统里链表代码不多,抽出来添加了地址打印,运行一下便知细节。粗略的画了一下,丑了点。

#include <stdio.h>
#include <stdlib.h>

struct xLIST_ITEM
{
    unsigned int xItemValue;
    struct xLIST_ITEM * pxNext;
    struct xLIST_ITEM * pxPrevious;
    void * pvOwner;
    void * pvContainer;
};
typedef struct xLIST_ITEM ListItem_t;

struct xMINI_LIST_ITEM
{
    unsigned int xItemValue;
    struct xLIST_ITEM * pxNext;
    struct xLIST_ITEM * pxPrevious;
};
typedef struct xMINI_LIST_ITEM MiniListItem_t;

typedef struct xLIST
{
    unsigned long uxNumberOfItems;
    ListItem_t * pxIndex;
    MiniListItem_t xListEnd;
}List_t;

void vListInitialise(List_t * const pxList)
{
    printf("pxList = %p\n", pxList);

    printf("pxList->pxIndex = %p, &(pxList->xListEnd) = %p\n", pxList->pxIndex, &(pxList->xListEnd));
    pxList->pxIndex = (ListItem_t *) &(pxList->xListEnd);
    printf("pxList->pxIndex = %p, &(pxList->xListEnd) = %p\n", pxList->pxIndex, &(pxList->xListEnd));

    pxList->xListEnd.xItemValue = 0xFFFFFFFF;

    printf("pxList->xListEnd.pxNext = %p\n", pxList->xListEnd.pxNext);
    pxList->xListEnd.pxNext = (ListItem_t *) &(pxList->xListEnd);
    printf("pxList->xListEnd.pxNext = %p\n", pxList->xListEnd.pxNext);

    printf("pxList->xListEnd.pxPrevious = %p\n", pxList->xListEnd.pxPrevious);
    pxList->xListEnd.pxPrevious = (ListItem_t *) &(pxList->xListEnd);
    printf("pxList->xListEnd.pxPrevious = %p\n", pxList->xListEnd.pxPrevious);

    /* 形成回形,因此无论多少个pxNext、pxPrevious,地址都一样。以下是各种组合的地址打印
       xListEnd.pxPrevious->pxNext */
    printf("pxList->xListEnd.pxPrevious->pxNext = %p\n", pxList->xListEnd.pxPrevious->pxNext);
    printf("pxList->xListEnd.pxPrevious->pxNext->pxNext = %p\n", pxList->xListEnd.pxPrevious->pxNext->pxNext);
    printf("pxList->xListEnd.pxPrevious->pxNext->pxNext->pxNext = %p\n\n", pxList->xListEnd.pxPrevious->pxNext->pxNext->pxNext);

    /* xListEnd.pxPrevious->pxPrevious */
    printf("pxList->xListEnd.pxPrevious->pxPrevious = %p\n", pxList->xListEnd.pxPrevious->pxPrevious);
    printf("pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious = %p\n", pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious);
    printf("pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious->pxPrevious = %p\n\n", pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious->pxPrevious);

    /* xListEnd.pxNext->pxNext */
    printf("pxList->xListEnd.pxNext->pxNext = %p\n", pxList->xListEnd.pxNext->pxNext);
    printf("pxList->xListEnd.pxNext->pxNext->pxNext = %p\n", pxList->xListEnd.pxNext->pxNext->pxNext);
    printf("pxList->xListEnd.pxNext->pxNext->pxNext->pxNext = %p\n\n", pxList->xListEnd.pxNext->pxNext->pxNext->pxNext);

    /* xListEnd.pxNext->pxPrevious */
    printf("pxList->xListEnd.pxNext->pxPrevious = %p\n", pxList->xListEnd.pxNext->pxPrevious);
    printf("pxList->xListEnd.pxNext->pxPrevious->pxPrevious = %p\n", pxList->xListEnd.pxNext->pxPrevious->pxPrevious);
    printf("pxList->xListEnd.pxNext->pxPrevious->pxPrevious->pxPrevious = %p\n\n", pxList->xListEnd.pxNext->pxPrevious->pxPrevious->pxPrevious);

    /* xListEnd.pxNext->pxPrevious->pxNext */
    printf("pxList->xListEnd.pxNext->pxPrevious->pxNext = %p\n", pxList->xListEnd.pxNext->pxPrevious->pxNext);
    printf("pxList->xListEnd.pxNext->pxPrevious->pxNext->pxPrevious = %p\n\n", pxList->xListEnd.pxNext->pxPrevious->pxNext->pxPrevious);

    /* xListEnd.pxPrevious->pxNext->pxPrevious */
    printf("pxList->xListEnd.pxPrevious->pxNext->pxPrevious = %p\n", pxList->xListEnd.pxPrevious->pxNext->pxPrevious);
    printf("pxList->xListEnd.pxPrevious->pxNext->pxPrevious->pxNext = %p\n\n", pxList->xListEnd.pxPrevious->pxNext->pxPrevious->pxNext);

    pxList->uxNumberOfItems = (unsigned long) 0U;
}

void vListInsertEnd(List_t * const pxList, ListItem_t * const pxNewListItem)
{
    ListItem_t * const pxIndex = pxList->pxIndex;

    printf("pxIndex = %p, pxList->pxIndex = %p, pxNewListItem = %p\n", pxIndex, pxList->pxIndex, pxNewListItem);

    printf("pxNewListItem->pxNext = %p\n", pxNewListItem->pxNext);
    pxNewListItem->pxNext = pxIndex;
    printf("pxNewListItem->pxNext = %p, pxIndex = %p\n", pxNewListItem->pxNext, pxIndex);

    printf("pxNewListItem->pxPrevious = %p\n", pxNewListItem->pxPrevious);
    pxNewListItem->pxPrevious = pxIndex->pxPrevious;
    printf("pxNewListItem->pxPrevious = %p, pxIndex->pxPrevious = %p\n", pxNewListItem->pxPrevious, pxIndex->pxPrevious);

    printf("pxIndex->pxPrevious->pxNext = %p\n", pxIndex->pxPrevious->pxNext);
    pxIndex->pxPrevious->pxNext = pxNewListItem;
    printf("pxIndex->pxPrevious->pxNext = %p\n", pxIndex->pxPrevious->pxNext);

    printf("pxIndex->pxPrevious = %p\n", pxIndex->pxPrevious);
    pxIndex->pxPrevious = pxNewListItem;
    printf("pxIndex->pxPrevious = %p\n\n", pxIndex->pxPrevious);

    pxNewListItem->pvContainer = (void *) pxList;

    (pxList->uxNumberOfItems)++;
}

void vListInsert(List_t * const pxList, ListItem_t * const pxNewListItem)
{
    ListItem_t * pxIterator;
    const unsigned int xValueOfInsertion = pxNewListItem->xItemValue;

    if (xValueOfInsertion == 0xFFFFFFFF) {
        pxIterator = pxList->xListEnd.pxPrevious;
    }
    else {
        for (pxIterator = (ListItem_t *) &(pxList->xListEnd);
             pxIterator->pxNext->xItemValue <= xValueOfInsertion;
             pxIterator = pxIterator->pxNext) {
            /* There is nothing to do here, just iterating to the wanted
               insertion position. */
            }
    }

    pxNewListItem->pxNext = pxIterator->pxNext;
    pxNewListItem->pxNext->pxPrevious = pxNewListItem;
    pxNewListItem->pxPrevious = pxIterator;
    pxIterator->pxNext = pxNewListItem;

    pxNewListItem->pvContainer = (void *) pxList;

    (pxList->uxNumberOfItems)++;
}

unsigned int 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;
}

void main()
{
    List_t ac = {0}, * pac;

    ListItem_t new = {
        .xItemValue = 123,
        .pxNext = NULL,
        .pxPrevious = NULL,
        .pvOwner = NULL,
    };

    ListItem_t new2 = {
        .xItemValue = 456,
        .pxNext = NULL,
        .pxPrevious = NULL,
        .pvOwner = NULL,
    };

    ListItem_t new3 = {
        .xItemValue = 789,
        .pxNext = NULL,
        .pxPrevious = NULL,
        .pvOwner = NULL,
    };

    printf("ac = %p, new = %p, new2 = %p, new3 = %p\n", (void *)&ac, (void *)&new, &new2, &new3);
    pac = &ac;

    vListInitialise(&ac);

    vListInsertEnd(pac, &new);
    vListInsertEnd(pac, &new2);
    vListInsertEnd(pac, &new3);
    
    uxListRemove(&new2);
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值