简体   繁体   English

我可以编写一个自定义分配器来确定std :: vector的重新分配量吗?

[英]Can I write a custom allocator to decide the reallocation amount of std::vector?

To my understanding, a custom allocator must fit the requierements of the Allocator Concept . 据我了解,自定义分配器必须符合“ 分配器概念”的要求 Based on that interface though, I can't see how I would choose a new allocation amount when the vector has run out of reserve. 但是,基于该接口,我看不到向量用完后如何选择新的分配量。

For example, the current implementation on my machine will double the allocation size every time the reserve is exceeded during a push_back() . 例如,我的机器上的当前实现每次在push_back()期间超出reserve量时都会使分配大小加倍。 I'd like to provide a custom allocator that is slow and memory conscious. 我想提供一个自定义的分配器,它速度慢且具有内存意识。 It will only allocate the previous capacity+1 to accomodate the new element. 它只会分配以前的capacity+1来容纳新元素。

These are the interfaces of the concept I'm looking at: 这些是我正在研究的概念的接口:

a.allocate(n)
a.allocate(n, cvptr) (optional) 

I've made a working boilerplate allocator like so: 我已经制作了一个工作样板分配器,如下所示:

#include <limits>
#include <iostream>

template <class T> class MyAlloc {
public:

  // type definitions
  typedef T value_type;
  typedef T *pointer;
  typedef const T *const_pointer;
  typedef T &reference;
  typedef const T &const_reference;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;

  pointer address(reference value) const {
    return &value;
  }
  const_pointer address(const_reference value) const {
    return &value;
  }
  size_type max_size() const throw() {
    return std::numeric_limits<std::size_t>::max() / sizeof(T);
  }
  pointer allocate(size_type num, const void * = 0) {
    return (pointer)(::operator new(num * sizeof(T)));
  }
  void construct(pointer p, const T &value) {
    new ((void *)p) T(value);
  }
  void destroy(pointer p) {
    p->~T();
  }
  void deallocate(pointer p, size_type num) {
    ::operator delete((void *)p);
  }
};

Looking at the allocate function: 看一下allocate函数:

pointer allocate(size_type num, const void * = 0) {
  return (pointer)(::operator new(num * sizeof(T)));
}

I could allocate more or less memory here, but I don't see a way of reporting that back to the vector so that it knows what its current capacity is. 我可以在这里分配更多或更少的内存,但是我看不到一种将其报告回向量的方法,因此它无法知道当前的容量。

Maybe this falls outside the responsibility of an allocator? 也许这超出了分配器的责任?

The STL model that C++ inherited is based on a specific division between container and allocator. C ++继承的STL模型基于容器和分配器之间的特定划分。 The purpose of an allocator is to provide the memory that someone requests. 分配器的目的是提供某人请求的内存。 The decision about how much memory to allocate is entirely up to the container, without regard for which allocator it uses to provide that memory. 分配多少内存的决定完全取决于容器,而不考虑它使用哪个分配器来提供该内存。

That's the model C++ uses. 这就是C ++使用的模型。 You could write your own vector -like container that allows its allocator to specify how much it should allocate. 您可以编写自己的类似于vector的容器,该容器允许其分配器指定应分配的数量。 But other than that, no. 但是除此之外,没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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