1 /* Copyright (c) 2008, 2009 Nicira, 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.
24 /* The queue size must be a power of 2. */
25 BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));
27 /* Initializes 'q' as empty. */
29 byteq_init(struct byteq *q)
31 q->head = q->tail = 0;
34 /* Returns the number of bytes current queued in 'q'. */
36 byteq_used(const struct byteq *q)
38 return q->head - q->tail;
41 /* Returns the number of bytes that can be added to 'q' without overflow. */
43 byteq_avail(const struct byteq *q)
45 return BYTEQ_SIZE - byteq_used(q);
48 /* Returns true if no bytes are queued in 'q',
49 * false if at least one byte is queued. */
51 byteq_is_empty(const struct byteq *q)
53 return !byteq_used(q);
56 /* Returns true if 'q' has no room to queue additional bytes,
57 * false if 'q' has room for at least one more byte. */
59 byteq_is_full(const struct byteq *q)
61 return !byteq_avail(q);
64 /* Adds 'c' at the head of 'q', which must not be full. */
66 byteq_put(struct byteq *q, uint8_t c)
68 assert(!byteq_is_full(q));
73 /* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
74 * bytes of free space. */
76 byteq_putn(struct byteq *q, const void *p_, size_t n)
78 const uint8_t *p = p_;
79 assert(byteq_avail(q) >= n);
81 size_t chunk = MIN(n, byteq_headroom(q));
82 memcpy(byteq_head(q), p, chunk);
83 byteq_advance_head(q, chunk);
89 /* Appends null-terminated string 's' to the head of 'q', which must have
90 * enough space. The null terminator is not added to 'q'. */
92 byteq_put_string(struct byteq *q, const char *s)
94 byteq_putn(q, s, strlen(s));
97 /* Removes a byte from the tail of 'q' and returns it. 'q' must not be
100 byteq_get(struct byteq *q)
103 assert(!byteq_is_empty(q));
109 /* Writes as much of 'q' as possible to 'fd'. Returns 0 if 'q' is fully
110 * drained by the write, otherwise a positive errno value (e.g. EAGAIN if a
111 * socket or tty buffer filled up). */
113 byteq_write(struct byteq *q, int fd)
115 while (!byteq_is_empty(q)) {
116 ssize_t n = write(fd, byteq_tail(q), byteq_tailroom(q));
118 byteq_advance_tail(q, n);
127 /* Reads as much possible from 'fd' into 'q'. Returns 0 if 'q' is completely
128 * filled up by the read, EOF if end-of-file was reached before 'q' was filled,
129 * and otherwise a positive errno value (e.g. EAGAIN if a socket or tty buffer
132 byteq_read(struct byteq *q, int fd)
134 while (!byteq_is_full(q)) {
135 ssize_t n = read(fd, byteq_head(q), byteq_headroom(q));
137 byteq_advance_head(q, n);
139 return !n ? EOF : errno;
145 /* Returns the number of contiguous bytes of in-use space starting at the tail
148 byteq_tailroom(const struct byteq *q)
150 int used = byteq_used(q);
151 int tail_to_end = BYTEQ_SIZE - (q->tail & (BYTEQ_SIZE - 1));
152 return MIN(used, tail_to_end);
155 /* Returns the first in-use byte of 'q', the point at which data is removed
158 byteq_tail(const struct byteq *q)
160 return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
163 /* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
166 byteq_advance_tail(struct byteq *q, unsigned int n)
168 assert(byteq_tailroom(q) >= n);
172 /* Returns the byte after the last in-use byte of 'q', the point at which new
173 * data will be added to 'q'. */
175 byteq_head(struct byteq *q)
177 return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
180 /* Returns the number of contiguous bytes of free space starting at the head
183 byteq_headroom(const struct byteq *q)
185 int avail = byteq_avail(q);
186 int head_to_end = BYTEQ_SIZE - (q->head & (BYTEQ_SIZE - 1));
187 return MIN(avail, head_to_end);
190 /* Adds to 'q' the 'n' bytes after the last currently in-use byte of 'q'. 'q'
191 * must have at least 'n' bytes of headroom. */
193 byteq_advance_head(struct byteq *q, unsigned int n)
195 assert(byteq_headroom(q) >= n);