2 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
5 * "THE BEER-WARE LICENSE" (Revision 42):
6 * Sergey Lyubka wrote this file. As long as you retain this notice you
7 * can do whatever you want with this stuff. If we meet some day, and you think
8 * this stuff is worth it, you can buy me a beer in return.
11 #ifndef IO_HEADER_INCLUDED
12 #define IO_HEADER_INCLUDED
18 * I/O buffer descriptor
21 char *buf; /* IO Buffer */
22 size_t size; /* IO buffer size */
23 size_t head; /* Bytes read */
24 size_t tail; /* Bytes written */
25 size_t total; /* Total bytes read */
29 io_clear(struct io *io)
31 assert(io->buf != NULL);
33 io->total = io->tail = io->head = 0;
36 static __inline char *
37 io_space(struct io *io)
39 assert(io->buf != NULL);
41 assert(io->head <= io->size);
42 return (io->buf + io->head);
45 static __inline char *
46 io_data(struct io *io)
48 assert(io->buf != NULL);
50 assert(io->tail <= io->size);
51 return (io->buf + io->tail);
54 static __inline size_t
55 io_space_len(const struct io *io)
57 assert(io->buf != NULL);
59 assert(io->head <= io->size);
60 return (io->size - io->head);
63 static __inline size_t
64 io_data_len(const struct io *io)
66 assert(io->buf != NULL);
68 assert(io->head <= io->size);
69 assert(io->tail <= io->head);
70 return (io->head - io->tail);
74 io_inc_tail(struct io *io, size_t n)
76 assert(io->buf != NULL);
78 assert(io->tail <= io->head);
79 assert(io->head <= io->size);
81 assert(io->tail <= io->head);
82 if (io->tail == io->head)
83 io->head = io->tail = 0;
87 io_inc_head(struct io *io, size_t n)
89 assert(io->buf != NULL);
91 assert(io->tail <= io->head);
94 assert(io->head <= io->size);
97 #endif /* IO_HEADER_INCLUDED */