1 /* Copyright (c) 2008, 2009, 2012 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 /* The queue size must be a power of 2. */
24 BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));
26 /* Initializes 'q' as empty. */
28 byteq_init(struct byteq *q)
30 q->head = q->tail = 0;
33 /* Returns the number of bytes current queued in 'q'. */
35 byteq_used(const struct byteq *q)
37 return q->head - q->tail;
40 /* Returns the number of bytes that can be added to 'q' without overflow. */
42 byteq_avail(const struct byteq *q)
44 return BYTEQ_SIZE - byteq_used(q);
47 /* Returns true if no bytes are queued in 'q',
48 * false if at least one byte is queued. */
50 byteq_is_empty(const struct byteq *q)
52 return !byteq_used(q);
55 /* Returns true if 'q' has no room to queue additional bytes,
56 * false if 'q' has room for at least one more byte. */
58 byteq_is_full(const struct byteq *q)
60 return !byteq_avail(q);
63 /* Adds 'c' at the head of 'q', which must not be full. */
65 byteq_put(struct byteq *q, uint8_t c)
67 ovs_assert(!byteq_is_full(q));
72 /* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
73 * bytes of free space. */
75 byteq_putn(struct byteq *q, const void *p_, size_t n)
77 const uint8_t *p = p_;
78 ovs_assert(byteq_avail(q) >= n);
80 size_t chunk = MIN(n, byteq_headroom(q));
81 memcpy(byteq_head(q), p, chunk);
82 byteq_advance_head(q, chunk);
88 /* Appends null-terminated string 's' to the head of 'q', which must have
89 * enough space. The null terminator is not added to 'q'. */
91 byteq_put_string(struct byteq *q, const char *s)
93 byteq_putn(q, s, strlen(s));
96 /* Removes a byte from the tail of 'q' and returns it. 'q' must not be
99 byteq_get(struct byteq *q)
102 ovs_assert(!byteq_is_empty(q));
108 /* Writes as much of 'q' as possible to 'fd'. Returns 0 if 'q' is fully
109 * drained by the write, otherwise a positive errno value (e.g. EAGAIN if a
110 * socket or tty buffer filled up). */
112 byteq_write(struct byteq *q, int fd)
114 while (!byteq_is_empty(q)) {
115 ssize_t n = write(fd, byteq_tail(q), byteq_tailroom(q));
117 byteq_advance_tail(q, n);
126 /* Reads as much possible from 'fd' into 'q'. Returns 0 if 'q' is completely
127 * filled up by the read, EOF if end-of-file was reached before 'q' was filled,
128 * and otherwise a positive errno value (e.g. EAGAIN if a socket or tty buffer
131 byteq_read(struct byteq *q, int fd)
133 while (!byteq_is_full(q)) {
134 ssize_t n = read(fd, byteq_head(q), byteq_headroom(q));
136 byteq_advance_head(q, n);
138 return !n ? EOF : errno;
144 /* Returns the number of contiguous bytes of in-use space starting at the tail
147 byteq_tailroom(const struct byteq *q)
149 int used = byteq_used(q);
150 int tail_to_end = BYTEQ_SIZE - (q->tail & (BYTEQ_SIZE - 1));
151 return MIN(used, tail_to_end);
154 /* Returns the first in-use byte of 'q', the point at which data is removed
157 byteq_tail(const struct byteq *q)
159 return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
162 /* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
165 byteq_advance_tail(struct byteq *q, unsigned int n)
167 ovs_assert(byteq_tailroom(q) >= n);
171 /* Returns the byte after the last in-use byte of 'q', the point at which new
172 * data will be added to 'q'. */
174 byteq_head(struct byteq *q)
176 return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
179 /* Returns the number of contiguous bytes of free space starting at the head
182 byteq_headroom(const struct byteq *q)
184 int avail = byteq_avail(q);
185 int head_to_end = BYTEQ_SIZE - (q->head & (BYTEQ_SIZE - 1));
186 return MIN(avail, head_to_end);
189 /* Adds to 'q' the 'n' bytes after the last currently in-use byte of 'q'. 'q'
190 * must have at least 'n' bytes of headroom. */
192 byteq_advance_head(struct byteq *q, unsigned int n)
194 ovs_assert(byteq_headroom(q) >= n);