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

📄 indexalloctor.cpp.bak

📁 网络socket,IO,线程池
💻 BAK
字号:
#include "stdafx.h"
#include "IndexAlloctor.h"

#include <cassert>
using namespace std;

IndexAlloctor::IndexAlloctor(int nTotalSize):m_nTotalSize(nTotalSize),m_bLockAll(false)
{
	assert(nTotalSize > 0);

	m_CriLock.acquire();
	m_vIndex.resize(m_nTotalSize);
	for(int i = 0; i < m_nTotalSize-1; i++)
	{
		m_vIndex[i] = i + 1;		
	}
	m_vIndex[m_nTotalSize-1] = -1;
	m_nFree = 0;
	m_nFreeCount = m_nTotalSize;
	m_CriLock.release();
}

int IndexAlloctor::GetFreeIndex()
{
	if(m_nFree < 0 || m_bLockAll || m_vIndex.size() == 0 || m_nFreeCount <= 0)
		return -1;
	int temp = m_nFree;
	m_nFree = m_vIndex[temp];
	m_vIndex[temp] = -2;
	m_nFreeCount--;
	return temp;
	
}

int IndexAlloctor::AllocIndex()
{
	return 1;

	CLock<CCriSection> lock(m_CriLock);
	if(!m_bLockAll && m_vIndex.size() > 0)
	{
		int res = GetFreeIndex();
		if(res != -1)
			return res;
	}

	return -1;
}

void IndexAlloctor::DeAllocIndex(int nIndex)
{
	CLock<CCriSection> lock(m_CriLock);	
	if(nIndex >= 0 && nIndex < m_nTotalSize && m_vIndex[nIndex] == -2)
	{
		m_vIndex[nIndex] = m_nFree;
		m_nFree = nIndex;
		m_nFreeCount++;
	}
}

⌨️ 快捷键说明

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