boost pool內存池庫使用簡要介紹

      我廠內存次神馬的通常都本身實現。我曾經也本身寫過一個demo(mempool)。後來發現boost庫裏面有一個內存池庫boost pool,貌似很好用,使用挺好,例子能夠貼出來。 ios

      boost一共有4種內存池,爲pool, object_pool, singleton_pool, pool_alloc。其中前三種應該應用都不少,我這裏僅僅只有前2個demo. git

1 pool

      pool是最簡單的內存池類,能夠返回一個無符號的內存指針。由於我平時C用的遠比C++多,因此這個類其實更適合我使用。 github

      它的原理我沒有細看。理論上也是每次分配一大塊內存,這個內存卡會被分紅小塊,經過鏈表進行相連。鏈表至少記錄當前能夠分配的內存以及已經分配過正在使用的內存。每次分配都從能夠分配鏈表分配便可。 shell

      他只要有2個函數能夠供你們使用:malloc和free。聽說free不用程序猿本身搞,pool類能夠自行回收。anyway,本身顯示調用free確定沒問題。 vim

      下面是調用例子 數組

typedef struct _my_pool_test_t
{
    int a;
    int b[];
} my_pool_test_t;

void test_pool()
{
    pool<> pl( sizeof(my_pool_test_t)  + 5* sizeof(int) );
    my_pool_test_t* test = (my_pool_test_t*)pl.malloc();
    test->a = 100;
    for(int i=0; i<5; i++)
    {
        test->b[i] = i+2;
    }

    cout << "pool a:\t" << test->a << endl;
    cout << "pool b2:\t" << test->b[2] << endl;
    pl.free(test);
}
      其中my_pool_test_t內部使用了 柔性數組。就是申請一個 my_pool_test_t,賦值並使用。

2 object_pool

      從名字也能夠看出來,這個內存池是對象內存次,是分配object的。 函數

      他和pool最大的區別也在此。 pool每次須要指定分配大小,它不須要,可是他須要指定分配的類型(其實經過類型能算出大小)。 spa

      示例代碼以下: .net

class Class_Demo
{
    public:
        int a;
        int b;
        Class_Demo(int ia=1, int ib =2 ) : a(ia), b(ib) {}
};

void test_object_pool()
{
    object_pool <Class_Demo> pclass;
    Class_Demo *cl = pclass.malloc();
    cl = pclass.construct(2,3);
    cout << "object pool a:\t" << cl -> a << endl;
    cout << "object pool b:\t" << cl -> b << endl;
}
      這個就是直接分配一個類Class_Demo的內存池,賦值打印完事。

3 所有代碼

test_mem.cpp 指針

/***************************************************************************
 * 
 * Copyright (c) 2014 Baidu.com, Inc. All Rights Reserved
 * 
 **************************************************************************/
 
 
 
/**
 * @file test_mem.cpp
 * @author liujun05(com@baidu.com)
 * @date 2014/02/26 14:04:16
 * @brief 
 *  
 **/

#include<boost/pool/object_pool.hpp>
#include<boost/pool/pool.hpp>
#include<iostream>
using namespace std;
using namespace boost;

typedef struct _my_pool_test_t
{
    int a;
    int b[];
} my_pool_test_t;

class Class_Demo
{
    public:
        int a;
        int b;
        Class_Demo(int ia=1, int ib =2 ) : a(ia), b(ib) {}
};

void test_pool()
{
    pool<> pl( sizeof(my_pool_test_t)  + 5* sizeof(int) );
    my_pool_test_t* test = (my_pool_test_t*)pl.malloc();
    test->a = 100;
    for(int i=0; i<5; i++)
    {
        test->b[i] = i+2;
    }

    cout << "pool a:\t" << test->a << endl;
    cout << "pool b2:\t" << test->b[2] << endl;
	pl.free(test);
}

void test_object_pool()
{
    object_pool <Class_Demo> pclass;
    Class_Demo *cl = pclass.malloc();
    cl = pclass.construct(2,3);
    cout << "object pool a:\t" << cl -> a << endl;
    cout << "object pool b:\t" << cl -> b << endl;
}

int main()
{
    test_pool();
    test_object_pool();
    return 0;
}

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */
      編譯(請自行指定boost庫地址)

g++ test_mem.cpp -o mem -I ../include/ -L../lib -lboost_system -lboost_thread
      執行,發現一切預期以內
liujun05@cq01-rdqa-dev012.cq01:~/test/boost/test$ ./mem 
pool a: 100
pool b2:        4
object pool a:  2
object pool b:  3
相關文章
相關標籤/搜索