Annotation of src/usr.bin/aucat/abuf.c, Revision 1.31
1.31 ! jmc 1: /* $OpenBSD: abuf.c,v 1.30 2017/01/03 07:33:22 ratchov Exp $ */
1.1 ratchov 2: /*
1.26 ratchov 3: * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
1.1 ratchov 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: /*
1.26 ratchov 18: * Simple byte fifo.
1.1 ratchov 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.
1.7 ratchov 24: */
1.1 ratchov 25: #include <stdlib.h>
26:
1.13 ratchov 27: #include "abuf.h"
1.26 ratchov 28: #include "utils.h"
1.1 ratchov 29:
1.17 ratchov 30: #ifdef DEBUG
31: void
1.26 ratchov 32: abuf_log(struct abuf *buf)
1.17 ratchov 33: {
1.26 ratchov 34: log_putu(buf->start);
35: log_puts("+");
36: log_putu(buf->used);
37: log_puts("/");
38: log_putu(buf->len);
1.17 ratchov 39: }
1.26 ratchov 40: #endif
1.17 ratchov 41:
42: void
1.26 ratchov 43: abuf_init(struct abuf *buf, unsigned int len)
1.1 ratchov 44: {
1.26 ratchov 45: buf->data = xmalloc(len);
46: buf->len = len;
1.1 ratchov 47: buf->used = 0;
48: buf->start = 0;
49: }
50:
51: void
1.26 ratchov 52: abuf_done(struct abuf *buf)
1.1 ratchov 53: {
1.28 ratchov 54: #ifdef DEBUG
1.26 ratchov 55: if (buf->used > 0) {
56: if (log_level >= 3) {
57: log_puts("deleting non-empty buffer, used = ");
58: log_putu(buf->used);
59: log_puts("\n");
1.17 ratchov 60: }
61: }
62: #endif
1.30 ratchov 63: xfree(buf->data);
1.26 ratchov 64: buf->data = (void *)0xdeadbeef;
1.1 ratchov 65: }
66:
67: /*
1.26 ratchov 68: * return the reader pointer and the number of bytes available
1.1 ratchov 69: */
70: unsigned char *
1.26 ratchov 71: abuf_rgetblk(struct abuf *buf, int *rsize)
1.1 ratchov 72: {
1.26 ratchov 73: int count;
1.1 ratchov 74:
1.26 ratchov 75: count = buf->len - buf->start;
76: if (count > buf->used)
77: count = buf->used;
1.1 ratchov 78: *rsize = count;
1.26 ratchov 79: return buf->data + buf->start;
1.3 ratchov 80: }
81:
82: /*
1.31 ! jmc 83: * discard "count" bytes at the start position.
1.3 ratchov 84: */
85: void
1.26 ratchov 86: abuf_rdiscard(struct abuf *buf, int count)
1.3 ratchov 87: {
1.17 ratchov 88: #ifdef DEBUG
1.26 ratchov 89: if (count < 0 || count > buf->used) {
90: log_puts("abuf_rdiscard: bad count = ");
91: log_putu(count);
92: log_puts("\n");
93: panic();
1.22 ratchov 94: }
1.17 ratchov 95: #endif
1.3 ratchov 96: buf->used -= count;
97: buf->start += count;
98: if (buf->start >= buf->len)
99: buf->start -= buf->len;
100: }
101:
102: /*
1.26 ratchov 103: * advance the writer pointer by "count" bytes
1.3 ratchov 104: */
105: void
1.26 ratchov 106: abuf_wcommit(struct abuf *buf, int count)
1.3 ratchov 107: {
1.17 ratchov 108: #ifdef DEBUG
1.26 ratchov 109: if (count < 0 || count > (buf->len - buf->used)) {
110: log_puts("abuf_wcommit: bad count = ");
111: log_putu(count);
112: log_puts("\n");
113: panic();
1.17 ratchov 114: }
115: #endif
1.3 ratchov 116: buf->used += count;
1.1 ratchov 117: }
118:
119: /*
1.26 ratchov 120: * get writer pointer and the number of bytes writable
1.1 ratchov 121: */
122: unsigned char *
1.26 ratchov 123: abuf_wgetblk(struct abuf *buf, int *rsize)
1.1 ratchov 124: {
1.26 ratchov 125: int end, avail, count;
1.1 ratchov 126:
1.26 ratchov 127: end = buf->start + buf->used;
1.1 ratchov 128: if (end >= buf->len)
129: end -= buf->len;
1.26 ratchov 130: avail = buf->len - buf->used;
1.1 ratchov 131: count = buf->len - end;
132: if (count > avail)
1.26 ratchov 133: count = avail;
1.1 ratchov 134: *rsize = count;
1.26 ratchov 135: return buf->data + end;
1.1 ratchov 136: }