Annotation of src/usr.bin/sndiod/abuf.c, Revision 1.1
1.1 ! ratchov 1: /* $OpenBSD$ */
! 2: /*
! 3: * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
! 4: *
! 5: * Permission to use, copy, modify, and distribute this software for any
! 6: * purpose with or without fee is hereby granted, provided that the above
! 7: * copyright notice and this permission notice appear in all copies.
! 8: *
! 9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
! 10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
! 11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
! 12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
! 13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
! 14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
! 15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
! 16: */
! 17: /*
! 18: * Simple byte fifo.
! 19: *
! 20: * The abuf data is split in two parts: (1) valid data available to the reader
! 21: * (2) space available to the writer, which is not necessarily unused. It works
! 22: * as follows: the write starts filling at offset (start + used), once the data
! 23: * is ready, the writer adds to used the count of bytes available.
! 24: */
! 25: #include <stdio.h>
! 26: #include <stdlib.h>
! 27: #include <string.h>
! 28:
! 29: #include "abuf.h"
! 30: #include "defs.h"
! 31: #include "utils.h"
! 32:
! 33: #ifdef DEBUG
! 34: void
! 35: abuf_log(struct abuf *buf)
! 36: {
! 37: log_putu(buf->start);
! 38: log_puts("+");
! 39: log_putu(buf->used);
! 40: log_puts("/");
! 41: log_putu(buf->len);
! 42: }
! 43: #endif
! 44:
! 45: void
! 46: abuf_init(struct abuf *buf, unsigned int len)
! 47: {
! 48: buf->data = xmalloc(len);
! 49: buf->len = len;
! 50: buf->used = 0;
! 51: buf->start = 0;
! 52: }
! 53:
! 54: void
! 55: abuf_done(struct abuf *buf)
! 56: {
! 57: #ifdef DEBUG
! 58: if (buf->used > 0) {
! 59: if (log_level >= 3) {
! 60: log_puts("deleting non-empty buffer, used = ");
! 61: log_putu(buf->used);
! 62: log_puts("\n");
! 63: }
! 64: }
! 65: #endif
! 66: xfree(buf->data);
! 67: buf->data = (void *)0xdeadbeef;
! 68: }
! 69:
! 70: /*
! 71: * return the reader pointer and the number of bytes available
! 72: */
! 73: unsigned char *
! 74: abuf_rgetblk(struct abuf *buf, int *rsize)
! 75: {
! 76: unsigned int count;
! 77:
! 78: count = buf->len - buf->start;
! 79: if (count > buf->used)
! 80: count = buf->used;
! 81: *rsize = count;
! 82: return buf->data + buf->start;
! 83: }
! 84:
! 85: /*
! 86: * discard "count" bytes at the start postion.
! 87: */
! 88: void
! 89: abuf_rdiscard(struct abuf *buf, int count)
! 90: {
! 91: #ifdef DEBUG
! 92: if (count < 0 || count > buf->used) {
! 93: log_puts("abuf_rdiscard: bad count = ");
! 94: log_putu(count);
! 95: log_puts("\n");
! 96: panic();
! 97: }
! 98: #endif
! 99: buf->used -= count;
! 100: buf->start += count;
! 101: if (buf->start >= buf->len)
! 102: buf->start -= buf->len;
! 103: }
! 104:
! 105: /*
! 106: * advance the writer pointer by "count" bytes
! 107: */
! 108: void
! 109: abuf_wcommit(struct abuf *buf, int count)
! 110: {
! 111: #ifdef DEBUG
! 112: if (count < 0 || count > (buf->len - buf->used)) {
! 113: log_puts("abuf_wcommit: bad count = ");
! 114: log_putu(count);
! 115: log_puts("\n");
! 116: panic();
! 117: }
! 118: #endif
! 119: buf->used += count;
! 120: }
! 121:
! 122: /*
! 123: * get writer pointer and the number of bytes writable
! 124: */
! 125: unsigned char *
! 126: abuf_wgetblk(struct abuf *buf, int *rsize)
! 127: {
! 128: int end, avail, count;
! 129:
! 130: end = buf->start + buf->used;
! 131: if (end >= buf->len)
! 132: end -= buf->len;
! 133: avail = buf->len - buf->used;
! 134: count = buf->len - end;
! 135: if (count > avail)
! 136: count = avail;
! 137: *rsize = count;
! 138: return buf->data + end;
! 139: }