summaryrefslogtreecommitdiff
path: root/extras/ezio/byteq.c
blob: 31d48aad23acf8a969432875dd3bac4c4eb3607c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Copyright (c) 2008, 2009 Nicira Networks, Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * In addition, as a special exception, Nicira Networks gives permission
 * to link the code of its release of vswitchd with the OpenSSL project's
 * "OpenSSL" library (or with modified versions of it that use the same
 * license as the "OpenSSL" library), and distribute the linked
 * executables.  You must obey the GNU General Public License in all
 * respects for all of the code used other than "OpenSSL".  If you modify
 * this file, you may extend this exception to your version of the file,
 * but you are not obligated to do so.  If you do not wish to do so,
 * delete this exception statement from your version.
 *
 */

#include <config.h>
#include "extras/ezio/byteq.h"
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "util.h"

/* The queue size must be a power of 2. */
BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));

static uint8_t *head(struct byteq *);
static int headroom(const struct byteq *);
static void advance_head(struct byteq *, unsigned int n);
static int tailroom(const struct byteq *);
static const uint8_t *tail(const struct byteq *);
static void advance_tail(struct byteq *, unsigned int n);

/* Initializes 'q' as empty. */
void
byteq_init(struct byteq *q)
{
    q->head = q->tail = 0;
}

/* Returns the number of bytes current queued in 'q'. */
int
byteq_used(const struct byteq *q)
{
    return q->head - q->tail;
}

/* Returns the number of bytes that can be added to 'q' without overflow. */
int
byteq_avail(const struct byteq *q)
{
    return BYTEQ_SIZE - byteq_used(q);
}

/* Returns true if no bytes are queued in 'q',
 * false if at least one byte is queued.  */
bool
byteq_is_empty(const struct byteq *q)
{
    return !byteq_used(q);
}

/* Returns true if 'q' has no room to queue additional bytes,
 * false if 'q' has room for at least one more byte.  */
bool
byteq_is_full(const struct byteq *q)
{
    return !byteq_avail(q);
}

/* Adds 'c' at the head of 'q', which must not be full. */
void
byteq_put(struct byteq *q, uint8_t c)
{
    assert(!byteq_is_full(q));
    *head(q) = c;
    q->head++;
}

/* Adds the 'n' bytes in 'p' at the head of 'q', which must have at least 'n'
 * bytes of free space. */
void
byteq_putn(struct byteq *q, const void *p_, size_t n)
{
    const uint8_t *p = p_;
    assert(byteq_avail(q) >= n);
    while (n > 0) {
        size_t chunk = MIN(n, headroom(q));
        memcpy(head(q), p, chunk);
        advance_head(q, chunk);
        p += chunk;
        n -= chunk;
    }
}

/* Appends null-terminated string 's' to the head of 'q', which must have
 * enough space.  The null terminator is not added to 'q'. */
void
byteq_put_string(struct byteq *q, const char *s)
{
    byteq_putn(q, s, strlen(s));
}

/* Removes a byte from the tail of 'q' and returns it.  'q' must not be
 * empty. */
uint8_t
byteq_get(struct byteq *q)
{
    uint8_t c;
    assert(!byteq_is_empty(q));
    c = *tail(q);
    q->tail++;
    return c;
}

/* Writes as much of 'q' as possible to 'fd'.  Returns 0 if 'q' is fully
 * drained by the write, otherwise a positive errno value (e.g. EAGAIN if a
 * socket or tty buffer filled up). */
int
byteq_write(struct byteq *q, int fd)
{
    while (!byteq_is_empty(q)) {
        ssize_t n = write(fd, tail(q), tailroom(q));
        if (n > 0) {
            advance_tail(q, n);
        } else {
            assert(n < 0);
            return errno;
        }
    }
    return 0;
}

/* Reads as much possible from 'fd' into 'q'.  Returns 0 if 'q' is completely
 * filled up by the read, EOF if end-of-file was reached before 'q' was filled,
 * and otherwise a positive errno value (e.g. EAGAIN if a socket or tty buffer
 * was drained). */
int
byteq_read(struct byteq *q, int fd)
{
    while (!byteq_is_full(q)) {
        ssize_t n = read(fd, head(q), headroom(q));
        if (n > 0) {
            advance_head(q, n);
        } else {
            return !n ? EOF : errno;
        }
    }
    return 0;
}

/* Returns the number of contiguous bytes of in-use space starting at the tail
 * of 'q'. */
static int
tailroom(const struct byteq *q)
{
    int used = byteq_used(q);
    int tail_to_end = BYTEQ_SIZE - (q->tail & (BYTEQ_SIZE - 1));
    return MIN(used, tail_to_end);
}

/* Returns the first in-use byte of 'q', the point at which data is removed
 * from 'q'. */
static const uint8_t *
tail(const struct byteq *q)
{
    return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
}

/* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
 * of tailroom. */
static void
advance_tail(struct byteq *q, unsigned int n)
{
    assert(tailroom(q) >= n);
    q->tail += n;
}

/* Returns the byte after the last in-use byte of 'q', the point at which new
 * data will be added to 'q'. */
static uint8_t *
head(struct byteq *q)
{
    return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
}

/* Returns the number of contiguous bytes of free space starting at the head
 * of 'q'. */
static int
headroom(const struct byteq *q)
{
    int avail = byteq_avail(q);
    int head_to_end = BYTEQ_SIZE - (q->head & (BYTEQ_SIZE - 1));
    return MIN(avail, head_to_end);
}

/* Adds to 'q' the 'n' bytes after the last currently in-use byte of 'q'.  'q'
 * must have at least 'n' bytes of headroom. */
static void
advance_head(struct byteq *q, unsigned int n)
{
    assert(headroom(q) >= n);
    q->head += n;
}