Pointers STL容器、分配器和指针包装器

Pointers STL容器、分配器和指针包装器,pointers,stl,containers,allocator,libc++,Pointers,Stl,Containers,Allocator,Libc++,我制作了一个定制的slab分配器,它使用mmap分配固定大小的段池。这些段在逻辑上是连续的,但在物理上是离散的。 我还定义了一个指针包装类,它包含池的逻辑起点的偏移量。 指针类如下所示: template<typename T> struct offptr_t { typedef offptr_t<T> this_t; typedef T element_type; typedef OFFSET difference_type; templ

我制作了一个定制的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)
    {}
    // ...
};
模板
结构主任{
typedef offptr\t本;
类型定义T元素_类型;
typedef偏移量差_类型;
模板结构重新绑定{typedef offptr_t other;};
办公室主任(施工成员池*p,偏移量o)
:池(p),偏移量(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; };
    // ...
};
模板
结构内存池分配器{
公众:
typedef mem_pool_分配器此\u t;
类型定义T值_类型;
类型定义关闭指针;
typedef const offptr_t const_指针;
typedef T&reference;
类型定义常数T和常数U参考;
typedef size_t size_type;
typedef int64_t difference_type;
模板结构重新绑定{typedef mem_pool_分配器other;};
// ...
};
然后我将指针和迭代器类定义为STL所需:

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
名称空间std{
模板
结构指针特性{
typedef typename offptr\t指针;
模板结构重新绑定{typedef offptr_t other;};
};
模板
结构迭代器{
typedef typename offptr\t指针;
typedef typename指针::差分类型差分类型;
typedef typename指针::元素\类型值\类型;
typedef typename指针::元素\类型和引用;
typedef std::随机访问迭代器标记迭代器类别;
};
}//名称空间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);
}
模板
_LIBCPP_INLINE_可见性INLINE
无效的
__向量基::uuu destruct_u在_u端(const_指针uu new_ulast,false_utype)NOEXCEPT
{
while(\uuuu new\u last!=\uuuuu end\uu)
__alloc_特征::销毁(_alloc(),const_cast(--u end_u));
}
模板
_LIBCPP_INLINE_可见性INLINE
无效的
__向量基::uuu destruct_u在_u端(const_指针uu new_last,true_type)u NOEXCEPT
{
__结束=连续铸造(新铸造和最后铸造);
}
Vector正在指针类型上使用const_cast,const_cast只能与原始指针/引用一起使用,不能重载,这意味着不可能使用自定义的类似指针的对象


我是否做错了什么,或者这只是libc++中STL实现的一个缺陷?

这在我看来像是libc++中的一个bug。我正在解决一个问题…

另一个问题是如何让自定义分配器与STL容器一起工作,即使const_cast问题已经解决。我的分配器使用类似指针的对象而不是原始指针来表示内存中的位置,但向量和字符串使用SBO/SSO,它需要分配器返回的“指针”类型也可以表示指向其内部缓冲区的原始指针,即一段代码将元素的引用转换为“指针”类型,而不管元素是否在内部缓冲区中。我仍然不知道如何使它工作。我们将一步一步地进行。请随时谷歌我的名字(这将导致一个工作电子邮件)和联系我离线。