简体   繁体   中英

Custom allocator for nested containers

I am working on custom allocators. So far, I have tried to work on simple containers: std::list, std::vector, std::basic_string, etc...

My custom allocator is a static buffer allocator, its implementation is straightforward:

#include <memory>

template <typename T>
class StaticBufferAlloc : std::allocator<T>
{
private:
    T *memory_ptr;
    std::size_t memory_size;

public:
    typedef std::size_t size_type;
    typedef T *pointer;
    typedef T value_type;

    StaticBufferAlloc(T *memory_ptr, size_type memory_size) : memory_ptr(memory_ptr), memory_size(memory_size) {}
    StaticBufferAlloc(const StaticBufferAlloc &other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size){};
    pointer allocate(size_type n, const void *hint = 0) { return memory_ptr; } // when allocate return the buffer
    void deallocate(T *ptr, size_type n) {}                                    // empty cause the deallocation is buffer creator's responsability

    size_type max_size() const { return memory_size; }
};

I am using it in this fashion:

  using inner = std::vector<int, StaticBufferAlloc<int>>;
  int buffer[201];
  auto alloc1 = StaticBufferAlloc<int>(&buffer[100], 50);
  inner v1(0, alloc1);
  assert(v1.size() == 0);
  const int N = 10; 
  // insert 10 integers
  for (size_t i = 0; i < N; i++)  {
    v1.push_back(i);
  }
  assert(v1.size() == N);

All good so far, when I grow N past the max buffer size it throws and that's expected.


Now, I am trying to work with nested containers. In short, am trying to have a vector of the vector (matrix), where the parent vector and all its underlying elements (that are vectors ie containers) share the same static buffer for allocation . It looks like scoped_allocator can be a solution for my problem.

  using inner = std::vector<int, StaticBufferAlloc<int>>;
  using outer = std::vector<inner, std::scoped_allocator_adaptor<StaticBufferAlloc<inner>>>;

  int buffer[201];
  auto alloc1 = StaticBufferAlloc<int>(&buffer[100], 50);
  auto alloc2 = StaticBufferAlloc<int>(&buffer[150], 50);
  inner v1(0, alloc1);
  inner v2(0, alloc2);
  assert(v1.size() == 0);
  assert(v2.size() == 0);

  const int N = 10; 
  // insert 10 integers
  for (size_t i = 0; i < N; i++)
  {
    v1.push_back(i);
    v2.push_back(i);
  }
  assert(v1.size() == N);
  assert(v2.size() == N);

  outer v // <- how to construct this vector with the outer buffer? 
  v.push_back(v1);
  v.push_back(v2); 
  ...

My question is how to initialize the outer vector on its constructor call with its static buffer?

Creating a scoped allocator in C++11/C++14 was a little bit challenging. So I opted for a very modern solution introduced in C++17. Instead of implementing an allocator, I used polymorphic_allocator . Polymorphic allocators are scoped allocators, standard containers will automatically pass the allocators to sub-objects.

Basically, the idea was to use a polymorphic allocator and inject it with monotonic_buffer_resource . The monotonic_buffer_resource can be initialized with a memory resource .

Writing a custom memory resource was very simple:

class custom_resource : public std::pmr::memory_resource
{
public:
  explicit custom_resource(std::pmr::memory_resource *up = std::pmr::get_default_resource())
      : _upstream{up}
  {
  }

  void *do_allocate(size_t bytes, size_t alignment) override
  {
    return _upstream; //do nothing, don't grow just return ptr
  }

  void do_deallocate(void *ptr, size_t bytes, size_t alignment) override
  {
    //do nothing, don't deallocate
  }

  bool do_is_equal(const std::pmr::memory_resource &other) const noexcept override
  {
    return this == &other;
  }

private:
  std::pmr::memory_resource *_upstream;
};

Using it is even simpler:

 std::byte buffer[512];
 custom_resource resource;
 std::pmr::monotonic_buffer_resource pool{std::data(buffer), std::size(buffer), &resource};
 std::pmr::vector<std::pmr::vector<int>> outer(&pool)

It is important to note that std::pmr::vector<T> is just std::vector<T, polymorphic_allocator> .

Useful resources:


std::pmr is cool but it requires modern versions of gcc to run (9+). Fortunately, Reddit is full of kind strangers. A C++14 solution can be found here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM