对象池简单实现

一个简单内存池实现

Lock.hpp

#pragma once
#include <mutex>
#include <condition_variable>

typedef std::mutex Mutex;
typedef std::recursive_mutex RecursiveMutex;
typedef std::lock_guard<RecursiveMutex> RecursiveLock;
typedef std::lock_guard<Mutex> GuardLock;

typedef std::unique_lock<Mutex> UniqueLock;
typedef std::condition_variable Condition;

MemPool.hpp

#pragma once

#include <list>
#include <map>
#include "Lock.hpp"

class MemElem
{
public:
	MemElem() {}
	virtual ~MemElem(){}
	void SetMemId(int memid)
	{
		m_memid = memid;
	}
	int GetMemId()
	{
		return m_memid;
	}
	virtual void Init() = 0;
	virtual void Clear() = 0;
protected:
	int m_memid{ 0 };
};

template <typename T>
class MemPool
{
public:
	MemPool(int initCount, int expand)
	{
		m_list.clear();
		m_use.clear();
		m_expand = expand;
		m_index = 0;
		m_count = 0;

		Expand(initCount);
	}

	~MemPool()
	{
		for (auto& it : m_list)
		{
			delete it;
		}
		for (auto& it : m_use)
		{
			delete it.second;
		}
		m_list.clear();
		m_use.clear();
	}

	T* NewElem()
	{
		GuardLock lock(m_mutex);
		if (m_list.size() <= 0)
		{
			Expand(m_expand);
		}
		if (m_list.size() > 0)
		{
			T* t = m_list.front();
			m_use.emplace(t->GetMemId(), t);
			m_list.pop_front();
			t->Init();
			return t;
		}
		return nullptr;
	}

	bool DelElem(int id)
	{
		GuardLock lock(m_mutex);
		auto it = m_use.find(id);
		if (it == m_use.end())
		{
			return false;
		}
		T* t = it->second;
		t->Clear();
		m_list.push_back(t);
		m_use.erase(it);
		return true;
	}

	int UseSize()
	{
		return m_use.size();
	}

private:
	bool Expand(int expand)
	{
		for (int i = 0; i < expand; ++i)
		{
			T* t = new T;
			if (t == nullptr)
			{
				return false;
			}
			m_list.push_back(t);
			t->SetMemId(m_index++);
		}
		m_count += expand;
		return true;
	}

private:
	std::list<T*> m_list;
	std::map<int, T*> m_use;
	int m_expand;
	int m_index;
	Mutex m_mutex;
	int m_count;
};

Player.h

#pragma once

#include "MemPool.hpp"

class Player : public MemElem
{
public:
	void Init();
	void Clear();
private:

};

class PlayerPool
{
public:
	PlayerPool();
	~PlayerPool();
	void Init(int initCount = 100, int expand = 50);
	Player* NewPlayer();
	bool DelPlayer(Player* player);
	int PlayerNum();
private:
	MemPool<Player>* m_pPlayers;
};

extern std::unique_ptr<PlayerPool> g_pPlayerPool;

Player.cpp

#include "Player.h"

#include "iostream"
using namespace std;

void Player::Init()
{
	cout << "Player(" << GetMemId() << ") Init" << endl;
}

void Player::Clear()
{
	cout << "Player(" << GetMemId() << ") Clear" << endl;
}

std::unique_ptr<PlayerPool> g_pPlayerPool = nullptr;
PlayerPool::PlayerPool(){}
PlayerPool::~PlayerPool()
{
	delete m_pPlayers;
	m_pPlayers = nullptr;
}
void PlayerPool::Init(int initCount, int expand)
{
	m_pPlayers = new MemPool<Player>(initCount, expand);
}
Player* PlayerPool::NewPlayer()
{
	return m_pPlayers->NewElem();
}

bool PlayerPool::DelPlayer(Player* player)
{
	return m_pPlayers->DelElem(player->GetMemId());
}

int PlayerPool::PlayerNum()
{
	return m_pPlayers->UseSize();
}

Test.cpp

#include "Player.h"
#include <iostream>
using namespace std;

int main()
{
	g_pPlayerPool = std::make_unique<PlayerPool>();
	g_pPlayerPool->Init(100, 50);
	Player* p1 = g_pPlayerPool->NewPlayer();
	Player* p2 = g_pPlayerPool->NewPlayer();
	Player* p3 = g_pPlayerPool->NewPlayer();
	cout << g_pPlayerPool->PlayerNum() << endl;
	g_pPlayerPool->DelPlayer(p2);
	cout << g_pPlayerPool->PlayerNum() << endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值