1 /* Copyright (c) 2008, 2009, 2012, 2013 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.
23 /* Initializes 'q' as an empty byteq that uses the 'size' bytes of 'buffer' to
24 * store data. 'size' must be a power of 2.
26 * The caller must ensure that 'buffer' remains available to the byteq as long
27 * as 'q' is in use. */
29 byteq_init(struct byteq *q, uint8_t *buffer, size_t size)
31 ovs_assert(is_pow2(size));
34 q->head = q->tail = 0;
37 /* Returns the number of bytes current queued in 'q'. */
39 byteq_used(const struct byteq *q)
41 return q->head - q->tail;
44 /* Returns the number of bytes that can be added to 'q' without overflow. */
46 byteq_avail(const struct byteq *q)
48 return q->size - byteq_used(q);
51 /* Returns true if no bytes are queued in 'q',
52 * false if at least one byte is queued. */
54 byteq_is_empty(const struct byteq *q)
56 return !byteq_used(q);
59 /* Returns true if 'q' has no room to queue additional bytes,
60 * false if 'q' has room for at least one more byte. */
62 byteq_is_full(const struct byteq *q)
64 return !byteq_avail(q);
67 /* Adds 'c' at the head of 'q', which must not be full. */
69 byteq_put(struct byteq *q, uint8_t c)
71 ovs_assert(!byteq_is_full(q));
76 /* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
77 * bytes of free space. */
79 byteq_putn(struct byteq *q, const void *p_, size_t n)
81 const uint8_t *p = p_;
82 ovs_assert(byteq_avail(q) >= n);
84 size_t chunk = MIN(n, byteq_headroom(q));
85 memcpy(byteq_head(q), p, chunk);
86 byteq_advance_head(q, chunk);
92 /* Appends null-terminated string 's' to the head of 'q', which must have
93 * enough space. The null terminator is not added to 'q'. */
95 byteq_put_string(struct byteq *q, const char *s)
97 byteq_putn(q, s, strlen(s));
100 /* Removes a byte from the tail of 'q' and returns it. 'q' must not be
103 byteq_get(struct byteq *q)
106 ovs_assert(!byteq_is_empty(q));
112 /* Writes as much of 'q' as possible to 'fd'. Returns 0 if 'q' is fully
113 * drained by the write, otherwise a positive errno value (e.g. EAGAIN if a
114 * socket or tty buffer filled up). */
116 byteq_write(struct byteq *q, int fd)
118 while (!byteq_is_empty(q)) {
119 ssize_t n = write(fd, byteq_tail(q), byteq_tailroom(q));
121 byteq_advance_tail(q, n);
130 /* Reads as much possible from 'fd' into 'q'. Returns 0 if 'q' is completely
131 * filled up by the read, EOF if end-of-file was reached before 'q' was filled,
132 * and otherwise a positive errno value (e.g. EAGAIN if a socket or tty buffer
135 byteq_read(struct byteq *q, int fd)
137 while (!byteq_is_full(q)) {
138 ssize_t n = read(fd, byteq_head(q), byteq_headroom(q));
140 byteq_advance_head(q, n);
142 return !n ? EOF : errno;
148 /* Returns the number of contiguous bytes of in-use space starting at the tail
151 byteq_tailroom(const struct byteq *q)
153 int used = byteq_used(q);
154 int tail_to_end = q->size - (q->tail & (q->size - 1));
155 return MIN(used, tail_to_end);
158 /* Returns the first in-use byte of 'q', the point at which data is removed
161 byteq_tail(const struct byteq *q)
163 return &q->buffer[q->tail & (q->size - 1)];
166 /* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
169 byteq_advance_tail(struct byteq *q, unsigned int n)
171 ovs_assert(byteq_tailroom(q) >= n);
175 /* Returns the byte after the last in-use byte of 'q', the point at which new
176 * data will be added to 'q'. */
178 byteq_head(struct byteq *q)
180 return &q->buffer[q->head & (q->size - 1)];
183 /* Returns the number of contiguous bytes of free space starting at the head
186 byteq_headroom(const struct byteq *q)
188 int avail = byteq_avail(q);
189 int head_to_end = q->size - (q->head & (q->size - 1));
190 return MIN(avail, head_to_end);
193 /* Adds to 'q' the 'n' bytes after the last currently in-use byte of 'q'. 'q'
194 * must have at least 'n' bytes of headroom. */
196 byteq_advance_head(struct byteq *q, unsigned int n)
198 ovs_assert(byteq_headroom(q) >= n);