[BACK]Return to abuf.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/abuf.c, Revision 1.26

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