c++11 boost - pool 库 - 内存池

2018-07-10 21:36:19

pool 为内存池类,分配失败返回空指针,不会抛出异常。它只能作为如 int,double等普通数据类型分配。

template <typename UserAllocator = default_user_allocator_new_delete>
class pool
{
  private:
    pool(const pool &);
    void operator=(const pool &);

  public:

    explicit pool(size_type requested_size);
    ~pool();

    bool release_memory();  //释放所有未分配的内存
    bool purge_memory();    //强制释放所有内存

    bool is_from(void * chunk) const;   //判断内存块是否从本对象出去
    size_type get_requested_size() const;  //分配内存块大小

    void * malloc();     //分配
    void * ordered_malloc();   //分配,同时合并空闲块链表
    void * ordered_malloc(size_type n);  //连续分配n块内存

    void free(void * chunk);   //归还内存
    void ordered_free(void * chunk);
    void free(void * chunks, size_type n);
    void ordered_free(void * chunks, size_type n);
};


object_pool 可以用来分配对象内存池。

template <typename ElementType, typename UserAllocator = default_user_allocator_new_delete>
class object_pool
{
  private:
    object_pool(const object_pool &);
    void operator=(const object_pool &);

  public:
    typedef ElementType element_type;
    typedef UserAllocator user_allocator;
    typedef typename pool<UserAllocator>::size_type size_type;
    typedef typename pool<UserAllocator>::difference_type difference_type;

    object_pool();
    ~object_pool();

    element_type * malloc();
    void free(element_type * p);
    bool is_from(element_type * p) const;

    element_type * construct();   //创建对象,目前只支持3个参数的构造
    // other construct() functions
    void destroy(element_type * p);  //销毁对象
};
// Copyright (c) 2015
// Author: Chrono Law
#include <iostream>
using namespace std;

#include <boost/core/ignore_unused.hpp>

//#define BOOST_SYSTEM_NO_DEPRECATED
//#define BOOST_POOL_NO_MT
#include <boost/pool/pool.hpp>
using namespace boost;

//////////////////////////////////////////

void case1()
{
    pool<> pl(sizeof(int));

    int *p = static_cast<int*>(pl.malloc());
    assert(pl.is_from(p));

    pl.free(p);
    for (int i = 0;i < 100; ++i)
    {   pl.ordered_malloc(10);  }
}

//////////////////////////////////////////

#include <boost/pool/object_pool.hpp>

struct demo_class
{
public:
    int a,b,c,d;
    demo_class(int x = 1, int y = 2, int z = 3):
        a(x),b(y),c(z){}
};

void case2()
{
    object_pool<demo_class> pl;

    auto p = pl.malloc();
    assert(pl.is_from(p));

    assert(p->a!=1 || p->b != 2 || p->c !=3);

    p = pl.construct(7, 8, 9);
    assert(p->a == 7);
    

    object_pool<string> pls;
    for (int i = 0; i < 10 ; ++i)
    {
        string *ps = pls.construct("hello object_pool");
        cout << *ps << endl;
    }
}

//辅助模板函数,支持任意数量构造函数
template<typename P, typename ... Args>
inline typename P::element_type*
construct(P& p, Args&& ... args)
{
    typename P::element_type* mem = p.malloc();
    assert(mem != 0);
    new (mem) typename P::element_type(
                std::forward<Args>(args)...);
    return mem;
}

struct demo_class2
{
    demo_class2(int, int, int, int)
    {   cout << "demo_class ctor" << endl;}
    ~demo_class2()
    {   cout << "demo_class dtor" << endl;}
};

void case3()
{
    object_pool<demo_class2> pl;
    auto d = construct(pl, 1,2,3,4);

    boost::ignore_unused(d);
}

//////////////////////////////////////////

#define BOOST_POOL_NO_MT
#include <boost/pool/singleton_pool.hpp>

struct pool_tag{};
typedef singleton_pool<pool_tag, sizeof(int)> spl;

void case4()
{
    int *p = (int *)spl::malloc();
    assert(spl::is_from(p));
    spl::release_memory();
}


#include <boost/pool/pool_alloc.hpp>

void case5()
{
    vector<int, pool_allocator<int> > v;

    v.push_back(10);
    cout << v.size();
}

int main()
{
    case1();
    case2();
    case3();
    case4();
    case5();
}
[root@192 c++]# g++ -std=c++11  main.cpp 
[root@192 c++]# ./a.out 
hello object_pool
hello object_pool
hello object_pool
hello object_pool
hello object_pool
hello object_pool
hello object_pool
hello object_pool
hello object_pool
hello object_pool
demo_class ctor
demo_class dtor


 备注

1.编译器版本gcc4.8.5,运行环境centos7 64位
2.本文只做简单记录用,详细用法请参考 Boost Library,或者是罗剑锋的 boost程序库完全开发指南 书本
3原文地址http://www.freecls.com/a/2712/9e


©著作权归作者所有
收藏
推荐阅读
简介
天降大任于斯人也,必先苦其心志。