| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* @file queue.c |
| 3 |
|
|
* @brief Generic FIFO queue implementation for embedded safety-critical use. |
| 4 |
|
|
* @version 1.0.2 |
| 5 |
|
|
* @date 2025-10-26 |
| 6 |
|
|
* |
| 7 |
|
|
* @details |
| 8 |
|
|
* Provides deterministic enqueue/dequeue operations on a caller-supplied |
| 9 |
|
|
* memory buffer. Implementation uses explicit byte-wise copying and avoids |
| 10 |
|
|
* standard library dependencies to ensure deterministic behavior. |
| 11 |
|
|
* |
| 12 |
|
|
* MISRA Deviation: DV-QUEUE-001 (Rule 11.4) |
| 13 |
|
|
* Controlled cast from void* to uint8_t* for raw byte access. |
| 14 |
|
|
* Safe because data is not type-reinterpreted. |
| 15 |
|
|
* |
| 16 |
|
|
* @note |
| 17 |
|
|
* Internal helper function `copy_bytes(uint8_t*, const uint8_t*, uint16_t)` is tested |
| 18 |
|
|
* indirectly via DV_QUEUE_001 unit tests, including: |
| 19 |
|
|
* - pushing/popping int, struct, char array elements, |
| 20 |
|
|
* - wrap-around behavior, |
| 21 |
|
|
* - zero-byte edge case, |
| 22 |
|
|
* - NULL pointer handling. |
| 23 |
|
|
*/ |
| 24 |
|
|
|
| 25 |
|
|
#include "queue.h" |
| 26 |
|
|
|
| 27 |
|
|
/* PRIVATE macro controls function visibility: |
| 28 |
|
|
* - static in production builds (default) |
| 29 |
|
|
* - global in UNIT_TESTS build for test coverage |
| 30 |
|
|
* This allows testing of internal functions without exposing them in production. |
| 31 |
|
|
*/ |
| 32 |
|
|
#ifdef UNIT_TESTS |
| 33 |
|
|
#define PRIVATE |
| 34 |
|
|
#else |
| 35 |
|
|
#define PRIVATE static |
| 36 |
|
|
#endif |
| 37 |
|
|
|
| 38 |
|
|
/* Internal helper: byte-wise deterministic copy. |
| 39 |
|
|
* @note MISRA Deviation DV-QUEUE-001 applies here. |
| 40 |
|
|
* Do not expose externally; tested via queue_push/queue_pop. */ |
| 41 |
|
|
PRIVATE void copy_bytes(uint8_t *dst, const uint8_t *src, uint16_t size); |
| 42 |
|
|
|
| 43 |
|
|
/* -------------------------- */ |
| 44 |
|
|
/* Queue API implementation */ |
| 45 |
|
|
/* -------------------------- */ |
| 46 |
|
|
|
| 47 |
|
35 |
queue_status_t queue_init(queue_t *q, void *buffer, uint16_t element_size, uint16_t capacity) |
| 48 |
|
|
{ |
| 49 |
4/4
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 2 times.
|
35 |
if ((q == (queue_t *)0) || |
| 50 |
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 2 times.
|
31 |
(buffer == (void *)0) || |
| 51 |
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27 times.
|
29 |
(element_size == 0U) || |
| 52 |
|
|
(capacity == 0U)) |
| 53 |
|
|
{ |
| 54 |
|
8 |
return QUEUE_ERROR; |
| 55 |
|
|
} |
| 56 |
|
|
|
| 57 |
|
27 |
q->buffer = buffer; |
| 58 |
|
27 |
q->element_size = element_size; |
| 59 |
|
27 |
q->capacity = capacity; |
| 60 |
|
27 |
q->head = 0U; |
| 61 |
|
27 |
q->tail = 0U; |
| 62 |
|
27 |
q->count = 0U; |
| 63 |
|
|
|
| 64 |
|
27 |
return QUEUE_OK; |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
31 |
queue_status_t queue_push(queue_t *q, const void *item) |
| 68 |
|
|
{ |
| 69 |
3/4
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 29 times.
|
31 |
if ((q == (queue_t *)0) || (item == (const void *)0)) |
| 70 |
|
|
{ |
| 71 |
|
2 |
return QUEUE_ERROR; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 28 times.
|
29 |
if (q->count >= q->capacity) |
| 75 |
|
|
{ |
| 76 |
|
1 |
return QUEUE_FULL; |
| 77 |
|
|
} |
| 78 |
|
|
|
| 79 |
|
|
/* MISRA Deviation DV-QUEUE-001: controlled cast for byte-wise copy */ |
| 80 |
|
28 |
uint8_t *base = (uint8_t *)q->buffer; |
| 81 |
|
|
|
| 82 |
|
28 |
const uint32_t offset = (uint32_t)q->tail * (uint32_t)q->element_size; |
| 83 |
|
28 |
copy_bytes(&base[offset], (const uint8_t *)item, q->element_size); |
| 84 |
|
|
|
| 85 |
|
28 |
q->tail = (uint16_t)(((uint32_t)q->tail + 1U) % (uint32_t)q->capacity); |
| 86 |
|
28 |
q->count = (uint16_t)((uint32_t)q->count + 1U); |
| 87 |
|
|
|
| 88 |
|
28 |
return QUEUE_OK; |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
21 |
queue_status_t queue_pop(queue_t *q, void *item) |
| 92 |
|
|
{ |
| 93 |
3/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 19 times.
|
21 |
if ((q == (queue_t *)0) || (item == (void *)0)) |
| 94 |
|
|
{ |
| 95 |
|
2 |
return QUEUE_ERROR; |
| 96 |
|
|
} |
| 97 |
|
|
|
| 98 |
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 18 times.
|
19 |
if (q->count == 0U) |
| 99 |
|
|
{ |
| 100 |
|
1 |
return QUEUE_EMPTY; |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
|
|
/* MISRA Deviation DV-QUEUE-001: controlled cast for byte-wise copy */ |
| 104 |
|
18 |
const uint8_t *base = (const uint8_t *)q->buffer; |
| 105 |
|
|
|
| 106 |
|
18 |
const uint32_t offset = (uint32_t)q->head * (uint32_t)q->element_size; |
| 107 |
|
18 |
copy_bytes((uint8_t *)item, &base[offset], q->element_size); |
| 108 |
|
|
|
| 109 |
|
18 |
q->head = (uint16_t)(((uint32_t)q->head + 1U) % (uint32_t)q->capacity); |
| 110 |
|
18 |
q->count = (uint16_t)((uint32_t)q->count - 1U); |
| 111 |
|
|
|
| 112 |
|
18 |
return QUEUE_OK; |
| 113 |
|
|
} |
| 114 |
|
|
|
| 115 |
|
7 |
bool queue_is_empty(const queue_t *q) |
| 116 |
|
|
{ |
| 117 |
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 2 times.
|
7 |
return (q == (const queue_t *)0) ? true : (q->count == 0U); |
| 118 |
|
|
} |
| 119 |
|
|
|
| 120 |
|
4 |
bool queue_is_full(const queue_t *q) |
| 121 |
|
|
{ |
| 122 |
3/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
|
4 |
return (q == (const queue_t *)0) ? false : (q->count == q->capacity); |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
/* -------------------------- */ |
| 126 |
|
|
/* Internal helper functions */ |
| 127 |
|
|
/* -------------------------- */ |
| 128 |
|
|
|
| 129 |
|
|
/** |
| 130 |
|
|
* @brief Deterministic byte-wise copy of memory. |
| 131 |
|
|
* @param[out] dst Destination buffer. |
| 132 |
|
|
* @param[in] src Source buffer. |
| 133 |
|
|
* @param[in] size Number of bytes to copy. |
| 134 |
|
|
* |
| 135 |
|
|
* @details |
| 136 |
|
|
* Used internally by queue_push/queue_pop to implement type-agnostic element storage. |
| 137 |
|
|
* The PRIVATE macro controls visibility: |
| 138 |
|
|
* - static in production builds |
| 139 |
|
|
* - non-static in UNIT_TESTS build for test coverage |
| 140 |
|
|
* |
| 141 |
|
|
* @note MISRA Deviation DV-QUEUE-001: controlled cast for raw memory access. |
| 142 |
|
|
* @note Branch with NULL pointers is exercised indirectly in unit tests DV_QUEUE_001. |
| 143 |
|
|
*/ |
| 144 |
|
49 |
PRIVATE void copy_bytes(uint8_t *dst, const uint8_t *src, uint16_t size) |
| 145 |
|
|
{ |
| 146 |
4/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 46 times.
|
49 |
if ((dst == (uint8_t *)0) || (src == (const uint8_t *)0)) |
| 147 |
|
|
{ |
| 148 |
|
3 |
return; |
| 149 |
|
|
} |
| 150 |
|
|
|
| 151 |
2/2
✓ Branch 0 taken 178 times.
✓ Branch 1 taken 46 times.
|
224 |
for (uint16_t i = 0U; i < size; i = (uint16_t)(i + 1U)) |
| 152 |
|
|
{ |
| 153 |
|
178 |
dst[i] = src[i]; |
| 154 |
|
|
} |
| 155 |
|
|
} |
| 156 |
|
|
|