KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
queue.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  *
4  * Copyright 2013 KM3NeT Collaboration
5  *
6  * All Rights Reserved.
7  *
8  * queue.c
9  *
10  * Created on: 2 mei 2013
11  * Author: Vincent van Beveren
12  */
13 
14 
15 #include "coll/queue.h"
16 #include <string.h>
17 
18 void * qQueueIP(Queue * q)
19 {
20  if ( q->length == q->descr->capacity ) return NULL;
21 
22  void * ptr = q->writePtr;
23 
24  if (q->writePtr == q->descr->memoryLast ) q->writePtr = q->descr->memoryFirst;
25  else q->writePtr += q->descr->entrySize;
26  ++(q->length);
27  return ptr;
28 
29 
30 
31 }
32 
33 void * qDeQueueIP(Queue * q)
34 {
35  if (q->length == 0) return NULL;
36 
37  void * data = q->readPtr;
38 
39  if (q->readPtr == q->descr->memoryLast) q->readPtr = q->descr->memoryFirst;
40  else q->readPtr += q->descr->entrySize;
41  --(q->length);
42  return data;
43 }
44 
45 bool qRemoveBack(Queue * q)
46 {
47  if (q->length == 0) return false;
48 
49  if (q->readPtr == q->descr->memoryFirst) q->readPtr = q->descr->memoryLast;
50  else q->readPtr -= q->descr->entrySize;
51  --(q->length);
52  return true;
53 }
54 
55 bool qContains(Queue * q, void * data)
56 {
57  uint8_t * cur = qFirst(q);
58 
59  while (cur != NULL)
60  {
61  if (memcmp(data, cur, q->descr->entrySize) == 0) return true;
62  cur = qNext(q, cur);
63  }
64 
65  return false;
66 }
67 
const uint16_t entrySize
size of an entry
Definition: queue.h:50
const uint16_t capacity
capacity in entries
Definition: queue.h:49
static void * qNext(Queue *q, void *ptr)
Peek at the next element in the queue.
Definition: queue.h:236
uint16_t length
length of the queue
Definition: queue.h:62
const QueueDescr *const descr
pointer to the descriptor
Definition: queue.h:59
void * writePtr
write pointer
Definition: queue.h:61
void * readPtr
read pointer
Definition: queue.h:60
Queue state.
Definition: queue.h:58
Queue is created in statically allocated memory.
void *const memoryFirst
first element pointer
Definition: queue.h:47
bool qContains(Queue *q, void *data)
Iterates the queue to check for an element.
Definition: queue.c:55
static void * qFirst(Queue *q)
Peek at the first element in the queue.
Definition: queue.h:209
bool qRemoveBack(Queue *q)
Delete element from the back for the queue.
Definition: queue.c:45
void * qQueueIP(Queue *q)
Queue data to the end of the queue in place.
Definition: queue.c:18
void * qDeQueueIP(Queue *q)
Dequeue an element from the front of the queue in place.
Definition: queue.c:33
void *const memoryLast
last element pointer
Definition: queue.h:48