⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mbuflib.c

📁 VXWORKS源代码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* mbufLib.c - BSD mbuf interface library *//* Copyright 1984 - 2001 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01i,15oct01,rae  merge from truestack ver 01l, base 01h01h,25aug98,n_s  corrected error handling of buffer allocate calls. spr #2210401g,19sep97,vin  added clBlk specific code, changed pointers to refcounts		 of clusters to refcounts since refcount has been moved to                 clBlk01f,12aug97,vin	 changed mBlkGet to take _pNetDpool01e,22nov96,vin  added cluster support replaced m_get(..) with mBlkGet(..).01d,29aug96,vin  made the code use only m_get.01c,06jun96,vin  made compatible with BSD4.4 mbufs.01b,13nov95,dzb  changed to validate mbufId.type off MBUF_VALID (SPR #4066).01a,08nov94,dzb  written.*//*DESCRIPTIONThis library contains routines to create, build, manipulate, anddelete BSD mbufs.  It serves as a back-end to the zbuf API providedby zbufLib.Note: The user should protect mbufs before calling these routines.  For many      operations, it may be appropriate to guard with the spl semaphore.NOMANUAL*//* includes */#include "vxWorks.h"#include "zbufLib.h"#include "mbufLib.h"#include "errnoLib.h"#include "intLib.h"#include "stdlib.h"#include "memPartLib.h"#ifdef VIRTUAL_STACK#include "netinet/vsLib.h"#endif/* globals */MBUF_ID			_mbufIdHead = NULL;	/* Head ID of free chain *//* locals */LOCAL BOOL		mbufInit = FALSE;	/* library initialized ? */LOCAL MBUF_DESC		mbufDescHead = NULL;	/* Head of free desc list *//* declare mbuf interface function table */LOCAL ZBUF_FUNC		mbufFunc =    {    (FUNCPTR) _mbufCreate,    (FUNCPTR) _mbufDelete,    (FUNCPTR) _mbufInsert,    (FUNCPTR) _mbufInsertBuf,    (FUNCPTR) _mbufInsertCopy,    (FUNCPTR) _mbufExtractCopy,    (FUNCPTR) _mbufCut,    (FUNCPTR) _mbufSplit,    (FUNCPTR) _mbufDup,    (FUNCPTR) _mbufLength,    (FUNCPTR) _mbufSegFind,    (FUNCPTR) _mbufSegNext,    (FUNCPTR) _mbufSegPrev,    (FUNCPTR) _mbufSegData,    (FUNCPTR) _mbufSegLength    };/* static function declarations */LOCAL void		_mbufBufFree (MBUF_DESC mbufDesc, VOIDFUNCPTR freeRtn,                            int freeArg);LOCAL MBUF_SEG *	_mbufSegFindPrev (MBUF_ID mbufId, MBUF_SEG mbufSeg,		            int *pOffset);/********************************************************************************* _mbufLibInit - initialize the BSD mbuf interface library** This routine initializes the BSD mbuf interface library and publishes* the mbufLib API to the caller.  Lists of free ID structures and* free mbuf cluster descriptors are allocated in this routine.** The mbufLib API func table "mbufFunc" is published to the caller upon* completion of initialization.  Typically, this func table is used by* the zbuf facility to call mbuf routines within this library to perform* buffering operations.  Even though the zbuf interface defines the ZBUF_FUNC* struct, it doesn't necessarily mean that zbufs must be present.  This* library may be used even if zbufs have been scaled out of the system.** RETURNS:* A pointer to a func table containing the mbufLib API,* or NULL if the mbuf interface could not be initialized.** NOMANUAL*/void * _mbufLibInit (void)    {    int			ix;			/* counter for list init */    if (mbufInit == TRUE)			/* already initialized ? */        return ((void *) &mbufFunc);    if ((_mbufIdHead = (MBUF_ID) KHEAP_ALLOC((sizeof (struct mbufId) *	MBUF_ID_INC))) == NULL)			/* alloc space for ID list */        return (NULL);    if ((mbufDescHead = (MBUF_DESC) KHEAP_ALLOC((sizeof (struct mbufDesc) *	MBUF_DESC_INC))) == NULL)		/* alloc space for desc list */	{	KHEAP_FREE((char *)_mbufIdHead);        return (NULL);	}	    /* divide up into an sll of free mbuf ID's, _mbufIdHead as the head */    for (ix = 0; ix < (MBUF_ID_INC - 1); ix++)        _mbufIdHead[ix].mbufIdNext = &_mbufIdHead[ix + 1];    _mbufIdHead[ix].mbufIdNext = NULL;    /* divide up into an sll of free buf desc, mbufDescHead as the head */    for (ix = 0; ix < (MBUF_DESC_INC - 1); ix++)        mbufDescHead[ix].mbufDescNext = &mbufDescHead[ix + 1];    mbufDescHead[ix].mbufDescNext = NULL;    mbufInit = TRUE;				/* init successful */    return ((void *) &mbufFunc);		/* return mbuf func table */    }/********************************************************************************* _mbufCreate - create an empty mbuf ID** This routine creates an mbuf ID, which remains empty (i.e., does not* contain any mbufs) until mbufs are added by the mbuf insertion routines.* Operations performed on mbufs require an mbuf ID, which is returned by* this routine.** RETURNS:* An mbuf ID, or NULL if an mbuf ID could not be created.** SEE ALSO: _mbufCreate** NOMANUAL*/MBUF_ID _mbufCreate (void)    {    MBUF_ID		mbufId;			/* obtained mbuf ID */    int			ix;			/* counter for list init */    int			lockKey = intLock ();	/* int lock cookie */    if ((mbufId = _mbufIdHead) != NULL)		/* free list empty ? */        {        _mbufIdHead = mbufId->mbufIdNext;	/* pop first ID off list */        intUnlock (lockKey);        mbufId->type = MBUF_VALID;		/* init new ID type */        mbufId->mbufHead = NULL;		/* init new ID head */        }    else					/* list is empty */	{        intUnlock (lockKey);        if ((mbufId = (MBUF_ID) KHEAP_ALLOC((sizeof (struct mbufId) *            MBUF_ID_INC))) != NULL)		/* alloc more IDs */            {            for (ix = 0; ix < (MBUF_ID_INC - 1); ix++)	/* into an sll */                mbufId[ix].mbufIdNext = &mbufId[ix + 1];            lockKey = intLock ();            mbufId[ix].mbufIdNext = _mbufIdHead;            _mbufIdHead = mbufId->mbufIdNext;	/* hook head onto new list */            intUnlock (lockKey);            mbufId->type = MBUF_VALID;		/* init new ID type */            mbufId->mbufHead = NULL;		/* init new ID head */            }        }    return (mbufId);				/* return new ID */    }/********************************************************************************* _mbufDelete - delete an mbuf ID and free any associated mbufs** This routine will free any mbufs associated with <mbufId>, then* delete the mbuf ID itself.  <mbufId> should not be used after this* routine executes successfully.** RETURNS:* OK, or ERROR if the mbuf ID could not be deleted.** SEE ALSO: _mbufCreate** NOMANUAL*/STATUS _mbufDelete    (    MBUF_ID		mbufId		/* mbuf ID to free */    )    {    /* already free ? */        if (mbufId == NULL || mbufId->type != MBUF_VALID)			{	errno = S_mbufLib_ID_INVALID;		/* invalid if already free */	return (ERROR);	}    MBUF_ID_DELETE(mbufId);			/* delete mbuf ID */    return (OK);				/* return OK, always */    }/********************************************************************************* _mbufInsert - insert an mbuf chain into another mbuf chain** This routine inserts all <mbufId2> mbufs into <mbufId1> at the* specified byte location** The location of insertion is specified by <mbufSeg> and <offset>.  Note* that insertion within a chain occurs before the byte location specified* by <mbufSeg> and <offset>.  Additionally, note that <mbufSeg> and <offset>* must be "NULL" and "0", respectively, when inserting into an empty mbuf ID.** After all the <mbufId2> mbufs are inserted into <mbufId1>, the mbuf ID* <mbufId2> is deleted.  <mbufId2> should not be used after this routine* executes successfully.** RETURNS:* The mbuf pointer associated with the first inserted mbuf,* or NULL if the operation failed.** NOMANUAL*/MBUF_SEG _mbufInsert    (    MBUF_ID		mbufId1,	/* mbuf ID to insert <mbufId2> into */    MBUF_SEG		mbufSeg,	/* mbuf base for <offset> */    int			offset,		/* relative byte offset */    MBUF_ID		mbufId2		/* mbuf ID to insert into <mbufId1> */    )    {    MBUF_ID		mbufIdNew;		/* dummy ID for dup operation */    MBUF_SEG *		pMbufPrev;		/* mbuf prev to insert */    MBUF_SEG		mbufEnd;		/* last Id2 mbuf */    int			maxLen = MBUF_END;	/* offset for last Id2 byte */    /* find the mbuf ptr previous to the point of insertion */    if ((pMbufPrev = _mbufSegFindPrev (mbufId1, mbufSeg, &offset)) == NULL)        return (NULL);    if ((mbufEnd = _mbufSegFind (mbufId2, NULL, &maxLen)) == NULL)	return (NULL);				/* find end mbuf of Id2 */    if (offset == 0)				/* if prepend... */	{	mbufEnd->m_next = *pMbufPrev;	*pMbufPrev = mbufId2->mbufHead;        }    else					/* else insert... */        {	mbufSeg = *pMbufPrev;			/* find insertion mbuf */        if ((mbufIdNew = _mbufDup (mbufId1, mbufSeg, offset,            mbufSeg->m_len - offset)) == NULL)            return (NULL);			/* dup later portion */        mbufSeg->m_len = offset;		/* shorten to first portion */        mbufIdNew->mbufHead->m_next = mbufSeg->m_next;        mbufEnd->m_next = mbufIdNew->mbufHead;	/* insert Id2 */        mbufSeg->m_next = mbufId2->mbufHead;        MBUF_ID_DELETE_EMPTY(mbufIdNew);	/* delete dup ID */	}    mbufEnd = mbufId2->mbufHead;		/* save head for return */    if (mbufId2->type == MBUF_VALID)	{        MBUF_ID_DELETE_EMPTY(mbufId2);		/* delete ref to Id2 */	}    return (mbufEnd);				/* return inserted mbuf */    }/********************************************************************************* _mbufInsertBuf - create a cluster from a buffer and insert into an mbuf chain** This routine creates an mbuf cluster from the user buffer <buf> and* inserts it at the specified byte location in <mbufId>.** The location of insertion is specified by <mbufSeg> and <offset>.  Note* that insertion within a chain occurs before the byte location specified* by <mbufSeg> and <offset>.  Additionally, note that <mbufSeg> and <offset>* must be "NULL" and "0", respectively, when inserting into an empty mbuf ID.** The user provided free routine <freeRtn> will be called when the mbuf* cluster created from <buf> is not being referenced by any more mbufs.* If <freeRtn> is NULL, the mbuf will function normally, except that the* user will not be notified when no more mbufs reference cluster containing* <buf>.  <freeRtn> will be called from the context of the task that last* references the cluster.  <freeRtn> should be declared as follows:* .CS*       void freeRtn*           (*           caddr_t     buf,    /@ pointer to user buffer @/*           int         freeArg /@ user provided argument to free routine @/*           )* .CE** RETURNS:* The mbuf pointer associated with the inserted mbuf cluster,* or NULL if the operation failed.** NOMANUAL*/MBUF_SEG _mbufInsertBuf    (    MBUF_ID		mbufId,		/* mbuf	ID which buffer is inserted */    MBUF_SEG		mbufSeg,	/* mbuf base for <offset> */    int			offset,		/* relative byte offset */    caddr_t		buf,		/* user buffer for mbuf cluster */    int			len,		/* number of bytes to insert */    VOIDFUNCPTR		freeRtn,	/* user free routine */    int			freeArg		/* argument to free routine */    )    {    MBUF_ID		mbufIdNew;		/* mbuf ID containing <buf> */    MBUF_DESC 		mbufDesc;		/* desc for <buf> cluster */    MBUF_SEG		mbufNew;		/* mbuf for <buf> cluster */    CL_BLK_ID		pClBlk;			/* pointer to cluster blk */    int			lockKey;		/* int lock cookie */    int			ix;			/* counter for list init */    if (len <= 0)				/* have to insert some bytes */        {	errno = S_mbufLib_LENGTH_INVALID;	return (NULL);	}    MBUF_ID_CREATE (mbufIdNew);			/* create new mbuf ID for buf */    if (mbufIdNew == NULL)	return (NULL);    lockKey = intLock ();    if ((mbufDesc = mbufDescHead) != NULL)	/* free list empty ? */        {        mbufDescHead = mbufDesc->mbufDescNext;	/* pop first desc off list */        intUnlock (lockKey);	}    else					/* list is empty */	{        intUnlock (lockKey);	if ((mbufDesc = (MBUF_DESC) KHEAP_ALLOC((sizeof (struct mbufDesc) *	    MBUF_DESC_INC))) != NULL)		/* alloc more desc's */            {            for (ix = 0; ix < (MBUF_DESC_INC - 1); ix++)                mbufDesc[ix].mbufDescNext = &mbufDesc[ix + 1];            lockKey = intLock ();            mbufDesc[ix].mbufDescNext = mbufDescHead;            mbufDescHead = mbufDesc->mbufDescNext;/* hook head onto new list */            intUnlock (lockKey);	    }        }    if (mbufDesc == NULL)			/* able to get a new desc ? */	{        MBUF_ID_DELETE_EMPTY(mbufIdNew);	return (NULL);	}    mbufDesc->buf = buf;    /* get mbuf for cluster */        if ( (mbufNew = mBlkGet (_pNetDpool, M_WAIT, MT_DATA)) == NULL)        {	/* release on fail */	lockKey = intLock ();	mbufDescHead = mbufDesc;	intUnlock (lockKey);	MBUF_ID_DELETE_EMPTY (mbufIdNew);			return (NULL);	}    pClBlk = clBlkGet (_pNetDpool, M_WAIT);         if (pClBlk == NULL)			/* out of cl Blks */        {        m_free (mbufNew);	lockKey = intLock ();	mbufDescHead = mbufDesc;	intUnlock (lockKey);	MBUF_ID_DELETE_EMPTY (mbufIdNew);			return (NULL);        }    mbufNew->pClBlk		= pClBlk;    /* build <buf> into an mbuf cluster */    mbufNew->m_data		= buf;    mbufNew->m_len		= len;    mbufNew->m_flags		|= M_EXT;    mbufNew->m_extBuf		= buf;    mbufNew->m_extSize		= len;    mbufNew->m_extFreeRtn	= (FUNCPTR) _mbufBufFree;    mbufNew->m_extRefCnt	= 1;    mbufNew->m_extArg1		= (int) mbufDesc;    mbufNew->m_extArg2		= (int) freeRtn;    mbufNew->m_extArg3		= freeArg;    mbufIdNew->mbufHead = mbufNew;		/* put cluster into new ID */    /* insert the new mbuf ID with <buf> into <mbufId> */    if ((mbufSeg = _mbufInsert (mbufId, mbufSeg, offset, mbufIdNew)) == NULL)	{        mbufNew->m_extArg2 = (int)NULL;		/* don't call freeRtn on fail */        MBUF_ID_DELETE(mbufIdNew);	}    return (mbufSeg);				/* return inserted mbuf */    }/********************************************************************************* _mbufBufFree - free a user cluster buffer** This routine is called when a cluster buffer is deleted, and no other* mbuf clusters share the buffer space.  This routine calls the user free* routine connected in _mbufInsertBuf(), then pushes the buffer decsriptor* back onto the desc free list.** RETURNS:* N/A** NOMANUAL*/LOCAL void _mbufBufFree    (    MBUF_DESC		mbufDesc,	/* desc of mbuf to free */    VOIDFUNCPTR		freeRtn,	/* user free routine */    int			freeArg		/* argument to free routine */    )    {    int			lockKey;		/* int lock cookie */    if (freeRtn != NULL)			/* call free rtn if present */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -