📄 net_buf.c
字号:
/*
*********************************************************************************************************
* uC/TCP-IP
* The Embedded TCP/IP Suite
*
* (c) Copyright 2003-2006; Micrium, Inc.; Weston, FL
*
* All rights reserved. Protected by international copyright laws.
*
* uC/TCP-IP is provided in source form for FREE evaluation, for educational
* use or peaceful research. If you plan on using uC/TCP-IP in a commercial
* product you need to contact Micrium to properly license its use in your
* product. We provide ALL the source code for your convenience and to help
* you experience uC/TCP-IP. The fact that the source code is provided does
* NOT mean that you can use it without paying a licensing fee.
*
* Knowledge of the source code may NOT be used to develop a similar product.
*
* Please help us continue to provide the Embedded community with the finest
* software available. Your honesty is greatly appreciated.
*********************************************************************************************************
*/
/*
*********************************************************************************************************
*
* NETWORK BUFFER MANAGEMENT
*
* Filename : net_buf.c
* Version : V1.87
* Programmer(s) : ITJ
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* INCLUDE FILES
*********************************************************************************************************
*/
#define NET_BUF_MODULE
#include <net.h>
/*$PAGE*/
/*
*********************************************************************************************************
* LOCAL DEFINES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL CONSTANTS
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL TABLES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* LOCAL GLOBAL VARIABLES
*********************************************************************************************************
*/
/*$PAGE*/
/*
*********************************************************************************************************
* LOCAL FUNCTION PROTOTYPES
*********************************************************************************************************
*/
#if (NET_BUF_CFG_NBR_SMALL > 0)
static void NetBuf_InitSmall(void);
#endif
#if (NET_BUF_CFG_NBR_LARGE > 0)
static void NetBuf_InitLarge(void);
#endif
static void NetBuf_ClrHdr (NET_BUF_HDR *pbuf_hdr);
static void NetBuf_Discard (NET_BUF *pbuf,
NET_TYPE type);
/*
*********************************************************************************************************
* LOCAL CONFIGURATION ERRORS
*********************************************************************************************************
*/
/*$PAGE*/
/*
*********************************************************************************************************
* NetBuf_Init()
*
* Description : (1) Initialize Network Buffer Management Module :
*
* (a) Initialize buffers
* (b) Initialize buffer error counters
*
*
* Argument(s) : none.
*
* Return(s) : none.
*
* Caller(s) : Net_Init().
*
* This function is an INTERNAL network protocol suite function & MUST NOT be called by
* application function(s).
*
* Note(s) : none.
*********************************************************************************************************
*/
void NetBuf_Init (void)
{
/* -------------------- INIT BUFs --------------------- */
#if (NET_BUF_CFG_NBR_SMALL > 0)
NetBuf_InitSmall(); /* Init SMALL bufs. */
#endif
#if (NET_BUF_CFG_NBR_LARGE > 0)
NetBuf_InitLarge(); /* Init LARGE bufs. */
#endif
/* ---------------- INIT BUF ERR CTRS ----------------- */
#if (NET_CTR_CFG_ERR_EN == DEF_ENABLED)
NetBuf_ErrNoneAvailCtr = 0;
NetBuf_ErrInvalidTypeCtr = 0;
NetBuf_ErrSizeCtr = 0;
NetBuf_ErrLenCtr = 0;
NetBuf_ErrLostCtr = 0;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
NetBuf_ErrNullPtrCtr = 0;
NetBuf_ErrNotUsedCtr = 0;
NetBuf_ErrIxCtr = 0;
#endif
#endif
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetBuf_GetMaxSize()
*
* Description : Get maximum possible buffer allocation size starting at a specific buffer index.
*
* Argument(s) : pbuf Pointer to a network buffer.
*
* ix_start Requested buffer index to store buffer data.
*
* Return(s) : none.
*
* Caller(s) : various.
*
* This function is an INTERNAL network protocol suite function & SHOULD NOT be called by
* application function(s).
*
* Note(s) : (1) The 'LARGE BUF MAX SIZE' pre-processor conditional does NOT need to verify SMALL
* buffer configuration since 'net_buf.h' ensures that NET_BUF_CFG_DATA_SIZE_LARGE
* MUST be configured with a value greater than NET_BUF_CFG_DATA_SIZE_SMALL (see
* 'net_buf.h CONFIGURATION ERRORS').
*
* (2) The 'NO BUFS' pre-processor 'else'-conditional code will never be compiled/linked
* since 'net_buf.h' ensures that at least one of the two configuration constants
* (NET_BUF_CFG_NBR_SMALL or NET_BUF_CFG_NBR_LARGE) will be configured with a value
* greater than zero (see 'net_buf.h CONFIGURATION ERRORS'). The 'else'-conditional
* code is included for completeness & as an extra precaution in case 'net_buf.h' is
* incorrectly modified.
*
* (3) Although network buffers' 'Data' buffer declared with an additional CPU word size
* (see 'net_buf.h NETWORK BUFFER DATA TYPE Note #4b'), this additional CPU word
* size does NOT increase the overall useable network buffer 'Data' size.
*********************************************************************************************************
*/
/*$PAGE*/
NET_BUF_SIZE NetBuf_GetMaxSize (NET_BUF *pbuf,
NET_BUF_SIZE ix_start)
{
#if ((NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED) && \
(NET_CTR_CFG_ERR_EN == DEF_ENABLED) && \
(CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL))
CPU_SR cpu_sr;
#endif
NET_BUF_HDR *pbuf_hdr;
NET_BUF_SIZE max_size;
if (pbuf != (NET_BUF *)0) { /* Chk pbuf's max size. */
pbuf_hdr = &pbuf->Hdr;
#if (NET_ERR_CFG_ARG_CHK_DBG_EN == DEF_ENABLED)
switch (pbuf_hdr->Type) {
#if (NET_BUF_CFG_NBR_SMALL > 0)
case NET_BUF_TYPE_SMALL:
#endif
#if (NET_BUF_CFG_NBR_LARGE > 0)
case NET_BUF_TYPE_LARGE:
#endif
break;
case NET_BUF_TYPE_NONE:
default:
NET_CTR_ERR_INC(NetBuf_ErrInvalidTypeCtr);
return (0); /* Prevent 'break NOT reachable' warning. */
}
#endif
if (ix_start < pbuf_hdr->Size) {
max_size = pbuf_hdr->Size - ix_start;
} else {
max_size = 0;
}
} else { /* Else chk buf cfg sizes. */
#if (NET_BUF_CFG_NBR_LARGE > 0) /* ------------ LARGE BUF MAX SIZE ------------ */
if (ix_start < NET_BUF_CFG_DATA_SIZE_LARGE) {
max_size = NET_BUF_CFG_DATA_SIZE_LARGE - ix_start;
} else {
max_size = 0;
}
#elif (NET_BUF_CFG_NBR_SMALL > 0) /* ------------ SMALL BUF MAX SIZE ------------ */
if (ix_start < NET_BUF_CFG_DATA_SIZE_SMALL) {
max_size = NET_BUF_CFG_DATA_SIZE_SMALL - ix_start;
} else {
max_size = 0;
}
#else /* ----------------- NO BUFS ------------------ */
/* See Note #2. */
NET_CTR_ERR_INC(NetBuf_ErrSizeCtr);
max_size = 0;
#endif
}
return (max_size);
}
/*$PAGE*/
/*
*********************************************************************************************************
* NetBuf_Get()
*
* Description : (1) Allocate & initialize a network buffer of sufficient size :
*
* (a) Get buffer of sufficient size
* (b) Validate buffer
* (c) Initialize buffer
* (d) Update buffer pool statistics
* (e) Return pointer to buffer
* OR
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -