3.11 singleton_pool
singleton_pool与pool的接口完全一致,可以分配简单数据类型(POD)的内存指针,但它是一个单件,并提供线程安全。
由于目前Boost还未提供标准的单件库,singleton_pool在其内部实现了一个较简单、泛型的单件类,保证在main()函数运行之前就创建单件(详情可参考4.6.1小节,117页)。
singleton_pool位于名字空间boost,为了使用singleton_pool组件,需要包含头文件<boost/pool/singleton_pool.hpp>,即:
- #include <boost/pool/singleton_pool.hpp>
- using namespace boost;
3.11.1 类摘要
singleton_pool的类摘要如下:
- 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 void free(void * ptr, std::size_t n);
- static void ordered_free(void * ptr, size_type n);
-
- static bool release_memory();
- static bool purge_memory();
- };