2013-02-01 3 views
1

mmap을 사용하여 고정 크기 세그먼트 풀을 할당하는 사용자 정의 슬래브 할당자를 만들었습니다. 이러한 세그먼트는 논리적으로 연속적이지만 물리적으로 분리되어 있습니다. 또한 풀의 논리적 시작점에서 오프셋을 포함하는 포인터 래퍼 클래스를 정의했습니다. 여기 STL 컨테이너, 할당 자 및 포인터 래퍼

template<typename T> 
struct offptr_t { 
    typedef offptr_t<T> this_t; 
    typedef T element_type; 
    typedef OFFSET difference_type; 
    template<typename U> struct rebind { typedef offptr_t<U> other; }; 
    offptr_t(const mem_pool *p, OFFSET o) 
    : pool(p), offset(o) 
    {} 
    // ... 
}; 

가 할당입니다 :

template<typename T> 
struct mem_pool_allocator { 
public: 
    typedef mem_pool_allocator<T> this_t; 
    typedef T value_type; 
    typedef offptr_t<T> pointer; 
    typedef const offptr_t<T> const_pointer; 
    typedef T& reference; 
    typedef const T& const_reference; 
    typedef size_t size_type; 
    typedef int64_t difference_type; 
    template< class U > struct rebind { typedef mem_pool_allocator<U> other; }; 
    // ... 
}; 

가 그럼 난 pointer_traits을 정의 STL과 같은 iterator_traits 클래스가 필요합니다 :

namespace std { 
    template<typename T> 
    struct pointer_traits<offptr_t<T>> { 
     typedef typename offptr_t<T> pointer; 
     template<typename U> struct rebind { typedef offptr_t<U> other; }; 
    }; 

    template<typename T> 
    struct iterator_traits<offptr_t<T>> { 
     typedef typename offptr_t<T> pointer; 
     typedef typename pointer::difference_type difference_type; 
     typedef typename pointer::element_type value_type; 
     typedef typename pointer::element_type &reference; 
     typedef std::random_access_iterator_tag iterator_category; 
    }; 
} // End of namespace std 

것은 내가 이러한 클래스를 사용할 때처럼 포인터 클래스 보인다 libC++의 STL 컨테이너에서 C++/v1/vector에 여러 컴파일 오류가 발생했습니다 :

template <class _Tp, class _Allocator> 
_LIBCPP_INLINE_VISIBILITY inline 
void 
__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, false_type) _NOEXCEPT 
{ 
    while (__new_last != __end_) 
     __alloc_traits::destroy(__alloc(), const_cast<pointer>(--__end_)); 
} 


template <class _Tp, class _Allocator> 
_LIBCPP_INLINE_VISIBILITY inline 
void 
__vector_base<_Tp, _Allocator>::__destruct_at_end(const_pointer __new_last, true_type) _NOEXCEPT 
{ 
    __end_ = const_cast<pointer>(__new_last); 
} 

벡터는 포인터 유형에 const_cast <>을 사용하고 있습니다. const_cast <>는 원시 포인터/참조에서만 사용할 수 있으며 오버로드 할 수 없습니다. 즉, 사용자 정의 포인터와 같은 객체로 작업하는 것이 불가능합니다.

내가 잘못했거나 libC++에서 STL 구현의 결함 일 뿐인가?

답변

0

이것은 libC++의 버그와 같습니다. 난 고치기 위해 노력하고있어.

+0

답 해줘서 고마워! – Windoze

+0

또 다른 질문은 const_cast 문제가 해결 된 경우에도 STL 컨테이너에서 맞춤 할당자를 사용하는 것입니다. 내 할당자는 메모리의 위치를 ​​나타 내기 위해 포인터 대신에 포인터와 같은 객체를 사용하지만 벡터와 문자열은 SBO/SSO를 사용하고 있습니다. 할당 자에 의해 반환 된 "포인터"유형은 내부 포인터를 가리키는 원시 포인터를 나타낼 수도 있습니다 즉, 코드 조각은 요소가 내부 버퍼에 있는지 여부에 관계없이 요소의 참조를 "포인터"유형으로 변환합니다. 나는 아직도 어떻게 작동하는지 잘 모른다. – Windoze

+0

한 번에 한 걸음 씩 나아갈 것입니다. 언제든지 Google에 내 이름 (실제 이메일로 연결됨)을 보내고 오프라인으로 연락하십시오. –