?? blocklist.c
字號(hào):
/*
* Copyright (c) 2000-2008
* Author: Weiming Zhou
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
*/
#include <stdlib.h>
#include "CapiGlobal.h"
#include "BlockList.h"
/** 整塊內(nèi)存鏈表的創(chuàng)建函數(shù)
@param UINT uDataSize - 數(shù)據(jù)大小
@param UINT uMaxDataCount - 鏈表中最大可存放數(shù)據(jù)的個(gè)數(shù)
@return BLOCKLIST * -整塊內(nèi)存鏈表指針
*/
BLOCKLIST * BlockList_Create(UINT uDataSize, UINT uMaxDataCount)
{
BLOCKLIST *pList;
SINGLENODE *pNode;
UINT i;
pList = (BLOCKLIST *)malloc( sizeof(BLOCKLIST)
+ uMaxDataCount * (uDataSize + sizeof(SINGLENODE)) );
if ( pList != NULL )
{
pList->pBlock = (void *)((char *)pList +sizeof(BLOCKLIST) );
/* 建立空鏈表 */
pList->pEmpty = (SINGLENODE *)pList->pBlock;
pNode = pList->pEmpty;
for (i = 0; i < uMaxDataCount-1; i++)
{
pNode->pNext = (SINGLENODE *)((char *)pNode + sizeof(SINGLENODE)
+ uDataSize);
pNode = pNode->pNext;
}
pNode->pNext = NULL;
pList->uFreeCount = uMaxDataCount;
pList->uDataSize = uDataSize;
pList->uMaxDataCount = uMaxDataCount;
pList->pHead = NULL;
}
return pList;
}
/** 整塊內(nèi)存鏈表的釋放函數(shù)
@param BLOCKLIST *pList - 整塊內(nèi)存鏈表指針
@return void - 無
*/
void BlockList_Destroy(BLOCKLIST *pList)
{
if ( pList != NULL )
{
free( pList );
}
}
/** 整塊內(nèi)存鏈表的插入數(shù)據(jù)函數(shù)
@param BLOCKLIST *pList - 整塊內(nèi)存鏈表指針
@param void *pData - 要插入的節(jié)點(diǎn)數(shù)據(jù)指針
@param UINT uDataLen - 要插入的數(shù)據(jù)的長度
@return INT - 失敗返回-1,成功返回0。
*/
INT BlockList_InsertHead(BLOCKLIST *pList, void *pData, UINT uDataLen)
{
SINGLENODE *pNode;
if ( uDataLen > pList->uDataSize )
{
return -1;
}
pNode = pList->pEmpty;
if ( pNode != NULL )
{
pList->pEmpty = pNode->pNext;
}
else
{
return -1;
}
pNode->pNext = pList->pHead;
memcpy(pNode->pData, pData, uDataLen);
pList->pHead = pNode;
pList->uFreeCount--;
return 0;
}
/** 整塊內(nèi)存鏈表的刪除頭部節(jié)點(diǎn)函數(shù)
@param BLOCKLIST *pList - 整塊內(nèi)存鏈表指針
@return void - 無
*/
void BlockList_DeleteHead(BLOCKLIST *pList)
{
SINGLENODE *pPopNode; /* 用來指向要彈出數(shù)據(jù)的節(jié)點(diǎn)的指針 */
/* 參數(shù)校驗(yàn) */
if ( pList == NULL || pList->pHead == NULL )
{
return;
}
/* 彈出鏈表頭節(jié)點(diǎn) */
pPopNode = pList->pHead;
pList->pHead = pList->pHead->pNext;
/* 將彈出的節(jié)點(diǎn)加入到空鏈表的頭部 */
pPopNode->pNext = pList->pEmpty;
pList->pEmpty = pPopNode;
/* 將鏈表節(jié)點(diǎn)數(shù)量加1 */
pList->uFreeCount++;
return;
}
/** 整塊內(nèi)存鏈表的內(nèi)存分配函數(shù)
從空鏈中彈出一個(gè)節(jié)點(diǎn),將其作為分配的內(nèi)存
@param BLOCKLIST *pList - 整塊內(nèi)存鏈表指針
@return void * - 成功返回分配到的內(nèi)存指針,失敗返回NULL
*/
void * BlockList_Alloc(BLOCKLIST *pList)
{
SINGLENODE *pNode;
pNode = pList->pEmpty;
if ( pNode != NULL )
{
pList->pEmpty = pNode->pNext;
pList->uFreeCount--;
return (void *)pNode;
}
return NULL;
}
/** 整塊內(nèi)存鏈表的內(nèi)存釋放函數(shù)
將內(nèi)存釋放掉加入到空鏈中
@param BLOCKLIST *pList - 整塊內(nèi)存鏈表指針
@param void *pData - 要釋放的數(shù)據(jù)指針
@return void - 無
*/
void BlockList_Free(BLOCKLIST *pList, void *pData)
{
SINGLENODE *pNode;
pNode= (SINGLENODE *)pData;
pNode->pNext = pList->pEmpty;
pList->pEmpty = pNode;
pList->uFreeCount++;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -