sndio/sndiod/abuf.c

139 lines
3.0 KiB
C
Raw Normal View History

2012-09-02 17:13:30 -05:00
/* $OpenBSD$ */
/*
2012-11-10 07:58:53 -06:00
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
2012-09-02 17:13:30 -05:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* Simple byte fifo.
*
* The abuf data is split in two parts: (1) valid data available to the reader
* (2) space available to the writer, which is not necessarily unused. It works
* as follows: the write starts filling at offset (start + used), once the data
* is ready, the writer adds to used the count of bytes available.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "abuf.h"
#include "utils.h"
#ifdef DEBUG
void
abuf_log(struct abuf *buf)
{
log_putu(buf->start);
log_puts("+");
log_putu(buf->used);
log_puts("/");
log_putu(buf->len);
}
#endif
void
2012-11-10 17:47:00 -06:00
abuf_init(struct abuf *buf, unsigned int len)
2012-09-02 17:13:30 -05:00
{
2012-11-10 17:47:00 -06:00
buf->data = xmalloc(len);
2012-09-02 17:13:30 -05:00
buf->len = len;
buf->used = 0;
buf->start = 0;
}
void
abuf_done(struct abuf *buf)
{
2016-01-08 10:24:48 -06:00
#ifdef DEBUG
2012-09-02 17:13:30 -05:00
if (buf->used > 0) {
if (log_level >= 3) {
log_puts("deleting non-empty buffer, used = ");
log_putu(buf->used);
log_puts("\n");
}
}
#endif
2012-10-06 07:18:49 -05:00
xfree(buf->data);
2012-09-02 17:13:30 -05:00
buf->data = (void *)0xdeadbeef;
}
/*
2012-11-11 10:51:27 -06:00
* return the reader pointer and the number of bytes available
2012-09-02 17:13:30 -05:00
*/
unsigned char *
2012-10-24 07:53:37 -05:00
abuf_rgetblk(struct abuf *buf, int *rsize)
2012-09-02 17:13:30 -05:00
{
2014-11-17 01:08:10 -06:00
int count;
2012-09-02 17:13:30 -05:00
count = buf->len - buf->start;
if (count > buf->used)
count = buf->used;
*rsize = count;
2012-11-02 13:05:29 -05:00
return buf->data + buf->start;
2012-09-02 17:13:30 -05:00
}
/*
2012-11-11 10:51:27 -06:00
* discard "count" bytes at the start postion.
2012-09-02 17:13:30 -05:00
*/
void
2012-10-24 07:53:37 -05:00
abuf_rdiscard(struct abuf *buf, int count)
2012-09-02 17:13:30 -05:00
{
#ifdef DEBUG
2012-10-24 07:53:37 -05:00
if (count < 0 || count > buf->used) {
2012-09-02 17:13:30 -05:00
log_puts("abuf_rdiscard: bad count = ");
log_putu(count);
log_puts("\n");
panic();
}
#endif
buf->used -= count;
buf->start += count;
if (buf->start >= buf->len)
buf->start -= buf->len;
}
/*
2012-11-11 10:51:27 -06:00
* advance the writer pointer by "count" bytes
2012-09-02 17:13:30 -05:00
*/
void
2012-10-24 07:53:37 -05:00
abuf_wcommit(struct abuf *buf, int count)
2012-09-02 17:13:30 -05:00
{
#ifdef DEBUG
2012-10-24 07:53:37 -05:00
if (count < 0 || count > (buf->len - buf->used)) {
2012-09-02 17:13:30 -05:00
log_puts("abuf_wcommit: bad count = ");
log_putu(count);
log_puts("\n");
panic();
}
#endif
buf->used += count;
}
/*
2012-11-11 10:51:27 -06:00
* get writer pointer and the number of bytes writable
2012-09-02 17:13:30 -05:00
*/
unsigned char *
2012-10-24 07:53:37 -05:00
abuf_wgetblk(struct abuf *buf, int *rsize)
2012-09-02 17:13:30 -05:00
{
2012-10-24 07:53:37 -05:00
int end, avail, count;
2012-09-02 17:13:30 -05:00
end = buf->start + buf->used;
if (end >= buf->len)
end -= buf->len;
avail = buf->len - buf->used;
count = buf->len - end;
if (count > avail)
count = avail;
*rsize = count;
2012-11-02 13:05:29 -05:00
return buf->data + end;
2012-09-02 17:13:30 -05:00
}