]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
ee0a77329c6fd693959473e374eb6b313b9184cd
[libucw.git] / lib / fastbuf.h
1 /*
2  *      UCW Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2007 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_FASTBUF_H
12 #define _UCW_FASTBUF_H
13
14 #include <string.h>
15 #include <alloca.h>
16
17 /*
18  *  Generic buffered I/O. You supply hooks to be called for low-level operations
19  *  (swapping of buffers, seeking and closing), we do the rest.
20  *
21  *  Buffer layout when reading:
22  *
23  *  +----------------+---------------------------+
24  *  | read data      | free space                |
25  *  +----------------+---------------------------+
26  *  ^        ^        ^                           ^
27  *  buffer   bptr     bstop                       bufend
28  *
29  *  After the last character is read, bptr == bstop and buffer refill
30  *  is deferred to the next read attempt. This gives us an easy way
31  *  how to implement bungetc().
32  *
33  *  When writing:
34  *
35  *  +--------+--------------+--------------------+
36  *  | unused | written data | free space         |
37  *  +--------+--------------+--------------------+
38  *  ^         ^              ^                    ^
39  *  buffer    bstop          bptr                 bufend
40  *
41  *  Dirty tricks:
42  *
43  *    - You can mix reads and writes on the same stream, but you must
44  *      call bflush() in between and remember that the file position
45  *      points after the flushed buffer which is not necessarily the same
46  *      as after the data you've read.
47  *    - The spout/refill hooks can change not only bptr and bstop, but also
48  *      the location of the buffer; fb-mem.c takes advantage of it.
49  *    - In some cases, the user of the bdirect interface can be allowed to modify
50  *      the data in the buffer to avoid unnecessary copying. If the back-end
51  *      allows such modifications, it can set can_overwrite_buffer accordingly:
52  *              *  0 if no modification is allowed,
53  *              *  1 if the user can modify the buffer on the condition that
54  *                   the modifications will be undone before calling the next
55  *                   fastbuf operation
56  *              *  2 if the user is allowed to overwrite the data in the buffer
57  *                   if bdirect_read_commit_modified() is called afterwards.
58  *                   In this case, the back-end must be prepared for trimming
59  *                   of the buffer which is done by the commit function.
60  */
61
62 struct fastbuf {
63   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
64   byte *bptr, *bstop;                   /* Access pointers */
65   byte *buffer, *bufend;                /* Start and end of the buffer */
66   byte *name;                           /* File name for error messages */
67   sh_off_t pos;                         /* Position of bstop in the file */
68   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
69   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
70   int (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed; returns success */
71   void (*close)(struct fastbuf *);      /* Close the stream */
72   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
73   int can_overwrite_buffer;             /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
74 };
75
76 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
77
78 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
79 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
80 struct fastbuf *bopen_tmp(uns buflen);
81 struct fastbuf *bfdopen(int fd, uns buflen);
82 struct fastbuf *bfdopen_shared(int fd, uns buflen);
83 void bfilesync(struct fastbuf *b);
84
85 #define TEMP_FILE_NAME_LEN 256
86 void temp_file_name(byte *name);
87
88 /* FastIO on in-memory streams */
89
90 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
91 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
92
93 /* FastIO on memory mapped files */
94
95 struct fastbuf *bopen_mm(byte *name, uns mode);
96
97 /* FastIO on files opened with O_DIRECT (see fb-direct.c for description) */
98
99 struct asio_queue;
100 struct fastbuf *fbdir_open(byte *name, uns mode, struct asio_queue *io_queue);
101 struct fastbuf *fbdir_open_try(byte *name, uns mode, struct asio_queue *io_queue);
102 struct fastbuf *fbdir_open_fd(int fd, struct asio_queue *io_queue);
103 struct fastbuf *fbdir_open_tmp(struct asio_queue *io_queue);
104
105 /* FastI on file descriptors with limit */
106
107 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
108
109 /* FastIO on static buffers */
110
111 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
112 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
113 static inline uns
114 fbbuf_count_written(struct fastbuf *f)
115 {
116   return f->bptr - f->bstop;
117 }
118
119 /* FastIO on recyclable growing buffers */
120
121 struct fastbuf *fbgrow_create(unsigned basic_size);
122 void fbgrow_reset(struct fastbuf *b);                   /* Reset stream and prepare for writing */
123 void fbgrow_rewind(struct fastbuf *b);                  /* Prepare for reading */
124
125 /* FastO with atomic writes for multi-threaded programs */
126
127 struct fb_atomic {
128   struct fastbuf fb;
129   struct fb_atomic_file *af;
130   byte *expected_max_bptr;
131   uns slack_size;
132 };
133 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
134
135 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
136 void fbatomic_internal_write(struct fastbuf *b);
137
138 static inline void
139 fbatomic_commit(struct fastbuf *b)
140 {
141   if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
142     fbatomic_internal_write(b);
143 }
144
145 /* Configuring stream parameters */
146
147 int bconfig(struct fastbuf *f, uns type, int data);
148
149 #define BCONFIG_IS_TEMP_FILE 0
150
151 /* Universal functions working on all fastbuf's */
152
153 void bclose(struct fastbuf *f);
154 void bflush(struct fastbuf *f);
155 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
156 void bsetpos(struct fastbuf *f, sh_off_t pos);
157 void brewind(struct fastbuf *f);
158 sh_off_t bfilesize(struct fastbuf *f);          // -1 if not seekable
159
160 static inline sh_off_t btell(struct fastbuf *f)
161 {
162   return f->pos + (f->bptr - f->bstop);
163 }
164
165 int bgetc_slow(struct fastbuf *f);
166 static inline int bgetc(struct fastbuf *f)
167 {
168   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
169 }
170
171 int bpeekc_slow(struct fastbuf *f);
172 static inline int bpeekc(struct fastbuf *f)
173 {
174   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
175 }
176
177 static inline void bungetc(struct fastbuf *f)
178 {
179   f->bptr--;
180 }
181
182 void bputc_slow(struct fastbuf *f, uns c);
183 static inline void bputc(struct fastbuf *f, uns c)
184 {
185   if (f->bptr < f->bufend)
186     *f->bptr++ = c;
187   else
188     bputc_slow(f, c);
189 }
190
191 static inline uns
192 bavailr(struct fastbuf *f)
193 {
194   return f->bstop - f->bptr;
195 }
196
197 static inline uns
198 bavailw(struct fastbuf *f)
199 {
200   return f->bufend - f->bptr;
201 }
202
203 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
204 static inline uns bread(struct fastbuf *f, void *b, uns l)
205 {
206   if (bavailr(f) >= l)
207     {
208       memcpy(b, f->bptr, l);
209       f->bptr += l;
210       return l;
211     }
212   else
213     return bread_slow(f, b, l, 0);
214 }
215
216 static inline uns breadb(struct fastbuf *f, void *b, uns l)
217 {
218   if (bavailr(f) >= l)
219     {
220       memcpy(b, f->bptr, l);
221       f->bptr += l;
222       return l;
223     }
224   else
225     return bread_slow(f, b, l, 1);
226 }
227
228 void bwrite_slow(struct fastbuf *f, void *b, uns l);
229 static inline void bwrite(struct fastbuf *f, void *b, uns l)
230 {
231   if (bavailw(f) >= l)
232     {
233       memcpy(f->bptr, b, l);
234       f->bptr += l;
235     }
236   else
237     bwrite_slow(f, b, l);
238 }
239
240 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
241 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
242 byte *bgets0(struct fastbuf *f, byte *b, uns l);
243
244 struct mempool;
245 struct bb_t;
246 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
247 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
248
249 struct bgets_stk_struct {
250   struct fastbuf *f;
251   byte *old_buf, *cur_buf, *src;
252   uns old_len, cur_len, src_len;
253 };
254 void bgets_stk_init(struct bgets_stk_struct *s);
255 void bgets_stk_step(struct bgets_stk_struct *s);
256 #define bgets_stk(fb) ({ struct bgets_stk_struct _s; _s.f = (fb); for (bgets_stk_init(&_s); _s.cur_len; _s.cur_buf = alloca(_s.cur_len), bgets_stk_step(&_s)); _s.cur_buf; })
257
258 static inline void
259 bputs(struct fastbuf *f, byte *b)
260 {
261   bwrite(f, b, strlen(b));
262 }
263
264 static inline void
265 bputs0(struct fastbuf *f, byte *b)
266 {
267   bwrite(f, b, strlen(b)+1);
268 }
269
270 static inline void
271 bputsn(struct fastbuf *f, byte *b)
272 {
273   bputs(f, b);
274   bputc(f, '\n');
275 }
276
277 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
278 static inline void
279 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
280 {
281   if (bavailr(f) >= l && bavailw(t) >= l)
282     {
283       memcpy(t->bptr, f->bptr, l);
284       t->bptr += l;
285       f->bptr += l;
286     }
287   else
288     bbcopy_slow(f, t, l);
289 }
290
291 int bskip_slow(struct fastbuf *f, uns len);
292 static inline int bskip(struct fastbuf *f, uns len)
293 {
294   if (bavailr(f) >= len)
295     {
296       f->bptr += len;
297       return 1;
298     }
299   else
300     return bskip_slow(f, len);
301 }
302
303 /* Direct I/O on buffers */
304
305 static inline uns
306 bdirect_read_prepare(struct fastbuf *f, byte **buf)
307 {
308   if (f->bptr == f->bstop && !f->refill(f))
309     {
310       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
311       return 0;
312     }
313   *buf = f->bptr;
314   return bavailr(f);
315 }
316
317 static inline void
318 bdirect_read_commit(struct fastbuf *f, byte *pos)
319 {
320   f->bptr = pos;
321 }
322
323 static inline void
324 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
325 {
326   f->bptr = pos;
327   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
328 }
329
330 static inline uns
331 bdirect_write_prepare(struct fastbuf *f, byte **buf)
332 {
333   if (f->bptr == f->bufend)
334     f->spout(f);
335   *buf = f->bptr;
336   return bavailw(f);
337 }
338
339 static inline void
340 bdirect_write_commit(struct fastbuf *f, byte *pos)
341 {
342   f->bptr = pos;
343 }
344
345 /* Formatted output */
346
347 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
348 int vbprintf(struct fastbuf *b, char *msg, va_list args);
349
350 #endif