KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fifo.h
Go to the documentation of this file.
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 KM3NeT Collaboration
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : fifo.h
11  * Created : 2 sep. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 #ifndef FIFO_H_
16 #define FIFO_H_
17 
18 /**
19  * @file
20  *
21  * @ingroup collections
22  *
23  * Lightweight Fifo implementation.
24  *
25  */
26 
27 #include <stdbool.h>
28 
29 /**
30  * Fifo meta data.
31  */
32 typedef struct {
33  const unsigned int cap; /**
34  unsigned int rp;
35  unsigned int wp;
36 } FifoMeta;
37 
38 
39 #define fifoCreate(NAME, TYPE, CAP) \
40  struct { \
41  FifoMeta meta; \
42  TYPE data[CAP]; \
43  } NAME = { .meta = { .cap = CAP, .rp = 0, .wp = 0 } };
44 
45 static inline bool _fifoEmpty(FifoMeta * meta)
46 {
47  return meta->rp == meta->wp;
48 }
49 
50 static inline bool _fifoFull(FifoMeta * meta)
51 {
52  return ( ( meta->wp + 1 ) % meta->cap == meta->rp);
53 }
54 
55 
56 static inline unsigned int _fifoRpInc(FifoMeta * meta)
57 {
58  if (_fifoEmpty(meta)) return 0;
59  int rp = meta->rp;
60  meta->rp = ( meta->rp + 1 ) % meta->cap;
61  return rp;
62 }
63 
64 static inline unsigned int _fifoWpInc(FifoMeta * meta)
65 {
66  if (_fifoFull(meta)) return 0;
67  int wp = meta->wp;
68  meta->wp = ( meta->wp + 1 ) % meta->cap;
69  return wp;
70 }
71 
72 
73 #define fifoEmpty(FIFO) (_fifoEmpty(&(FIFO.meta)))
74 
75 #define fifoFull(FIFO) (_fifoFull(&(FIFO.meta)))
76 
77 #define fifoRd(FIFO) (FIFO.data[_fifoRpInc(&(FIFO.meta))])
78 
79 #define fifoWr(FIFO) (FIFO.data[_fifoWpInc(&(FIFO.meta))])
80 
81 #endif /* FIFO_H_ */