1 /*
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright © 2022 Keith Packard
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
17 *
18 * 3. Neither the name of the copyright holder nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
27 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33 * OF THE POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 #ifndef _STDIO_BUFIO_H_
37 #define _STDIO_BUFIO_H_
38
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <sys/lock.h>
42
43 #define __BALL 0x0001 /* bufio buf is allocated by stdio */
44 #define __BLBF 0x0002 /* bufio is line buffered */
45 #define __BFALL 0x0004 /* FILE is allocated by stdio */
46 #define __BFPTR 0x0008 /* funcs need pointers instead of ints */
47
48 union __file_bufio_cookie {
49 int fd;
50 void *ptr;
51 };
52
53 struct __file_bufio {
54 struct __file_ext xfile;
55 const void *ptr;
56 uint8_t dir;
57 uint8_t bflags;
58 __off_t pos; /* FD position */
59 char *buf;
60 int size; /* sizeof buf */
61 int len; /* valid data in buf */
62 int off; /* offset of data in buf */
63 union {
64 ssize_t (*read_int)(int fd, void *buf, size_t count);
65 ssize_t (*read_ptr)(void *ptr, void *buf, size_t count);
66 };
67 union {
68 ssize_t (*write_int)(int fd, const void *buf, size_t count);
69 ssize_t (*write_ptr)(void *ptr, const void *buf, size_t count);
70 };
71 union {
72 __off_t (*lseek_int)(int fd, __off_t offset, int whence);
73 __off_t (*lseek_ptr)(void *ptr, __off_t offset, int whence);
74 };
75 union {
76 int (*close_int)(int fd);
77 int (*close_ptr)(void *ptr);
78 };
79 #ifndef __SINGLE_THREAD__
80 _LOCK_T lock;
81 #endif
82 };
83
84 #define FDEV_SETUP_BUFIO(_fd, _buf, _size, _read, _write, _lseek, _close, _rwflag, _bflags) \
85 { \
86 .xfile = FDEV_SETUP_EXT(__bufio_put, __bufio_get, \
87 __bufio_flush, __bufio_close, \
88 __bufio_seek, __bufio_setvbuf, \
89 (_rwflag) | __SBUF), \
90 .ptr = (void *) (intptr_t) (_fd), \
91 .dir = 0, \
92 .bflags = (_bflags), \
93 .pos = 0, \
94 .buf = _buf, \
95 .size = _size, \
96 .len = 0, \
97 .off = 0, \
98 .read_int = _read, \
99 .write_int = _write, \
100 .lseek_int = _lseek, \
101 .close_int = _close, \
102 }
103
104 #define FDEV_SETUP_BUFIO_PTR(_ptr, _buf, _size, _read, _write, _lseek, _close, _rwflag, _bflags) \
105 { \
106 .xfile = FDEV_SETUP_EXT(__bufio_put, __bufio_get, \
107 __bufio_flush, __bufio_close, \
108 __bufio_seek, __bufio_setvbuf, \
109 (_rwflag) | __SBUF), \
110 .ptr = _ptr, \
111 .dir = 0, \
112 .bflags = (_bflags) | __BFPTR, \
113 .pos = 0, \
114 .buf = _buf, \
115 .size = _size, \
116 .len = 0, \
117 .off = 0, \
118 .read_ptr = _read, \
119 .write_ptr = _write, \
120 .lseek_ptr = _lseek, \
121 .close_ptr = _close, \
122 }
123
__bufio_lock_init(FILE * f)124 static inline void __bufio_lock_init(FILE *f) {
125 (void) f;
126 __lock_init(((struct __file_bufio *) f)->lock);
127 }
128
__bufio_lock_close(FILE * f)129 static inline void __bufio_lock_close(FILE *f) {
130 (void) f;
131 __lock_release(((struct __file_bufio *) f)->lock);
132 __lock_close(((struct __file_bufio *) f)->lock);
133 }
134
__bufio_lock(FILE * f)135 static inline void __bufio_lock(FILE *f) {
136 (void) f;
137 __lock_acquire(((struct __file_bufio *) f)->lock);
138 }
139
__bufio_unlock(FILE * f)140 static inline void __bufio_unlock(FILE *f) {
141 (void) f;
142 __lock_release(((struct __file_bufio *) f)->lock);
143 }
144
145 int
146 __bufio_flush_locked(FILE *f);
147
148 int
149 __bufio_fill_locked(FILE *f);
150
151 int
152 __bufio_setdir_locked(FILE *f, uint8_t dir);
153
154 int
155 __bufio_flush(FILE *f);
156
157 int
158 __bufio_put(char c, FILE *f);
159
160 int
161 __bufio_get(FILE *f);
162
163 off_t
164 __bufio_seek(FILE *f, off_t offset, int whence);
165
166 int
167 __bufio_setvbuf(FILE *f, char *buf, int mode, size_t size);
168
169 int
170 __bufio_close(FILE *f);
171
172 #endif /* _STDIO_BUFIO_H_ */
173