123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- #include "stdafx.h"
- #include "IndexAlloc.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CIndexAlloc::CIndexAlloc()
- {
- m_lpIndexs = NULL;
- m_nIndexNum = 0;
- }
- CIndexAlloc::~CIndexAlloc()
- {
- if(m_lpIndexs)
- delete [] m_lpIndexs;
- m_lpIndexs = NULL;
- }
- /************************************************************************/
- /*
- 函数:Initialize
- 描述:初始化
- 参数:
- IN CONST DWORD dwMaxNum 最大索引个数
- 返回:TRUE=成功,FALSE=失败
- */
- /************************************************************************/
- BOOL CIndexAlloc::Initialize(IN CONST int dwMaxNum)
- {
- if(dwMaxNum <= 0)
- return FALSE;
- m_nIndexNum = dwMaxNum;
- m_lpIndexs = new SINDEX[dwMaxNum];
- for(int i = 0; i<dwMaxNum; i++)
- m_lpIndexs[i].m_nIdx = i;
- return TRUE;
- }
- /************************************************************************/
- /*
- 函数:AllocIndex
- 描述:分配可用的索引
- 参数:
- 返回:索引值
- */
- /************************************************************************/
- int CIndexAlloc::AllocIndex()
- {
- CAutoLock autolock(&m_lock);
- int dwIdx = -1;
- for(int i=0; i<m_nIndexNum; i++)
- {
- if(m_lpIndexs[i].m_bUsed == FALSE)
- {
- dwIdx = m_lpIndexs[i].m_nIdx;
- m_lpIndexs[i].m_bUsed = TRUE;
- break;
- }
- }
- return dwIdx;
- }
- /************************************************************************/
- /*
- 函数:AllocIndex
- 描述:释放一个索引
- 参数:
- 返回:索引值
- */
- /************************************************************************/
- void CIndexAlloc::FreeIndex(IN CONST int dwIndex)
- {
- if(dwIndex < 0)
- return;
- CAutoLock autolock(&m_lock);
- m_lpIndexs[dwIndex].m_bUsed = FALSE;
- }
|