]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
The is_temp_file variable was originally a good idea, but it made
[libucw.git] / lib / fastbuf.h
1 /*
2  *      Sherlock Library -- Fast Buffered I/O
3  *
4  *      (c) 1997--2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _SHERLOCK_FASTBUF_H
11 #define _SHERLOCK_FASTBUF_H
12
13 #ifndef EOF
14 #include <stdio.h>
15 #endif
16
17 #include <string.h>
18
19 #include "lib/unaligned.h"
20
21 /*
22  *  Generic buffered I/O. You supply hooks to be called for low-level operations
23  *  (swapping of buffers, seeking and closing), we do the rest.
24  *
25  *  Buffer layout when reading:
26  *
27  *  +----------------+---------------------------+
28  *  | read data      | free space                |
29  *  +----------------+---------------------------+
30  *  ^        ^        ^                           ^
31  *  buffer   bptr     bstop                       bufend
32  *
33  *  After the last character is read, bptr == bstop and buffer refill
34  *  is deferred to the next read attempt. This gives us an easy way
35  *  how to implement bungetc().
36  *
37  *  When writing:
38  *
39  *  +--------+--------------+--------------------+
40  *  | unused | written data | free space         |
41  *  +--------+--------------+--------------------+
42  *  ^         ^              ^                    ^
43  *  buffer    bstop          bptr                 bufend
44  *
45  *  Dirty tricks:
46  *
47  *    - You can mix reads and writes on the same stream, but you must
48  *      call bflush() in between and remember that the file position
49  *      points after the flushed buffer which is not necessarily the same
50  *      as after the data you've read.
51  *    - The spout/refill hooks can change not only bptr and bstop, but also
52  *      the location of the buffer; fb-mem.c takes advantage of it.
53  */
54
55 struct fastbuf {
56   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
57   byte *bptr, *bstop;                   /* Access pointers */
58   byte *buffer, *bufend;                /* Start and end of the buffer */
59   byte *name;                           /* File name for error messages */
60   sh_off_t pos;                         /* Position of bstop in the file */
61   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
62   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
63   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
64   void (*close)(struct fastbuf *);      /* Close the stream */
65   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
66 };
67
68 /* FastIO on standard files */
69
70 struct fastbuf *bopen(byte *name, uns mode, uns buffer);
71 struct fastbuf *bopen_tmp(uns buffer);
72 struct fastbuf *bfdopen(int fd, uns buffer);
73 struct fastbuf *bfdopen_shared(int fd, uns buffer);
74
75 /* FastIO on in-memory streams */
76
77 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
78 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
79
80 /* FastIO on memory mapped files */
81
82 struct fastbuf *bopen_mm(byte *name, uns mode);
83
84 /* Configuring stream parameters */
85
86 int bconfig(struct fastbuf *f, uns type, int data);
87
88 #define BCONFIG_IS_TEMP_FILE 0
89
90 /* Universal functions working on all fastbuf's */
91
92 void bclose(struct fastbuf *f);
93 void bflush(struct fastbuf *f);
94 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
95 void bsetpos(struct fastbuf *f, sh_off_t pos);
96
97 static inline sh_off_t btell(struct fastbuf *f)
98 {
99   return f->pos + (f->bptr - f->bstop);
100 }
101
102 int bgetc_slow(struct fastbuf *f);
103 static inline int bgetc(struct fastbuf *f)
104 {
105   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
106 }
107
108 int bpeekc_slow(struct fastbuf *f);
109 static inline int bpeekc(struct fastbuf *f)
110 {
111   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
112 }
113
114 static inline void bungetc(struct fastbuf *f)
115 {
116   f->bptr--;
117 }
118
119 void bputc_slow(struct fastbuf *f, uns c);
120 static inline void bputc(struct fastbuf *f, uns c)
121 {
122   if (f->bptr < f->bufend)
123     *f->bptr++ = c;
124   else
125     bputc_slow(f, c);
126 }
127
128 int bgetw_slow(struct fastbuf *f);
129 static inline int bgetw(struct fastbuf *f)
130 {
131   int w;
132   if (f->bptr + 2 <= f->bstop)
133     {
134       w = GET_U16(f->bptr);
135       f->bptr += 2;
136       return w;
137     }
138   else
139     return bgetw_slow(f);
140 }
141
142 u32 bgetl_slow(struct fastbuf *f);
143 static inline u32 bgetl(struct fastbuf *f)
144 {
145   u32 l;
146   if (f->bptr + 4 <= f->bstop)
147     {
148       l = GET_U32(f->bptr);
149       f->bptr += 4;
150       return l;
151     }
152   else
153     return bgetl_slow(f);
154 }
155
156 u64 bgetq_slow(struct fastbuf *f);
157 static inline u64 bgetq(struct fastbuf *f)
158 {
159   u64 l;
160   if (f->bptr + 8 <= f->bstop)
161     {
162       l = GET_U64(f->bptr);
163       f->bptr += 8;
164       return l;
165     }
166   else
167     return bgetq_slow(f);
168 }
169
170 u64 bget5_slow(struct fastbuf *f);
171 static inline u64 bget5(struct fastbuf *f)
172 {
173   u64 l;
174   if (f->bptr + 5 <= f->bstop)
175     {
176       l = GET_U40(f->bptr);
177       f->bptr += 5;
178       return l;
179     }
180   else
181     return bget5_slow(f);
182 }
183
184 void bputw_slow(struct fastbuf *f, uns w);
185 static inline void bputw(struct fastbuf *f, uns w)
186 {
187   if (f->bptr + 2 <= f->bufend)
188     {
189       PUT_U16(f->bptr, w);
190       f->bptr += 2;
191     }
192   else
193     bputw_slow(f, w);
194 }
195
196 void bputl_slow(struct fastbuf *f, u32 l);
197 static inline void bputl(struct fastbuf *f, u32 l)
198 {
199   if (f->bptr + 4 <= f->bufend)
200     {
201       PUT_U32(f->bptr, l);
202       f->bptr += 4;
203     }
204   else
205     bputl_slow(f, l);
206 }
207
208 void bputq_slow(struct fastbuf *f, u64 l);
209 static inline void bputq(struct fastbuf *f, u64 l)
210 {
211   if (f->bptr + 8 <= f->bufend)
212     {
213       PUT_U64(f->bptr, l);
214       f->bptr += 8;
215     }
216   else
217     bputq_slow(f, l);
218 }
219
220 void bput5_slow(struct fastbuf *f, u64 l);
221 static inline void bput5(struct fastbuf *f, u64 l)
222 {
223   if (f->bptr + 5 <= f->bufend)
224     {
225       PUT_U40(f->bptr, l);
226       f->bptr += 5;
227     }
228   else
229     bput5_slow(f, l);
230 }
231
232 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
233 static inline uns bread(struct fastbuf *f, void *b, uns l)
234 {
235   if (f->bptr + l <= f->bstop)
236     {
237       memcpy(b, f->bptr, l);
238       f->bptr += l;
239       return l;
240     }
241   else
242     return bread_slow(f, b, l, 0);
243 }
244
245 static inline uns breadb(struct fastbuf *f, void *b, uns l)
246 {
247   if (f->bptr + l <= f->bstop)
248     {
249       memcpy(b, f->bptr, l);
250       f->bptr += l;
251       return l;
252     }
253   else
254     return bread_slow(f, b, l, 1);
255 }
256
257 void bwrite_slow(struct fastbuf *f, void *b, uns l);
258 static inline void bwrite(struct fastbuf *f, void *b, uns l)
259 {
260   if (f->bptr + l <= f->bufend)
261     {
262       memcpy(f->bptr, b, l);
263       f->bptr += l;
264     }
265   else
266     bwrite_slow(f, b, l);
267 }
268
269 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
270 byte *bgets0(struct fastbuf *f, byte *b, uns l);
271
272 static inline void
273 bputs(struct fastbuf *f, byte *b)
274 {
275   bwrite(f, b, strlen(b));
276 }
277
278 static inline void
279 bputs0(struct fastbuf *f, byte *b)
280 {
281   bwrite(f, b, strlen(b)+1);
282 }
283
284 static inline void
285 bputsn(struct fastbuf *f, byte *b)
286 {
287   bputs(f, b);
288   bputc(f, '\n');
289 }
290
291 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
292 static inline void
293 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
294 {
295   if (f->bptr + l <= f->bstop &&
296       t->bptr + l <= t->bufend)
297     {
298       memcpy(t->bptr, f->bptr, l);
299       t->bptr += l;
300       f->bptr += l;
301     }
302   else
303     bbcopy_slow(f, t, l);
304 }
305
306 /* I/O on addr_int_t */
307
308 #ifdef CPU_64BIT_POINTERS
309 #define bputa(x,p) bputq(x,p)
310 #define bgeta(x) bgetq(x)
311 #else
312 #define bputa(x,p) bputl(x,p)
313 #define bgeta(x) bgetl(x)
314 #endif
315
316 /* Direct I/O on buffers */
317
318 static inline int
319 bdirect_read_prepare(struct fastbuf *f, byte **buf)
320 {
321   if (f->bptr == f->bstop && !f->refill(f))
322     return EOF;
323   *buf = f->bptr;
324   return f->bstop - f->bptr;
325 }
326
327 static inline void
328 bdirect_read_commit(struct fastbuf *f, byte *pos)
329 {
330   f->bptr = pos;
331 }
332
333 static inline int
334 bdirect_write_prepare(struct fastbuf *f, byte **buf)
335 {
336   if (f->bptr == f->bufend)
337     f->spout(f);
338   *buf = f->bptr;
339   return f->bufend - f->bptr;
340 }
341
342 static inline void
343 bdirect_write_commit(struct fastbuf *f, byte *pos)
344 {
345   f->bptr = pos;
346 }
347
348 #endif