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