1 /* Copyright (c) 2008, 2009 Nicira Networks, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include "extras/ezio/byteq.h"
24 /* The queue size must be a power of 2. */
25 BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));
27 static uint8_t *head(struct byteq *);
28 static int headroom(const struct byteq *);
29 static void advance_head(struct byteq *, unsigned int n);
30 static int tailroom(const struct byteq *);
31 static const uint8_t *tail(const struct byteq *);
32 static void advance_tail(struct byteq *, unsigned int n);
34 /* Initializes 'q' as empty. */
36 byteq_init(struct byteq *q)
38 q->head = q->tail = 0;
41 /* Returns the number of bytes current queued in 'q'. */
43 byteq_used(const struct byteq *q)
45 return q->head - q->tail;
48 /* Returns the number of bytes that can be added to 'q' without overflow. */
50 byteq_avail(const struct byteq *q)
52 return BYTEQ_SIZE - byteq_used(q);
55 /* Returns true if no bytes are queued in 'q',
56 * false if at least one byte is queued. */
58 byteq_is_empty(const struct byteq *q)
60 return !byteq_used(q);
63 /* Returns true if 'q' has no room to queue additional bytes,
64 * false if 'q' has room for at least one more byte. */
66 byteq_is_full(const struct byteq *q)
68 return !byteq_avail(q);
71 /* Adds 'c' at the head of 'q', which must not be full. */
73 byteq_put(struct byteq *q, uint8_t c)
75 assert(!byteq_is_full(q));
80 /* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
81 * bytes of free space. */
83 byteq_putn(struct byteq *q, const void *p_, size_t n)
85 const uint8_t *p = p_;
86 assert(byteq_avail(q) >= n);
88 size_t chunk = MIN(n, headroom(q));
89 memcpy(head(q), p, chunk);
90 advance_head(q, chunk);
96 /* Appends null-terminated string 's' to the head of 'q', which must have
97 * enough space. The null terminator is not added to 'q'. */
99 byteq_put_string(struct byteq *q, const char *s)
101 byteq_putn(q, s, strlen(s));
104 /* Removes a byte from the tail of 'q' and returns it. 'q' must not be
107 byteq_get(struct byteq *q)
110 assert(!byteq_is_empty(q));
116 /* Writes as much of 'q' as possible to 'fd'. Returns 0 if 'q' is fully
117 * drained by the write, otherwise a positive errno value (e.g. EAGAIN if a
118 * socket or tty buffer filled up). */
120 byteq_write(struct byteq *q, int fd)
122 while (!byteq_is_empty(q)) {
123 ssize_t n = write(fd, tail(q), tailroom(q));
134 /* Reads as much possible from 'fd' into 'q'. Returns 0 if 'q' is completely
135 * filled up by the read, EOF if end-of-file was reached before 'q' was filled,
136 * and otherwise a positive errno value (e.g. EAGAIN if a socket or tty buffer
139 byteq_read(struct byteq *q, int fd)
141 while (!byteq_is_full(q)) {
142 ssize_t n = read(fd, head(q), headroom(q));
146 return !n ? EOF : errno;
152 /* Returns the number of contiguous bytes of in-use space starting at the tail
155 tailroom(const struct byteq *q)
157 int used = byteq_used(q);
158 int tail_to_end = BYTEQ_SIZE - (q->tail & (BYTEQ_SIZE - 1));
159 return MIN(used, tail_to_end);
162 /* Returns the first in-use byte of 'q', the point at which data is removed
164 static const uint8_t *
165 tail(const struct byteq *q)
167 return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
170 /* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
173 advance_tail(struct byteq *q, unsigned int n)
175 assert(tailroom(q) >= n);
179 /* Returns the byte after the last in-use byte of 'q', the point at which new
180 * data will be added to 'q'. */
182 head(struct byteq *q)
184 return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
187 /* Returns the number of contiguous bytes of free space starting at the head
190 headroom(const struct byteq *q)
192 int avail = byteq_avail(q);
193 int head_to_end = BYTEQ_SIZE - (q->head & (BYTEQ_SIZE - 1));
194 return MIN(avail, head_to_end);
197 /* Adds to 'q' the 'n' bytes after the last currently in-use byte of 'q'. 'q'
198 * must have at least 'n' bytes of headroom. */
200 advance_head(struct byteq *q, unsigned int n)
202 assert(headroom(q) >= n);