root / branches / encoders / code / lib / include / libdragonfly / ring_buffer.h @ 1345
History | View | Annotate | Download (4.44 KB)
1 | 1345 | chihsiuh | /**
|
---|---|---|---|
2 | * Copyright (c) 2007 Colony Project
|
||
3 | *
|
||
4 | * Permission is hereby granted, free of charge, to any person
|
||
5 | * obtaining a copy of this software and associated documentation
|
||
6 | * files (the "Software"), to deal in the Software without
|
||
7 | * restriction, including without limitation the rights to use,
|
||
8 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
9 | * copies of the Software, and to permit persons to whom the
|
||
10 | * Software is furnished to do so, subject to the following
|
||
11 | * conditions:
|
||
12 | *
|
||
13 | * The above copyright notice and this permission notice shall be
|
||
14 | * included in all copies or substantial portions of the Software.
|
||
15 | *
|
||
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||
17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||
18 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||
19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||
20 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||
21 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||
22 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||
23 | * OTHER DEALINGS IN THE SOFTWARE.
|
||
24 | **/
|
||
25 | |||
26 | |||
27 | /** @file ring_buffer.h
|
||
28 | * @brief Ring Buffer
|
||
29 | 88 | bcoltin | *
|
30 | 1345 | chihsiuh | * Include macros to create a ring buffer. Originally written for 15-410.
|
31 | * Modified for use in ATMega128 serial driver
|
||
32 | 88 | bcoltin | *
|
33 | 1345 | chihsiuh | * @author Cornell Wright (cgwright)
|
34 | * @author Jason P. Winters (jpwinter)
|
||
35 | *
|
||
36 | **/
|
||
37 | 88 | bcoltin | |
38 | #ifndef _RING_BUFFER_H
|
||
39 | #define _RING_BUFFER_H
|
||
40 | |||
41 | /** @brief Creates the struct for a new ring buffer. This just expands to a
|
||
42 | * structure definition and thus should be invoked in the global context.
|
||
43 | *
|
||
44 | * @param struct_name Name of the ring buffer struct.
|
||
45 | *
|
||
46 | * @param size The size of the buffer to create.
|
||
47 | *
|
||
48 | * @param type The type of object that the queue is to hold.
|
||
49 | *
|
||
50 | * @param ... Name or names of the instance or instances of the ring buffer(s)
|
||
51 | * to be created.
|
||
52 | */
|
||
53 | #define RING_BUFFER_NEW(struct_name, size, type, ...) \
|
||
54 | struct struct_name { \
|
||
55 | type queue[size]; \ |
||
56 | uint8_t start; \ |
||
57 | uint8_t end; \ |
||
58 | uint8_t buffer_size; \ |
||
59 | } __VA_ARGS__ |
||
60 | |||
61 | |||
62 | /** @brief Initializes the ring buffer, setting its size to the correct value
|
||
63 | *
|
||
64 | * @param buf The ring buffer to initialize.
|
||
65 | *
|
||
66 | * @param size The size of the ring buffer (in array elements)
|
||
67 | */
|
||
68 | #define RING_BUFFER_INIT(buf, size) \
|
||
69 | do { \
|
||
70 | (buf).buffer_size = size; \ |
||
71 | } while(0) |
||
72 | |||
73 | |||
74 | /** @brief Sets the specified ring buffer to be empty.
|
||
75 | *
|
||
76 | * @param buf The ring buffer to make empty.
|
||
77 | */
|
||
78 | #define RING_BUFFER_CLEAR(buf) \
|
||
79 | do { \
|
||
80 | (buf).start = 0; \
|
||
81 | (buf).end = 0; \
|
||
82 | } while (0) |
||
83 | |||
84 | /** @brief Returns true if the ring buffer specified is empty.
|
||
85 | *
|
||
86 | * @param buf The buffer to check emptiness of.
|
||
87 | */
|
||
88 | #define RING_BUFFER_EMPTY(buf) ((buf).start == (buf).end)
|
||
89 | |||
90 | |||
91 | /** @brief Returns true if the ring buffer specified is full.
|
||
92 | *
|
||
93 | * @param buf The buffer to check fullness of.
|
||
94 | */
|
||
95 | #define RING_BUFFER_FULL(buf) \
|
||
96 | (((buf).end + 1) % (buf).buffer_size == (buf).start)
|
||
97 | |||
98 | |||
99 | /** @brief Adds val to ring buffer buf. Note that val must be of the type
|
||
100 | * used in creating the ring buffer to prevent the compiler from spewing
|
||
101 | * confusing errors. Also, this assumes that the ring buffer is not full.
|
||
102 | *
|
||
103 | * @param buf The ring buffer to add to.
|
||
104 | * @param val The value to add.
|
||
105 | */
|
||
106 | #define RING_BUFFER_ADD(buf, val) \
|
||
107 | do { \
|
||
108 | (buf).queue[(buf).end] = val; \ |
||
109 | (buf).end = ((buf).end + 1) % (buf).buffer_size; \
|
||
110 | } while (0) |
||
111 | |||
112 | |||
113 | /** @brief Removes the value at the head of the ring buffer and puts it in
|
||
114 | * val_ret. Note that val_ret must be the same type used in creating the ring
|
||
115 | * buffer to prevent the compiler from spewing confusing errors. Also, this
|
||
116 | * assumes that the ring buffer is not empty.
|
||
117 | *
|
||
118 | * @param buf The ring buffer to remove from.
|
||
119 | * @param val_ret Where to put the value removed from the head of the ring
|
||
120 | * buffer.
|
||
121 | */
|
||
122 | #define RING_BUFFER_REMOVE(buf, val_ret) \
|
||
123 | do { \
|
||
124 | (val_ret) = (buf).queue[(buf).start]; \ |
||
125 | (buf).start = ((buf).start + 1) % (buf).buffer_size; \
|
||
126 | } while (0) |
||
127 | |||
128 | /** @brief Checks the value at the head of the ring buffer without removing it
|
||
129 | * and puts it into val_ret. Note that val_ret must be the same type used in
|
||
130 | * creating the ring buffer to prevent the compiler from spewing confusing
|
||
131 | * errors. Also, this assumes that the ring buffer is not empty.
|
||
132 | *
|
||
133 | * @param buf The ring buffer to check from
|
||
134 | * @param val_ret where to put the value checked from the head of the ring
|
||
135 | */
|
||
136 | #define RING_BUFFER_PEEK(buf, val_ret) \
|
||
137 | do { \
|
||
138 | (val_ret) = (buf).queue[(buf).start]; \ |
||
139 | } while(0) |
||
140 | |||
141 | #endif /* _RING_BUFFER_H */ |