1. 참고링크 [Bottom] [Top]
Boost Pool Library - http://www.boost.org/libs/pool/doc/
[번역] Boost Pool Library - http://jacking75.cafe24.com/Boost/Pool/index.htm
2. boost::pool 사용하기 [Bottom] [Top]
namespace boost { class pool; class object_pool; class singleton_pool; class pool_allocator; }
3. boost::object_pool 라이브러리 [Bottom] [Top]
// C4291 컴파일러 경고 비활성화 // : boost::object_pool::construct() 함수 사용 시 컴파일러 경고 발생 // (내부적으로 Placement new 사용) #pragma warning( disable: 4291 ) #include <boost/pool/object_pool.hpp>
- 다른 Pool 라이브러리 와는 달리 타입(객체) 기반의 Pool 라이브러리 (객체 단위로 할당/해제)
- 특징 - 객체 할당과 해제 시 생성자와 소멸자 호출도 가능하다.
3.1. 클래스 [Bottom] [Top]
template <typename T, typename UserAllocator> class object_pool : protected pool<UserAllocator> { public: typedef T element_type; typedef UserAllocator user_allocator; typedef typename pool<UserAllocator>::size_type size_type; typedef typename pool<UserAllocator>::difference_type difference_type; public: explicit object_pool(const size_type next_size = 32) : pool<UserAllocator>(sizeof(T), next_size); ~object_pool(); element_type * malloc(); void free(element_type * const chunk); bool is_from(element_type * const chunk) const; element_type * construct(); void destroy(element_type * const chunk); size_type get_next_size() const; void set_next_size(const size_type x); };
- 생성자
- object_pool() - 생성자, 처음 객체 할당 요청 시 시스템 메모리로 부터 할당될 객체 개수 지정 가능 (기본값: 32개)
- 객체 할당/해제 멤버 함수군1
malloc() - 객체 할당, 생성자 호출 안 함, 오류 발생 시 0(NULL) 반환
free() - 객체 해제, 소멸자 호출 안 함
- 객체 할당/해제 멤버 함수군2
construct() - 객체 할당, 생성자 호출함, 오류 발생 시 0(NULL) 반환
destroy() - 객체 해제, 소멸자 호출함
- 기타 멤버 함수
- is_from() - 해당 객체 Pool 에서 할당된 객체인지 확인 (해당 객체에서 할당된 객체일 경우: true, 아닐 경우: false)
- get_next_size() - 다음 시스템 메모리로 부터 할당될 객체 개수를 반환
- set_next_size() - 다음 시스템 메모리로 부터 할당될 객체 개수를 지정
4. boost::pool_alloc 라이브러리 [Bottom] [Top]
namespace boost { template <typename T, typename UserAllocator, typename Mutex, unsigned NextSize> class pool_allocator; template<typename UserAllocator, typename Mutex, unsigned NextSize> class pool_allocator<void, UserAllocator, Mutex, NextSize>; template <typename T, typename UserAllocator, typename Mutex, unsigned NextSize> class fast_pool_allocator; template <typename UserAllocator, typename Mutex, unsigned NextSize> class fast_pool_allocator<void, UserAllocator, Mutex, NextSize>; }
- STL 의 표준 할당자(Standard Allocator) 요구 사항을 충족, STL 의 컨테이터에서 사용 가능.
내부적으로 boost::singleton_pool 사용, Thread-safe 보장.
- 2 가지의 boost::pool_alloc 할당자 제공
boost::pool_allocator - std::vector 와 같은 연속 공간을 할당해야 할 경우 효율적이다.
boost::fast_pool_allocator - std::list 와 같은 단일 공간을 할당해야 할 경우 효율적이다.
