📄 cb_util.c
字号:
/****************************************************************************
*****************************************************************************
**
** File Name
** ---------
**
** CB_UTIL.C
**
*****************************************************************************
*****************************************************************************
**
** Description
** -----------
**
** Communications buffers utilities.
**
** Communications buffers (ComBufs) are a structure with an attached buffer
** that provide a way for headers to be prepended and stripped from a packet
** as it passes through the communications stack without data copies or
** moves.
**
** This source module contains a collection of communication buffers related
** utilities.
**
*****************************************************************************
*****************************************************************************
**
** Source Change Indices
** ---------------------
**
** Porting: <none>0----<major> Customization: <none>0----<major>
**
*****************************************************************************
*****************************************************************************
** **
** ETHERNET/IP EXAMPLE CODE **
** COPYRIGHT (c) 2000-2005 ODVA (Open DeviceNet Vendor Association) **
** & ControlNet International Ltd. **
** **
** All rights reserved, except as specifically licensed in writing. **
** Use of the Ethernet/IP Example Protocol Software is subject to **
** ODVA's and ControlNet International's Terms of Use Agreement. **
** The following work constitutes example program code and is intended **
** merely to illustrate useful programming techniques. The user is **
** responsible for applying the code correctly. The code is provided **
** AS IS without warranty and is in no way guaranteed to be error-free. **
** **
*****************************************************************************
*****************************************************************************
*/
/****************************************************************************
*****************************************************************************
**
** Change Log
** ----------
**
**
*****************************************************************************
*****************************************************************************
*/
#include "gs.h" /* general services component public interfaces */
#include "cb.h" /* communication buffers public interfaces */
/****************************************************************************
**
** Public Services
**
*****************************************************************************
*/
/*---------------------------------------------------------------------------
** CB_DeleteComBuf()
**---------------------------------------------------------------------------
*/
BOOL CB_DeleteComBuf( CB_ComBufType *pComBuf )
{
GS_UseCritical();
/*
** If debugging, then first make sure that the Use-Count is non-zero.
** Otherwise, somebody miscounted and there is a programming bug.
*/
GS_EnterCritical( );
/* Must check the use count within a critical region */
GS_LogEventIf((StatusType) pComBuf->bUseCount == 0, (UINT16) CB_DELETE_WITH_ZERO_USECOUNT,(void *) 0,(void *) pComBuf,(UINT8) FATAL );
/*
** Must do the decrement and test inside a critical section to insure
** the combuf doesn't get double-deallocated if multiple tasks
** are deleting at the same time in preemptive environments.
*/
{
if ( --( pComBuf->bUseCount ) == 0 )
{
GS_ExitCritical( );
GS_Free( pComBuf );
return( TRUE );
}
}
GS_ExitCritical( );
return( FALSE );
} /* end of CB_DeleteComBuf() */
/*---------------------------------------------------------------------------
** CB_NewComBuf()
**---------------------------------------------------------------------------
*/
CB_ComBufType *CB_NewComBuf( UINT32 iSize ) /* jjw 9/26/99 changed from UINT16 to UINT32 for msvc */
{
CB_ComBufType *pCb;
UINT16 iCbSize;
/*
** Compute total size of combuf (struct & buffer). Malloc memory for it.
** Bail if the allocation failed.
*/
pCb = (CB_ComBufType*)GS_Malloc( iSize + sizeof( CB_ComBufType ) );
if ( pCb == NULL )
{
return( NULL );
}
/*
** Grab the actual heap block size from the allocated block.
**
** Initialize the data structure portion of the combuf.
** Fill in requested and max buffer sizes (used by CB_TradeForBigger).
** Zero the current data size.
** Set the data pointer to 1 past the end of the buffer.
** Set the use count to unused.
*/
iCbSize = *( (UINT16*)( pCb ) );
pCb->iReqSize = (UINT16) iSize;
pCb->iBufSize = iCbSize - sizeof( CB_ComBufType );
pCb->iDataSize = 0;
pCb->pData = (UINT8*)( pCb ) + iCbSize;
pCb->bUseCount = 1;
/*
** Send back a pointer to the new ComBuf.
*/
return( pCb );
} /* end of CB_NewComBuf() */
/*---------------------------------------------------------------------------
** CB_TradeForBigger()
**---------------------------------------------------------------------------
*/
CB_ComBufType *CB_TradeForBigger( CB_ComBufType *pComBuf,
UINT32 iNewDataSize )
{
CB_ComBufType *pComBufNew;
UINT16 iSpaceNeeded;
/*
** Compute new buffer space requirement which is equal to the
** amount of header stuff stripped off since the combuf was allocated,
** plus the remaining data plus the new data size. (aka the original
** request size plus the new data size.)
*/
iSpaceNeeded = pComBuf->iReqSize + iNewDataSize;
/*
** Check if the current combuf is big enough to fit the new data.
*/
if ( iSpaceNeeded > ( pComBuf->iBufSize ) )
{
/*
** Combuf too small. Try to allocate a bigger one.
** Return NULL if the allocation failed, keeping the original intact.
*/
pComBufNew = CB_NewComBuf( iSpaceNeeded );
if ( pComBufNew == NULL )
{
return( NULL );
}
/*
** Copy remaining data over to the new combuf & toss the old one.
*/
CB_PrependComBuf( pComBufNew, pComBuf->pData, pComBuf->iDataSize );
CB_DeleteComBuf( pComBuf );
}
else
{
/*
** Original combuf big enough. Recycle it.
*/
pComBufNew = pComBuf;
}
return( pComBufNew );
} /* end of CB_TradeForBigger() */
/*---------------------------------------------------------------------------
** CB_TradeForBiggerClear()
**---------------------------------------------------------------------------
*/
CB_ComBufType *CB_TradeForBiggerClear( CB_ComBufType *pComBuf,
UINT32 iNewDataSize ) /* jjw 9/29/99 changed size */
{
CB_ComBufType *pComBufNew;
UINT16 iNewSize;
/*
** Compute new buffer space requirement which is equal to the
** amount of header stuff stripped off since the combuf was allocated,
** plus the new data size.
*/
iNewSize =(UINT16) (( pComBuf->iReqSize - pComBuf->iDataSize ) + iNewDataSize);
/*
** Allocate new or recycle as appropriate.
*/
if ( iNewSize > pComBuf->iBufSize )
{
/*
** Original is too small. Attempt to allocate a new, bigger combuf.
** Toss the original if successful. Clear the original if not.
*/
pComBufNew = CB_NewComBuf( iNewSize );
if ( pComBufNew != NULL )
{
CB_DeleteComBuf( pComBuf );
return( pComBufNew );
}
else
{
CB_ClearComBuf( pComBuf );
return( NULL );
}
}
/*
** Original plenty big enough.
** Clear the contents of the original and return it.
*/
CB_ClearComBuf( pComBuf );
return( pComBuf );
} /* end of CB_TradeForBiggerClear() */
/*---------------------------------------------------------------------------
** CB_WordAlignComBuf()
**---------------------------------------------------------------------------
*/
#ifdef WORD_ALIGN_WORD_DATA
void CB_WordAlignComBuf( CB_ComBufType *pComBuf, BOOL fMoveData )
{
/*
** Adjust only if data is not aligned to a word boundary.
*/
if ( (UINT32)(pComBuf->pData) & 1L )
{
/*
** Move the existing data in the combuf if requested,
** then adjust the size and data pointer.
*/
if ( fMoveData )
{
UC_CopyMem( pComBuf->pData - 1, pComBuf->pData, pComBuf->iDataSize );
}
CB_ExpandComBuf( pComBuf, 1 );
pComBuf->iDataSize--;
}
} /* end of CB_WordAlignComBuf() */
#endif
/****************************************************************************
**
** End of CB_UTIL.C
**
*****************************************************************************
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -