~james-page/ubuntu/saucy/openvswitch/1.12-snapshot

« back to all changes in this revision

Viewing changes to lib/byteq.c

  • Committer: James Page
  • Date: 2013-08-21 10:16:57 UTC
  • mfrom: (1.1.20)
  • Revision ID: james.page@canonical.com-20130821101657-3o0z0qeiv5zkwlzi
New upstream snapshot

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2008, 2009, 2012 Nicira, Inc.
 
1
/* Copyright (c) 2008, 2009, 2012, 2013 Nicira, Inc.
2
2
 *
3
3
 * Licensed under the Apache License, Version 2.0 (the "License");
4
4
 * you may not use this file except in compliance with the License.
20
20
#include <unistd.h>
21
21
#include "util.h"
22
22
 
23
 
/* The queue size must be a power of 2. */
24
 
BUILD_ASSERT_DECL(!(BYTEQ_SIZE & (BYTEQ_SIZE - 1)));
25
 
 
26
 
/* Initializes 'q' as empty. */
 
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.
 
25
 *
 
26
 * The caller must ensure that 'buffer' remains available to the byteq as long
 
27
 * as 'q' is in use. */
27
28
void
28
 
byteq_init(struct byteq *q)
 
29
byteq_init(struct byteq *q, uint8_t *buffer, size_t size)
29
30
{
 
31
    ovs_assert(is_pow2(size));
 
32
    q->buffer = buffer;
 
33
    q->size = size;
30
34
    q->head = q->tail = 0;
31
35
}
32
36
 
41
45
int
42
46
byteq_avail(const struct byteq *q)
43
47
{
44
 
    return BYTEQ_SIZE - byteq_used(q);
 
48
    return q->size - byteq_used(q);
45
49
}
46
50
 
47
51
/* Returns true if no bytes are queued in 'q',
147
151
byteq_tailroom(const struct byteq *q)
148
152
{
149
153
    int used = byteq_used(q);
150
 
    int tail_to_end = BYTEQ_SIZE - (q->tail & (BYTEQ_SIZE - 1));
 
154
    int tail_to_end = q->size - (q->tail & (q->size - 1));
151
155
    return MIN(used, tail_to_end);
152
156
}
153
157
 
156
160
const uint8_t *
157
161
byteq_tail(const struct byteq *q)
158
162
{
159
 
    return &q->buffer[q->tail & (BYTEQ_SIZE - 1)];
 
163
    return &q->buffer[q->tail & (q->size - 1)];
160
164
}
161
165
 
162
166
/* Removes 'n' bytes from the tail of 'q', which must have at least 'n' bytes
173
177
uint8_t *
174
178
byteq_head(struct byteq *q)
175
179
{
176
 
    return &q->buffer[q->head & (BYTEQ_SIZE - 1)];
 
180
    return &q->buffer[q->head & (q->size - 1)];
177
181
}
178
182
 
179
183
/* Returns the number of contiguous bytes of free space starting at the head
182
186
byteq_headroom(const struct byteq *q)
183
187
{
184
188
    int avail = byteq_avail(q);
185
 
    int head_to_end = BYTEQ_SIZE - (q->head & (BYTEQ_SIZE - 1));
 
189
    int head_to_end = q->size - (q->head & (q->size - 1));
186
190
    return MIN(avail, head_to_end);
187
191
}
188
192