12

我正在尝试实现一个类,该类在内存中跟随一个任意类型的数组:

template<class T>
class Buf
{
    size_t n;
    int refs;
    explicit Buf(size_t n) : n(n) { }
    // other declarations are here as appropriate

    // Followed in memory by:
    // T items[n];
};

这很容易operator new

template<class T>
Buf<T> *make_buf(size_t n)
{
    // Assume the caller will take care of constructing the array elements
    return new(operator new(sizeof(Buf<T>) + sizeof(T) * n)) Buf<T>(n);
}

template<class T>
void free_buf(Buf<T> *p)
{
    // Assume the caller has taken care of destroying the array elements
    p->~Buf<T>();
    return operator delete(p);
}

template<class T>
T *get_buf_array(Buf<T> *p)
{
    return reinterpret_cast<T *>(reinterpret_cast<char *>(p) + sizeof(Buf<T>));
}

但是现在,我如何使用一些符合标准的分配器 SomeAllocator来实现这一点?

是否保证SomeAllocator::rebind<char>::other::allocate将返回适合任何类型对象对齐的内存?如果是这样,我是否可以安全地使用某种 char 类型的分配器?如果没有,我是否有其他选择,或者一般分配器不可能完成这项任务?(在最坏的情况下,我想我可以将指针投射到uintptr_t并手动对齐它们,但我想知道是否有更好的方法。)

4

4 回答 4

1

我在想解决方案可能是通过创建一个名义上的早期数组。

+-----------+
|Buf<T>     |
+-------+---+---+-------+-------+
|T[0]   | T[1]  |T[2]   |  T[3]...
+-------+-------+-------+-------+

With the non-overlapping T[2], T[3], ... being the required array of T.

template<class T>
class Buf
{
    size_t n;
    int refs;
    explicit Buf(size_t n) : n(n) { }
    // other declarations are here as appropriate

    // Followed in memory by:
    // T items[n];
};

被破坏的元素的数量将是:-

const size_t lead = ( sizeof(Buf<T>) + sizeof(T) - 1) / sizeof(T);

最后 i 的原始内存可以通过

(reinterpret_cast<T*>( this ) )[ i + lead ];
于 2015-12-31T20:22:01.467 回答
0

恐怕您对 C++ 标准的要求做出了毫无根据的假设。您尝试做的事情通常不可能。

默认分配器(new 或 malloc)需要返回一个指针,该指针指向适合对齐的内存块any complete object type with a fundamental alignment requirement。大小必须是at least as large as the requested size. 自定义分配器有不同的要求,具体取决于它们分配的内容。一种类型的分配器不能保证返回适合另一种类型的存储。当然,如果您是实现自定义分配器的人,您可以确保它返回您需要的内容。

编译器需要满足一些关于内存布局的约束,但它不能保证某些东西在其他东西之后立即放置在内存中。可能会插入填充字节以满足对齐要求。

最近的 C++ 标准为处理对齐提供了相当多的支持。那里的某个地方可能有一个答案。我怀疑这是你没有告诉我们的一些要求。也许还有另一种方法可以解决。

于 2014-08-04T03:59:43.873 回答
0

我认为您符合标准的分配器是否符合标准?由于您不需要将分配器与 stl 数据结构一起使用,因此即使您也可以这样做,但实际上并没有必要满足它的要求,因为我认为这是一种巧妙的方法,在这种情况下,您可以通过使用带有自定义 stl 样式分配器的 std::vector 作为模板参数来实现缓冲区。关于 operator new 和 operator new[] 的对齐保证,我建议你看看:

C++ 的新操作是否可以保证地址返回的对齐?.

如果您的对齐问题是针对原始类型(例如双精度数等),则您几乎可以被 std::align 覆盖,正如您在http://en.cppreference.com/w/cpp/memory/align中看到的那样。

但是,如果您有更奇怪/更大的对齐要求,例如将每个元素对齐到缓存行等,或者 T 是大小为 sizeof(T)mod 对齐!= 0 的类型,则在分配 T 的数组时可能会遇到问题。在这种情况下,即使数组的第一个元素对齐以满足要求,但这并不意味着所有后续元素也将对齐。

于 2014-10-02T10:49:38.233 回答
0

通过将分配器重新绑定到std::aligned_storage.

typedef std::aligned_storage_t< 1, std::max( alignof (Buf<T>), alignof (T) ) >
        unit_type; // lowest-common-denominator type for aligned allocation

std::size_t unit_count // number of unit_type array elements needed
    = ( sizeof (Buf<T>) + sizeof (T) * n // = actual used storage
           + sizeof (unit_type) - 1 )    //   + alignment padding
      / sizeof (unit_type);              // divided by element size

typedef typename std::allocator_traits< SomeAllocator >::rebind_alloc< unit_type >
        rebound;

rebound a( alloc_parm ); // Retain this somewhere if statefulness is allowed.
ptr = std::allocator_traits< rebound >::allocate( a, unit_count );

(请记住,所有分配器访问都通过allocator_traits!)

于 2015-06-13T07:01:54.477 回答