]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
Try to merge recent changes in v3.9 to image branch...
[libucw.git] / lib / fastbuf.h
1 /*
2  *      UCW Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2004 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 #ifndef EOF
15 #include <stdio.h>
16 #endif
17
18 #include <string.h>
19
20 #include "lib/unaligned.h"
21
22 /*
23  *  Generic buffered I/O. You supply hooks to be called for low-level operations
24  *  (swapping of buffers, seeking and closing), we do the rest.
25  *
26  *  Buffer layout when reading:
27  *
28  *  +----------------+---------------------------+
29  *  | read data      | free space                |
30  *  +----------------+---------------------------+
31  *  ^        ^        ^                           ^
32  *  buffer   bptr     bstop                       bufend
33  *
34  *  After the last character is read, bptr == bstop and buffer refill
35  *  is deferred to the next read attempt. This gives us an easy way
36  *  how to implement bungetc().
37  *
38  *  When writing:
39  *
40  *  +--------+--------------+--------------------+
41  *  | unused | written data | free space         |
42  *  +--------+--------------+--------------------+
43  *  ^         ^              ^                    ^
44  *  buffer    bstop          bptr                 bufend
45  *
46  *  Dirty tricks:
47  *
48  *    - You can mix reads and writes on the same stream, but you must
49  *      call bflush() in between and remember that the file position
50  *      points after the flushed buffer which is not necessarily the same
51  *      as after the data you've read.
52  *    - The spout/refill hooks can change not only bptr and bstop, but also
53  *      the location of the buffer; fb-mem.c takes advantage of it.
54  *    - In some cases, the user of the bdirect interface can be allowed to modify
55  *      the data in the buffer to avoid unnecessary copying. If the back-end
56  *      allows such modifications, it can set can_overwrite_buffer accordingly:
57  *              *  0 if no modification is allowed,
58  *              *  1 if the user can modify the buffer on the condition that
59  *                   the modifications will be undone before calling the next
60  *                   fastbuf operation
61  *              *  2 if the user is allowed to overwrite the data in the buffer
62  *                   if bdirect_read_commit_modified() is called afterwards.
63  *                   In this case, the back-end must be prepared for trimming
64  *                   of the buffer which is done by the commit function.
65  */
66
67 struct fastbuf {
68   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
69   byte *bptr, *bstop;                   /* Access pointers */
70   byte *buffer, *bufend;                /* Start and end of the buffer */
71   byte *name;                           /* File name for error messages */
72   sh_off_t pos;                         /* Position of bstop in the file */
73   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
74   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
75   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
76   void (*close)(struct fastbuf *);      /* Close the stream */
77   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
78   int can_overwrite_buffer;             /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
79 };
80
81 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
82
83 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
84 struct fastbuf *bopen_tmp(uns buflen);
85 struct fastbuf *bfdopen(int fd, uns buflen);
86 struct fastbuf *bfdopen_shared(int fd, uns buflen);
87 void bfilesync(struct fastbuf *b);
88
89 /* FastIO on in-memory streams */
90
91 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
92 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
93
94 /* FastIO on memory mapped files */
95
96 struct fastbuf *bopen_mm(byte *name, uns mode);
97
98 /* FastI on file descriptors with limit */
99
100 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
101
102 /* FastIO on static buffers */
103
104 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
105 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
106 static inline uns
107 fbbuf_count_written(struct fastbuf *f)
108 {
109   return f->bptr - f->bstop;
110 }
111
112 /* FastIO on recyclable growing buffers */
113
114 struct fastbuf *fbgrow_create(unsigned basic_size);
115 void fbgrow_reset(struct fastbuf *b);                   /* Reset stream and prepare for writing */
116 void fbgrow_rewind(struct fastbuf *b);                  /* Prepare for reading */
117
118 /* Configuring stream parameters */
119
120 int bconfig(struct fastbuf *f, uns type, int data);
121
122 #define BCONFIG_IS_TEMP_FILE 0
123
124 /* Universal functions working on all fastbuf's */
125
126 void bclose(struct fastbuf *f);
127 void bflush(struct fastbuf *f);
128 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
129 void bsetpos(struct fastbuf *f, sh_off_t pos);
130 void brewind(struct fastbuf *f);
131 sh_off_t bfilesize(struct fastbuf *f);
132
133 static inline sh_off_t btell(struct fastbuf *f)
134 {
135   return f->pos + (f->bptr - f->bstop);
136 }
137
138 int bgetc_slow(struct fastbuf *f);
139 static inline int bgetc(struct fastbuf *f)
140 {
141   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
142 }
143
144 int bpeekc_slow(struct fastbuf *f);
145 static inline int bpeekc(struct fastbuf *f)
146 {
147   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
148 }
149
150 static inline void bungetc(struct fastbuf *f)
151 {
152   f->bptr--;
153 }
154
155 void bputc_slow(struct fastbuf *f, uns c);
156 static inline void bputc(struct fastbuf *f, uns c)
157 {
158   if (f->bptr < f->bufend)
159     *f->bptr++ = c;
160   else
161     bputc_slow(f, c);
162 }
163
164 static inline uns
165 bavailr(struct fastbuf *f)
166 {
167   return f->bstop - f->bptr;
168 }
169
170 static inline uns
171 bavailw(struct fastbuf *f)
172 {
173   return f->bufend - f->bptr;
174 }
175
176 int bgetw_slow(struct fastbuf *f);
177 static inline int bgetw(struct fastbuf *f)
178 {
179   int w;
180   if (bavailr(f) >= 2)
181     {
182       w = GET_U16(f->bptr);
183       f->bptr += 2;
184       return w;
185     }
186   else
187     return bgetw_slow(f);
188 }
189
190 u32 bgetl_slow(struct fastbuf *f);
191 static inline u32 bgetl(struct fastbuf *f)
192 {
193   u32 l;
194   if (bavailr(f) >= 4)
195     {
196       l = GET_U32(f->bptr);
197       f->bptr += 4;
198       return l;
199     }
200   else
201     return bgetl_slow(f);
202 }
203
204 u64 bgetq_slow(struct fastbuf *f);
205 static inline u64 bgetq(struct fastbuf *f)
206 {
207   u64 l;
208   if (bavailr(f) >= 8)
209     {
210       l = GET_U64(f->bptr);
211       f->bptr += 8;
212       return l;
213     }
214   else
215     return bgetq_slow(f);
216 }
217
218 u64 bget5_slow(struct fastbuf *f);
219 static inline u64 bget5(struct fastbuf *f)
220 {
221   u64 l;
222   if (bavailr(f) >= 5)
223     {
224       l = GET_U40(f->bptr);
225       f->bptr += 5;
226       return l;
227     }
228   else
229     return bget5_slow(f);
230 }
231
232 void bputw_slow(struct fastbuf *f, uns w);
233 static inline void bputw(struct fastbuf *f, uns w)
234 {
235   if (bavailw(f) >= 2)
236     {
237       PUT_U16(f->bptr, w);
238       f->bptr += 2;
239     }
240   else
241     bputw_slow(f, w);
242 }
243
244 void bputl_slow(struct fastbuf *f, u32 l);
245 static inline void bputl(struct fastbuf *f, u32 l)
246 {
247   if (bavailw(f) >= 4)
248     {
249       PUT_U32(f->bptr, l);
250       f->bptr += 4;
251     }
252   else
253     bputl_slow(f, l);
254 }
255
256 void bputq_slow(struct fastbuf *f, u64 l);
257 static inline void bputq(struct fastbuf *f, u64 l)
258 {
259   if (bavailw(f) >= 8)
260     {
261       PUT_U64(f->bptr, l);
262       f->bptr += 8;
263     }
264   else
265     bputq_slow(f, l);
266 }
267
268 void bput5_slow(struct fastbuf *f, u64 l);
269 static inline void bput5(struct fastbuf *f, u64 l)
270 {
271   if (bavailw(f) >= 5)
272     {
273       PUT_U40(f->bptr, l);
274       f->bptr += 5;
275     }
276   else
277     bput5_slow(f, l);
278 }
279
280 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
281 static inline uns bread(struct fastbuf *f, void *b, uns l)
282 {
283   if (bavailr(f) >= l)
284     {
285       memcpy(b, f->bptr, l);
286       f->bptr += l;
287       return l;
288     }
289   else
290     return bread_slow(f, b, l, 0);
291 }
292
293 static inline uns breadb(struct fastbuf *f, void *b, uns l)
294 {
295   if (bavailr(f) >= l)
296     {
297       memcpy(b, f->bptr, l);
298       f->bptr += l;
299       return l;
300     }
301   else
302     return bread_slow(f, b, l, 1);
303 }
304
305 void bwrite_slow(struct fastbuf *f, void *b, uns l);
306 static inline void bwrite(struct fastbuf *f, void *b, uns l)
307 {
308   if (bavailw(f) >= l)
309     {
310       memcpy(f->bptr, b, l);
311       f->bptr += l;
312     }
313   else
314     bwrite_slow(f, b, l);
315 }
316
317 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
318 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
319 byte *bgets0(struct fastbuf *f, byte *b, uns l);
320
321 static inline void
322 bputs(struct fastbuf *f, byte *b)
323 {
324   bwrite(f, b, strlen(b));
325 }
326
327 static inline void
328 bputs0(struct fastbuf *f, byte *b)
329 {
330   bwrite(f, b, strlen(b)+1);
331 }
332
333 static inline void
334 bputsn(struct fastbuf *f, byte *b)
335 {
336   bputs(f, b);
337   bputc(f, '\n');
338 }
339
340 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
341 static inline void
342 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
343 {
344   if (bavailr(f) >= l && bavailw(t) >= l)
345     {
346       memcpy(t->bptr, f->bptr, l);
347       t->bptr += l;
348       f->bptr += l;
349     }
350   else
351     bbcopy_slow(f, t, l);
352 }
353
354 int bskip_slow(struct fastbuf *f, uns len);
355 static inline int bskip(struct fastbuf *f, uns len)
356 {
357   if (bavailr(f) >= len)
358     {
359       f->bptr += len;
360       return 1;
361     }
362   else
363     return bskip_slow(f, len);
364 }
365
366 /* I/O on addr_int_t */
367
368 #ifdef CPU_64BIT_POINTERS
369 #define bputa(x,p) bputq(x,p)
370 #define bgeta(x) bgetq(x)
371 #else
372 #define bputa(x,p) bputl(x,p)
373 #define bgeta(x) bgetl(x)
374 #endif
375
376 /* Direct I/O on buffers */
377
378 static inline uns
379 bdirect_read_prepare(struct fastbuf *f, byte **buf)
380 {
381   if (f->bptr == f->bstop && !f->refill(f))
382     {
383       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
384       return 0;
385     }
386   *buf = f->bptr;
387   return bavailr(f);
388 }
389
390 static inline void
391 bdirect_read_commit(struct fastbuf *f, byte *pos)
392 {
393   f->bptr = pos;
394 }
395
396 static inline void
397 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
398 {
399   f->bptr = pos;
400   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
401 }
402
403 static inline uns
404 bdirect_write_prepare(struct fastbuf *f, byte **buf)
405 {
406   if (f->bptr == f->bufend)
407     f->spout(f);
408   *buf = f->bptr;
409   return bavailw(f);
410 }
411
412 static inline void
413 bdirect_write_commit(struct fastbuf *f, byte *pos)
414 {
415   f->bptr = pos;
416 }
417
418 /* Formatted output */
419
420 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
421 int vbprintf(struct fastbuf *b, char *msg, va_list args);
422
423 #endif