4 * @brief Meilhaus circular buffer implementation.
5 * @note Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
6 * @author Guenter Gebhardt
7 * @author Krzysztof Gantzke (k.gantzke@meilhaus.de)
11 * Copyright (C) 2007 Meilhaus Electronic GmbH (support@meilhaus.de)
13 * This file is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #ifndef _MECIRC_BUF_H_
29 #define _MECIRC_BUF_H_
35 typedef struct me_circ_buf
{
37 // unsigned int count;
43 static int inline me_circ_buf_values(me_circ_buf_t
* buf
)
45 // return ((buf->head - buf->tail) & (buf->count - 1));
46 return ((buf
->head
- buf
->tail
) & (buf
->mask
));
49 static int inline me_circ_buf_space(me_circ_buf_t
* buf
)
51 // return ((buf->tail - (buf->head + 1)) & (buf->count - 1));
52 return ((buf
->tail
- (buf
->head
+ 1)) & (buf
->mask
));
55 static int inline me_circ_buf_values_to_end(me_circ_buf_t
* buf
)
59 // end = buf->count - buf->tail;
60 // n = (buf->head + end) & (buf->count - 1);
61 end
= buf
->mask
+ 1 - buf
->tail
;
62 n
= (buf
->head
+ end
) & (buf
->mask
);
63 return (n
< end
) ? n
: end
;
66 static int inline me_circ_buf_space_to_end(me_circ_buf_t
* buf
)
71 // end = buf->count - 1 - buf->head;
72 // n = (end + buf->tail) & (buf->count - 1);
73 end
= buf
->mask
- buf
->head
;
74 n
= (end
+ buf
->tail
) & (buf
->mask
);
75 return (n
<= end
) ? n
: (end
+ 1);
81 /// @note buf->mask = buf->count-1 = ME4600_AI_CIRC_BUF_COUNT-1
85 typedef struct me_circ_buf_32b
{
88 unsigned int mask
; //buffor size-1 must be 2^n-1 to work
93 typedef struct me_circ_buf_16b
{
96 unsigned int mask
; //buffor size-1 must be 2^n-1 to work
99 # endif //_CBUFF_32b_t
101 /** How many values is in buffer */
102 static int inline me_circ_buf_values(me_circ_buf_t
* buf
)
104 return ((buf
->head
- buf
->tail
) & (buf
->mask
));
107 /** How many space left */
108 static int inline me_circ_buf_space(me_circ_buf_t
* buf
)
110 return ((buf
->tail
- (buf
->head
+ 1)) & (buf
->mask
));
113 /** How many values can be read from buffor in one chunck. */
114 static int inline me_circ_buf_values_to_end(me_circ_buf_t
* buf
)
117 buf
->head
) ? (buf
->head
- buf
->tail
) : (buf
->mask
- buf
->tail
+
121 /** How many values can be write to buffer in one chunck. */
122 static int inline me_circ_buf_space_to_end(me_circ_buf_t
* buf
)
125 buf
->head
) ? (buf
->mask
- buf
->head
+ 1) : (buf
->tail
-
131 #endif //_MECIRC_BUF_H_