Queue
Queue implementation in C
Functions
Queue

Functions

void queue_init (queue_t *queue, queue_array_t *queueArray, uint16_t queueSize, uint8_t dataSize)
 
uint8_t queue_enqueue (queue_t *queue, const void *value)
 
uint8_t queue_dequeue (queue_t *queue, void *value)
 
void queue_print (queue_t *queue, void(*printFn)(const void *data), void *data)
 
uint16_t queue_count (queue_t *queue)
 

Detailed Description

Function Documentation

◆ queue_count()

uint16_t queue_count ( queue_t queue)

Description:

This function is used to get the number of elements in the queue.

Parameters
queueQueue to get the number of elements.
Returns
Number of elements.

◆ queue_dequeue()

uint8_t queue_dequeue ( queue_t queue,
void *  value 
)

Description:

This function is used to get a element from the queue.

Parameters
queueQueue.
valuePointer to variable to which the dequeued data will be copied.
Returns
-1 if the queue is empty (underflow), 0 otherwise.

Example:

#define QUEUESIZE sizeof(uint16_t) * 10
uint8_t error;
uint16_t data, retval;
queue_t queue;
queue_array_t queue_array[QUEUESIZE] = {0};
queue_init(&queue, queue_array, QUEUESIZE, sizeof(uint16_t));
data = 1;
int error = queue_enqueue(&queue, (void *) &data);
int error = queue_dequeue(&queue, (void *) &retval);

◆ queue_enqueue()

uint8_t queue_enqueue ( queue_t queue,
const void *  value 
)

Description:

This function is used to put a element in the queue.

Parameters
queueQueue.
valuePointer to variable whose value is going to be inserted to the queue.
Returns
-1 if the queue is full (overflow), 0 otherwise.

Example:

#define QUEUESIZE sizeof(uint16_t) * 10
uint8_t error;
uint16_t data;
queue_t queue;
queue_array_t queue_array[QUEUESIZE] = {0};
queue_init(&queue, queue_array, QUEUESIZE, sizeof(uint16_t));
data = 1;
int error = queue_enqueue(&queue, (void *) &data);

◆ queue_init()

void queue_init ( queue_t queue,
queue_array_t queueArray,
uint16_t  queueSize,
uint8_t  dataSize 
)

Description:

This function is used to initialize a queue structure.

Parameters
queueQueue to be initialized.
queueArrayPointer to the array used to store the data.
queueSizeSize of the queue array.
dataSizeSize of every data element.
Returns
None.

Example:

#define QUEUESIZE sizeof(uint16_t) * 10
queue_t queue;
queue_array_t queue_array[QUEUESIZE] = {0};
queue_init(&queue, queue_array, QUEUESIZE, sizeof(uint16_t));

◆ queue_print()

void queue_print ( queue_t queue,
void(*)(const void *data)  printFn,
void *  data 
)

Description:

This function is used to print the values of the queue.

Parameters
queueQueue.
printFnPointer to the function used to print the elements.
dataPointer to a variable with the correct size that will store every element of the queue and is passed to printFn.
Returns
None.

Example:

#define QUEUESIZE sizeof(uint16_t) * 3
void
printUint16(const void *data)
{
printf("%d :: ", *((const uint16_t *) data));
}
int
main(int argc, char** argv)
{
uint8_t error, i;
uint16_t print_data;
const uint16_t data[3] = {0, 1, 2};
queue_t queue;
queue_array_t queue_array[QUEUESIZE] = {0};
queue_init(&queue, queue_array, QUEUESIZE, sizeof(uint16_t));
for (i = 0; i < 3; i++)
{
int error = queue_enqueue(&queue, (void *) &data[i]);
}
queue_print(queue, printUint16, (void *) &print_data);
}