Boost Development Guide - 3.10 singleton_pool

singleton_pool

The interface of singleton_pool is exactly the same as that of pool, and memory pointers of simple data types (POD) can be allocated, but it is a singleton.

singleton_pool is located in the namespace boost. In order to use the singleton_pool component, the header file <boost/pool/singleton_pool.hpp> needs to be included, namely:

#include <boost/pool/singleton_pool.hpp>
using namespace boost;

singleton_pool uses the boost.thread library to provide thread safety guarantee by default, so the boost_thread library needs to be linked. If multi-threading is not used, the macro BOOST_POOL_NO_MT can be defined before the header file.

class summary

template <typename Tag, unsigned RequestedSize>
class singleton_ pool
{
    
    
public:
   static bool is_from(void * ptr);
   static void* malloc(); //分配内存
   static void* ordered_malloc();
   static void* ordered_malloc(size_type n);
   
   static void free(void * ptr); //归还内存
   static void ordered_free(void* ptr);
   static voidfree(void * ptr, std: : size_t n);
   static voidordered_free (void * ptr, size_type n);
   static bool release_memory(); //释放内存
   static bool purge_memory();
);

usage

singleton_pool mainly has two template type parameters (the rest can use default values). The first Tag is only used to mark different singletons, which can be empty classes or even declarations (this usage is also used for boost.exception). The second parameter RequestedSize is equivalent to the integer requested_size in the pool constructor, indicating the size of the memory block allocated by the pool.

The interface of singleton_pool is exactly the same as that of pool, but the member functions are all static, so there is no need to declare an instance of singleton_pool. Use field operators directly ::to call static member functions. Because singleton_pool is a singleton, its life cycle is as long as the entire program. Unless release_memory() or purge_memory() is called manually, singleton_pool will not automatically release the occupied memory. Except for these two points, the usage of singleton_pool is exactly the same as pool.

#define BOOST_POOL_NO_MT //不使用多线程
#include <boost/pool/singleton_pool.hpp>

struct pool_tag {
    
    }; //仅仅用于标记的空类
typedef singleton_pool<pool_tag, sizeof(int)> spl; //内存池定义

int main()
{
    
    
	int* p = (int*)spl::malloc(); //分配一个整数内存块
	assert(spl::is_from(p));
	spl::release_memory(); //释放所有未被分配的内存
} //spl的内存直到程序结束才完全释放,而不是退出作用域

When singleton_pool is used, it is best to use typedef to simplify the name, otherwise it will make the type name too long and difficult to use. As shown in the code:

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

The class pool_tag used for marking can be simplified again, and the tag class can be declared directly in the template parameter list, so that the type definition of singleton_pool can be completed in one statement, for example:

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

code example

#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();
}

int main()
{
    
    
	case4();
}

Guess you like

Origin blog.csdn.net/qq_36314864/article/details/132066934