Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / extras / ezio / byteq.c
1 /* Copyright (c) 2008, 2009 Nicira Networks, Inc.
2  *
3  * This program is free software: you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  * In addition, as a special exception, Nicira Networks gives permission
17  * to link the code of its release of vswitchd with the OpenSSL project's
18  * "OpenSSL" library (or with modified versions of it that use the same
19  * license as the "OpenSSL" library), and distribute the linked
20  * executables.  You must obey the GNU General Public License in all
21  * respects for all of the code used other than "OpenSSL".  If you modify
22  * this file, you may extend this exception to your version of the file,
23  * but you are not obligated to do so.  If you do not wish to do so,
24  * delete this exception statement from your version.
25  *
26  */
27
28 #include <config.h>
29 #include "extras/ezio/byteq.h"
30 #include <assert.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include "util.h"
35
36 /* The queue size must be a power of 2. */
37 BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));
38
39 static uint8_t *head(struct byteq *);
40 static int headroom(const struct byteq *);
41 static void advance_head(struct byteq *, unsigned int n);
42 static int tailroom(const struct byteq *);
43 static const uint8_t *tail(const struct byteq *);
44 static void advance_tail(struct byteq *, unsigned int n);
45
46 /* Initializes 'q' as empty. */
47 void
48 byteq_init(struct byteq *q)
49 {
50     q->head = q->tail = 0;
51 }
52
53 /* Returns the number of bytes current queued in 'q'. */
54 int
55 byteq_used(const struct byteq *q)
56 {
57     return q->head - q->tail;
58 }
59
60 /* Returns the number of bytes that can be added to 'q' without overflow. */
61 int
62 byteq_avail(const struct byteq *q)
63 {
64     return BYTEQ_SIZE - byteq_used(q);
65 }
66
67 /* Returns true if no bytes are queued in 'q',
68  * false if at least one byte is queued.  */
69 bool
70 byteq_is_empty(const struct byteq *q)
71 {
72     return !byteq_used(q);
73 }
74
75 /* Returns true if 'q' has no room to queue additional bytes,
76  * false if 'q' has room for at least one more byte.  */
77 bool
78 byteq_is_full(const struct byteq *q)
79 {
80     return !byteq_avail(q);
81 }
82
83 /* Adds 'c' at the head of 'q', which must not be full. */
84 void
85 byteq_put(struct byteq *q, uint8_t c)
86 {
87     assert(!byteq_is_full(q));
88     *head(q) = c;
89     q->head++;
90 }
91
92 /* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
93  * bytes of free space. */
94 void
95 byteq_putn(struct byteq *q, const void *p_, size_t n)
96 {
97     const uint8_t *p = p_;
98     assert(byteq_avail(q) >= n);
99     while (n > 0) {
100         size_t chunk = MIN(n, headroom(q));
101         memcpy(head(q), p, chunk);
102         advance_head(q, chunk);
103         p += chunk;
104         n -= chunk;
105     }
106 }
107
108 /* Appends null-terminated string 's' to the head of 'q', which must have
109  * enough space.  The null terminator is not added to 'q'. */
110 void
111 byteq_put_string(struct byteq *q, const char *s)
112 {
113     byteq_putn(q, s, strlen(s));
114 }
115
116 /* Removes a byte from the tail of 'q' and returns it.  'q' must not be
117  * empty. */
118 uint8_t
119 byteq_get(struct byteq *q)
120 {
121     uint8_t c;
122     assert(!byteq_is_empty(q));
123     c = *tail(q);
124     q->tail++;
125     return c;
126 }
127
128 /* Writes as much of 'q' as possible to 'fd'.  Returns 0 if 'q' is fully
129  * drained by the write, otherwise a positive errno value (e.g. EAGAIN if a
130  * socket or tty buffer filled up). */
131 int
132 byteq_write(struct byteq *q, int fd)
133 {
134     while (!byteq_is_empty(q)) {
135         ssize_t n = write(fd, tail(q), tailroom(q));
136         if (n > 0) {
137             advance_tail(q, n);
138         } else {
139             assert(n < 0);
140             return errno;
141         }
142     }
143     return 0;
144 }
145
146 /* Reads as much possible from 'fd' into 'q'.  Returns 0 if 'q' is completely
147  * filled up by the read, EOF if end-of-file was reached before 'q' was filled,
148  * and otherwise a positive errno value (e.g. EAGAIN if a socket or tty buffer
149  * was drained). */
150 int
151 byteq_read(struct byteq *q, int fd)
152 {
153     while (!byteq_is_full(q)) {
154         ssize_t n = read(fd, head(q), headroom(q));
155         if (n > 0) {
156             advance_head(q, n);
157         } else {
158             return !n ? EOF : errno;
159         }
160     }
161     return 0;
162 }
163 \f
164 /* Returns the number of contiguous bytes of in-use space starting at the tail
165  * of 'q'. */
166 static int
167 tailroom(const struct byteq *q)
168 {
169     int used = byteq_used(q);
170     int tail_to_end = BYTEQ_SIZE - (q->tail & (BYTEQ_SIZE - 1));
171     return MIN(used, tail_to_end);
172 }
173
174 /* Returns the first in-use byte of 'q', the point at which data is removed
175  * from 'q'. */
176 static const uint8_t *
177 tail(const struct byteq *q)
178 {
179     return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
180 }
181
182 /* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
183  * of tailroom. */
184 static void
185 advance_tail(struct byteq *q, unsigned int n)
186 {
187     assert(tailroom(q) >= n);
188     q->tail += n;
189 }
190
191 /* Returns the byte after the last in-use byte of 'q', the point at which new
192  * data will be added to 'q'. */
193 static uint8_t *
194 head(struct byteq *q)
195 {
196     return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
197 }
198
199 /* Returns the number of contiguous bytes of free space starting at the head
200  * of 'q'. */
201 static int
202 headroom(const struct byteq *q)
203 {
204     int avail = byteq_avail(q);
205     int head_to_end = BYTEQ_SIZE - (q->head & (BYTEQ_SIZE - 1));
206     return MIN(avail, head_to_end);
207 }
208
209 /* Adds to 'q' the 'n' bytes after the last currently in-use byte of 'q'.  'q'
210  * must have at least 'n' bytes of headroom. */
211 static void
212 advance_head(struct byteq *q, unsigned int n)
213 {
214     assert(headroom(q) >= n);
215     q->head += n;
216 }