/src/S2OPC/src/Common/helpers/sopc_circular_queue.c
Line | Count | Source |
1 | | /* |
2 | | * Licensed to Systerel under one or more contributor license |
3 | | * agreements. See the NOTICE file distributed with this work |
4 | | * for additional information regarding copyright ownership. |
5 | | * Systerel licenses this file to you under the Apache |
6 | | * License, Version 2.0 (the "License"); you may not use this |
7 | | * file except in compliance with the License. You may obtain |
8 | | * a copy of the License at |
9 | | * |
10 | | * http://www.apache.org/licenses/LICENSE-2.0 |
11 | | * |
12 | | * Unless required by applicable law or agreed to in writing, |
13 | | * software distributed under the License is distributed on an |
14 | | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
15 | | * KIND, either express or implied. See the License for the |
16 | | * specific language governing permissions and limitations |
17 | | * under the License. |
18 | | */ |
19 | | |
20 | | /** |
21 | | * \file sopc_circular_queue.c |
22 | | * \brief Fixed-capacity circular buffer implementing SOPC_CircularQueue. |
23 | | * |
24 | | * Layout of the ring (indices into \c slots, modulo \c allocatedCap): |
25 | | * - Occupied range is \c length consecutive slots starting at \c head |
26 | | * - Append writes at index (head + length) modulo allocatedCap (computed without division) |
27 | | * - Prepend moves head one step backward then writes at the new head |
28 | | * - Logical order head -> tail wraps when head + length exceeds allocatedCap |
29 | | */ |
30 | | |
31 | | #include "sopc_circular_queue.h" |
32 | | |
33 | | #include "sopc_mem_alloc.h" |
34 | | |
35 | | SOPC_CircularQueue* SOPC_CircularQueue_Create(size_t sizeMax) |
36 | 0 | { |
37 | | /* Unlimited capacity (sizeMax == 0) is intentionally unsupported. */ |
38 | 0 | if (0 == sizeMax || sizeMax > UINT32_MAX) |
39 | 0 | { |
40 | 0 | return NULL; |
41 | 0 | } |
42 | | |
43 | 0 | SOPC_CircularQueue* queue = SOPC_Calloc(1, sizeof(SOPC_CircularQueue)); |
44 | 0 | if (NULL == queue) |
45 | 0 | { |
46 | 0 | return NULL; |
47 | 0 | } |
48 | | |
49 | 0 | queue->slots = SOPC_Calloc(sizeMax, sizeof(uintptr_t)); |
50 | 0 | if (NULL == queue->slots) |
51 | 0 | { |
52 | 0 | SOPC_Free(queue); |
53 | 0 | return NULL; |
54 | 0 | } |
55 | | |
56 | | /* head and length remain 0 from Calloc; physical size matches capacity. */ |
57 | 0 | queue->maxLength = (uint32_t) sizeMax; |
58 | 0 | queue->allocatedCap = (uint32_t) sizeMax; |
59 | 0 | return queue; |
60 | 0 | } |
61 | | |
62 | | uintptr_t SOPC_CircularQueue_Prepend(SOPC_CircularQueue* queue, uintptr_t value) |
63 | 0 | { |
64 | 0 | if (NULL == queue || 0 == value || queue->length >= queue->maxLength) |
65 | 0 | { |
66 | 0 | return 0; |
67 | 0 | } |
68 | | |
69 | | /* Move head one step backward in the ring (wrap to the last slot). */ |
70 | 0 | queue->head = (0 == queue->head) ? queue->allocatedCap - 1 : queue->head - 1; |
71 | 0 | queue->slots[queue->head] = value; |
72 | 0 | queue->length++; |
73 | 0 | return value; |
74 | 0 | } |
75 | | |
76 | | uintptr_t SOPC_CircularQueue_Append(SOPC_CircularQueue* queue, uintptr_t value) |
77 | 0 | { |
78 | 0 | if (NULL == queue || 0 == value || queue->length >= queue->maxLength) |
79 | 0 | { |
80 | 0 | return 0; |
81 | 0 | } |
82 | | |
83 | | /* Next free slot is immediately after the last live element (may wrap). */ |
84 | 0 | uint32_t idx = SOPC_CircularQueue_Internal_SlotIndex(queue, queue->length); |
85 | 0 | queue->slots[idx] = value; |
86 | 0 | queue->length++; |
87 | 0 | return value; |
88 | 0 | } |
89 | | |
90 | | uintptr_t SOPC_CircularQueue_PopHead(SOPC_CircularQueue* queue) |
91 | 0 | { |
92 | 0 | if (NULL == queue || 0 == queue->length) |
93 | 0 | { |
94 | 0 | return 0; |
95 | 0 | } |
96 | | |
97 | 0 | uintptr_t value = queue->slots[queue->head]; |
98 | 0 | queue->slots[queue->head] = 0; |
99 | | /* Advance head past the removed element; wrap at allocatedCap. */ |
100 | 0 | queue->head = SOPC_CircularQueue_Internal_SlotIndex(queue, 1); |
101 | 0 | queue->length--; |
102 | 0 | return value; |
103 | 0 | } |
104 | | |
105 | | uintptr_t SOPC_CircularQueue_PopLast(SOPC_CircularQueue* queue) |
106 | 0 | { |
107 | 0 | if (NULL == queue || 0 == queue->length) |
108 | 0 | { |
109 | 0 | return 0; |
110 | 0 | } |
111 | | |
112 | | /* Tail index is the last occupied slot: head + length - 1 (modulo). */ |
113 | 0 | uint32_t idx = SOPC_CircularQueue_Internal_SlotIndex(queue, queue->length - 1); |
114 | 0 | uintptr_t value = queue->slots[idx]; |
115 | 0 | queue->slots[idx] = 0; |
116 | 0 | queue->length--; |
117 | 0 | return value; |
118 | 0 | } |
119 | | |
120 | | uintptr_t SOPC_CircularQueue_GetHead(SOPC_CircularQueue* queue) |
121 | 0 | { |
122 | 0 | if (NULL == queue || 0 == queue->length) |
123 | 0 | { |
124 | 0 | return 0; |
125 | 0 | } |
126 | 0 | return queue->slots[queue->head]; |
127 | 0 | } |
128 | | |
129 | | uintptr_t SOPC_CircularQueue_GetLast(SOPC_CircularQueue* queue) |
130 | 0 | { |
131 | 0 | if (NULL == queue || 0 == queue->length) |
132 | 0 | { |
133 | 0 | return 0; |
134 | 0 | } |
135 | 0 | uint32_t idx = SOPC_CircularQueue_Internal_SlotIndex(queue, queue->length - 1); |
136 | 0 | return queue->slots[idx]; |
137 | 0 | } |
138 | | |
139 | | void SOPC_CircularQueue_Apply(SOPC_CircularQueue* queue, void (*pFn)(uintptr_t val)) |
140 | 0 | { |
141 | 0 | if (NULL == queue || NULL == pFn || 0 == queue->length) |
142 | 0 | { |
143 | 0 | return; |
144 | 0 | } |
145 | | |
146 | | /* Walk logical order from oldest to newest; id is always 0 (not stored). */ |
147 | 0 | for (uint32_t i = 0; i < queue->length; i++) |
148 | 0 | { |
149 | 0 | uint32_t idx = SOPC_CircularQueue_Internal_SlotIndex(queue, i); |
150 | 0 | pFn(queue->slots[idx]); |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | | SOPC_CircularQueueIterator SOPC_CircularQueue_GetIterator(SOPC_CircularQueue* queue) |
155 | 0 | { |
156 | 0 | SOPC_CircularQueueIterator it = {.queue = queue, .nextOffset = 0}; |
157 | 0 | return it; |
158 | 0 | } |
159 | | |
160 | | uintptr_t SOPC_CircularQueue_RemoveAt(SOPC_CircularQueue* queue, uint32_t index) |
161 | 0 | { |
162 | 0 | if (NULL == queue || index >= queue->length) |
163 | 0 | { |
164 | 0 | return 0; |
165 | 0 | } |
166 | | |
167 | 0 | uint32_t slot = SOPC_CircularQueue_Internal_SlotIndex(queue, index); |
168 | 0 | uintptr_t value = queue->slots[slot]; |
169 | | |
170 | | /* Close the gap: shift later elements one step toward the head. */ |
171 | 0 | for (uint32_t j = index; j + 1 < queue->length; j++) |
172 | 0 | { |
173 | 0 | uint32_t cur = SOPC_CircularQueue_Internal_SlotIndex(queue, j); |
174 | 0 | uint32_t nxt = SOPC_CircularQueue_Internal_SlotIndex(queue, j + 1); |
175 | 0 | queue->slots[cur] = queue->slots[nxt]; |
176 | 0 | } |
177 | 0 | uint32_t last = SOPC_CircularQueue_Internal_SlotIndex(queue, queue->length - 1); |
178 | 0 | queue->slots[last] = 0; |
179 | 0 | queue->length--; |
180 | 0 | return value; |
181 | 0 | } |
182 | | |
183 | | void SOPC_CircularQueue_Clear(SOPC_CircularQueue* queue) |
184 | 0 | { |
185 | 0 | if (NULL == queue) |
186 | 0 | { |
187 | 0 | return; |
188 | 0 | } |
189 | | /* Drop logical content only; values are not freed (caller Apply first). |
190 | | * Stale slot data is ignored while length is 0 and overwritten on next write. */ |
191 | 0 | queue->head = 0; |
192 | 0 | queue->length = 0; |
193 | 0 | } |
194 | | |
195 | | void SOPC_CircularQueue_Delete(SOPC_CircularQueue* queue) |
196 | 0 | { |
197 | 0 | if (NULL == queue) |
198 | 0 | { |
199 | 0 | return; |
200 | 0 | } |
201 | 0 | SOPC_Free(queue->slots); |
202 | 0 | queue->slots = NULL; |
203 | 0 | SOPC_Free(queue); |
204 | 0 | } |
205 | | |
206 | | uint32_t SOPC_CircularQueue_GetCapacity(SOPC_CircularQueue* queue) |
207 | 0 | { |
208 | 0 | if (NULL == queue) |
209 | 0 | { |
210 | 0 | return 0; |
211 | 0 | } |
212 | 0 | return queue->maxLength; |
213 | 0 | } |
214 | | |
215 | | bool SOPC_CircularQueue_SetCapacity(SOPC_CircularQueue* queue, size_t sizeMax) |
216 | 0 | { |
217 | 0 | if (NULL == queue || 0 == sizeMax || sizeMax > UINT32_MAX) |
218 | 0 | { |
219 | 0 | return false; |
220 | 0 | } |
221 | | /* Cannot set a capacity below the number of elements already stored. */ |
222 | 0 | if (queue->length > sizeMax) |
223 | 0 | { |
224 | 0 | return false; |
225 | 0 | } |
226 | | |
227 | 0 | uint32_t newMax = (uint32_t) sizeMax; |
228 | |
|
229 | 0 | if (newMax > queue->allocatedCap) |
230 | 0 | { |
231 | | /* Grow physical buffer: copy the (possibly wrapped) ring into a |
232 | | * contiguous layout starting at index 0 so head can be reset. */ |
233 | 0 | uintptr_t* newSlots = SOPC_Calloc(newMax, sizeof(uintptr_t)); |
234 | 0 | if (NULL == newSlots) |
235 | 0 | { |
236 | 0 | return false; |
237 | 0 | } |
238 | 0 | for (uint32_t i = 0; i < queue->length; i++) |
239 | 0 | { |
240 | 0 | newSlots[i] = queue->slots[SOPC_CircularQueue_Internal_SlotIndex(queue, i)]; |
241 | 0 | } |
242 | 0 | SOPC_Free(queue->slots); |
243 | 0 | queue->slots = newSlots; |
244 | 0 | queue->head = 0; |
245 | 0 | queue->allocatedCap = newMax; |
246 | 0 | } |
247 | | /* Shrink path: only lower maxLength; keep allocatedCap so later growth |
248 | | * within the previous physical size needs no realloc. */ |
249 | | |
250 | 0 | queue->maxLength = newMax; |
251 | | return true; |
252 | 0 | } |