可识别分配器的`std :: array`样式的容器? [英] Allocator-aware `std::array`-style container?

查看:128
本文介绍了可识别分配器的`std :: array`样式的容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一些处理密码秘密的代码,并创建了std::pmr::memory_resource的自定义ZeroedMemory实现,该实现可处理释放时的内存清理并使用必须使用的魔术进行封装,以防止优化编译器被淘汰离开手术.这样做的目的是避免专门化std::array,因为缺少虚拟析构函数意味着在类型擦除之后进行破坏会导致释放内存而不进行清理.

I'm writing some code that handles cryptographic secrets, and I've created a custom ZeroedMemory implementation of std::pmr::memory_resource which handles sanitizes memory on deallocation and encapsulates using the magic you have to use to prevent optimizing compilers from eliding away the operation. The idea was to avoid specializing std::array, because the lack of a virtual destructor means that destruction after type erasure would cause memory to be freed without being sanitized.

不幸的是,我后来才意识到std::array不是AllocatorAwareContainer.我的std::pmr::polymorphic_allocator方法有点误入歧途,因为std::array中显然没有空间存储指向特定分配器实例的指针.但是,我仍然无法理解为什么不允许使用std::allocator_traits<A>::is_always_equal::value == true的分配器,而且我可以轻松地将解决方案重新实现为通用的Allocator,而不是易于使用的std::pmr::memory_resource ...

Unfortunately, I came to realize afterwards that std::array isn't an AllocatorAwareContainer. My std::pmr::polymorphic_allocator approach was a bit misguided, since obviously there's no room in an std::array to store a pointer to a specific allocator instance. Still, I can't fathom why allocators for which std::allocator_traits<A>::is_always_equal::value == true wouldn't be allowed, and I could easily re-implement my solution as a generic Allocator instead of the easier-to-use std::pmr::memory_resource...

现在,我通常可以只使用std::pmr::vector来代替,但是std::array的一个不错的功能是数组的长度是该类型的一部分.例如,如果要处理32字节的密钥,则不必进行运行时检查,以确保传递给我的函数的std::array<uint8_t, 32>参数的长度实际上是正确的.实际上,那些很好地转换为const std::span<uint8_t, 32>,大大简化了需要与C代码互操作的编写函数,因为它们使我基本上可以免费处理任何来源的任意内存块.

Now, I could normally just use an std::pmr::vector instead, but one of the nice features of std::array is that the length of the array is part of the type. If I'm dealing with a 32-byte key, for example, I don't have to do runtime checks to be sure that the std::array<uint8_t, 32> parameter someone passed to my function is, in fact, the right length. In fact, those cast down nicely to a const std::span<uint8_t, 32>, which vastly simplifies writing functions that need to interoperate with C code because they enable me to handle arbitrary memory blocks from any source basically for free.

具有讽刺意味的是,std::tuple需要分配器...但是我不禁想像要处理32字节std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, ...>所需的typedef.

Ironically, std::tuple takes allocators... but I shudder to imagine the typedef needed to handle a 32-byte std::tuple<uint8_t, uint8_t, uint8_t, uint8_t, ...>.

所以:是否有任何标准格式的类型可以容纳固定数量的同类型项,即la std::array,但是具有分配器感知能力(并且最好将这些项存储在相邻区域中,因此可以将其降级-投放到std::span)?

So: is there any standard-ish type that holds a fixed number of homogenously-typed items, a la std::array, but is allocator aware (and preferably stores the items in a continguous region, so it can be down-cast to an std::span)?

推荐答案

这听起来像是XY问题.您似乎在滥用分配器.分配器用于处理运行时内存分配和释放,而不是挂钩堆栈内存.您正在尝试做的事情—使用—后将内存清零应该真的用析构函数来完成.您可能要为此编写一个类Key:

This sounds like a XY problem. You seem to be misusing allocators. Allocators are used to handle runtime memory allocation and deallocation, not to hook stack memory. What you are trying to do — zeroing the memory after using — should really be done with a destructor. You may want to write a class Key for this:

class Key {
public:
    // ...
    ~Key()
    {
        secure_clear(*this); // for illustration
    }
    // ...
private:
    std::array<std::uint8_t, 32> key;
};

您可以轻松实现迭代器和跨度支持.而且您不需要使用分配器.

You can easily implement iterator and span support. And you don't need to play with allocators.

如果您想减少样板代码并使新的类自动迭代器/span友好,请使用继承:

If you want to reduce boilerplate code and make the new class automatically iterator / span friendly, use inheritance:

class Key :public std::array<std::uint8_t, 32> {
public:
    // ...
    ~Key()
    {
        secure_clear(*this); // for illustration
    }
    // ...
};

这篇关于可识别分配器的`std :: array`样式的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆