?? splist.c
字號(hào):
/*
* Copyright (c) 2006-2008
* Author: Weiming Zhou
*
* 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 "SpList.h"
/** 空間鏈表的創(chuàng)建函數(shù)
@param UINT uSpaceCount - 空間數(shù)量
@param UINT uDataSize -數(shù)據(jù)大小
@param UINT uPos -在管理數(shù)組中的位置
@return SPLIST * -成功返回空間鏈表指針,失敗返回NULL
*/
SPLIST *SpList_Create(UINT uSpaceCount, UINT uDataSize, UINT uPos)
{
SPLIST *pSpList;
SPNODE *pNode;
UINT i;
pSpList = (SPLIST *)malloc( sizeof(SPLIST)
+ uSpaceCount * (uDataSize + sizeof(SPNODE)) );
if ( pSpList != NULL )
{
pSpList->pBlock = (void *)((char *)pSpList +sizeof(SPLIST) );
/* 創(chuàng)建自由空間鏈表 */
pSpList->pHead = (SPNODE *)pSpList->pBlock;
pNode = pSpList->pHead;
for (i = 0; i < uSpaceCount - 1; i++) /* 將uSpaceCount改為uSpaceCount-1 */
{
pNode->uPos = uPos;
pNode->pNext = (SPNODE *)((char *)pNode + sizeof(SPNODE)
+ uDataSize);
pNode = pNode->pNext;
}
pNode->uPos = uPos; /* 新增加的一行代碼 */
pNode->pNext = NULL;
pSpList->uFreeCount = uSpaceCount;
}
return pSpList;
}
/** 空間鏈表的釋放函數(shù)
@param SPLIST *pSpList - 空間鏈表指針
@return void -無
*/
void SpList_Destroy(SPLIST *pSpList)
{
if ( pSpList != NULL )
{
free( pSpList );
}
}
/** 空間鏈表的分配內(nèi)存函數(shù)
@param SPLIST *pSpList - 空間鏈表指針
@return void * -成功返回分配到的內(nèi)存,如果沒有自由內(nèi)存進(jìn)行分配則返回NULL
*/
void *SpList_Alloc(SPLIST *pSpList)
{
SPNODE *pNode;
pNode = pSpList->pHead;
if ( pNode != NULL )
{
pSpList->pHead = pNode->pNext;
pSpList->uFreeCount--;
return (void *)((char *)pNode + sizeof(SPNODE));
}
return NULL;
}
/** 空間鏈表的釋放內(nèi)存函數(shù)
@param SPLIST *pSpList -空間鏈表指針
@param void *pData -要釋放的內(nèi)存數(shù)據(jù)指針
@return void -無
*/
void SpList_Free(SPLIST *pSpList, void *pData)
{
SPNODE *pNode;
pNode= (SPNODE *)((char *)pData - sizeof(SPNODE));
pNode->pNext = pSpList->pHead;
pSpList->pHead = pNode;
pSpList->uFreeCount++;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -