1 /* Copyright (c) 2008, 2009 Nicira Networks, Inc.
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.
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.
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/>.
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.
29 #include "extras/ezio/byteq.h"
36 /* The queue size must be a power of 2. */
37 BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));
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);
46 /* Initializes 'q' as empty. */
48 byteq_init(struct byteq *q)
50 q->head = q->tail = 0;
53 /* Returns the number of bytes current queued in 'q'. */
55 byteq_used(const struct byteq *q)
57 return q->head - q->tail;
60 /* Returns the number of bytes that can be added to 'q' without overflow. */
62 byteq_avail(const struct byteq *q)
64 return BYTEQ_SIZE - byteq_used(q);
67 /* Returns true if no bytes are queued in 'q',
68 * false if at least one byte is queued. */
70 byteq_is_empty(const struct byteq *q)
72 return !byteq_used(q);
75 /* Returns true if 'q' has no room to queue additional bytes,
76 * false if 'q' has room for at least one more byte. */
78 byteq_is_full(const struct byteq *q)
80 return !byteq_avail(q);
83 /* Adds 'c' at the head of 'q', which must not be full. */
85 byteq_put(struct byteq *q, uint8_t c)
87 assert(!byteq_is_full(q));
92 /* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
93 * bytes of free space. */
95 byteq_putn(struct byteq *q, const void *p_, size_t n)
97 const uint8_t *p = p_;
98 assert(byteq_avail(q) >= n);
100 size_t chunk = MIN(n, headroom(q));
101 memcpy(head(q), p, chunk);
102 advance_head(q, chunk);
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'. */
111 byteq_put_string(struct byteq *q, const char *s)
113 byteq_putn(q, s, strlen(s));
116 /* Removes a byte from the tail of 'q' and returns it. 'q' must not be
119 byteq_get(struct byteq *q)
122 assert(!byteq_is_empty(q));
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). */
132 byteq_write(struct byteq *q, int fd)
134 while (!byteq_is_empty(q)) {
135 ssize_t n = write(fd, tail(q), tailroom(q));
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
151 byteq_read(struct byteq *q, int fd)
153 while (!byteq_is_full(q)) {
154 ssize_t n = read(fd, head(q), headroom(q));
158 return !n ? EOF : errno;
164 /* Returns the number of contiguous bytes of in-use space starting at the tail
167 tailroom(const struct byteq *q)
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);
174 /* Returns the first in-use byte of 'q', the point at which data is removed
176 static const uint8_t *
177 tail(const struct byteq *q)
179 return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
182 /* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
185 advance_tail(struct byteq *q, unsigned int n)
187 assert(tailroom(q) >= n);
191 /* Returns the byte after the last in-use byte of 'q', the point at which new
192 * data will be added to 'q'. */
194 head(struct byteq *q)
196 return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
199 /* Returns the number of contiguous bytes of free space starting at the head
202 headroom(const struct byteq *q)
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);
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. */
212 advance_head(struct byteq *q, unsigned int n)
214 assert(headroom(q) >= n);