Skip to content

queue_header

๐Ÿงฉ Queue Library (MISRA-C Compliant FIFO)

A deterministic, zero-dynamic-memory, and MISRA-C:2012 compliant FIFO queue with a documented controlled deviation (DV-QUEUE-001, Rule 11.4) โ€” designed for embedded and safety-critical applications (ISO 26262 / IEC 61508 / DO-178C).


GitHub License GitHub top language GitHub Release GitHub branch check runs CI Pipeline MISRA


๐Ÿงฉ Compliance Verification

The QUEUE_LIB project includes a complete, auditable verification chain ensuring traceable MISRA-C:2012 compliance, documentation quality, and code integrity.

Verification Area Tool / Method Evidence / Report
Static Analysis cppcheck (open-source) + gcc -Wall -Wextra -pedantic Verified code safety and type correctness
Formatting Rules clang-format Ensures consistent style and readability (MISRA ยง2.1)
Cyclomatic Complexity lizard Code Complexity Report
Code Coverage gcov + gcovr Coverage Report
Documentation doxygen Generated API Documentation
MISRA Deviation Record Manual documentation DV-QUEUE-001 (Rule 11.4)
Unit Tests Unity Framework Run locally and in CI (GitHub Actions)
Continuous Integration GitHub Actions Builds, tests, and publishes reports automatically

๐ŸŒ All reports and generated documentation are available online
and deployed automatically using GitHub Actions
๐Ÿ”— niwciu.github.io/QUEUE_LIB


๐Ÿš€ Key Features

  • โœ… Written in C99, MISRA-C:2012 compliant (1 controlled deviation โ€“ DV-QUEUE-001, Rule 11.4, see docs/compliance/MISRA_Deviations.md)
  • โœ… No dynamic memory allocation (static buffers only)
  • โœ… Deterministic execution time (O(element_size) per operation)
  • โœ… Type-agnostic โ€“ works with any struct or primitive type
  • โœ… Compatible with bare-metal, RTOS, and safety-critical systems
  • โœ… Unit tests ready (Unity framework) with branch and statement coverage for all queue operations including DV-QUEUE-001 edge cases
  • โœ… Controlled MISRA deviation fully documented and verified

๐Ÿ“ File Structure

/queue_lib/
โ”‚
โ”œโ”€โ”€ .github/
โ”‚   โ””โ”€โ”€ workflows
โ”‚       โ”œโ”€โ”€ CI_Pipeline.yml         # CI workflow
โ”‚       โ””โ”€โ”€ mkdocs-deploy.yml       # Library web page deploy workflow
โ”‚
โ”œโ”€โ”€ docs/                           # Files required for deploy library webpage and documentation
โ”œโ”€โ”€ examples/                       # Ready to run examples
โ”‚   โ”œโ”€โ”€ 1_basic_integer_queue      
โ”‚   โ””โ”€โ”€ 2_log_queue                 
โ”œโ”€โ”€ lib/
โ”‚   โ””โ”€โ”€ queue/    
โ”‚       โ”œโ”€โ”€ queue.c                 # Implementation (MISRA-C compliant)
โ”‚       โ””โ”€โ”€ queue.h                 # Public API header
โ”œโ”€โ”€ test/
โ”‚   โ”œโ”€โ”€ _config_scripts/        
โ”‚   โ”‚   โ”œโ”€โ”€ CI/  
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ CI.py               # Python script running configured targets
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ config.yaml         # Customizable CI workflow setup            
โ”‚   โ”‚   โ””โ”€โ”€ venv_setup/
โ”‚   โ”‚       โ””โ”€โ”€ requirements.txt    # Python tools required by scripts in project
โ”‚   โ”‚       โ””โ”€โ”€ venv_setup.py       # Script for automated creating of virtual environment and install requirements
โ”‚   โ”œโ”€โ”€ queue/                      # Queue lib Unit Tests (Unity)
โ”‚   โ”œโ”€โ”€ template/                   # Unit Tests module template files
โ”‚   โ””โ”€โ”€ unity/                      # Unit Tests framework files
โ”œโ”€โ”€ .clang-format                   # clang-format rules
โ”œโ”€โ”€ LICENSE                             
โ”œโ”€โ”€ mkdocs.yml                      # MkDocs deploy settings
โ””โ”€โ”€ README.md

โš™๏ธ API Reference

queue_init

queue_status_t queue_init(queue_t *q, void *buffer, uint16_t element_size, uint16_t capacity);

Initializes a queue instance.

Parameter Description
q Pointer to queue control structure
buffer Pointer to user-provided buffer
element_size Size of a single element (bytes)
capacity Maximum number of elements in the queue

Returns:

  • QUEUE_OK โ€“ successfully initialized
  • QUEUE_ERROR โ€“ invalid parameters

queue_push

queue_status_t queue_push(queue_t *q, const void *item);

Adds an element to the queue (tail).

Returns:

  • QUEUE_OK โ€“ successfully added
  • QUEUE_FULL โ€“ queue is full
  • QUEUE_ERROR โ€“ invalid parameters

queue_pop

queue_status_t queue_pop(queue_t *q, void *item);

Removes the oldest element from the queue (head).

Returns:

  • QUEUE_OK โ€“ element successfully read
  • QUEUE_EMPTY โ€“ queue is empty
  • QUEUE_ERROR โ€“ invalid parameters

queue_is_empty

bool queue_is_empty(const queue_t *q);

Returns true if the queue contains no elements.

queue_is_full

bool queue_is_full(const queue_t *q);

Returns true if the queue has reached its capacity.


๐Ÿง  Example 1: Basic Integer Queue

#include "queue.h"
#include <stdio.h>

#define QUEUE_CAPACITY 5U

static int buffer[QUEUE_CAPACITY];
static queue_t int_queue;

int main(void)
{
    queue_init(&int_queue, buffer, sizeof(int), QUEUE_CAPACITY);

    int value = 10;
    (void)queue_push(&int_queue, &value);

    int out = 0;
    (void)queue_pop(&int_queue, &out);

    printf("Read value: %d\n", out); // Output: 10
    return 0;
}

๐Ÿงพ Example 2: Log Queue (Fixed-Size Structs)

#include "queue.h"
#include <stdio.h>
#include <string.h>

#define LOG_ENTRY_SIZE     (32U)
#define LOG_QUEUE_CAPACITY (20U)

typedef struct
{
    uint8_t data[LOG_ENTRY_SIZE];
} log_entry_t;

static log_entry_t log_buffer[LOG_QUEUE_CAPACITY];
static queue_t log_queue;

static void log_queue_init(void)
{
    queue_init(&log_queue, log_buffer, sizeof(log_entry_t), LOG_QUEUE_CAPACITY);
}

static void log_queue_push(const char *msg)
{
    log_entry_t entry;
    uint16_t len = 0U;

    while ((msg[len] != '\0') && (len < LOG_ENTRY_SIZE))
    {
        entry.data[len] = (uint8_t)msg[len];
        len++;
    }

    while (len < LOG_ENTRY_SIZE)
    {
        entry.data[len++] = 0U;
    }

    (void)queue_push(&log_queue, &entry);
}

static void log_queue_pop_and_print(void)
{
    log_entry_t entry;
    if (queue_pop(&log_queue, &entry) == QUEUE_OK)
    {
        printf("LOG: %s\n", entry.data);
    }
}

int main(void)
{
    log_queue_init();
    log_queue_push("System initialized");
    log_queue_push("Temperature sensor ready");
    log_queue_push("Main loop started");

    for (uint16_t i = 0U; i < 4U; i++)
    {
        log_queue_pop_and_print();
    }
}

๐Ÿ’ฌ Example Output:

LOG: System initialized
LOG: Temperature sensor ready
LOG: Main loop started

โ–ถ๏ธ Running examples:

๐Ÿš€ 1_basic_integer_queue

  1. Go to project main folder.
  2. Open examples folder, compile and run the example
cd examples/1_basic_integer_queue
cmake -S./ -B out -G"Unix Makefiles"
cd out
make run

๐Ÿš€ 2_log_queue

  1. Go to project main folder.
  2. Open examples folder, compile and run the example
cd examples/2_log_queue
cmake -S./ -B out -G"Unix Makefiles"
cd out
make run

๐Ÿงฉ Integration

Just add queue.c and queue.h to your project and include:

#include "queue.h"

You can compile it as part of your embedded firmware or as a portable C module.


๐Ÿงช Unit Tests (Unity)

All unit tests include coverage for the DV-QUEUE-001 deviation:

  • Push/pop of different data types (int, struct, char array)
  • Wrap-around behavior
  • Edge cases (zero-byte elements, NULL pointers)
  • Branch coverage in copy_bytes

Run tests:

cd test/queue
cmake -S./ -B out -G"Unix Makefiles"
cd out
make run

๐Ÿงฐ Safety / Compliance Notes

  • No dynamic memory โ†’ static allocation only
  • No memcpy/memmove โ†’ deterministic byte-copy routine (copy_bytes)
  • MISRA-C:2012 ready โ€“ controlled deviation DV-QUEUE-001 fully documented and verified
  • Thread safety โ€“ not inherently thread-safe; protect operations in RTOS/ISR environments
  • Deterministic timing: O(element_size) per operation
  • Compliance verification: static analysis, unit/integration tests, code review

DV-QUEUE-001:

  • Rule 11.4 โ€“ cast between void* and uint8_t*
  • Justification: controlled, local casts for byte-level operations
  • Verified: unit tests covering all branches of copy_bytes, multiple data types

โš ๏ธ Safety-Critical Additional Requirements

Follow these for ISO 26262 / IEC 61508 compliance:

  1. Static code analysis
  2. Full unit/integration testing with coverage report
  3. Peer code reviews
  4. Thread safety in multi-context environments
  5. Traceable version control and documentation

๐Ÿ“œ License

Licensed under the MIT License (see LICENSE file).

ยฉ 2025 niwciu


๐Ÿ‘‰ Explore full documentation online:
https://niwciu.github.io/QUEUE_LIB



myEmbeddedWayBanner
Part of the myEmbeddedWay safety-oriented C library collection.