List insert: Can't understand the role of "pxIndex" in vListInsertEnd

Hey, guys, can I ask you something?I’m really disruption.

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

	/* Only effective when configASSERT() is also defined, these tests may catch
	the list data structures being overwritten in memory.  They will not catch
	data errors caused by incorrect configuration or use of FreeRTOS. */
	listTEST_LIST_INTEGRITY( pxList );
	listTEST_LIST_ITEM_INTEGRITY( pxNewListItem );

	/* Insert a new list item into pxList, but rather than sort the list,
	makes the new list item the last item to be removed by a call to
	listGET_OWNER_OF_NEXT_ENTRY(). */
	pxNewListItem->pxNext = pxIndex;
	pxNewListItem->pxPrevious = pxIndex->pxPrevious;

	/* Only used during decision coverage testing. */
	mtCOVERAGE_TEST_DELAY();

	pxIndex->pxPrevious->pxNext = pxNewListItem;
	pxIndex->pxPrevious = pxNewListItem;

	/* Remember which list the item is in. */
	pxNewListItem->pvContainer = ( void * ) pxList;

	( pxList->uxNumberOfItems )++;
}

In this function, why does the variable “pxIndex” point to the head of “pxList” but the end of it?

pxIndex is used to walk through list. It points to the last item returned by a call to listGET_OWNER_OF_NEXT_ENTRY ().

+---------+     +---------+    +---------+    +---------+
|         |     |         |    |         |    |         |
|   1     +---->|   2     +--->|    3    +--->|   End   |
|         |     |         |    |         |    |         |
+---------+     +---------+    +---------+    +---------+
                     ^                                   
                     |                                   
                     |                                   
                     |                                   
                 pxIndex                                 

In the above example, the previous call to listGET_OWNER_OF_NEXT_ENTRY returned 2 and the next call would return 3.

vListInsertEnd inserts a new list item into pxList such that the new list item will be the last item to be removed by a call to listGET_OWNER_OF_NEXT_ENTRY().

+----------+    +---------+     +---------+    +---------+    +---------+
|          |    |         |     |         |    |         |    |         |
|    1     +--->|   New   +---->|   2     +--->|    3    +--->|   End   |
|          |    |         |     |         |    |         |    |         |
+----------+    +---------+     +---------+    +---------+    +---------+
                                    ^                                   
                                    |                                   
                                    |                                   
                                    |                                   
                                pxIndex                                 
1 Like

Thank you for the quick reply, I seem to understand some of it