KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
varqueue.h File Reference

A variable length queue implementation. More...

#include <stdint.h>
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  VarQueue
 Variable length queue structure. More...
 

Macros

#define VQ_INIT(NAME, SIZE)
 Initializes the variable length queue. More...
 

Functions

bool vqQueue (VarQueue *const vq, uint16_t length, uint8_t *buffer)
 Queues an element on the queue. More...
 
bool vqDeQueue (VarQueue *const vq, uint16_t *length, uint8_t *buffer)
 DeQueues an element from the queue. More...
 
bool vqPeek (VarQueue *const vq, uint16_t *length, uint8_t *buffer)
 Peeks an element from the queue. More...
 
bool vqRemove (VarQueue *const vq)
 Removes the first element from the queue (not the last). More...
 
uint16_t vqDeQueueLength (VarQueue *const vq)
 Returns the lenght of the next element to dequeue. More...
 
uint16_t vqFree (VarQueue *const vq)
 Returns the number of bytes free for the next element. More...
 
static void vqReset (VarQueue *const vq)
 Resets the queue, such that is is empty. More...
 

Detailed Description

A variable length queue implementation.

Queue is currently limited to 16 bits, e.g. it can not be larger than 64K.

To create a queue you will first need to initialize the a byte array, and configure the VarQueue structure, like this:

static uint8_t _myQueueMem[1024];
static VarQueue _myQueue = {
.mem = _myQueueMem,
.size = sizeof(_myQueueMem),
}

Note that the actual capacity of the queue is less, since each element requires a 2 byte header. So, you can store one 1022 sized element in the queue above, or two 510 sized elements, etc...

The code above is also written down my the VQ_INIT function, which you may also use.

Definition in file varqueue.h.

Macro Definition Documentation

#define VQ_INIT (   NAME,
  SIZE 
)
Value:
static uint8_t NAME ## _mem[SIZE];\
static VarQueue NAME = { \
.mem = NAME ## _mem, \
.size = SIZE, \
.used = 0, \
.read = 0, \
.write = 0 \
}
uint8_t *const mem
Pointer to memory.
Definition: varqueue.h:52
Variable length queue structure.
Definition: varqueue.h:50

Initializes the variable length queue.

Parameters
NAMEVariable name of the queue structure. A byte array named NAME_mem will be created as heap.
SIZEThe size of the queue in bytes.

Definition at line 66 of file varqueue.h.

Function Documentation

bool vqDeQueue ( VarQueue *const  vq,
uint16_t *  length,
uint8_t *  buffer 
)

DeQueues an element from the queue.

Parameters
vqPointer to the queue structure.
lengthPointer containing the length of the buffer. On return this will be filled with the actual length of the element.
bufferThe buffer to copy into.
Return values
trueElement was dequeued and is now in the buffer false Element was not dequeued, either the length of the buffer is not sufficient or there is no element in the queue.

Definition at line 50 of file varqueue.c.

uint16_t vqDeQueueLength ( VarQueue *const  vq)

Returns the lenght of the next element to dequeue.

Parameters
vqThe pointer to the queue structure.
Returns
Either the size of the next element, or 0 if there are no elements on the queue.

Definition at line 108 of file varqueue.c.

uint16_t vqFree ( VarQueue *const  vq)

Returns the number of bytes free for the next element.

The header is already substracted from this number, so this is really the number of bytes you can still put into the queue.

Parameters
vqThe pointer to the queue structure.
Returns
Number of bytes free for the next element.

Definition at line 114 of file varqueue.c.

bool vqPeek ( VarQueue *const  vq,
uint16_t *  length,
uint8_t *  buffer 
)

Peeks an element from the queue.

The actual element is not removed from the queue.

Parameters
vqPointer to the queue structure.
lengthPointer containing the length of the buffer. On return this will be filled with the actual length of the element.
bufferThe buffer to copy into.
Return values
trueElement was dequeued and is now in the buffer false Element was not dequeued, either the length of the buffer is not sufficient
bool vqQueue ( VarQueue *const  vq,
uint16_t  length,
uint8_t *  buffer 
)

Queues an element on the queue.

Parameters
vqPointer to the variable queue structure
lengthlength of the element in the buffer
bufferThe buffer to copy the element from
Return values
trueElement was queued. false The element was not queued (out of space?).

Definition at line 24 of file varqueue.c.

bool vqRemove ( VarQueue *const  vq)

Removes the first element from the queue (not the last).

Parameters
vqThe pointer to the queue structure.
Return values
trueElement was removed. false Element was not removed because the queue was already empty.

Definition at line 98 of file varqueue.c.

static void vqReset ( VarQueue *const  vq)
inlinestatic

Resets the queue, such that is is empty.

Parameters
vqPointer the the variable queue element.

Definition at line 150 of file varqueue.h.