/home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/circularBuffer.hpp Source File

HiCR: /home/runner/work/HiCR/HiCR/include/hicr/frontends/channel/circularBuffer.hpp Source File
HiCR
circularBuffer.hpp
Go to the documentation of this file.
1/*
2 * Copyright 2025 Huawei Technologies Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
24#pragma once
25
27
28namespace HiCR::channel
29{
30
42{
43 public:
44
52 CircularBuffer(size_t capacity, __volatile__ size_t *headAdvanceCounter, __volatile__ size_t *tailAdvanceCounter)
53 : _capacity(capacity),
54 _headAdvanceCounter(headAdvanceCounter),
55 _tailAdvanceCounter(tailAdvanceCounter)
56
57 {}
58
59 virtual ~CircularBuffer() = default;
60
72 [[nodiscard]] __INLINE__ size_t getHeadPosition() const noexcept { return *_headAdvanceCounter % _capacity; }
73
85 [[nodiscard]] __INLINE__ size_t getTailPosition() const noexcept { return *_tailAdvanceCounter % _capacity; }
86
93 __INLINE__ void advanceHead(const size_t n = 1)
94 {
95 // Current depth
96 const auto curDepth = getDepth();
97
98 // Calculating new depth
99 const auto newDepth = curDepth + n;
100
101 // Sanity check
102 if (newDepth > _capacity)
103 HICR_THROW_FATAL("New buffer depth (_depth (%lu) + n (%lu) = %lu) exceeded capacity (%lu) on increase. This is probably a bug in HiCR.\n", curDepth, n, newDepth, _capacity);
104
105 // Advance head
106 *_headAdvanceCounter = *_headAdvanceCounter + n;
107 }
108
115 __INLINE__ void advanceTail(const size_t n = 1)
116 {
117 // Current depth
118 const auto curDepth = getDepth();
119
120 // Sanity check
121 if (n > curDepth)
122 HICR_THROW_FATAL("Circular buffer depth (%lu) smaller than number of elements (%lu) to decrease on advance tail. This is probably a bug in HiCR.\n", curDepth, n);
123
124 // Advance tail
125 *_tailAdvanceCounter = *_tailAdvanceCounter + n;
126 }
127
135 [[nodiscard]] __INLINE__ size_t getCapacity() const noexcept { return _capacity; }
136
148 [[nodiscard]] __INLINE__ size_t getDepth() const noexcept { return calculateDepth(*_headAdvanceCounter, *_tailAdvanceCounter); }
149
158 [[nodiscard]] __INLINE__ bool isFull() const noexcept { return getDepth() == _capacity; }
159
168 [[nodiscard]] __INLINE__ bool isEmpty() const noexcept { return *_headAdvanceCounter == *_tailAdvanceCounter; }
169
170 private:
171
175 const size_t _capacity;
176
180 __volatile__ size_t *const _headAdvanceCounter;
181
185 __volatile__ size_t *const _tailAdvanceCounter;
186
187 protected:
188
195 __INLINE__ static size_t calculateDepth(const size_t headAdvanceCounter, const size_t tailAdvanceCounter)
196 {
197 if (headAdvanceCounter < tailAdvanceCounter)
198 {
199 HICR_THROW_FATAL("Head index (%lu) < tail index (%lu). This is a critical bug in HiCR!\n", headAdvanceCounter, tailAdvanceCounter);
200 }
201 return headAdvanceCounter - tailAdvanceCounter;
202 }
203};
204
205} // namespace HiCR::channel
Generic class type for circular buffer.
Definition circularBuffer.hpp:42
__INLINE__ bool isEmpty() const noexcept
Definition circularBuffer.hpp:168
__INLINE__ size_t getCapacity() const noexcept
Definition circularBuffer.hpp:135
__INLINE__ size_t getDepth() const noexcept
Definition circularBuffer.hpp:148
static __INLINE__ size_t calculateDepth(const size_t headAdvanceCounter, const size_t tailAdvanceCounter)
Definition circularBuffer.hpp:195
__INLINE__ size_t getTailPosition() const noexcept
Definition circularBuffer.hpp:85
__INLINE__ void advanceHead(const size_t n=1)
Definition circularBuffer.hpp:93
CircularBuffer(size_t capacity, __volatile__ size_t *headAdvanceCounter, __volatile__ size_t *tailAdvanceCounter)
Definition circularBuffer.hpp:52
__INLINE__ void advanceTail(const size_t n=1)
Definition circularBuffer.hpp:115
__INLINE__ bool isFull() const noexcept
Definition circularBuffer.hpp:158
__INLINE__ size_t getHeadPosition() const noexcept
Definition circularBuffer.hpp:72
Provides a failure model and corresponding exception classes.
#define HICR_THROW_FATAL(...)
Definition exceptions.hpp:81