How can I write a stateful allocator in C++11, given requirements on copy construction?

前端 未结 3 1780
借酒劲吻你
借酒劲吻你 2020-12-02 11:24

As far as I can tell, the requirements on an allocator to be used with STL containers are laid out in Table 28 of section 17.6.3.5 of the C++11 standard.

I\'m a bit

3条回答
  •  情歌与酒
    2020-12-02 12:02

    Equality of allocators does not imply that they must have exactly the same internal state, only that they must both be able to deallocate memory that was allocated with either allocator. Cross-type equality of allocators a == b for an allocator a of type X and allocator b of type Y is defined in table 28 as "same as a == Y::template rebind::other(b)". In other words, a == b if memory allocated by a can be deallocated by an allocator instantiated by rebinding b to a's value_type.

    Your freelist allocators need not be able to deallocate nodes of arbitrary type, you only need to ensure that memory allocated by FreelistAllocator can be deallocated by FreelistAllocator::template rebind::other. Given that FreelistAllocator::template rebind::other is the same type as FreelistAllocator in most sane implementations, this is fairly easy to achieve.

    Simple example (Live demo at Coliru):

    template 
    class FreelistAllocator {
        union node {
            node* next;
            typename std::aligned_storage::type storage;
        };
    
        node* list = nullptr;
    
        void clear() noexcept {
            auto p = list;
            while (p) {
                auto tmp = p;
                p = p->next;
                delete tmp;
            }
            list = nullptr;
        }
    
    public:
        using value_type = T;
        using size_type = std::size_t;
        using propagate_on_container_move_assignment = std::true_type;
    
        FreelistAllocator() noexcept = default;
        FreelistAllocator(const FreelistAllocator&) noexcept {}
        template 
        FreelistAllocator(const FreelistAllocator&) noexcept {}
        FreelistAllocator(FreelistAllocator&& other) noexcept :  list(other.list) {
            other.list = nullptr;
        }
    
        FreelistAllocator& operator = (const FreelistAllocator&) noexcept {
            // noop
            return *this;
        }
    
        FreelistAllocator& operator = (FreelistAllocator&& other) noexcept {
            clear();
            list = other.list;
            other.list = nullptr;
            return *this;
        }
    
        ~FreelistAllocator() noexcept { clear(); }
    
        T* allocate(size_type n) {
            std::cout << "Allocate(" << n << ") from ";
            if (n == 1) {
                auto ptr = list;
                if (ptr) {
                    std::cout << "freelist\n";
                    list = list->next;
                } else {
                    std::cout << "new node\n";
                    ptr = new node;
                }
                return reinterpret_cast(ptr);
            }
    
            std::cout << "::operator new\n";
            return static_cast(::operator new(n * sizeof(T)));
        }
    
        void deallocate(T* ptr, size_type n) noexcept {
            std::cout << "Deallocate(" << static_cast(ptr) << ", " << n << ") to ";
            if (n == 1) {
                std::cout << "freelist\n";
                auto node_ptr = reinterpret_cast(ptr);
                node_ptr->next = list;
                list = node_ptr;
            } else {
                std::cout << "::operator delete\n";
                ::operator delete(ptr);
            }
        }
    };
    
    template 
    inline bool operator == (const FreelistAllocator&, const FreelistAllocator&) {
        return true;
    }
    
    template 
    inline bool operator != (const FreelistAllocator&, const FreelistAllocator&) {
        return false;
    }
    

提交回复
热议问题