Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a custom allocator inside a std::scoped_allocator_adaptor with std::unordered_map

I'm trying to use a simple memory pool allocator with std::unordered_map. I have used this same allocator seemingly successfully with both std::string and std::vector. I want the items contained in the unordered_map (and vector) to also use this allocator so I've wrapped my allocator in std::scoped_allocator_adaptor.

Simplified definition set:

template <typename T>
using mm_alloc = std::scoped_allocator_adaptor<lake_alloc<T>>;

using mm_string = std::basic_string<char, std::char_traits<char>, mm_alloc<char>>;
using mm_vector = std::vector<mm_string, mm_alloc<mm_string>>;
using mm_map = std::unordered_map<mm_string, mm_vector, std::hash<mm_string>, std::equal_to<mm_string>, mm_alloc<std::pair<mm_string, mm_vector>>>;

Initialised as such:

lake pool;
mm_map map { mm_alloc<std::pair<mm_string, mm_vector>>{pool} };

lake_alloc is shown below with the rest of the iterator code. The error I'm getting in Clang 3.3 is that it cannot its allocator_type (in this case the mm_alloc of the pair of string to vector) to its own __pointer_allocator. That is an internal type used for the hash map implementation. Partial error output below:

lib/c++/v1/__hash_table:848:53: error: no matching conversion for functional-style
      cast from 'const allocator_type' (aka 'const std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<std::__1::pair<std::__1::basic_string<char,
      std::__1::char_traits<char>, std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<char, krystal::lake> > >, std::__1::vector<std::__1::basic_string<char,
      std::__1::char_traits<char>, std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<char, krystal::lake> > >,
      std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<char, krystal::lake> > >, krystal::lake> > > >, krystal::lake> >') to '__pointer_allocator' (aka
      'std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<std::__1::__hash_node<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<char, krystal::lake> > >, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>,
      std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<char, krystal::lake> > >, std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<std::__1::basic_string<char,
      std::__1::char_traits<char>, std::__1::scoped_allocator_adaptor<krystal::krystal_alloc<char, krystal::lake> > >, krystal::lake> > > >, void *> *, krystal::lake> >')
    : __bucket_list_(nullptr, __bucket_list_deleter(__pointer_allocator(__a), 0)),
                                                    ^~~~~~~~~~~~~~~~~~~~~~~

GCC 4.7.1 gives me a similar error in its hash map internal structures so clearly I'm doing it wrong but this is my first foray in allocators in the STL and I'm at a loss.

The custom allocator follows, It's a simple implementation with some holes in it but this version works well in a contained test case with a couple of megs of data in vectors and strings.

#include <cstddef>
#include <memory>
#include <scoped_allocator>

class lake {
    const size_t block_size_;
    mutable std::vector<std::unique_ptr<uint8_t[]>> blocks_;
    mutable uint8_t *arena_, *pos_;

    static constexpr const size_t DefaultBlockSize = 48 * 1024;

    void add_block(size_t of_size) const {
        blocks_.emplace_back(new uint8_t[of_size]);
        pos_ = arena_ = blocks_.back().get();
    }

    inline void add_block() const { add_block(block_size_); }

public:
    lake(const size_t block_size)
    : block_size_ {block_size}
    {
        add_block();
    }
    lake() : lake(DefaultBlockSize) {}

    void* allocate(size_t n) const {
        if (pos_ + n - arena_ > block_size_) {
            if (n > block_size_)
                add_block(n); // single-use large block
            else
                add_block();
        }

        auto result = pos_;
        pos_ += n;
        return result;
    }

    void deallocate(void* p, size_t n) const {
    }
};


template <typename T, typename Alloc>
class krystal_alloc {
    const Alloc* allocator_;

public:
    using value_type = T;
    using size_type = size_t;
    using difference_type = ptrdiff_t;
    using pointer = T*;
    using const_pointer = const T*;
    using reference = T&;
    using const_reference = const T&;

    template <typename U>
    struct rebind { typedef krystal_alloc<U, Alloc> other; };

    krystal_alloc() : allocator_{ new Alloc() } {} // not used
    krystal_alloc(const Alloc& alloc) : allocator_{ &alloc } {}

    pointer address(reference v) {
        return 0;
    }

    const_pointer address(const_reference v) {
        return 0;
    }

    size_type max_size() const {
        return static_cast<size_type>(-1) / sizeof(value_type);
    }

    pointer allocate(size_type n) {
        return static_cast<pointer>(allocator_->allocate(sizeof(T) * n));
    }

    void deallocate(pointer p, size_type n) {
        allocator_->deallocate(p, n);
    }
};

template <typename T, typename Alloc, typename U>
inline bool operator==(const krystal_alloc<T, Alloc>&, const krystal_alloc<U, Alloc>) { return true; }

template <typename T, typename Alloc, typename U>
inline bool operator!=(const krystal_alloc<T, Alloc>&, const krystal_alloc<U, Alloc>) { return false; }


// -- standard usage
template <typename T>
using lake_alloc = krystal_alloc<T, lake>;
like image 254
zenmumbler Avatar asked Jul 07 '13 23:07

zenmumbler


1 Answers

I believe the basic error is that your krystal_alloc is lacking a "converting constructor":

template <class U>
    krystal_alloc(const krystal_alloc<U, Alloc>& u)
        : allocator_(u.allocator_) {}

I'm not sure I implemented it correctly, that's just my best guess. You'll need a friend statement to make this work:

template <class U, class A> friend class krystal_alloc;

Also I recommend that you add "const" to the key_type in your allocators for unordered_map:

using mm_map = std::unordered_map<mm_string, mm_vector, std::hash<mm_string>,
                              std::equal_to<mm_string>,
                              mm_alloc<std::pair<const mm_string, mm_vector>>>;

And I think that you can use lake_alloc instead of mm_alloc on your inner containers. Your example compiles for me both ways. I didn't test it for run time behavior.

like image 186
Howard Hinnant Avatar answered Oct 30 '22 08:10

Howard Hinnant