IndexAlloc.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "stdafx.h"
  2. #include "IndexAlloc.h"
  3. #ifdef _DEBUG
  4. #define new DEBUG_NEW
  5. #undef THIS_FILE
  6. static char THIS_FILE[] = __FILE__;
  7. #endif
  8. CIndexAlloc::CIndexAlloc()
  9. {
  10. m_lpIndexs = NULL;
  11. m_nIndexNum = 0;
  12. }
  13. CIndexAlloc::~CIndexAlloc()
  14. {
  15. if(m_lpIndexs)
  16. delete [] m_lpIndexs;
  17. m_lpIndexs = NULL;
  18. }
  19. /************************************************************************/
  20. /*
  21. 函数:Initialize
  22. 描述:初始化
  23. 参数:
  24. IN CONST DWORD dwMaxNum 最大索引个数
  25. 返回:TRUE=成功,FALSE=失败
  26. */
  27. /************************************************************************/
  28. BOOL CIndexAlloc::Initialize(IN CONST int dwMaxNum)
  29. {
  30. if(dwMaxNum <= 0)
  31. return FALSE;
  32. m_nIndexNum = dwMaxNum;
  33. m_lpIndexs = new SINDEX[dwMaxNum];
  34. for(int i = 0; i<dwMaxNum; i++)
  35. m_lpIndexs[i].m_nIdx = i;
  36. return TRUE;
  37. }
  38. /************************************************************************/
  39. /*
  40. 函数:AllocIndex
  41. 描述:分配可用的索引
  42. 参数:
  43. 返回:索引值
  44. */
  45. /************************************************************************/
  46. int CIndexAlloc::AllocIndex()
  47. {
  48. CAutoLock autolock(&m_lock);
  49. int dwIdx = -1;
  50. for(int i=0; i<m_nIndexNum; i++)
  51. {
  52. if(m_lpIndexs[i].m_bUsed == FALSE)
  53. {
  54. dwIdx = m_lpIndexs[i].m_nIdx;
  55. m_lpIndexs[i].m_bUsed = TRUE;
  56. break;
  57. }
  58. }
  59. return dwIdx;
  60. }
  61. /************************************************************************/
  62. /*
  63. 函数:AllocIndex
  64. 描述:释放一个索引
  65. 参数:
  66. 返回:索引值
  67. */
  68. /************************************************************************/
  69. void CIndexAlloc::FreeIndex(IN CONST int dwIndex)
  70. {
  71. if(dwIndex < 0)
  72. return;
  73. CAutoLock autolock(&m_lock);
  74. m_lpIndexs[dwIndex].m_bUsed = FALSE;
  75. }