【VxWorks5.5源码阅读笔记三】链表模块

先看lstLib.h

/* lstLib.h - doubly linked list library header */

/* Copyright 1984-2001 Wind River Systems, Inc. */

/*
modification history
--------------------
01n,19sep01,pcm  added lstLibInit () (SPR 20698)
01m,02apr93,edm  ifdef'd out non-ASMLANGUAGE portions
01l,22sep92,rrr  added support for c++
01k,04jul92,jcf  cleaned up.
01j,26may92,rrr  the tree shuffle
01i,04oct91,rrr  passed through the ansification filter
		  -changed VOID to void
		  -changed copyright notice
01h,23oct90,shl  included "vxWorks.h" so include file order isn't crucial.
01g,05oct90,shl  added ANSI function prototypes.
                 made #endif ANSI style.
                 added copyright notice.
01f,10aug90,dnw  added declaration of lstInsert().
01e,07aug90,shl  added IMPORT type to function declarations.
01d,21may86,llk	 added forward declaration of lstNStep.
01c,03jun84,dnw  changed list.{head,tail} to list.node.
		 added declarations of lst{First,Last,Next,Previous}.
01b,27jan84,ecs  added inclusion test.
01b,15mar83,dnw  changed name from lstlb to lstLib
*/

#ifndef __INClstLibh
#define __INClstLibh

#ifndef _ASMLANGUAGE

#ifdef __cplusplus
extern "C" {
#endif

#include "vxWorks.h"

/* type definitions */

typedef struct node		/* Node of a linked list. */
    {
    struct node *next;		/* Points at the next node in the list */
    struct node *previous;	/* Points at the previous node in the list */
    } NODE;


/* HIDDEN */

typedef struct			/* Header for a linked list. */
    {
    NODE node;			/* Header list node */
    int count;			/* Number of nodes in list */
    } LIST;

/* END_HIDDEN */


/* function declarations */


#if defined(__STDC__) || defined(__cplusplus)

extern void	lstLibInit (void);
extern NODE *	lstFirst (LIST *pList);
extern NODE *	lstGet (LIST *pList);
extern NODE *	lstLast (LIST *pList);
extern NODE *	lstNStep (NODE *pNode, int nStep);
extern NODE *	lstNext (NODE *pNode);
extern NODE *	lstNth (LIST *pList, int nodenum);
extern NODE *	lstPrevious (NODE *pNode);
extern int 	lstCount (LIST *pList);
extern int 	lstFind (LIST *pList, NODE *pNode);
extern void 	lstAdd (LIST *pList, NODE *pNode);
extern void 	lstConcat (LIST *pDstList, LIST *pAddList);
extern void 	lstDelete (LIST *pList, NODE *pNode);
extern void 	lstExtract (LIST *pSrcList, NODE *pStartNode, NODE *pEndNode,
	  		    LIST *pDstList);
extern void 	lstFree (LIST *pList);
extern void 	lstInit (LIST *pList);
extern void 	lstInsert (LIST *pList, NODE *pPrev, NODE *pNode);

#else	/* __STDC__ */

extern void	lstLibInit ();
extern NODE *	lstFirst ();
extern NODE *	lstGet ();
extern NODE *	lstLast ();
extern NODE *	lstNStep ();
extern NODE *	lstNext ();
extern NODE *	lstNth ();
extern NODE *	lstPrevious ();
extern int 	lstCount ();
extern int 	lstFind ();
extern void 	lstAdd ();
extern void 	lstConcat ();
extern void 	lstDelete ();
extern void 	lstExtract ();
extern void 	lstFree ();
extern void 	lstInit ();
extern void 	lstInsert ();

#endif	/* __STDC__ */

#ifdef __cplusplus
}
#endif

#endif /* ~ _ASMLANGUAGE */

#endif /* __INClstLibh */

现在来说明一下#if defined(STDC) || defined(__cplusplus)的意思。

这两个都是标准宏,_STDC_表示是是否符合标准C,_cplusplus表示是否是C++编译器

但是其实这里不知道为啥要把NODE和LIST分开,之前看的链表都是放在一起的。而且节点没有存放数据的地方。

lstLib.c

/* lstLib.c - doubly linked list subroutine library */

/* Copyright 1984-2001 Wind River Systems, Inc. */
#include "copyright_wrs.h"

/*
modification history
--------------------
02a,19sep01,pcm  added lstLibInit () to bring module into image (SPR 20698)
01z,05oct98,jmp  doc: cleanup.
01y,14oct95,jdi  doc: fixed typo in lstNth().
01x,13feb95,jdi  doc format change.
01w,20jan93,jdi  documentation cleanup for 5.1.
01v,09jul92,hdn  put an optimized lstGet()
01u,26may92,rrr  the tree shuffle
01t,25nov91,rrr  cleanup of some ansi warnings.
01s,04oct91,rrr  passed through the ansification filter
                  -changed functions to ansi style
		  -changed VOID to void
		  -changed copyright notice
01r,30apr91,jdi	 documentation tweaks.
01q,05apr91,jdi	 documentation -- removed header parens and x-ref numbers;
		 doc review by dnw.
01p,11feb91,jaa	 documentation cleanup.
01o,05nov87,jlf  documentation
01n,02apr87,ecs  hushed lint in lstFree.
01m,25mar87,jlf  documentation
01l,21dec86,dnw  changed to not get include files from default directories.
01k,01jul86,jlf  documentation.
01j,21may86,llk	 added lstFree and lstNStep.
01i,09apr86,rdc  added lstFind.
01h,20jul85,jlf  documentation.
01g,19sep84,jlf  fixed spacing in comments by adding .ne's.
01f,08sep84,jlf  added comments and copyright notice.
01e,29jun84,dnw  added lstConcat and lstExtract.
01d,03jun84,dnw  added lstFirst, lstLast.
		 changed list.{head,tail} to list.node.{next,previous}.
		 cleaned up comments, etc.
01c,07may84,ecs  added lstNext, lstPrevious, and lstCount.
01b,09jun83,ecs  modified the documentation
01a,06aug82,dnw  created from old singly-linked-list lib which is now "slllb".
*/


/*
DESCRIPTION
This subroutine library supports the creation and maintenance of a
doubly linked list.  The user supplies a list descriptor (type LIST)
that will contain pointers to the first and last nodes in the list,
and a count of the number of nodes in the list.  The nodes in the
list can be any user-defined structure, but they must reserve space
for two pointers as their first elements.  Both the forward and
backward chains are terminated with a NULL pointer.

The linked-list library simply manipulates the linked-list data structures;
no kernel functions are invoked.  In particular, linked lists by themselves
provide no task synchronization or mutual exclusion.  If multiple tasks will
access a single linked list, that list must be guarded with some
mutual-exclusion mechanism (e.g., a mutual-exclusion semaphore).

NON-EMPTY LIST:
.CS
   ---------             --------          --------
   | head--------------->| next----------->| next---------
   |       |             |      |          |      |      |
   |       |       ------- prev |<---------- prev |      |
   |       |       |     |      |          |      |      |
   | tail------    |     | ...  |    ----->| ...  |      |
   |       |  |    v                 |                   v
   |count=2|  |  -----               |                 -----
   ---------  |   ---                |                  ---
              |    -                 |                   -
              |                      |
              ------------------------
.CE

EMPTY LIST:
.CS
	-----------
        |  head------------------
        |         |             |
        |  tail----------       |
        |         |     |       v
        | count=0 |   -----   -----
        -----------    ---     ---
                        -	-
.CE

INCLUDE FILES: lstLib.h
*/


/* LINTLIBRARY */

#include "vxWorks.h"
#include "lstLib.h"
#include "stdlib.h"

#define HEAD	node.next		/* first node in list */
#define TAIL	node.previous		/* last node in list */

/*********************************************************************
*
* lstLibInit - initializes lstLib module
*
* This routine pulls lstLib into the vxWorks image.
*
* RETURNS: N/A
*/
void lstLibInit (void)
    {
    return;
    }

/*********************************************************************
*
* lstInit - initialize a list descriptor
*
* This routine initializes a specified list to an empty list.
*
* RETURNS: N/A
*/

void lstInit
    (
    FAST LIST *pList     /* ptr to list descriptor to be initialized */
    )
    {
    pList->HEAD	 = NULL;
    pList->TAIL  = NULL;
    pList->count = 0;
    }
/*************************************************************************
*
* lstAdd - add a node to the end of a list
*
* This routine adds a specified node to the end of a specified list.
*
* RETURNS: N/A
*/

void lstAdd
    (
    LIST *pList,        /* pointer to list descriptor */
    NODE *pNode         /* pointer to node to be added */
    )
    {
    lstInsert (pList, pList->TAIL, pNode);
    }
/**************************************************************************
*
* lstConcat - concatenate two lists
*
* This routine concatenates the second list to the end of the first list.
* The second list is left empty.  Either list (or both) can be
* empty at the beginning of the operation.
*
* RETURNS: N/A
*/

void lstConcat
    (
    FAST LIST *pDstList,        /* destination list */
    FAST LIST *pAddList         /* list to be added to dstList */
    )
    {
    if (pAddList->count == 0)		/* nothing to do if AddList is empty */
	return;

    if (pDstList->count == 0)
	*pDstList = *pAddList;
    else
	{
	/* both lists non-empty; update DstList pointers */

	pDstList->TAIL->next     = pAddList->HEAD;
	pAddList->HEAD->previous = pDstList->TAIL;
	pDstList->TAIL           = pAddList->TAIL;

	pDstList->count += pAddList->count;
	}

    /* make AddList empty */

    lstInit (pAddList);
    }
/**************************************************************************
*
* lstCount - report the number of nodes in a list
*
* This routine returns the number of nodes in a specified list.
*
* RETURNS:
* The number of nodes in the list.
*/

int lstCount
    (
    LIST *pList         /* pointer to list descriptor */
    )
    {
    return (pList->count);
    }
/**************************************************************************
*
* lstDelete - delete a specified node from a list
*
* This routine deletes a specified node from a specified list.
*
* RETURNS: N/A
*/

void lstDelete
    (
    FAST LIST *pList,   /* pointer to list descriptor */
    FAST NODE *pNode    /* pointer to node to be deleted */
    )
    {
    if (pNode->previous == NULL)
	pList->HEAD = pNode->next;
    else
	pNode->previous->next = pNode->next;

    if (pNode->next == NULL)
	pList->TAIL = pNode->previous;
    else
	pNode->next->previous = pNode->previous;

    /* update node count */

    pList->count--;
    }
/************************************************************************
*
* lstExtract - extract a sublist from a list
*
* This routine extracts the sublist that starts with <pStartNode> and ends
* with <pEndNode> from a source list.  It places the extracted list in
* <pDstList>.
*
* RETURNS: N/A
*/

void lstExtract
    (
    FAST LIST *pSrcList,      /* pointer to source list */
    FAST NODE *pStartNode,    /* first node in sublist to be extracted */
    FAST NODE *pEndNode,      /* last node in sublist to be extracted */
    FAST LIST *pDstList       /* ptr to list where to put extracted list */
    )
    {
    FAST int i;
    FAST NODE *pNode;

    /* fix pointers in original list */

    if (pStartNode->previous == NULL)
	pSrcList->HEAD = pEndNode->next;
    else
	pStartNode->previous->next = pEndNode->next;

    if (pEndNode->next == NULL)
	pSrcList->TAIL = pStartNode->previous;
    else
	pEndNode->next->previous = pStartNode->previous;


    /* fix pointers in extracted list */

    pDstList->HEAD = pStartNode;
    pDstList->TAIL = pEndNode;

    pStartNode->previous = NULL;
    pEndNode->next       = NULL;


    /* count number of nodes in extracted list and update counts in lists */

    i = 0;

    for (pNode = pStartNode; pNode != NULL; pNode = pNode->next)
	i++;

    pSrcList->count -= i;
    pDstList->count = i;
    }
/************************************************************************
*
* lstFirst - find first node in list
*
* This routine finds the first node in a linked list.
*
* RETURNS
* A pointer to the first node in a list, or
* NULL if the list is empty.
*/

NODE *lstFirst
    (
    LIST *pList         /* pointer to list descriptor */
    )
    {
    return (pList->HEAD);
    }
/************************************************************************
*
* lstGet - delete and return the first node from a list
*
* This routine gets the first node from a specified list, deletes the node
* from the list, and returns a pointer to the node gotten.
*
* RETURNS
* A pointer to the node gotten, or
* NULL if the list is empty.
*/

NODE *lstGet
    (
    FAST LIST *pList    /* ptr to list from which to get node */
    )
    {
    FAST NODE *pNode = pList->HEAD;

    if (pNode != NULL)                      /* is list empty? */
	{
	pList->HEAD = pNode->next;          /* make next node be 1st */

	if (pNode->next == NULL)            /* is there any next node? */
	    pList->TAIL = NULL;             /*   no - list is empty */
	else
	    pNode->next->previous = NULL;   /*   yes - make it 1st node */

	pList->count--;                     /* update node count */
	}

    return (pNode);
    }
/************************************************************************
*
* lstInsert - insert a node in a list after a specified node
*
* This routine inserts a specified node in a specified list.
* The new node is placed following the list node <pPrev>.
* If <pPrev> is NULL, the node is inserted at the head of the list.
*
* RETURNS: N/A
*/

void lstInsert
    (
    FAST LIST *pList,   /* pointer to list descriptor */
    FAST NODE *pPrev,   /* pointer to node after which to insert */
    FAST NODE *pNode    /* pointer to node to be inserted */
    )
    {
    FAST NODE *pNext;

    if (pPrev == NULL)
	{				/* new node is to be first in list */
	pNext = pList->HEAD;
	pList->HEAD = pNode;
	}
    else
	{				/* make prev node point fwd to new */
	pNext = pPrev->next;
	pPrev->next = pNode;
	}

    if (pNext == NULL)
	pList->TAIL = pNode;		/* new node is to be last in list */
    else
	pNext->previous = pNode;	/* make next node point back to new */


    /* set pointers in new node, and update node count */

    pNode->next		= pNext;
    pNode->previous	= pPrev;

    pList->count++;
    }
/************************************************************************
*
* lstLast - find the last node in a list
*
* This routine finds the last node in a list.
*
* RETURNS
* A pointer to the last node in the list, or
* NULL if the list is empty.
*/

NODE *lstLast
    (
    LIST *pList         /* pointer to list descriptor */
    )
    {
    return (pList->TAIL);
    }
/************************************************************************
*
* lstNext - find the next node in a list
*
* This routine locates the node immediately following a specified node.
*
* RETURNS:
* A pointer to the next node in the list, or
* NULL if there is no next node.
*/

NODE *lstNext
    (
    NODE *pNode         /* ptr to node whose successor is to be found */
    )
    {
    return (pNode->next);
    }
/************************************************************************
*
* lstNth - find the Nth node in a list
*
* This routine returns a pointer to the node specified by a number <nodenum>
* where the first node in the list is numbered 1.
* Note that the search is optimized by searching forward from the beginning
* if the node is closer to the head, and searching back from the end
* if it is closer to the tail.
*
* RETURNS:
* A pointer to the Nth node, or
* NULL if there is no Nth node.
*/

NODE *lstNth
    (
    FAST LIST *pList,           /* pointer to list descriptor */
    FAST int nodenum            /* number of node to be found */
    )
    {
    FAST NODE *pNode;

    /* verify node number is in list */

    if ((nodenum < 1) || (nodenum > pList->count))
	return (NULL);


    /* if nodenum is less than half way, look forward from beginning;
	otherwise look back from end */

    if (nodenum < (pList->count >> 1))
	{
	pNode = pList->HEAD;

	while (--nodenum > 0)
	    pNode = pNode->next;
	}

    else
	{
	nodenum -= pList->count;
	pNode = pList->TAIL;

	while (nodenum++ < 0)
	    pNode = pNode->previous;
	}

    return (pNode);
    }
/************************************************************************
*
* lstPrevious - find the previous node in a list
*
* This routine locates the node immediately preceding the node pointed to 
* by <pNode>.
*
* RETURNS:
* A pointer to the previous node in the list, or
* NULL if there is no previous node.
*/

NODE *lstPrevious
    (
    NODE *pNode         /* ptr to node whose predecessor is to be found */
    )
    {
    return (pNode->previous);
    }
/************************************************************************
*
* lstNStep - find a list node <nStep> steps away from a specified node
*
* This routine locates the node <nStep> steps away in either direction from 
* a specified node.  If <nStep> is positive, it steps toward the tail.  If
* <nStep> is negative, it steps toward the head.  If the number of steps is
* out of range, NULL is returned.
*
* RETURNS:
* A pointer to the node <nStep> steps away, or
* NULL if the node is out of range.
*/

NODE *lstNStep
    (
    FAST NODE *pNode,           /* the known node */
    int nStep                   /* number of steps away to find */
    )
    {
    int i;

    for (i = 0; i < abs (nStep); i++)
	{
	if (nStep < 0)
	    pNode = pNode->previous;
	else if (nStep > 0)
	    pNode = pNode->next;
	if (pNode == NULL)
	    break;
	}
    return (pNode);
    }

/************************************************************************
*
* lstFind - find a node in a list
*
* This routine returns the node number of a specified node (the 
* first node is 1).
*
* RETURNS:
* The node number, or
* ERROR if the node is not found.
*/

int lstFind
    (
    LIST *pList,                /* list in which to search */
    FAST NODE *pNode            /* pointer to node to search for */
    )
    {

    FAST NODE *pNextNode;
    FAST int index = 1;

    pNextNode = lstFirst (pList);

    while ((pNextNode != NULL) && (pNextNode != pNode))
	{
	index++;
	pNextNode = lstNext (pNextNode);
	}

    if (pNextNode == NULL)
	return (ERROR);
    else
	return (index);
    }

/************************************************************************
*
* lstFree - free up a list
*
* This routine turns any list into an empty list.
* It also frees up memory used for nodes.
*
* RETURNS: N/A
*
* SEE ALSO: free()
*/

void lstFree
    (
    LIST *pList         /* list for which to free all nodes */
    )
    {
    NODE *p1, *p2;

    if (pList->count > 0)
	{
	p1 = pList->HEAD;
	while (p1 != NULL)
	    {
	    p2 = p1->next;
	    free ((char *)p1);
	    p1 = p2;
	    }
	pList->count = 0;
	pList->HEAD = pList->TAIL = NULL;
	}
    }
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: VxWorks 5.5是一款实时操作系统(RTOS)的版本,该版本的开发文档提供了开发者所需的全部信息和指导,以便能够使用这个操作系统进行软件开发。 开发文档通常包含以下内容:系统架构和配置指南、安装和使用指南、API和函数参考手册、开发工具的说明和使用方法等。 在系统架构和配置指南中,文档会介绍VxWorks 5.5的整体结构和各个模块的功能。它还会提供配置系统所需的步骤和指导,以确保开发者正确地设置和调整操作系统。 安装和使用指南会详细说明如何在目标硬件上安装VxWorks 5.5,并配置开发环境。这些指南会包含从下载VxWorks 5.5到在硬件上运行和调试的步骤,以及可能遇到的常见问题的解决方法。 API和函数参考手册是开发者在使用VxWorks 5.5时的重要参考。其中会列出所有可用的API和函数,以及它们的参数、返回值和使用方法。开发者可以根据自己的需求和要解决的问题,查找和使用合适的API和函数。 开发文档还会介绍各种开发工具,例如编译器、调试器和性能分析工具。文档会解释这些工具的功能和使用方法,以及如何结合VxWorks 5.5进行开发和调试。 总之,VxWorks 5.5的开发文档对于那些希望使用这个实时操作系统进行软件开发的开发者来说是非常重要的。它提供了系统架构、安装和使用指南、API和函数参考手册,以及开发工具的说明和使用方法等方面的信息和指导,帮助开发者顺利地进行软件开发工作。 ### 回答2: VxWorks 5.5是一款嵌入式实时操作系统,其开发文档提供了详细的技术资料和指南,帮助开发人员理解和使用该操作系统。 VxWorks 5.5的开发文档包含以下主要内容: 1. 系统架构概述:介绍VxWorks 5.5的体系结构、内核组件和系统层次结构,帮助开发人员了解其基本原理和工作方式。 2. 编程指南:提供了使用VxWorks 5.5开发嵌入式应用程序的详细指南。该指南涵盖了任务管理、内存管理、进程间通信、中断处理和设备驱动程序等方面的内容,帮助开发人员编写高效、可靠的嵌入式软件。 3. API参考手册:列出了VxWorks 5.5支持的各种API函数和系统调用,包括任务控制、内存分配、文件系统和网络编程等。该手册提供了函数的详细说明、参数列表和用法示例,方便开发人员快速查找和使用API函数。 4. BSP开发指南:介绍了如何为特定的硬件平台开发VxWorks 5.5的板支持包(BSP)。该指南解释了BSP的概念、结构和要求,并提供了开发BSP所需的工具和技术。 5. 配置和调试指南:详细说明了如何配置和调试VxWorks 5.5系统。该指南包括系统启动过程的步骤、配置选项的设置和调试工具的使用方法,帮助开发人员快速调试和优化嵌入式应用程序。 VxWorks 5.5开发文档的编写充分考虑了各种应用场景和开发需求,并提供了丰富的示例和实践建议。通过阅读和理解开发文档,开发人员可以更好地利用VxWorks 5.5的功能和特性,开发出高质量和可靠的嵌入式应用程序。同时,VxWorks 5.5的开发文档也是学习嵌入式系统和实时操作系统开发的重要参考资料之一。 ### 回答3: VxWorks是一款实时操作系统 (RTOS),用于嵌入式系统开发。VxWorks 5.5是其版本之一,是一套非常重要的开发文档。它包含了VxWorks 5.5操作系统的安装、配置、编程接口等方面的详细说明,帮助开发人员更好地理解和使用这个操作系统。 首先,VxWorks 5.5开发文档提供了操作系统的安装和配置指南。通过这些指南,开发人员可以了解如何在目标硬件上正确地安装和配置VxWorks 5.5。这些指南详细介绍了安装和配置的步骤以及可能出现的问题和解决方案。 其次,VxWorks 5.5开发文档提供了丰富的编程接口文档。这些文档详细描述了VxWorks 5.5操作系统提供的函数和API,开发人员可以根据需要选择合适的API进行程序开发。这些编程接口文档还包括实时任务调度、内存管理、设备驱动程序等方面的说明,帮助开发人员编写高效可靠的嵌入式软件。 此外,VxWorks 5.5开发文档还提供了大量的示例代码和案例分析。这些示例代码和案例分析展示了如何使用VxWorks 5.5来解决各种实际问题,包括网络通信、文件系统、远程调试等。开发人员可以通过学习这些示例代码和案例分析,更好地理解VxWorks 5.5的使用方法,并运用到实际的项目开发中。 总之,VxWorks 5.5开发文档对于想要深入了解和使用该操作系统的开发人员来说,是一份非常宝贵的资。通过学习这些文档,开发人员可以掌握VxWorks 5.5的安装、配置和编程接口等方面的知识,为开发高质量的嵌入式软件提供有力支持。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小熊coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值