【问题标题】:STL containers, allocator, and pointer wrapperSTL 容器、分配器和指针包装器
【发布时间】:2013-02-01 07:36:48
【问题描述】:

我制作了一个自定义的slab分配器,它使用mmap来分配固定大小的段池。这些段在逻辑上是连续的,但在物理上是离散的。 我还定义了一个指针包装类,它包含从池的逻辑起点开始的偏移量。 指针类看起来像:

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; };
    // ...
};

然后我根据 STL 要求定义了 pointer_traits 和 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);
}

Vector 在指针类型上使用 const_cast,const_cast 只能与原始指针/引用一起使用,并且不能重载,这意味着它无法与自定义的类指针对象一起使用。

我做错了什么还是只是 libc++ 中 STL 实现的一个缺陷?

【问题讨论】:

    标签: pointers stl containers allocator libc++


    【解决方案1】:

    对我来说,这看起来像是 libc++ 中的错误。我正在修复...

    【讨论】:

    • 另一个问题是如何让自定义分配器与 STL 容器一起工作,即使 const_cast 问题已经解决。我的分配器使用类似指针的对象而不是原始指针来表示内存中的位置,但是向量和字符串使用的是 SBO/SSO,需要分配器返回的“指针”类型也可以表示指向其内部缓冲区的原始指针,即一段代码将元素的引用转换为“指针”类型,无论该元素是否在内部缓冲区中。我仍然不知道如何使它工作。
    • 我们会一步一步来。请随时用谷歌搜索我的名字(这将导致一封有效的电子邮件)并离线与我联系。
    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多