프로그래밍/c++
boost::pool_allocator와 boost::fast_pool_allocator의 차이
제페
2016. 2. 28. 08:23
반응형
boost 1.6 버전 기준이다.
boost::pool_allocator는 정렬된 할당/해제를 사용하는 반면,
boost::fast_pool_allocator는 정렬되거나 정렬되지 않은 할당/ 정렬되지 않은 해제를 사용한다.
정렬되지 않은 할당/해제는 메모리 노드의 끝 부분에 메모리를 하나 추가하면 그만이지만,
정렬된 할당/해제의 경우 이전 메모리를 찾는 과정이 필요하고 이 부분에서 부하가 생길 수 있다.
다만 정렬된 할당/해제의 경우 연속된 메모리(가변, new int[10]과 같은!)를 지원하기 때문에 사용처에 따라 잘 구분을 해야할 것이다.
boost::pool_allocator의 allocate / deallocate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | // pool allocator static pointer allocate(const size_type n) { const pointer ret = static_cast<pointer>( singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) ); if ((ret == 0) && n) boost::throw_exception(std::bad_alloc()); return ret; } static void deallocate(const pointer ptr, const size_type n) { singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize, MaxSize>::ordered_free(ptr, n); } // 할당에는 ordered_malloc을 // 해제에는 ordered_free를 | cs |
boost::fast_pool_allocator의 allocate / deallocate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // fast pool allocator static pointer allocate(const size_type n) { const pointer ret = (n == 1) ? static_cast<pointer>( (singleton_pool<fast_pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize, MaxSize>::malloc)() ) : static_cast<pointer>( singleton_pool<fast_pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize, MaxSize>::ordered_malloc(n) ); if (ret == 0) boost::throw_exception(std::bad_alloc()); return ret; } static void deallocate(const pointer ptr, const size_type n) { //! Deallocate memory. if (n == 1) (singleton_pool<fast_pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr); else (singleton_pool<fast_pool_allocator_tag, sizeof(T), UserAllocator, Mutex, NextSize, MaxSize>::free)(ptr, n); } // 1개 할당에는 malloc을 n개 할당에는 ordered_malloc을(단, 1 < n) // 해제엔 항상 free를 | cs |
결론
pool_allocator는 vector와 같이 연속된 메모리를 사용할 때 적합.
fast_pool_allocator는 list, map, set과 같은 연속된 메모리를 사용하는 자료구조가 아닐 때 적합.
반응형