memory_scoped_allocator_adaptor

memory_scoped_allocator_adaptor (std::scoped_allocator_adaptor)

<scoped_allocator> 헤더에 정의되어 있어요. 다단계 컨테이너(멱집합·리스트·튜플·맵의 벡터 등)에 사용할 수 있는 할당자예요. 하나의 외부 할당자 타입 OuterAlloc과 0개 이상의 내부 할당자 타입 InnerAlloc...으로 인스턴스화돼요. C++11부터 도입됐어요.

출처: cppreference

본문

template< class OuterAlloc, class... InnerAllocs >
class scoped_allocator_adaptor
    : public OuterAlloc;

(C++11부터)

std::scoped_allocator_adaptor 클래스 템플릿은 다단계 컨테이너에 사용할 수 있는 할당자예요. scoped_allocator_adaptor로 직접 구성한 컨테이너는 요소를 할당할 때 OuterAlloc을 사용하는데, 요소가 그 자체로 컨테이너이면 첫 번째 내부 할당자를 사용해요. 그 컨테이너의 요소들이 다시 컨테이너라면 두 번째 내부 할당자를 쓰는 식이에요. 컨테이너의 단계 수가 내부 할당자 수보다 많으면 마지막 내부 할당자가 더 중첩된 컨테이너에 재사용돼요.

이 어댑터의 목적은 중첩 컨테이너의 상태 있는(stateful) 할당자를 올바르게 초기화하는 거예요. 예를 들어 중첩 컨테이너의 모든 단계가 같은 공유 메모리 세그먼트에 놓여야 할 때 써요. 어댑터의 생성자는 목록의 모든 할당자 인자를 받고, 각 중첩 컨테이너는 필요할 때 어댑터에서 할당자 상태를 얻어요.

중첩 타입

타입 정의
outer_allocator_type OuterAlloc
inner_allocator_type sizeof...(InnerAllocs)가 0이면 scoped_allocator_adaptor<OuterAlloc>, 아니면 scoped_allocator_adaptor<InnerAllocs...>
value_type std::allocator_traits<OuterAlloc>::value_type
size_type std::allocator_traits<OuterAlloc>::size_type
difference_type std::allocator_traits<OuterAlloc>::difference_type
pointer std::allocator_traits<OuterAlloc>::pointer
const_pointer std::allocator_traits<OuterAlloc>::const_pointer

propagate_on_container_copy_assignment, propagate_on_container_move_assignment, propagate_on_container_swap, is_always_equal은 구성 할당자 중 하나라도 truestd::true_type, 그외 std::false_type.

멤버 함수

  • (생성자): 새 scoped_allocator_adaptor 생성
  • inner_allocator: 내부 할당자 참조 얻기
  • outer_allocator: 외부 할당자 참조 얻기
  • allocate: 외부 할당자로 초기화되지 않은 저장 공간 할당
  • deallocate: 외부 할당자로 저장 공간 해제
  • max_size: 외부 할당자가 지원하는 최대 할당 크기 반환
  • construct: 적절하면 내부 할당자를 생성자에 전달해 할당된 저장 공간에 객체 구성
  • destroy: 할당된 저장 공간의 객체 파괴
  • select_on_container_copy_construction: 어댑터와 모든 할당자의 상태 복사

예제

#include <boost/interprocess/allocators/adaptive_pool.hpp>
#include <boost/interprocess/managed_shared_memory.hpp>
#include <scoped_allocator>
#include <vector>

namespace bi = boost::interprocess;
template<class T> using alloc =
    bi::adaptive_pool<T, bi::managed_shared_memory::segment_manager>;

using ipc_row = std::vector<int, alloc<int>>;
using ipc_matrix = std::vector<ipc_row,
    std::scoped_allocator_adaptor<alloc<ipc_row>>>;

int main()
{
    bi::managed_shared_memory s(bi::create_only, "Demo", 65536);
    ipc_matrix v(s.get_segment_manager());
    // 내부 벡터들은 외부 벡터의 scoped_allocator_adaptor 에서 할당자 인자를 얻어요
    v.resize(1);
    v[0].push_back(1);
    v.emplace_back(2);
    bi::shared_memory_object::remove("Demo");
}

주의 (Notes)

일반적인 구현은 std::scoped_allocator_adaptor<InnerAllocs...> 인스턴스를 멤버 객체로 보유해요. std::pmr::polymorphic_allocator는 uses-allocator construction을 따라 중첩 컨테이너로 전파되므로 scoped_allocator_adaptor가 필요 없고, 그것과 함께 동작하지도 않아요.

더 알아보기 (Learn more)

cppreference