您当前的位置: 首页 > 

phymat.nico

暂无认证

  • 1浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

boost之内存池使用实例

phymat.nico 发布时间:2017-10-19 16:02:48 ,浏览量:1

需要说明的就是初始化的时候指定的是内存池分配内存块的大小,而且内存池是非线程安全的,多线程要加锁

#ifndef BUFFER_POOL_H
#define BUFFER_POOL_H

#include "boost_comm.h"
using namespace boost;

namespace seemmo
{
	class buffer_pool
	{
	public:
		buffer_pool(int chunk_size);
		~buffer_pool();
	protected:
		static boost::mutex mtx_;
		static buffer_pool *instance()
		{
			boost::unique_lock wlock(mtx_);
			static buffer_pool inst(1024);
			return &inst;
		}
	public:
		static char *comm_sz1024_buffer()
		{
			boost::unique_lock wlock(instance()->mtx_1_);
			return (char *)instance()->m_buffer_bp.malloc();
		}
		static void comm_sz1024_free(void *p)
		{
			boost::unique_lock wlock(instance()->mtx_1_);
			instance()->m_buffer_bp.free(p);
		}

		static unsigned char *bin_value_alloc()
		{
			boost::unique_lock wlock(instance()->mtx_2_);
			return (unsigned char *)instance()->m_bin_value_.malloc();
		}

		static void bin_value_free(void *p)
		{
			boost::unique_lock wlock(instance()->mtx_2_);
			instance()->m_bin_value_.free(p);
		}

	protected:
		boost::mutex mtx_1_;
		boost::pool m_buffer_bp;

		boost::mutex mtx_2_;
		boost::pool m_bin_value_;
	private:
	};
}

#endif // BUFFER_POOL_H
#include "buffer_pool.h"

#include "config.h"

namespace seemmo
{
	boost::mutex buffer_pool::mtx_;

	buffer_pool::buffer_pool(int chunk_size)
		:m_buffer_bp(BUFFER_POOL_COMM_CHUNK_SIZE),//1k
		m_bin_value_(BUFFER_POOL_BIN_CHUNK_SIZE)//256k
	{
	}

	buffer_pool::~buffer_pool()
	{
	}
}

关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

0.0413s