📄 mbuflib.c
字号:
(*freeRtn) (mbufDesc->buf, freeArg); lockKey = intLock (); mbufDesc->mbufDescNext = mbufDescHead; /* push desc onto free list */ mbufDescHead = mbufDesc; intUnlock (lockKey); }/********************************************************************************* _mbufInsertCopy - copy buffer data into an mbuf chain** This routine copies <len> bytes of data from the user buffer <buf> and* inserts it at the specified byte location in <mbufId>. The user buffer* is in no way tied to the mbuf chain after this operation; a separate copy* of the data is made.** 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.** RETURNS:* The mbuf pointer associated with the first inserted mbuf,* or NULL if the operation failed.** NOMANUAL*/MBUF_SEG _mbufInsertCopy ( MBUF_ID mbufId, /* mbuf ID into which data is copied */ MBUF_SEG mbufSeg, /* mbuf base for <offset> */ int offset, /* relative byte offset */ caddr_t buf, /* buffer from which data is copied */ int len /* number of byte to copy */ ) { MBUF_ID mbufIdNew; /* dummy ID for dup operation */ MBUF_SEG mbufNew; /* mbuf for copy */ MBUF_SEG * pMbufPrev; /* mbuf previous to mbufNew */ int length; /* length of each copy */ if (len <= 0) /* have to copy some bytes */ { errno = S_mbufLib_LENGTH_INVALID; return (NULL); } MBUF_ID_CREATE (mbufIdNew); /* create new ID for copy */ if (mbufIdNew == NULL) return (NULL); pMbufPrev = &mbufIdNew->mbufHead; /* init prev ptr to head */ while (len) /* while more to copy */ { /* obtain a new mbuf with a data buffer pointed */ if ( (mbufNew = mBufClGet (M_WAIT, MT_DATA, len, FALSE)) == NULL) { /* release on fail */ MBUF_ID_DELETE(mbufIdNew); return (NULL); } length = min (len, mbufNew->m_extSize); /* num for copy */ bcopy (buf, mtod (mbufNew, char *), length); buf += length; /* bump to new buf position */ len -= length; mbufNew->m_len = length; /* set len to num copied */ *pMbufPrev = mbufNew; /* hook prev into new */ pMbufPrev = &mbufNew->m_next; /* update prev mbuf ptr */ } /* insert the new mbuf ID with copied data into <mbufId> */ if ((mbufSeg = _mbufInsert (mbufId, mbufSeg, offset, mbufIdNew)) == NULL) { /* release on fail */ MBUF_ID_DELETE(mbufIdNew); return (NULL); } return (mbufSeg); /* return inserted mbuf */ }/********************************************************************************* _mbufExtractCopy - copy data from an mbuf chain to a buffer** This routine copies <len> bytes of data from <mbufId> to the user* buffer <buf>.** The starting location of the copy is specified by <mbufSeg> and <offset>.** The number of bytes to be copied is given by <len>. If this parameter* is negative, or is larger than the number of bytes in the chain after the* specified byte location, the rest of the chain will be copied. * The bytes to be copied may span more than one mbuf.** RETURNS:* The number of bytes copied from the mbuf chain to the buffer,* or ERROR if the operation failed.** NOMANUAL*/int _mbufExtractCopy ( MBUF_ID mbufId, /* mbuf ID from which data is copied */ MBUF_SEG mbufSeg, /* mbuf base for <offset> */ int offset, /* relative byte offset */ caddr_t buf, /* buffer into which data is copied */ int len /* number of bytes to copy */ ) { caddr_t buf0 = buf; /* save starting position */ int length; /* length of each copy */ /* find the starting location for copying */ if ((mbufSeg = _mbufSegFind (mbufId, mbufSeg, &offset)) == NULL) return (ERROR); if (len < 0) /* negative = rest of chain */ len = INT_MAX; while (len && (mbufSeg != NULL)) /* while more to copy */ { length = min (len, (mbufSeg->m_len - offset)); /* num for copy */ bcopy (mtod (mbufSeg, char *) + offset, buf, length); buf += length; /* bump to new buf position */ len -= length; mbufSeg = mbufSeg->m_next; /* bump to next mbuf in chain */ offset = 0; /* no more offset */ } return ((int) buf - (int) buf0); /* return num bytes copied */ }/********************************************************************************* _mbufCut - cut bytes from an mbuf chain** This routine deletes <len> bytes from <mbufId> starting at the specified* byte location.** The starting location of deletion is specified by <mbufSeg> and <offset>.** The number of bytes to be cut is given by <len>. If this parameter* is negative, or is larger than the number of bytes in the chain after the* specified byte location, the rest of the chain will be deleted.* The bytes to be deleted may span more than one mbuf. If all the bytes* in any one mbuf are deleted, then the mbuf will be returned to the* system. No mbuf may have zero bytes left in it. ** Deleting bytes out of the middle of an mbuf will cause the mbuf to* be split into two mbufs. The first mbuf will contain the portion* of the mbuf before the deleted bytes, while the other mbuf will* contain the end portion that remains after <len> bytes are deleted.** This routine returns the mbuf pointer associated with the mbuf just* after the deleted bytes. In the case where bytes are cut off the end* of an mbuf chain, a value of MBUF_NONE is returned.** RETURNS:* The mbuf pointer associated with the mbuf following the deleted bytes,* or NULL if the operation failed.** NOMANUAL*/MBUF_SEG _mbufCut ( MBUF_ID mbufId, /* mbuf ID from which bytes are cut */ MBUF_SEG mbufSeg, /* mbuf base for <offset> */ int offset, /* relative byte offset */ int len /* number of bytes to cut */ ) { MBUF_ID mbufIdNew; /* dummy ID for dup operation */ MBUF_SEG * pMbufPrev; /* mbuf prev deleted mbuf */ int length; /* length of each cut */ /* find the mbuf ptr previous to the cut */ if ((pMbufPrev = _mbufSegFindPrev (mbufId, mbufSeg, &offset)) == NULL) return (NULL); if ((mbufSeg = *pMbufPrev) == NULL) /* find cut mbuf */ { errno = S_mbufLib_SEGMENT_NOT_FOUND; return (NULL); } if (len < 0) /* negative = rest of chain */ len = INT_MAX; while (len && (mbufSeg != NULL)) /* while more to cut */ { length = min (len, (mbufSeg->m_len - offset)); /* num for cut */ len -= length; if (offset != 0) /* if !cutting off front... */ { if (mbufSeg->m_len != (offset + length))/* cut from middle*/ { /* duplicate portion remaining after bytes to be cut */ if ((mbufIdNew = _mbufDup (mbufId, mbufSeg, offset + length, mbufSeg->m_len - offset - length)) == NULL) return (NULL); mbufIdNew->mbufHead->m_next = mbufSeg->m_next; mbufSeg->m_next = mbufIdNew->mbufHead;/* hook in saved data */ mbufSeg->m_len = offset; /* shorten to later portion */ MBUF_ID_DELETE_EMPTY(mbufIdNew);/* delete dup ID */ return (mbufSeg->m_next); /* return next real mbuf */ } else /* cut to end */ { mbufSeg->m_len -= length; /* decrease by len deleted */ pMbufPrev = &mbufSeg->m_next; /* update previous */ mbufSeg = mbufSeg->m_next; /* bump current mbuf to next */ } offset = 0; /* no more offset */ } else /* cutting off front... */ { if (length == mbufSeg->m_len) /* cutting whole mbuf ? */ { mbufSeg = m_free (mbufSeg); /* free and get next mbuf */ *pMbufPrev = mbufSeg; /* hook prev to next mbuf */ } else /* cut off front portion */ { mbufSeg->m_data += length; /* bump up offset */ mbufSeg->m_len -= length; /* taken from front */ } } } if (mbufSeg == NULL) /* special case - cut off end */ return (MBUF_NONE); return (mbufSeg); /* return next real mbuf */ }/********************************************************************************* _mbufSplit - split an mbuf chain into two separate mbuf chains** This routine splits <mbufId> into two separate chains at the specified* byte location. The first portion remains in <mbufId>, while the end* portion is returned in a newly created mbuf ID.** The location of the split is specified by <mbufSeg> and <offset>.** RETURNS:* The mbuf ID of a newly created chain containing the end portion of <mbufId>,* or NULL if the operation failed.** NOMANUAL*/MBUF_ID _mbufSplit ( MBUF_ID mbufId, /* mbuf ID to split into two */ MBUF_SEG mbufSeg, /* mbuf base for <offset> */ int offset /* relative byte offset */ ) { MBUF_ID mbufIdNew; /* mbuf ID of later portion */ MBUF_SEG * pMbufPrev; /* mbuf prev to insert */ /* find the mbuf ptr previous to the split */ if ((pMbufPrev = _mbufSegFindPrev (mbufId, mbufSeg, &offset)) == NULL) return (NULL); if (offset == 0) /* in middle of mbuf */ { MBUF_ID_CREATE (mbufIdNew); /* create ID for end portion */ if (mbufIdNew == NULL) return (NULL); mbufIdNew->mbufHead = *pMbufPrev; /* hook in new head */ *pMbufPrev = NULL; /* tie off first portion */ } else /* split on mbuf boundary */ { mbufSeg = *pMbufPrev; /* find split mbuf */ if ((mbufIdNew = _mbufDup (mbufId, mbufSeg, offset, mbufSeg->m_len - offset)) == NULL) return (NULL); /* dup later portion */ mbufIdNew->mbufHead->m_next = mbufSeg->m_next; mbufSeg->m_len = offset; mbufSeg->m_next = NULL; /* tie off first portion */ } return (mbufIdNew); /* return ID for end */ }/********************************************************************************* _mbufDup - duplicate an mbuf chain** This routine duplicates <len> bytes of <mbufId> starting at the specified* byte location, and returns the mbuf ID of the newly created duplicate mbuf.** The starting location of duplication is specified by <mbufSeg> and <offset>.* The number of bytes to be duplicated is given by <len>. If this* parameter is negative, or is larger than the than the number of bytes* in the chain after the specified byte location, the rest of the chain will* be duplicated.** Duplication of mbuf data only involves copying of the data when the mbuf* is not a cluster. If the mbuf to be duplicated is a cluster, the mbuf* pointer information is duplicated, while the data is not. This implies* that that the mbuf data is shared among all clusters associated* with a particular cluster data buffer.** RETURNS:* The mbuf ID of a newly created dupicate mbuf chain,* or NULL if the operation failed.** NOMANUAL*/MBUF_ID _mbufDup ( MBUF_ID mbufId, /* mbuf ID to duplicate */ MBUF_SEG mbufSeg, /* mbuf base for <offset> */ int offset, /* relative byte offset */ int len /* number of bytes to duplicate */ ) { MBUF_ID mbufIdNew; /* mbuf ID of duplicate */ MBUF_SEG mbufNew; /* mbuf for duplicate */ MBUF_SEG * pMbufPrev; /* mbuf prev to mbufNew */ /* find the starting location for duplicate */ if ((mbufSeg = _mbufSegFind (mbufId, mbufSeg, &offset)) == NULL) return (NULL); if (len < 0) /* negative = rest of chain */ len = INT_MAX; MBUF_ID_CREATE (mbufIdNew); /* get ID for duplicate */ if (mbufIdNew == NULL) return (NULL); pMbufPrev = &mbufIdNew->mbufHead; /* init prev ptr to head */ while (len && (mbufSeg != NULL)) /* while more to duplicate */ { /* get mbuf for duplicate */ if ( (mbufNew = mBlkGet (_pNetDpool, M_WAIT, mbufSeg->m_type)) == NULL) { /* release on fail */ MBUF_ID_DELETE(mbufIdNew); return (NULL); } mbufNew->m_len = min (len, (mbufSeg->m_len - offset)); len -= mbufNew->m_len; /* num to duplicate */ /* copy the cluster header mbuf info to duplicate */ mbufNew->m_data = mtod (mbufSeg, char *) + offset; mbufNew->m_flags = mbufSeg->m_flags; mbufNew->m_ext = mbufSeg->m_ext; /* bump share count */ { int s = intLock (); ++(mbufNew->m_extRefCnt); intUnlock (s); } *pMbufPrev = mbufNew; /* hook prev into duplicate */ pMbufPrev = &mbufNew->m_next; /* update prev mbuf ptr */ mbufSeg = mbufSeg->m_next; /* bump original chain */ offset = 0; /* no more offset */ } return (mbufIdNew); /* return ID of duplicate */ }/********************************************************************************* _mbufLength - determine the legnth in bytes of an mbuf chain** This routine returns the number of bytes in the mbuf chain <mbufId>.** RETURNS:* The number of bytes in the mbuf chain,* or ERROR if the operation failed** NOMANUAL*/int _mbufLength ( MBUF_ID mbufId /* mbuf ID to find length of */ ) { MBUF_SEG mbuf; int length = 0; /* total length */ if (mbufId == NULL || mbufId->type != MBUF_VALID) /* invalid ID ? */ { errno = S_mbufLib_ID_INVALID; return (ERROR); } for (mbuf = mbufId->mbufHead; mbuf != NULL; mbuf = mbuf->m_next) length += mbuf->m_len; return (length); /* return total length */ }#if FALSE/********************************************************************************* _mbufSegJoin - coalesce two adjacent mbuf cluster fragments.** This routine combines two or more contiguous mbufs into a single* mbuf. Such an operation is only feasible for joining mbufs* that have the same freeRtn and freeArg, and that follow eachother in* the chain. This could be useful for coalescing mbufs fragmented* by split operations.* * Not in service yet -> not clear that it is a useful routine.** NOMANUAL*/STATUS _mbufSegJoin ( MBUF_ID mbufId, /* mbuf ID containing mbufs to join */ MBUF_SEG mbufSeg /* first mbuf to join */ ) { MBUF_SEG mbufNext; STATUS status = ERROR; if (mbufId == NULL || mbufId->type != MBUF_VALID) /* invalid ID ? */ { errno = S_mbufLib_ID_INVALID; return (ERROR); } if (mbufId->mbufHead == NULL) /* is this chain empty ? */ { errno = S_mbufLib_ID_EMPTY; return (ERROR); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -