簡體   English   中英

具有預分配緩沖區的循環緩沖區?

[英]Circular buffer with pre-allocated buffer?

是否有任何庫具有可與預分配緩沖區一起使用的循環緩沖區類? 我查看了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();

也許是否可以使用一些特殊的分配器? 但是怎么樣?

另外,我對其他類型的容器類感興趣,比如固定大小的向量,可以與預先分配的緩沖區一起使用。

以下是一些與您可能感興趣的簡單自定義分配器相關的鏈接:

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構造函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM