]> git.proxmox.com Git - mirror_ovs.git/blob - lib/byteq.c
Global replace of Nicira Networks.
[mirror_ovs.git] / lib / byteq.c
1 /* Copyright (c) 2008, 2009 Nicira, Inc.
2 *
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16 #include <config.h>
17 #include "byteq.h"
18 #include <assert.h>
19 #include <errno.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include "util.h"
23
24 /* The queue size must be a power of 2. */
25 BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));
26
27 /* Initializes 'q' as empty. */
28 void
29 byteq_init(struct byteq *q)
30 {
31 q->head = q->tail = 0;
32 }
33
34 /* Returns the number of bytes current queued in 'q'. */
35 int
36 byteq_used(const struct byteq *q)
37 {
38 return q->head - q->tail;
39 }
40
41 /* Returns the number of bytes that can be added to 'q' without overflow. */
42 int
43 byteq_avail(const struct byteq *q)
44 {
45 return BYTEQ_SIZE - byteq_used(q);
46 }
47
48 /* Returns true if no bytes are queued in 'q',
49 * false if at least one byte is queued. */
50 bool
51 byteq_is_empty(const struct byteq *q)
52 {
53 return !byteq_used(q);
54 }
55
56 /* Returns true if 'q' has no room to queue additional bytes,
57 * false if 'q' has room for at least one more byte. */
58 bool
59 byteq_is_full(const struct byteq *q)
60 {
61 return !byteq_avail(q);
62 }
63
64 /* Adds 'c' at the head of 'q', which must not be full. */
65 void
66 byteq_put(struct byteq *q, uint8_t c)
67 {
68 assert(!byteq_is_full(q));
69 *byteq_head(q) = c;
70 q->head++;
71 }
72
73 /* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
74 * bytes of free space. */
75 void
76 byteq_putn(struct byteq *q, const void *p_, size_t n)
77 {
78 const uint8_t *p = p_;
79 assert(byteq_avail(q) >= n);
80 while (n > 0) {
81 size_t chunk = MIN(n, byteq_headroom(q));
82 memcpy(byteq_head(q), p, chunk);
83 byteq_advance_head(q, chunk);
84 p += chunk;
85 n -= chunk;
86 }
87 }
88
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'. */
91 void
92 byteq_put_string(struct byteq *q, const char *s)
93 {
94 byteq_putn(q, s, strlen(s));
95 }
96
97 /* Removes a byte from the tail of 'q' and returns it. 'q' must not be
98 * empty. */
99 uint8_t
100 byteq_get(struct byteq *q)
101 {
102 uint8_t c;
103 assert(!byteq_is_empty(q));
104 c = *byteq_tail(q);
105 q->tail++;
106 return c;
107 }
108
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). */
112 int
113 byteq_write(struct byteq *q, int fd)
114 {
115 while (!byteq_is_empty(q)) {
116 ssize_t n = write(fd, byteq_tail(q), byteq_tailroom(q));
117 if (n > 0) {
118 byteq_advance_tail(q, n);
119 } else {
120 assert(n < 0);
121 return errno;
122 }
123 }
124 return 0;
125 }
126
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
130 * was drained). */
131 int
132 byteq_read(struct byteq *q, int fd)
133 {
134 while (!byteq_is_full(q)) {
135 ssize_t n = read(fd, byteq_head(q), byteq_headroom(q));
136 if (n > 0) {
137 byteq_advance_head(q, n);
138 } else {
139 return !n ? EOF : errno;
140 }
141 }
142 return 0;
143 }
144
145 /* Returns the number of contiguous bytes of in-use space starting at the tail
146 * of 'q'. */
147 int
148 byteq_tailroom(const struct byteq *q)
149 {
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);
153 }
154
155 /* Returns the first in-use byte of 'q', the point at which data is removed
156 * from 'q'. */
157 const uint8_t *
158 byteq_tail(const struct byteq *q)
159 {
160 return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
161 }
162
163 /* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
164 * of tailroom. */
165 void
166 byteq_advance_tail(struct byteq *q, unsigned int n)
167 {
168 assert(byteq_tailroom(q) >= n);
169 q->tail += n;
170 }
171
172 /* Returns the byte after the last in-use byte of 'q', the point at which new
173 * data will be added to 'q'. */
174 uint8_t *
175 byteq_head(struct byteq *q)
176 {
177 return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
178 }
179
180 /* Returns the number of contiguous bytes of free space starting at the head
181 * of 'q'. */
182 int
183 byteq_headroom(const struct byteq *q)
184 {
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);
188 }
189
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. */
192 void
193 byteq_advance_head(struct byteq *q, unsigned int n)
194 {
195 assert(byteq_headroom(q) >= n);
196 q->head += n;
197 }