预分配缓冲区的循环缓冲区?

3

有没有一个可以与预分配缓冲区一起使用的循环缓冲区类库?我查看了Boost::circular_buffer,但它似乎所有的构造函数都需要一个分配器。我不想重新发明循环缓冲区类,但必须使用预分配的缓冲区。我希望有像这样的东西:

char buffer[1000];  // pre-allocated buffer.
circular_buffer_class cb; // a class that provides the interface as a circular buffer.
cb.attach(buffer, 1000); // attaching the preallocated buffer to the circular buffer class.
cb.do_something();

也许可以使用特殊分配器来完成?但是怎么做呢?
此外,我对其他类型的容器类也很感兴趣,比如可与预先分配的缓冲区一起使用的固定大小向量。

3
当您说“预分配缓冲区”时,它是否必须是您自己分配的缓冲区,或者仅告诉容器在内部预分配其自己的缓冲区就足够了? - Cameron
我的意思是我自己分配的那个。这是一个嵌入式系统的软件。我被给予一个指向内存区域和大小的指针,我可以使用它。 - user22097
4
那么,你最好编写一个自定义分配器类(custom allocator class),该类在内部使用该内存区域。然后,您可以将该分配器用于任何使用分配器的STL/Boost类。 - Remy Lebeau
我想知道是否已经存在这样的自定义分配器。我的需求应该不是那么罕见。我对分配器并不是很熟悉,希望能找到已经被证明过的。 - user22097
你可以使用Boost C++。http://www.boost.org/doc/libs/1_54_0/libs/circular_buffer/doc/circular_buffer.html 还有一个bounded_buffer的例子。 - Elvis Dukaj
1个回答

1
以下是与简单自定义分配器相关的一些链接,您可能会发现它们很有趣:
- Hinnant的short_alloc和对齐保证:Hinnant的short_alloc和对齐保证

http://howardhinnant.github.io/stack_alloc.html

你可以使用这个自定义分配器,它是一个派生作品,可能非常接近你的意图:
#pragma once
#include <memory>
#include <cstddef>
#include <cassert>

/**
 * @class fixed_allocator
 * @see https://en.cppreference.com/w/cpp/memory/allocator
 *
 * @tparam The data type which is to be allocated.
 * The type is important for correct data alignment.
 */
template<typename data_type>
class fixed_allocator: public std::allocator<data_type>
{
public:
    using value_type = data_type;

    /**
     * @struct rebind is essential for this class to work properly.
     * It tells std::allocator to use our implementation of allocate and
     * deallocate rather than the default operator new, delete.
     */
    template <class other_type> struct rebind
    {
        using other = fixed_allocator<other_type>;
    };

    ~fixed_allocator()                                  = default;
    fixed_allocator()                                   = delete;    
    fixed_allocator(fixed_allocator const&)             = default;  // Required by rebind.
    fixed_allocator(fixed_allocator &&)                 = default;
    fixed_allocator& operator=(fixed_allocator const&)  = default;
    fixed_allocator& operator=(fixed_allocator&&)       = default;

    /**
     * Create a fixed allocator for the specified data_type.
     *
     * @param buffer The fixed backing store buffer to use for allocation.
     * @param length The number of data_type units held in the
     *               backing store allocation.
     */
    fixed_allocator(value_type *buffer, std::size_t length)
        : buffer_(buffer), buffer_length_(length), in_use_(false)
    {}

    /**
     * Allocates n * sizeof(value_type) bytes of uninitialized storage by
     * calling ::operator new(std::size_t) or
     * ::operator new(std::size_t, std::align_val_t) (since C++17).
     *
     * @param length       The number of value_type elements to allocate.
     *                     Must be <= this->buffer_length_.
     *
     * @return value_type* The allocate data block.
     * @note               For this fixed allocation this function must only
     *                     be called once before deallocate is called.
     *
     * @throw std::bad_alloc If the allocation fails.
     */
    value_type* allocate(std::size_t length)
    {
        assert(length <= this->buffer_length_);
        assert(not this->in_use_);
        this->in_use_ = true;
        return this->buffer_;
    }

    /**
     * Releases the fixed allocation block from use.
     * @param buffer The memory block being freed.
     *               Must be the same as this->buffer_.
     * @param length The number of bytes freed. Unchecked.
     */
    void deallocate(value_type *buffer, std::size_t length)
    {
        (void) length;
        assert(buffer == this->buffer_);
        assert(this->in_use_);
        this->in_use_ = false;
    }

private:
    value_type* buffer_;
    std::size_t buffer_length_;
    bool        in_use_;
};

现在,您可以将此分配器的专用实例传递到boost::circular_buffer构造函数中。

我并不完全相信这个分配器是100%有效的,因为没有通过重新绑定的分配器进行分配的方法。但如果它能编译并正常工作,那就够好了,我想。 - Mooing Duck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接