]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
9cc646d3fac324f16466ee8e80b8dada9d629f8e
[libucw.git] / lib / fastbuf.h
1 /*
2  *      Sherlock 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 _SHERLOCK_FASTBUF_H
12 #define _SHERLOCK_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  */
55
56 struct fastbuf {
57   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
58   byte *bptr, *bstop;                   /* Access pointers */
59   byte *buffer, *bufend;                /* Start and end of the buffer */
60   byte *name;                           /* File name for error messages */
61   sh_off_t pos;                         /* Position of bstop in the file */
62   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
63   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
64   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
65   void (*close)(struct fastbuf *);      /* Close the stream */
66   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
67 };
68
69 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
70
71 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
72 struct fastbuf *bopen_tmp(uns buffer);
73 struct fastbuf *bfdopen(int fd, uns buffer);
74 struct fastbuf *bfdopen_shared(int fd, uns buffer);
75
76 /* FastIO on in-memory streams */
77
78 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
79 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
80
81 /* FastIO on memory mapped files */
82
83 struct fastbuf *bopen_mm(byte *name, uns mode);
84
85 /* FastI on file descriptors with limit */
86
87 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
88
89 /* FastIO on static buffers */
90
91 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size);
92 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
93 static inline uns
94 fbbuf_count_written(struct fastbuf *f)
95 {
96   return f->bptr - f->bstop;
97 }
98
99 /* Configuring stream parameters */
100
101 int bconfig(struct fastbuf *f, uns type, int data);
102
103 #define BCONFIG_IS_TEMP_FILE 0
104 #define BCONFIG_CAN_OVERWRITE 1
105   /* Specified whether the caller is allowed to perform the following optimized
106    * 0-copy write operation:
107    *    - get the buffer by bdirect_read_prepare()
108    *    - modify the buffer, e.g. by putting \0's inside
109    *    - call bflush() to let the fastbuf know
110    * 0: read-only memory
111    * 1: you can write into read-write memory, if you restore the original value
112    * 2: you can rewrite the original content */
113
114 /* Universal functions working on all fastbuf's */
115
116 void bclose(struct fastbuf *f);
117 void bflush(struct fastbuf *f);
118 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
119 void bsetpos(struct fastbuf *f, sh_off_t pos);
120 void brewind(struct fastbuf *f);
121
122 static inline sh_off_t btell(struct fastbuf *f)
123 {
124   return f->pos + (f->bptr - f->bstop);
125 }
126
127 int bgetc_slow(struct fastbuf *f);
128 static inline int bgetc(struct fastbuf *f)
129 {
130   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
131 }
132
133 int bpeekc_slow(struct fastbuf *f);
134 static inline int bpeekc(struct fastbuf *f)
135 {
136   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
137 }
138
139 static inline void bungetc(struct fastbuf *f)
140 {
141   f->bptr--;
142 }
143
144 void bputc_slow(struct fastbuf *f, uns c);
145 static inline void bputc(struct fastbuf *f, uns c)
146 {
147   if (f->bptr < f->bufend)
148     *f->bptr++ = c;
149   else
150     bputc_slow(f, c);
151 }
152
153 int bgetw_slow(struct fastbuf *f);
154 static inline int bgetw(struct fastbuf *f)
155 {
156   int w;
157   if (f->bptr + 2 <= f->bstop)
158     {
159       w = GET_U16(f->bptr);
160       f->bptr += 2;
161       return w;
162     }
163   else
164     return bgetw_slow(f);
165 }
166
167 u32 bgetl_slow(struct fastbuf *f);
168 static inline u32 bgetl(struct fastbuf *f)
169 {
170   u32 l;
171   if (f->bptr + 4 <= f->bstop)
172     {
173       l = GET_U32(f->bptr);
174       f->bptr += 4;
175       return l;
176     }
177   else
178     return bgetl_slow(f);
179 }
180
181 u64 bgetq_slow(struct fastbuf *f);
182 static inline u64 bgetq(struct fastbuf *f)
183 {
184   u64 l;
185   if (f->bptr + 8 <= f->bstop)
186     {
187       l = GET_U64(f->bptr);
188       f->bptr += 8;
189       return l;
190     }
191   else
192     return bgetq_slow(f);
193 }
194
195 u64 bget5_slow(struct fastbuf *f);
196 static inline u64 bget5(struct fastbuf *f)
197 {
198   u64 l;
199   if (f->bptr + 5 <= f->bstop)
200     {
201       l = GET_U40(f->bptr);
202       f->bptr += 5;
203       return l;
204     }
205   else
206     return bget5_slow(f);
207 }
208
209 void bputw_slow(struct fastbuf *f, uns w);
210 static inline void bputw(struct fastbuf *f, uns w)
211 {
212   if (f->bptr + 2 <= f->bufend)
213     {
214       PUT_U16(f->bptr, w);
215       f->bptr += 2;
216     }
217   else
218     bputw_slow(f, w);
219 }
220
221 void bputl_slow(struct fastbuf *f, u32 l);
222 static inline void bputl(struct fastbuf *f, u32 l)
223 {
224   if (f->bptr + 4 <= f->bufend)
225     {
226       PUT_U32(f->bptr, l);
227       f->bptr += 4;
228     }
229   else
230     bputl_slow(f, l);
231 }
232
233 void bputq_slow(struct fastbuf *f, u64 l);
234 static inline void bputq(struct fastbuf *f, u64 l)
235 {
236   if (f->bptr + 8 <= f->bufend)
237     {
238       PUT_U64(f->bptr, l);
239       f->bptr += 8;
240     }
241   else
242     bputq_slow(f, l);
243 }
244
245 void bput5_slow(struct fastbuf *f, u64 l);
246 static inline void bput5(struct fastbuf *f, u64 l)
247 {
248   if (f->bptr + 5 <= f->bufend)
249     {
250       PUT_U40(f->bptr, l);
251       f->bptr += 5;
252     }
253   else
254     bput5_slow(f, l);
255 }
256
257 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
258 static inline uns bread(struct fastbuf *f, void *b, uns l)
259 {
260   if (f->bptr + l <= f->bstop)
261     {
262       memcpy(b, f->bptr, l);
263       f->bptr += l;
264       return l;
265     }
266   else
267     return bread_slow(f, b, l, 0);
268 }
269
270 static inline uns breadb(struct fastbuf *f, void *b, uns l)
271 {
272   if (f->bptr + l <= f->bstop)
273     {
274       memcpy(b, f->bptr, l);
275       f->bptr += l;
276       return l;
277     }
278   else
279     return bread_slow(f, b, l, 1);
280 }
281
282 void bwrite_slow(struct fastbuf *f, void *b, uns l);
283 static inline void bwrite(struct fastbuf *f, void *b, uns l)
284 {
285   if (f->bptr + l <= f->bufend)
286     {
287       memcpy(f->bptr, b, l);
288       f->bptr += l;
289     }
290   else
291     bwrite_slow(f, b, l);
292 }
293
294 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
295 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
296 byte *bgets0(struct fastbuf *f, byte *b, uns l);
297
298 static inline void
299 bputs(struct fastbuf *f, byte *b)
300 {
301   bwrite(f, b, strlen(b));
302 }
303
304 static inline void
305 bputs0(struct fastbuf *f, byte *b)
306 {
307   bwrite(f, b, strlen(b)+1);
308 }
309
310 static inline void
311 bputsn(struct fastbuf *f, byte *b)
312 {
313   bputs(f, b);
314   bputc(f, '\n');
315 }
316
317 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
318 static inline void
319 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
320 {
321   if (f->bptr + l <= f->bstop &&
322       t->bptr + l <= t->bufend)
323     {
324       memcpy(t->bptr, f->bptr, l);
325       t->bptr += l;
326       f->bptr += l;
327     }
328   else
329     bbcopy_slow(f, t, l);
330 }
331
332 /* I/O on addr_int_t */
333
334 #ifdef CPU_64BIT_POINTERS
335 #define bputa(x,p) bputq(x,p)
336 #define bgeta(x) bgetq(x)
337 #else
338 #define bputa(x,p) bputl(x,p)
339 #define bgeta(x) bgetl(x)
340 #endif
341
342 /* Direct I/O on buffers */
343
344 static inline uns
345 bdirect_read_prepare(struct fastbuf *f, byte **buf)
346 {
347   if (f->bptr == f->bstop && !f->refill(f))
348     return 0;
349   *buf = f->bptr;
350   return f->bstop - f->bptr;
351 }
352
353 static inline void
354 bdirect_read_commit(struct fastbuf *f, byte *pos)
355 {
356   f->bptr = pos;
357 }
358
359 static inline uns
360 bdirect_write_prepare(struct fastbuf *f, byte **buf)
361 {
362   if (f->bptr == f->bufend)
363     f->spout(f);
364   *buf = f->bptr;
365   return f->bufend - f->bptr;
366 }
367
368 static inline void
369 bdirect_write_commit(struct fastbuf *f, byte *pos)
370 {
371   f->bptr = pos;
372 }
373
374 /* Formatted output */
375
376 int bprintf(struct fastbuf *b, byte *msg, ...);
377 int vbprintf(struct fastbuf *b, byte *msg, va_list args);
378
379 #endif