GCC Code Coverage Report


Directory: ./
File: PriorityQueue.cpp
Date: 2026-09-23 22:12:24
Exec Total Coverage
Lines: 128 161 79.5%
Functions: 11 13 84.6%
Branches: 53 90 58.9%

Line Branch Exec Source
1 // ======================================================================
2 // \title Os/Generic/PriorityQueue.cpp
3 // \brief priority queue implementation for Os::Queue
4 // ======================================================================
5 #include "Os/Generic/PriorityQueue.hpp"
6 #include <algorithm>
7 #include <cstring>
8 #include "Fw/LanguageHelpers.hpp"
9 #include "Fw/Types/Assert.hpp"
10 #include "Fw/Types/MemAllocator.hpp"
11 #include "config/MemoryAllocatorTypeEnumAc.hpp"
12
13 namespace Os {
14 namespace Generic {
15
16 7579 FwSizeType PriorityQueueHandle ::find_index() {
17 7579 FW_ASSERT(this->m_depth > 0);
18 7579 FwSizeType index = this->m_indices[this->m_startIndex % this->m_depth];
19 7579 this->m_startIndex = (this->m_startIndex + 1) % this->m_depth;
20 7579 return index;
21 }
22
23 7516 void PriorityQueueHandle ::return_index(FwSizeType index) {
24 7516 FW_ASSERT(this->m_depth > 0);
25 7516 this->m_indices[this->m_stopIndex % this->m_depth] = index;
26 7516 this->m_stopIndex = (this->m_stopIndex + 1) % this->m_depth;
27 7516 }
28
29 7570 void PriorityQueueHandle ::store_data(FwSizeType index, const U8* data, FwSizeType size) {
30 7570 FW_ASSERT(size <= this->m_maxSize);
31 7570 FW_ASSERT(index < this->m_depth);
32
33 7570 FwSizeType offset = this->m_maxSize * index;
34 7570 (void)::memcpy(this->m_data + offset, data, static_cast<size_t>(size));
35 7570 this->m_sizes[index] = size;
36 7570 }
37
38 7558 void PriorityQueueHandle ::load_data(FwSizeType index, U8* destination, FwSizeType size) {
39 7558 FW_ASSERT(size <= this->m_maxSize);
40 7558 FW_ASSERT(index < this->m_depth);
41 7558 FwSizeType offset = this->m_maxSize * index;
42 7558 (void)::memcpy(destination, this->m_data + offset, static_cast<size_t>(size));
43 7558 }
44
45 58 PriorityQueue::~PriorityQueue() {}
46
47 29 QueueInterface::Status PriorityQueue::create(FwEnumStoreType id,
48 const Fw::ConstStringBase& name,
49 FwSizeType depth,
50 FwSizeType messageSize) {
51 29 const FwEnumStoreType identifier = id;
52 29 QueueInterface::Status status = Os::QueueInterface::Status::OP_OK;
53 // Ensure we are created exactly once
54 29 FW_ASSERT(this->m_handle.m_indices == nullptr);
55 29 FW_ASSERT(this->m_handle.m_sizes == nullptr);
56 29 FW_ASSERT(this->m_handle.m_data == nullptr);
57
58 // Get the memory allocator configured for priority queues
59
3/3
✓ Branch 1 taken 29 times.
✓ Branch 4 taken 29 times.
✓ Branch 7 taken 29 times.
29 Fw::MemAllocator& allocator = Fw::MemAllocatorRegistry::getInstance().getAnAllocator(
60 Fw::MemoryAllocation::MemoryAllocatorType::OS_GENERIC_PRIORITY_QUEUE);
61
62 // Allocate indices list
63 29 void* allocation = nullptr;
64 29 FwSizeType size = 0;
65 29 FwSizeType* indices = nullptr;
66 29 FwSizeType* sizes = nullptr;
67 29 U8* data = nullptr;
68 29 U8* heap_pointer = nullptr;
69
70 // Prevent integer overflow when computing allocation size (depth * sizeof(FwSizeType))
71 29 FW_ASSERT(depth < std::numeric_limits<FwSizeType>::max() / sizeof(FwSizeType));
72
73 // Allocate indices list and construct it when valid
74 29 size = depth * sizeof(FwSizeType);
75
1/1
✓ Branch 1 taken 29 times.
29 allocation = allocator.allocate(identifier, size, alignof(FwSizeType));
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (allocation == nullptr) {
77 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
78
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 } else if (size < (depth * sizeof(FwSizeType))) {
79 ✗ allocator.deallocate(identifier, allocation);
80 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
81 } else {
82
1/1
✓ Branch 2 taken 29 times.
29 indices = Fw::arrayPlacementNew<FwSizeType>(Fw::ByteArray(static_cast<U8*>(allocation), size), depth);
83 }
84
85 // Allocate sizes list and construct it when valid
86
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (status == QueueInterface::Status::OP_OK) {
87 29 size = depth * sizeof(FwSizeType);
88
1/1
✓ Branch 1 taken 29 times.
29 allocation = allocator.allocate(identifier, size, alignof(FwSizeType));
89
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (allocation == nullptr) {
90 ✗ allocator.deallocate(identifier, indices);
91 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 } else if (size < (depth * sizeof(FwSizeType))) {
93 ✗ allocator.deallocate(identifier, indices);
94 ✗ allocator.deallocate(identifier, allocation);
95 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
96 } else {
97
1/1
✓ Branch 2 taken 29 times.
29 sizes = Fw::arrayPlacementNew<FwSizeType>(Fw::ByteArray(static_cast<U8*>(allocation), size), depth);
98 }
99 }
100 // Allocate data
101
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (status == QueueInterface::Status::OP_OK) {
102 // Prevent integer overflow when computing allocation size (depth * messageSize)
103 29 FW_ASSERT((depth == 0) || (messageSize <= std::numeric_limits<FwSizeType>::max() / depth));
104 29 size = depth * messageSize;
105
1/1
✓ Branch 1 taken 29 times.
29 allocation = allocator.allocate(identifier, size, alignof(U8));
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (allocation == nullptr) {
107 ✗ allocator.deallocate(identifier, indices);
108 ✗ allocator.deallocate(identifier, sizes);
109 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 } else if (size < (depth * messageSize)) {
111 ✗ allocator.deallocate(identifier, indices);
112 ✗ allocator.deallocate(identifier, sizes);
113 ✗ allocator.deallocate(identifier, allocation);
114 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
115 } else {
116 29 data = static_cast<U8*>(allocation);
117 }
118 }
119 // Allocate data for max heap
120
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (status == QueueInterface::Status::OP_OK) {
121 29 size = Types::MaxHeap::ELEMENT_SIZE * depth;
122
1/1
✓ Branch 1 taken 29 times.
29 allocation = allocator.allocate(identifier, size, Types::MaxHeap::ALIGNMENT);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 if (allocation == nullptr) {
124 ✗ allocator.deallocate(identifier, indices);
125 ✗ allocator.deallocate(identifier, sizes);
126 ✗ allocator.deallocate(identifier, data);
127 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 29 times.
29 } else if (size < (Types::MaxHeap::ELEMENT_SIZE * depth)) {
129 ✗ allocator.deallocate(identifier, indices);
130 ✗ allocator.deallocate(identifier, sizes);
131 ✗ allocator.deallocate(identifier, data);
132 ✗ allocator.deallocate(identifier, allocation);
133 ✗ status = QueueInterface::Status::ALLOCATION_FAILED;
134 } else {
135 29 heap_pointer = static_cast<U8*>(allocation);
136
1/1
✓ Branch 2 taken 29 times.
29 this->m_handle.m_heap.create(depth, Fw::ByteArray(static_cast<U8*>(allocation), size));
137 }
138 }
139 // Set up structures when all allocations succeeded
140
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (status == QueueInterface::Status::OP_OK) {
141 // Assign initial indices and sizes
142
2/2
✓ Branch 0 taken 345 times.
✓ Branch 1 taken 29 times.
374 for (FwSizeType i = 0; i < depth; i++) {
143 345 indices[i] = i;
144 345 sizes[i] = 0;
145 }
146 // Set local tracking variables
147 29 this->m_handle.m_id = id;
148 29 this->m_handle.m_maxSize = messageSize;
149 29 this->m_handle.m_indices = indices;
150 29 this->m_handle.m_data = data;
151 29 this->m_handle.m_sizes = sizes;
152 29 this->m_handle.m_heap_pointer = heap_pointer;
153 29 this->m_handle.m_startIndex = 0;
154 29 this->m_handle.m_stopIndex = 0;
155 29 this->m_handle.m_depth = depth;
156 29 this->m_handle.m_highMark = 0;
157 }
158 29 return status;
159 }
160
161 29 void PriorityQueue::teardown() {
162 29 this->teardownInternal();
163 29 }
164
165 29 void PriorityQueue::teardownInternal() {
166
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 if (this->m_handle.m_data != nullptr) {
167 // All backing arrays are allocated together in create()
168 29 FW_ASSERT(this->m_handle.m_indices != nullptr);
169 29 FW_ASSERT(this->m_handle.m_sizes != nullptr);
170 29 FW_ASSERT(this->m_handle.m_heap_pointer != nullptr);
171 29 const FwEnumStoreType identifier = this->m_handle.m_id;
172
2/2
✓ Branch 2 taken 29 times.
✓ Branch 5 taken 29 times.
29 Fw::MemAllocator& allocator = Fw::MemAllocatorRegistry::getInstance().getAnAllocator(
173 Fw::MemoryAllocation::MemoryAllocatorType::OS_GENERIC_PRIORITY_QUEUE);
174 29 allocator.deallocate(identifier, this->m_handle.m_data);
175 29 allocator.deallocate(identifier, this->m_handle.m_indices);
176 29 allocator.deallocate(identifier, this->m_handle.m_sizes);
177 29 this->m_handle.m_heap.teardown();
178 29 allocator.deallocate(identifier, this->m_handle.m_heap_pointer);
179
180 // Set these pointers to nullptr
181 29 this->m_handle.m_data = nullptr;
182 29 this->m_handle.m_indices = nullptr;
183 29 this->m_handle.m_sizes = nullptr;
184 }
185 29 }
186
187 7578 QueueInterface::Status PriorityQueue::send(const U8* buffer,
188 FwSizeType size,
189 FwQueuePriorityType priority,
190 QueueInterface::BlockingType blockType) {
191 // Check for sizing problem before locking
192
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7578 times.
7578 if (size > this->m_handle.m_maxSize) {
193 ✗ return QueueInterface::Status::SIZE_MISMATCH;
194 }
195 // Artificial block scope for scope lock ensuring an unlock in all cases and ensuring an unlock before notify
196 {
197
1/1
✓ Branch 1 taken 7577 times.
7578 Os::ScopeLock lock(this->m_handle.m_data_lock);
198
3/7
✓ Branch 1 taken 7579 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7579 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 7579 times.
7577 if (this->m_handle.m_heap.isFull() and blockType == BlockingType::NONBLOCKING) {
199 ✗ return QueueInterface::Status::FULL;
200 }
201 // Will loop and block until full is false
202 // @non-terminating@: condition-variable wait loop
203
2/3
✓ Branch 1 taken 7575 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7575 times.
7579 while (this->m_handle.m_heap.isFull()) {
204 ✗ this->m_handle.m_full.wait(this->m_handle.m_data_lock);
205 }
206
1/1
✓ Branch 1 taken 7578 times.
7575 FwSizeType index = this->m_handle.find_index();
207
208 // Space must exist, push must work
209
1/1
✓ Branch 1 taken 7573 times.
7578 const bool pushed = this->m_handle.m_heap.push(priority, index);
210 7573 FW_ASSERT(pushed);
211
1/1
✓ Branch 1 taken 7561 times.
7573 this->m_handle.store_data(index, buffer, size);
212 7561 this->m_handle.m_sizes[index] = size;
213
1/1
✓ Branch 1 taken 7561 times.
7561 this->m_handle.m_highMark = std::max(this->m_handle.m_highMark, this->getMessagesAvailable());
214 7564 }
215 7578 this->m_handle.m_empty.notify();
216 7583 return QueueInterface::Status::OP_OK;
217 }
218
219 8760 QueueInterface::Status PriorityQueue::receive(U8* destination,
220 FwSizeType capacity,
221 QueueInterface::BlockingType blockType,
222 FwSizeType& actualSize,
223 FwQueuePriorityType& priority) {
224 {
225
1/1
✓ Branch 1 taken 8763 times.
8760 Os::ScopeLock lock(this->m_handle.m_data_lock);
226
7/7
✓ Branch 1 taken 8759 times.
✓ Branch 3 taken 6309 times.
✓ Branch 4 taken 2450 times.
✓ Branch 5 taken 1205 times.
✓ Branch 6 taken 5104 times.
✓ Branch 7 taken 1205 times.
✓ Branch 8 taken 7554 times.
8763 if (this->m_handle.m_heap.isEmpty() and blockType == BlockingType::NONBLOCKING) {
227 1205 return QueueInterface::Status::EMPTY;
228 }
229 // Loop and lock while empty
230 // @non-terminating@: condition-variable wait loop
231
3/3
✓ Branch 1 taken 12590 times.
✓ Branch 3 taken 5105 times.
✓ Branch 4 taken 7485 times.
12638 while (this->m_handle.m_heap.isEmpty()) {
232
1/1
✓ Branch 1 taken 5084 times.
5105 this->m_handle.m_empty.wait(this->m_handle.m_data_lock);
233 }
234
235 FwSizeType index;
236 // Message must exist, so pop must pass and size must be valid
237
1/1
✓ Branch 1 taken 7533 times.
7485 const bool popped = this->m_handle.m_heap.pop(priority, index);
238 7533 FW_ASSERT(popped);
239 7533 actualSize = this->m_handle.m_sizes[index];
240 7533 FW_ASSERT(actualSize <= capacity);
241
1/1
✓ Branch 1 taken 7540 times.
7533 this->m_handle.load_data(index, destination, actualSize);
242
1/1
✓ Branch 1 taken 7477 times.
7540 this->m_handle.return_index(index);
243 8682 }
244 7553 this->m_handle.m_full.notify();
245 7550 return QueueInterface::Status::OP_OK;
246 }
247
248 7560 FwSizeType PriorityQueue::getMessagesAvailable() const {
249 7560 return this->m_handle.m_heap.getSize();
250 }
251
252 ✗ FwSizeType PriorityQueue::getMessageHighWaterMark() const {
253 // Safe to cast away const in this context because scope lock will restore unlocked state on return
254 ✗ Os::ScopeLock lock(const_cast<Mutex&>(this->m_handle.m_data_lock));
255 ✗ return this->m_handle.m_highMark;
256 ✗ }
257
258 ✗ QueueHandle* PriorityQueue::getHandle() {
259 ✗ return &this->m_handle;
260 }
261
262 } // namespace Generic
263 } // namespace Os
264