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

📄 ixosalbuffermgt.c

📁 IXP425 下NPE驱动的OSAL部分源码。
💻 C
📖 第 1 页 / 共 2 页
字号:
    poolPtr->mbufMemPtr = poolBufPtr;
    poolPtr->bufDataSize = dataSize;
    poolPtr->totalBufsInPool = count;
    poolPtr->mbufMemSize = mbufSize * count;
    poolPtr->dataMemSize = dataSize * count;

    currentMbufPtr = (IX_OSAL_MBUF *) poolBufPtr;

    poolPtr->nextFreeBuf = currentMbufPtr;

    for (i = 0; i < count; i++)
    {
        if (i < (count - 1))
        {
            nextMbufPtr =
                (IX_OSAL_MBUF *) ((UINT32) currentMbufPtr + mbufSize);
        }
        else
        {                      
						/* last mbuf in chain */
            nextMbufPtr = NULL;
        }
        IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (currentMbufPtr) = nextMbufPtr;
        IX_OSAL_MBUF_NET_POOL (currentMbufPtr) = poolPtr;

        IX_OSAL_MBUF_SYS_SIGNATURE_INIT(currentMbufPtr);

        if (poolDataPtr != NULL)
        {
            IX_OSAL_MBUF_MDATA (currentMbufPtr) = poolDataPtr;
            IX_OSAL_MBUF_ALLOCATED_BUFF_DATA(currentMbufPtr) = (UINT32) poolDataPtr;

            IX_OSAL_MBUF_MLEN (currentMbufPtr) = dataSize;
            IX_OSAL_MBUF_ALLOCATED_BUFF_LEN(currentMbufPtr) = dataSize;

            poolDataPtr = (VOID *) ((UINT32) poolDataPtr + dataSize);
        }

        currentMbufPtr = nextMbufPtr;
    }

    /*
     * update the number of free buffers in the pool 
     */
    poolPtr->freeBufsInPool = count;

    poolPtr->poolAllocType = IX_OSAL_MBUF_POOL_TYPE_USER_ALLOC;

    return poolPtr;

}

/* 
 * Get a mbuf ptr from the pool
 */
PUBLIC IX_OSAL_MBUF *
ixOsalMbufAlloc (IX_OSAL_MBUF_POOL * poolPtr)
{

#ifndef __linux_user
    INT32 lock;
#endif
		
    IX_OSAL_MBUF *newBufPtr = NULL;

    /*
     * check parameters 
     */
    if (poolPtr == NULL)
    {
        ixOsalLog (IX_OSAL_LOG_LVL_ERROR,
            IX_OSAL_LOG_DEV_STDOUT,
            "ixOsalMbufAlloc(): "
            "ERROR - Invalid Parameter\n", 0, 0, 0, 0, 0, 0);
        return NULL;
    }

#ifndef __linux_user
    lock = ixOsalIrqLock ();
#else
		ixOsalMutexLock(&ixOsalBufferPoolAccess[poolPtr->poolIdx],IX_OSAL_WAIT_FOREVER);
#endif
		
    newBufPtr = poolPtr->nextFreeBuf;
		
    if (newBufPtr)
    {
        poolPtr->nextFreeBuf = IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (newBufPtr);
        IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (newBufPtr) = NULL;

        /*
         * update the number of free buffers in the pool 
         */
        poolPtr->freeBufsInPool--;
    }
    else
    {
        /* Return NULL to indicate to caller that request is denied. */
			#ifndef __linux_user
    		ixOsalIrqUnlock (lock);
			#else
				ixOsalMutexUnlock(&ixOsalBufferPoolAccess[poolPtr->poolIdx]);
			#endif      

        return NULL;
    }

#ifdef IX_OSAL_BUFFER_FREE_PROTECTION
	/* Set Buffer Used Flag to indicate state.*/
    IX_OSAL_MBUF_SET_USED_FLAG(newBufPtr);
#endif

#ifndef __linux_user
    ixOsalIrqUnlock (lock);
#else
		ixOsalMutexUnlock(&ixOsalBufferPoolAccess[poolPtr->poolIdx]);
#endif
		
    return newBufPtr;
}

PUBLIC IX_OSAL_MBUF *
ixOsalMbufFree (IX_OSAL_MBUF * bufPtr)
{

#ifndef __linux_user
    INT32 lock;
#else
    INT32 index;
#endif
		
    IX_OSAL_MBUF_POOL *poolPtr;

    IX_OSAL_MBUF *nextBufPtr = NULL;

    /*
     * check parameters 
     */
    if (bufPtr == NULL)
    {
        ixOsalLog (IX_OSAL_LOG_LVL_ERROR,
            			 IX_OSAL_LOG_DEV_STDOUT,
            			 "ixOsalMbufFree(): "
            			 "ERROR - Invalid Parameter\n", 0, 0, 0, 0, 0, 0);

        return NULL;
    }

#ifndef __linux_user
    lock = ixOsalIrqLock ();
#else
    index = (((IX_OSAL_MBUF_POOL *)(bufPtr->ix_ctrl.ix_pool))->poolIdx);
		ixOsalMutexLock(&ixOsalBufferPoolAccess[index],IX_OSAL_WAIT_FOREVER);
#endif

#ifdef IX_OSAL_BUFFER_FREE_PROTECTION
	
	/* Prevention for Buffer freed more than once*/
    if(!IX_OSAL_MBUF_ISSET_USED_FLAG(bufPtr))
    {
	   	return NULL;
    }
    IX_OSAL_MBUF_CLEAR_USED_FLAG(bufPtr);
#endif
	
    poolPtr = IX_OSAL_MBUF_NET_POOL (bufPtr);

    /*
     * check the mbuf wrapper signature (if mbuf wrapper was used) 
     */
    if (poolPtr->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC)
    {
        IX_OSAL_ENSURE ( (IX_OSAL_MBUF_GET_SYS_SIGNATURE(bufPtr) == IX_OSAL_MBUF_SYS_SIGNATURE),
            "ixOsalBuffPoolBufFree: ERROR - Invalid mbuf signature.");
    }

    nextBufPtr = IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (bufPtr);

    IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR (bufPtr) = poolPtr->nextFreeBuf;
		
    poolPtr->nextFreeBuf = bufPtr;

    /*
     * update the number of free buffers in the pool 
     */
    poolPtr->freeBufsInPool++;

#ifndef __linux_user
    ixOsalIrqUnlock (lock);
#else
		ixOsalMutexUnlock(&ixOsalBufferPoolAccess[index]);
#endif

    return nextBufPtr;
}

PUBLIC VOID
ixOsalMbufChainFree (IX_OSAL_MBUF * bufPtr)
{
    while ((bufPtr = ixOsalMbufFree (bufPtr)));
}

/*
 * Function definition: ixOsalBuffPoolShow
 */
PUBLIC VOID
ixOsalMbufPoolShow (IX_OSAL_MBUF_POOL * poolPtr)
{
    IX_OSAL_MBUF *nextBufPtr;
    INT32 count = 0;

#ifndef __linux_user
    INT32 lock;
#endif
		
    /*
     * check parameters 
     */
    if (poolPtr == NULL)
    {
        ixOsalLog (IX_OSAL_LOG_LVL_ERROR,
            IX_OSAL_LOG_DEV_STDOUT,
            "ixOsalBuffPoolShow(): "
            "ERROR - Invalid Parameter", 0, 0, 0, 0, 0, 0);
        /*
         * return IX_FAIL; 
         */
        return;
    }

#ifndef __linux_user
    lock = ixOsalIrqLock ();
#else
		ixOsalMutexLock(&ixOsalBufferPoolAccess[poolPtr->poolIdx],IX_OSAL_WAIT_FOREVER);
#endif

		count = poolPtr->freeBufsInPool;
    nextBufPtr = poolPtr->nextFreeBuf;
    
#ifndef __linux_user
    ixOsalIrqUnlock (lock);
#else
		ixOsalMutexUnlock(&ixOsalBufferPoolAccess[poolPtr->poolIdx]);
#endif
		
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE,
        IX_OSAL_LOG_DEV_STDOUT, "=== POOL INFORMATION ===\n", 0, 0, 0,
        0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Pool Name:                   %s\n",
        (UINT32) poolPtr->name, 0, 0, 0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Pool Allocation Type:        %d\n",
        (UINT32) poolPtr->poolAllocType, 0, 0, 0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Pool Mbuf Mem Usage (bytes): %d\n",
        (UINT32) poolPtr->mbufMemSize, 0, 0, 0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Pool Data Mem Usage (bytes): %d\n",
        (UINT32) poolPtr->dataMemSize, 0, 0, 0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Mbuf Data Capacity  (bytes): %d\n",
        (UINT32) poolPtr->bufDataSize, 0, 0, 0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Total Mbufs in Pool:         %d\n",
        (UINT32) poolPtr->totalBufsInPool, 0, 0, 0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Available Mbufs:             %d\n", (UINT32) count, 0,
        0, 0, 0, 0);
    ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
        "Next Available Mbuf:         %p\n", (UINT32) nextBufPtr,
        0, 0, 0, 0, 0);

    if (poolPtr->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_USER_ALLOC)
    {
        ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE,
            IX_OSAL_LOG_DEV_STDOUT,
            "Mbuf Mem Area Start address: %p\n",
            (UINT32) poolPtr->mbufMemPtr, 0, 0, 0, 0, 0);
        ixOsalLog (IX_OSAL_LOG_LVL_MESSAGE, IX_OSAL_LOG_DEV_STDOUT,
            "Data Mem Area Start address: %p\n",
            (UINT32) poolPtr->dataMemPtr, 0, 0, 0, 0, 0);
    }
}

PUBLIC VOID
ixOsalMbufDataPtrReset (IX_OSAL_MBUF * bufPtr)
{
    IX_OSAL_MBUF_POOL *poolPtr;
    UINT8 *poolDataPtr;

    if (bufPtr == NULL)
    {
        ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT,
            "ixOsalBuffPoolBufDataPtrReset"
            ": ERROR - Invalid Parameter\n", 0, 0, 0, 0, 0, 0);
        return;
    }

    poolPtr = (IX_OSAL_MBUF_POOL *) IX_OSAL_MBUF_NET_POOL (bufPtr);

		poolDataPtr = poolPtr->dataMemPtr;

    if (poolPtr->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC)
    {
        if (IX_OSAL_MBUF_GET_SYS_SIGNATURE(bufPtr) != IX_OSAL_MBUF_SYS_SIGNATURE)
        {
            ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT,
                "ixOsalBuffPoolBufDataPtrReset"
                ": invalid mbuf, cannot reset mData pointer\n", 0, 0,
                0, 0, 0, 0);
            return;
        }
        IX_OSAL_MBUF_MDATA (bufPtr) = (UINT8*)IX_OSAL_MBUF_ALLOCATED_BUFF_DATA (bufPtr);
    }
    else
    {
        if (poolDataPtr)
        {
            UINT32 bufSize = poolPtr->bufDataSize;
            UINT32 bufDataAddr =
                (UINT32) IX_OSAL_MBUF_MDATA (bufPtr);
            UINT32 poolDataAddr = (UINT32) poolDataPtr;

            /*
             * the pointer is still pointing somewhere in the mbuf payload.
             * This operation moves the pointer to the beginning of the 
             * mbuf payload
             */
            bufDataAddr = ((bufDataAddr - poolDataAddr) / bufSize) * bufSize;
            IX_OSAL_MBUF_MDATA (bufPtr) = &poolDataPtr[bufDataAddr];
        }
        else
        {
            ixOsalLog (IX_OSAL_LOG_LVL_WARNING, IX_OSAL_LOG_DEV_STDOUT,
                "ixOsalBuffPoolBufDataPtrReset"
                ": cannot be used if user supplied NULL pointer for pool data area "
                "when pool was created\n", 0, 0, 0, 0, 0, 0);
            return;
        }
    }

}

/*
 * Function definition: ixOsalBuffPoolUninit
 */
PUBLIC IX_STATUS
ixOsalBuffPoolUninit (IX_OSAL_MBUF_POOL * pool)
{
    if (!pool)
    {
        ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT,
            "ixOsalBuffPoolUninit: NULL ptr \n", 0, 0, 0, 0, 0, 0);
        return IX_FAIL;
    }

    if (pool->freeBufsInPool != pool->totalBufsInPool)
    {
        ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT,
            "ixOsalBuffPoolUninit: need to return all ptrs to the pool first! \n",
            0, 0, 0, 0, 0, 0);
        return IX_FAIL;
    }

#ifdef __linux_user

		ixOsalMutexDestroy(&ixOsalBufferPoolAccess[pool->poolIdx]);		

#endif
		
    if (pool->poolAllocType == IX_OSAL_MBUF_POOL_TYPE_SYS_ALLOC)
    {
#ifdef IX_OSAL_BUFFER_ALLOC_SEPARATELY
				UINT32 i;
				IX_OSAL_MBUF* pBuf;
				
				pBuf = pool->nextFreeBuf;
				/* Freed the Buffer one by one till all the Memory is freed*/
				for (i= pool->freeBufsInPool; i >0 && pBuf!=NULL ;i--){
						IX_OSAL_MBUF* pBufTemp;
						pBufTemp = IX_OSAL_MBUF_NEXT_BUFFER_IN_PKT_PTR(pBuf);
					
						/* Freed MBUF Data Memory area*/

						IX_OSAL_BUFF_MEM_FREE((VOID *) (IX_OSAL_MBUF_ALLOCATED_BUFF_DATA(pBuf)) );

						/* Freed MBUF Struct Memory area*/

						IX_OSAL_BUFF_MEM_FREE(pBuf);
						pBuf = pBufTemp;
				}
				
#else    	

				IX_OSAL_BUFF_MEM_FREE (pool->mbufMemPtr);
        IX_OSAL_BUFF_MEM_FREE(pool->dataMemPtr);
#endif        
    }

    ixOsalBuffFreePools[pool->poolIdx / IX_OSAL_BUFF_FREE_BITS] &=
        ~(1 << (pool->poolIdx % IX_OSAL_BUFF_FREE_BITS));
    ixOsalBuffPoolsInUse--;
    return IX_SUCCESS;
}

/*
 * Function definition: ixOsalBuffPoolDataAreaSizeGet
 */
PUBLIC UINT32
ixOsalBuffPoolDataAreaSizeGet (UINT32 count, UINT32 size)
{
    UINT32 memorySize;
    memorySize = count * IX_OSAL_MBUF_POOL_SIZE_ALIGN (size);
    return memorySize;
}

/*
 * Function definition: ixOsalBuffPoolMbufAreaSizeGet
 */
PUBLIC UINT32
ixOsalBuffPoolMbufAreaSizeGet (UINT32 count)
{
    UINT32 memorySize;
    memorySize =
        count * IX_OSAL_MBUF_POOL_SIZE_ALIGN (sizeof (IX_OSAL_MBUF));
    return memorySize;
}

/*
 * Function definition: ixOsalBuffPoolFreeCountGet
 */
PUBLIC UINT32 ixOsalBuffPoolFreeCountGet(IX_OSAL_MBUF_POOL * poolPtr)
{

   return poolPtr->freeBufsInPool;

}

#endif /* IX_OSAL_USE_DEFAULT_BUFFER_MGT */

⌨️ 快捷键说明

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