diff options
| author | MerryMage <MerryMage@users.noreply.github.com> | 2021-02-06 19:13:03 +0000 | 
|---|---|---|
| committer | MerryMage <MerryMage@users.noreply.github.com> | 2021-02-06 19:16:00 +0000 | 
| commit | 8d002659987cd45cb77896fbb173159398138e73 (patch) | |
| tree | 35d6d7a67ebccfbc78f6e0d07600ea9ceb0d909b /src/common | |
| parent | 821fc4a7b6b04a9b59e81930f8a570f521080775 (diff) | |
ring_buffer: Remove granularity template argument
Non-obvious bug in RingBuffer::Push(std::vector<T>&) when granularity != 1
Just remove it altogether because we do not have a use for granularity != 1
Diffstat (limited to 'src/common')
| -rw-r--r-- | src/common/ring_buffer.h | 21 | 
1 files changed, 10 insertions, 11 deletions
| diff --git a/src/common/ring_buffer.h b/src/common/ring_buffer.h index 138fa0131..4a8d09806 100644 --- a/src/common/ring_buffer.h +++ b/src/common/ring_buffer.h @@ -19,15 +19,14 @@ namespace Common {  /// SPSC ring buffer  /// @tparam T            Element type  /// @tparam capacity     Number of slots in ring buffer -/// @tparam granularity  Slot size in terms of number of elements -template <typename T, std::size_t capacity, std::size_t granularity = 1> +template <typename T, std::size_t capacity>  class RingBuffer { -    /// A "slot" is made of `granularity` elements of `T`. -    static constexpr std::size_t slot_size = granularity * sizeof(T); +    /// A "slot" is made of a single `T`. +    static constexpr std::size_t slot_size = sizeof(T);      // T must be safely memcpy-able and have a trivial default constructor.      static_assert(std::is_trivial_v<T>);      // Ensure capacity is sensible. -    static_assert(capacity < std::numeric_limits<std::size_t>::max() / 2 / granularity); +    static_assert(capacity < std::numeric_limits<std::size_t>::max() / 2);      static_assert((capacity & (capacity - 1)) == 0, "capacity must be a power of two");      // Ensure lock-free.      static_assert(std::atomic_size_t::is_always_lock_free); @@ -47,7 +46,7 @@ public:          const std::size_t second_copy = push_count - first_copy;          const char* in = static_cast<const char*>(new_slots); -        std::memcpy(m_data.data() + pos * granularity, in, first_copy * slot_size); +        std::memcpy(m_data.data() + pos, in, first_copy * slot_size);          in += first_copy * slot_size;          std::memcpy(m_data.data(), in, second_copy * slot_size); @@ -74,7 +73,7 @@ public:          const std::size_t second_copy = pop_count - first_copy;          char* out = static_cast<char*>(output); -        std::memcpy(out, m_data.data() + pos * granularity, first_copy * slot_size); +        std::memcpy(out, m_data.data() + pos, first_copy * slot_size);          out += first_copy * slot_size;          std::memcpy(out, m_data.data(), second_copy * slot_size); @@ -84,9 +83,9 @@ public:      }      std::vector<T> Pop(std::size_t max_slots = ~std::size_t(0)) { -        std::vector<T> out(std::min(max_slots, capacity) * granularity); -        const std::size_t count = Pop(out.data(), out.size() / granularity); -        out.resize(count * granularity); +        std::vector<T> out(std::min(max_slots, capacity)); +        const std::size_t count = Pop(out.data(), out.size()); +        out.resize(count);          return out;      } @@ -113,7 +112,7 @@ private:      alignas(128) std::atomic_size_t m_write_index{0};  #endif -    std::array<T, granularity * capacity> m_data; +    std::array<T, capacity> m_data;  };  } // namespace Common | 
