一个简单内存池实现
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;
}

1028

被折叠的 条评论
为什么被折叠?



