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