]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
851a1a0c1ec2342cd869d47eecdb1baa17edf28f
[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  *    - 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 /* Configuring stream parameters */
113
114 int bconfig(struct fastbuf *f, uns type, int data);
115
116 #define BCONFIG_IS_TEMP_FILE 0
117
118 /* Universal functions working on all fastbuf's */
119
120 void bclose(struct fastbuf *f);
121 void bflush(struct fastbuf *f);
122 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
123 void bsetpos(struct fastbuf *f, sh_off_t pos);
124 void brewind(struct fastbuf *f);
125 void bskip(struct fastbuf *f, uns len);
126 sh_off_t bfilesize(struct fastbuf *f);
127
128 static inline sh_off_t btell(struct fastbuf *f)
129 {
130   return f->pos + (f->bptr - f->bstop);
131 }
132
133 int bgetc_slow(struct fastbuf *f);
134 static inline int bgetc(struct fastbuf *f)
135 {
136   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
137 }
138
139 int bpeekc_slow(struct fastbuf *f);
140 static inline int bpeekc(struct fastbuf *f)
141 {
142   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
143 }
144
145 static inline void bungetc(struct fastbuf *f)
146 {
147   f->bptr--;
148 }
149
150 void bputc_slow(struct fastbuf *f, uns c);
151 static inline void bputc(struct fastbuf *f, uns c)
152 {
153   if (f->bptr < f->bufend)
154     *f->bptr++ = c;
155   else
156     bputc_slow(f, c);
157 }
158
159 int bgetw_slow(struct fastbuf *f);
160 static inline int bgetw(struct fastbuf *f)
161 {
162   int w;
163   if (f->bptr + 2 <= f->bstop)
164     {
165       w = GET_U16(f->bptr);
166       f->bptr += 2;
167       return w;
168     }
169   else
170     return bgetw_slow(f);
171 }
172
173 u32 bgetl_slow(struct fastbuf *f);
174 static inline u32 bgetl(struct fastbuf *f)
175 {
176   u32 l;
177   if (f->bptr + 4 <= f->bstop)
178     {
179       l = GET_U32(f->bptr);
180       f->bptr += 4;
181       return l;
182     }
183   else
184     return bgetl_slow(f);
185 }
186
187 u64 bgetq_slow(struct fastbuf *f);
188 static inline u64 bgetq(struct fastbuf *f)
189 {
190   u64 l;
191   if (f->bptr + 8 <= f->bstop)
192     {
193       l = GET_U64(f->bptr);
194       f->bptr += 8;
195       return l;
196     }
197   else
198     return bgetq_slow(f);
199 }
200
201 u64 bget5_slow(struct fastbuf *f);
202 static inline u64 bget5(struct fastbuf *f)
203 {
204   u64 l;
205   if (f->bptr + 5 <= f->bstop)
206     {
207       l = GET_U40(f->bptr);
208       f->bptr += 5;
209       return l;
210     }
211   else
212     return bget5_slow(f);
213 }
214
215 void bputw_slow(struct fastbuf *f, uns w);
216 static inline void bputw(struct fastbuf *f, uns w)
217 {
218   if (f->bptr + 2 <= f->bufend)
219     {
220       PUT_U16(f->bptr, w);
221       f->bptr += 2;
222     }
223   else
224     bputw_slow(f, w);
225 }
226
227 void bputl_slow(struct fastbuf *f, u32 l);
228 static inline void bputl(struct fastbuf *f, u32 l)
229 {
230   if (f->bptr + 4 <= f->bufend)
231     {
232       PUT_U32(f->bptr, l);
233       f->bptr += 4;
234     }
235   else
236     bputl_slow(f, l);
237 }
238
239 void bputq_slow(struct fastbuf *f, u64 l);
240 static inline void bputq(struct fastbuf *f, u64 l)
241 {
242   if (f->bptr + 8 <= f->bufend)
243     {
244       PUT_U64(f->bptr, l);
245       f->bptr += 8;
246     }
247   else
248     bputq_slow(f, l);
249 }
250
251 void bput5_slow(struct fastbuf *f, u64 l);
252 static inline void bput5(struct fastbuf *f, u64 l)
253 {
254   if (f->bptr + 5 <= f->bufend)
255     {
256       PUT_U40(f->bptr, l);
257       f->bptr += 5;
258     }
259   else
260     bput5_slow(f, l);
261 }
262
263 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
264 static inline uns bread(struct fastbuf *f, void *b, uns l)
265 {
266   if (f->bptr + l <= f->bstop)
267     {
268       memcpy(b, f->bptr, l);
269       f->bptr += l;
270       return l;
271     }
272   else
273     return bread_slow(f, b, l, 0);
274 }
275
276 static inline uns breadb(struct fastbuf *f, void *b, uns l)
277 {
278   if (f->bptr + l <= f->bstop)
279     {
280       memcpy(b, f->bptr, l);
281       f->bptr += l;
282       return l;
283     }
284   else
285     return bread_slow(f, b, l, 1);
286 }
287
288 void bwrite_slow(struct fastbuf *f, void *b, uns l);
289 static inline void bwrite(struct fastbuf *f, void *b, uns l)
290 {
291   if (f->bptr + l <= f->bufend)
292     {
293       memcpy(f->bptr, b, l);
294       f->bptr += l;
295     }
296   else
297     bwrite_slow(f, b, l);
298 }
299
300 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
301 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
302 byte *bgets0(struct fastbuf *f, byte *b, uns l);
303
304 static inline void
305 bputs(struct fastbuf *f, byte *b)
306 {
307   bwrite(f, b, strlen(b));
308 }
309
310 static inline void
311 bputs0(struct fastbuf *f, byte *b)
312 {
313   bwrite(f, b, strlen(b)+1);
314 }
315
316 static inline void
317 bputsn(struct fastbuf *f, byte *b)
318 {
319   bputs(f, b);
320   bputc(f, '\n');
321 }
322
323 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
324 static inline void
325 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
326 {
327   if (f->bptr + l <= f->bstop &&
328       t->bptr + l <= t->bufend)
329     {
330       memcpy(t->bptr, f->bptr, l);
331       t->bptr += l;
332       f->bptr += l;
333     }
334   else
335     bbcopy_slow(f, t, l);
336 }
337
338 /* I/O on addr_int_t */
339
340 #ifdef CPU_64BIT_POINTERS
341 #define bputa(x,p) bputq(x,p)
342 #define bgeta(x) bgetq(x)
343 #else
344 #define bputa(x,p) bputl(x,p)
345 #define bgeta(x) bgetl(x)
346 #endif
347
348 /* Direct I/O on buffers */
349
350 static inline uns
351 bdirect_read_prepare(struct fastbuf *f, byte **buf)
352 {
353   if (f->bptr == f->bstop && !f->refill(f))
354     return 0;
355   *buf = f->bptr;
356   return f->bstop - f->bptr;
357 }
358
359 static inline void
360 bdirect_read_commit(struct fastbuf *f, byte *pos)
361 {
362   f->bptr = pos;
363 }
364
365 static inline void
366 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
367 {
368   f->bptr = pos;
369   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
370 }
371
372 static inline uns
373 bdirect_write_prepare(struct fastbuf *f, byte **buf)
374 {
375   if (f->bptr == f->bufend)
376     f->spout(f);
377   *buf = f->bptr;
378   return f->bufend - f->bptr;
379 }
380
381 static inline void
382 bdirect_write_commit(struct fastbuf *f, byte *pos)
383 {
384   f->bptr = pos;
385 }
386
387 /* Formatted output */
388
389 int bprintf(struct fastbuf *b, byte *msg, ...);
390 int vbprintf(struct fastbuf *b, byte *msg, va_list args);
391
392 #endif