Files
OpenCellular/common/queue.c
Vic Yang 8bed9853d7 Bug fix of queue empty slot calculation
Current implementation of queue_has_space doesn't handle the case where
queue tail has wrapped around the end. This CL fixes the bug.

BUG=None
TEST=Pass the test in the following CL.
BRANCH=link

Change-Id: I774f388081af50f0e930a6cbc3a723da1c8283b0
Signed-off-by: Vic Yang <victoryang@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/58031
Reviewed-by: Yung-Chieh Lo <yjlou@chromium.org>
2013-06-09 21:33:17 -07:00

58 lines
1.2 KiB
C

/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Queue data structure implementation.
*/
#include "queue.h"
#include "util.h"
void queue_reset(struct queue *queue)
{
queue->head = queue->tail = 0;
}
int queue_is_empty(const struct queue *q)
{
return q->head == q->tail;
}
int queue_has_space(const struct queue *q, int unit_count)
{
if (q->tail >= q->head)
return (q->tail + unit_count * q->unit_bytes) <=
(q->head + q->buf_bytes - q->unit_bytes);
else
return (q->tail + unit_count * q->unit_bytes) <=
(q->head - q->unit_bytes);
}
void queue_add_units(struct queue *q, const void *src, int unit_count)
{
const uint8_t *s = (const uint8_t *)src;
if (!queue_has_space(q, unit_count))
return;
for (unit_count *= q->unit_bytes; unit_count; unit_count--) {
q->buf[q->tail++] = *(s++);
q->tail %= q->buf_bytes;
}
}
int queue_remove_unit(struct queue *q, void *dest)
{
int count;
uint8_t *d = (uint8_t *)dest;
if (queue_is_empty(q))
return 0;
for (count = q->unit_bytes; count; count--) {
*(d++) = q->buf[q->head++];
q->head %= q->buf_bytes;
}
return 1;
}