#pragma once
#include <string>
#include <iostream>
using namespace std;
class Custom
{
public:
Custom(int id);
~Custom();
int getid();
private:
int m_id{ 0 };
};
class Custom1
{
public:
//如果使用boost_object管理内存,受限于boost的实现这里的参数个数不要超过4个
Custom1(int id, string str, int id2)
: m_id(id)
{
};
~Custom1() {};
int getid();
private:
int m_id{ 0 };
};
#include "Custom.h"
Custom::Custom(int id)
{
m_id = id;
}
Custom::~Custom()
{
}
int Custom::getid()
{
return m_id;
}
int Custom1::getid()
{
return m_id;
}
#pragma once
#include "boost/pool/pool.hpp"
#include "boost/pool/object_pool.hpp"
#include "boost/pool/singleton_pool.hpp"
#include <memory>
namespace wy {
namespace custompool {
static boost::pool<> localPool(sizeof(char));
template<typename N, typename... Args>
static boost::object_pool<N> localObjPool;
//从全局单例内存池申请原始数据类型(线程安全)
struct MyPoolTag{};
template<typename T>
static std::shared_ptr<void> wy_new_pod()
{
try
{
return std::shared_ptr<void>(boost::singleton_pool<MyPoolTag, sizeof(T)>::malloc());
}
catch (const std::bad_alloc&)
{
return nullptr;
}
}
//从静态全局线程池申请原始数据类型对象(非线程安全)
static std::shared_ptr<void> wy_new_pod_fast()
{
return std::shared_ptr<void>(localPool.malloc());
}
//申请静态全局内存池申请类对象(非线程安全)
template<typename T, typename... Args>
static std::shared_ptr<T> wy_new(Args... args)
{
try
{
return std::shared_ptr<T>(localObjPool<T>.construct(std::forward<Args>(args)...));
}
catch (const std::bad_alloc&)
{
return nullptr;
}
}
}
}
// MemoryPool.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <vector>
#include "Custom.h"
#include "ClassPoolTemplate.h"
using namespace std;
const int g_dataSize = 10000;
template<typename T>
void func_pod_fast(vector<std::shared_ptr<T>>& v)
{
for (int i=0; i < g_dataSize; ++i)
{
std::shared_ptr<T> t = std::static_pointer_cast<T>(wy::custompool::wy_new_pod_fast());
*t = i;
v.emplace_back(t);
}
}
template<typename T>
void func_pod(vector<std::shared_ptr<T>>& v)
{
for (int i = 0; i < g_dataSize; ++i)
{
std::shared_ptr<T> t = std::static_pointer_cast<T>(wy::custompool::wy_new_pod<T>());
*t = i;
v.emplace_back(t);
}
}
//受限于boost的是现实,这里的可变参数个数不要超过四个
template<typename T, typename... Args>
void func(vector<std::shared_ptr<T>>& v, Args... args)
{
for (int i = 0; i < g_dataSize; ++i)
{
std::shared_ptr<T> pT= std::static_pointer_cast<T>(wy::custompool::wy_new<T>(i, std::forward<Args>(args)...));
v.emplace_back(pT);
}
}
int main()
{
vector<std::shared_ptr<int>> v;
vector<std::shared_ptr<double>> v1;
vector<std::shared_ptr<Custom>> v3;
vector<std::shared_ptr<Custom1>> v4;
while (true)
{
//singleton_pool
func_pod<int>(v);
for (auto item : v)
{
cout << *item << " ";
}
cout << "---------------------------------------------------------------\n ";
//pool
func_pod_fast(v1);
for (auto item : v1)
{
cout << *item << " ";
}
//object_pool
func<Custom>(v3);
for (auto item : v3)
{
cout << item->getid() << " ";
}
cout << "---------------------------------------------------------------\n ";
//object_pool
func<Custom1>(v4, "test", 0);
for (auto item : v4)
{
cout << item->getid() << " ";
}
cout << "---------------------------------------------------------------\n ";
}
std::cout << "\nHello World!\n";
}
|