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