]> mj.ucw.cz Git - libucw.git/blob - lib/fastbuf.h
bugfix in image scaling (select the correct strategy)
[libucw.git] / lib / fastbuf.h
1 /*
2  *      UCW 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 _UCW_FASTBUF_H
12 #define _UCW_FASTBUF_H
13
14 #ifndef EOF
15 #include <stdio.h>
16 #endif
17
18 #include <string.h>
19 #include <alloca.h>
20
21 #include "lib/unaligned.h"
22
23 /*
24  *  Generic buffered I/O. You supply hooks to be called for low-level operations
25  *  (swapping of buffers, seeking and closing), we do the rest.
26  *
27  *  Buffer layout when reading:
28  *
29  *  +----------------+---------------------------+
30  *  | read data      | free space                |
31  *  +----------------+---------------------------+
32  *  ^        ^        ^                           ^
33  *  buffer   bptr     bstop                       bufend
34  *
35  *  After the last character is read, bptr == bstop and buffer refill
36  *  is deferred to the next read attempt. This gives us an easy way
37  *  how to implement bungetc().
38  *
39  *  When writing:
40  *
41  *  +--------+--------------+--------------------+
42  *  | unused | written data | free space         |
43  *  +--------+--------------+--------------------+
44  *  ^         ^              ^                    ^
45  *  buffer    bstop          bptr                 bufend
46  *
47  *  Dirty tricks:
48  *
49  *    - You can mix reads and writes on the same stream, but you must
50  *      call bflush() in between and remember that the file position
51  *      points after the flushed buffer which is not necessarily the same
52  *      as after the data you've read.
53  *    - The spout/refill hooks can change not only bptr and bstop, but also
54  *      the location of the buffer; fb-mem.c takes advantage of it.
55  *    - In some cases, the user of the bdirect interface can be allowed to modify
56  *      the data in the buffer to avoid unnecessary copying. If the back-end
57  *      allows such modifications, it can set can_overwrite_buffer accordingly:
58  *              *  0 if no modification is allowed,
59  *              *  1 if the user can modify the buffer on the condition that
60  *                   the modifications will be undone before calling the next
61  *                   fastbuf operation
62  *              *  2 if the user is allowed to overwrite the data in the buffer
63  *                   if bdirect_read_commit_modified() is called afterwards.
64  *                   In this case, the back-end must be prepared for trimming
65  *                   of the buffer which is done by the commit function.
66  */
67
68 struct fastbuf {
69   byte is_fastbuf[0];                   /* Dummy field for checking of type casts */
70   byte *bptr, *bstop;                   /* Access pointers */
71   byte *buffer, *bufend;                /* Start and end of the buffer */
72   byte *name;                           /* File name for error messages */
73   sh_off_t pos;                         /* Position of bstop in the file */
74   int (*refill)(struct fastbuf *);      /* Get a buffer with new data */
75   void (*spout)(struct fastbuf *);      /* Write buffer data to the file */
76   void (*seek)(struct fastbuf *, sh_off_t, int);  /* Slow path for bseek(), buffer already flushed */
77   void (*close)(struct fastbuf *);      /* Close the stream */
78   int (*config)(struct fastbuf *, uns, int);    /* Configure the stream */
79   int can_overwrite_buffer;             /* Can the buffer be altered? (see discussion above) 0=never, 1=temporarily, 2=permanently */
80 };
81
82 /* FastIO on standard files (specify buffer size 0 to enable mmaping) */
83
84 struct fastbuf *bopen(byte *name, uns mode, uns buflen);
85 struct fastbuf *bopen_try(byte *name, uns mode, uns buflen);
86 struct fastbuf *bopen_tmp(uns buflen);
87 struct fastbuf *bfdopen(int fd, uns buflen);
88 struct fastbuf *bfdopen_shared(int fd, uns buflen);
89 void bfilesync(struct fastbuf *b);
90
91 /* FastIO on in-memory streams */
92
93 struct fastbuf *fbmem_create(unsigned blocksize);       /* Create stream and return its writing fastbuf */
94 struct fastbuf *fbmem_clone_read(struct fastbuf *);     /* Create reading fastbuf */
95
96 /* FastIO on memory mapped files */
97
98 struct fastbuf *bopen_mm(byte *name, uns mode);
99
100 /* FastI on file descriptors with limit */
101
102 struct fastbuf *bopen_limited_fd(int fd, uns bufsize, uns limit);
103
104 /* FastIO on static buffers */
105
106 void fbbuf_init_read(struct fastbuf *f, byte *buffer, uns size, uns can_overwrite);
107 void fbbuf_init_write(struct fastbuf *f, byte *buffer, uns size);
108 static inline uns
109 fbbuf_count_written(struct fastbuf *f)
110 {
111   return f->bptr - f->bstop;
112 }
113
114 /* FastIO on recyclable growing buffers */
115
116 struct fastbuf *fbgrow_create(unsigned basic_size);
117 void fbgrow_reset(struct fastbuf *b);                   /* Reset stream and prepare for writing */
118 void fbgrow_rewind(struct fastbuf *b);                  /* Prepare for reading */
119
120 /* FastO with atomic writes for multi-threaded programs */
121
122 struct fb_atomic {
123   struct fastbuf fb;
124   struct fb_atomic_file *af;
125   byte *expected_max_bptr;
126   uns slack_size;
127 };
128 #define FB_ATOMIC(f) ((struct fb_atomic *)(f)->is_fastbuf)
129
130 struct fastbuf *fbatomic_open(byte *name, struct fastbuf *master, uns bufsize, int record_len);
131 void fbatomic_internal_write(struct fastbuf *b);
132
133 static inline void
134 fbatomic_commit(struct fastbuf *b)
135 {
136   if (b->bptr >= ((struct fb_atomic *)b)->expected_max_bptr)
137     fbatomic_internal_write(b);
138 }
139
140 /* Configuring stream parameters */
141
142 int bconfig(struct fastbuf *f, uns type, int data);
143
144 #define BCONFIG_IS_TEMP_FILE 0
145
146 /* Universal functions working on all fastbuf's */
147
148 void bclose(struct fastbuf *f);
149 void bflush(struct fastbuf *f);
150 void bseek(struct fastbuf *f, sh_off_t pos, int whence);
151 void bsetpos(struct fastbuf *f, sh_off_t pos);
152 void brewind(struct fastbuf *f);
153 sh_off_t bfilesize(struct fastbuf *f);
154
155 static inline sh_off_t btell(struct fastbuf *f)
156 {
157   return f->pos + (f->bptr - f->bstop);
158 }
159
160 int bgetc_slow(struct fastbuf *f);
161 static inline int bgetc(struct fastbuf *f)
162 {
163   return (f->bptr < f->bstop) ? (int) *f->bptr++ : bgetc_slow(f);
164 }
165
166 int bpeekc_slow(struct fastbuf *f);
167 static inline int bpeekc(struct fastbuf *f)
168 {
169   return (f->bptr < f->bstop) ? (int) *f->bptr : bpeekc_slow(f);
170 }
171
172 static inline void bungetc(struct fastbuf *f)
173 {
174   f->bptr--;
175 }
176
177 void bputc_slow(struct fastbuf *f, uns c);
178 static inline void bputc(struct fastbuf *f, uns c)
179 {
180   if (f->bptr < f->bufend)
181     *f->bptr++ = c;
182   else
183     bputc_slow(f, c);
184 }
185
186 static inline uns
187 bavailr(struct fastbuf *f)
188 {
189   return f->bstop - f->bptr;
190 }
191
192 static inline uns
193 bavailw(struct fastbuf *f)
194 {
195   return f->bufend - f->bptr;
196 }
197
198 int bgetw_slow(struct fastbuf *f);
199 static inline int bgetw(struct fastbuf *f)
200 {
201   int w;
202   if (bavailr(f) >= 2)
203     {
204       w = GET_U16(f->bptr);
205       f->bptr += 2;
206       return w;
207     }
208   else
209     return bgetw_slow(f);
210 }
211
212 u32 bgetl_slow(struct fastbuf *f);
213 static inline u32 bgetl(struct fastbuf *f)
214 {
215   u32 l;
216   if (bavailr(f) >= 4)
217     {
218       l = GET_U32(f->bptr);
219       f->bptr += 4;
220       return l;
221     }
222   else
223     return bgetl_slow(f);
224 }
225
226 u64 bgetq_slow(struct fastbuf *f);
227 static inline u64 bgetq(struct fastbuf *f)
228 {
229   u64 l;
230   if (bavailr(f) >= 8)
231     {
232       l = GET_U64(f->bptr);
233       f->bptr += 8;
234       return l;
235     }
236   else
237     return bgetq_slow(f);
238 }
239
240 u64 bget5_slow(struct fastbuf *f);
241 static inline u64 bget5(struct fastbuf *f)
242 {
243   u64 l;
244   if (bavailr(f) >= 5)
245     {
246       l = GET_U40(f->bptr);
247       f->bptr += 5;
248       return l;
249     }
250   else
251     return bget5_slow(f);
252 }
253
254 void bputw_slow(struct fastbuf *f, uns w);
255 static inline void bputw(struct fastbuf *f, uns w)
256 {
257   if (bavailw(f) >= 2)
258     {
259       PUT_U16(f->bptr, w);
260       f->bptr += 2;
261     }
262   else
263     bputw_slow(f, w);
264 }
265
266 void bputl_slow(struct fastbuf *f, u32 l);
267 static inline void bputl(struct fastbuf *f, u32 l)
268 {
269   if (bavailw(f) >= 4)
270     {
271       PUT_U32(f->bptr, l);
272       f->bptr += 4;
273     }
274   else
275     bputl_slow(f, l);
276 }
277
278 void bputq_slow(struct fastbuf *f, u64 l);
279 static inline void bputq(struct fastbuf *f, u64 l)
280 {
281   if (bavailw(f) >= 8)
282     {
283       PUT_U64(f->bptr, l);
284       f->bptr += 8;
285     }
286   else
287     bputq_slow(f, l);
288 }
289
290 void bput5_slow(struct fastbuf *f, u64 l);
291 static inline void bput5(struct fastbuf *f, u64 l)
292 {
293   if (bavailw(f) >= 5)
294     {
295       PUT_U40(f->bptr, l);
296       f->bptr += 5;
297     }
298   else
299     bput5_slow(f, l);
300 }
301
302 uns bread_slow(struct fastbuf *f, void *b, uns l, uns check);
303 static inline uns bread(struct fastbuf *f, void *b, uns l)
304 {
305   if (bavailr(f) >= l)
306     {
307       memcpy(b, f->bptr, l);
308       f->bptr += l;
309       return l;
310     }
311   else
312     return bread_slow(f, b, l, 0);
313 }
314
315 static inline uns breadb(struct fastbuf *f, void *b, uns l)
316 {
317   if (bavailr(f) >= l)
318     {
319       memcpy(b, f->bptr, l);
320       f->bptr += l;
321       return l;
322     }
323   else
324     return bread_slow(f, b, l, 1);
325 }
326
327 void bwrite_slow(struct fastbuf *f, void *b, uns l);
328 static inline void bwrite(struct fastbuf *f, void *b, uns l)
329 {
330   if (bavailw(f) >= l)
331     {
332       memcpy(f->bptr, b, l);
333       f->bptr += l;
334     }
335   else
336     bwrite_slow(f, b, l);
337 }
338
339 byte *bgets(struct fastbuf *f, byte *b, uns l); /* Non-std */
340 int bgets_nodie(struct fastbuf *f, byte *b, uns l);
341 byte *bgets0(struct fastbuf *f, byte *b, uns l);
342
343 struct mempool;
344 struct bb_t;
345 uns bgets_bb(struct fastbuf *f, struct bb_t *b, uns limit);
346 byte *bgets_mp(struct fastbuf *f, struct mempool *mp);
347
348 struct bgets_stk_struct {
349   struct fastbuf *f;
350   byte *old_buf, *cur_buf, *src;
351   uns old_len, cur_len, src_len;
352 };
353 void bgets_stk_init(struct bgets_stk_struct *s);
354 void bgets_stk_step(struct bgets_stk_struct *s);
355 #define bgets_stk(fb) ({ struct bgets_stk_struct _s; _s.f = (fb); for (bgets_stk_init(&_s); _s.cur_len; _s.cur_buf = alloca(_s.cur_len), bgets_stk_step(&_s)); _s.cur_buf; })
356
357 static inline void
358 bputs(struct fastbuf *f, byte *b)
359 {
360   bwrite(f, b, strlen(b));
361 }
362
363 static inline void
364 bputs0(struct fastbuf *f, byte *b)
365 {
366   bwrite(f, b, strlen(b)+1);
367 }
368
369 static inline void
370 bputsn(struct fastbuf *f, byte *b)
371 {
372   bputs(f, b);
373   bputc(f, '\n');
374 }
375
376 void bbcopy_slow(struct fastbuf *f, struct fastbuf *t, uns l);
377 static inline void
378 bbcopy(struct fastbuf *f, struct fastbuf *t, uns l)
379 {
380   if (bavailr(f) >= l && bavailw(t) >= l)
381     {
382       memcpy(t->bptr, f->bptr, l);
383       t->bptr += l;
384       f->bptr += l;
385     }
386   else
387     bbcopy_slow(f, t, l);
388 }
389
390 int bskip_slow(struct fastbuf *f, uns len);
391 static inline int bskip(struct fastbuf *f, uns len)
392 {
393   if (bavailr(f) >= len)
394     {
395       f->bptr += len;
396       return 1;
397     }
398   else
399     return bskip_slow(f, len);
400 }
401
402 /* I/O on addr_int_t */
403
404 #ifdef CPU_64BIT_POINTERS
405 #define bputa(x,p) bputq(x,p)
406 #define bgeta(x) bgetq(x)
407 #else
408 #define bputa(x,p) bputl(x,p)
409 #define bgeta(x) bgetl(x)
410 #endif
411
412 /* Direct I/O on buffers */
413
414 static inline uns
415 bdirect_read_prepare(struct fastbuf *f, byte **buf)
416 {
417   if (f->bptr == f->bstop && !f->refill(f))
418     {
419       *buf = NULL;  // This is not needed, but it helps to get rid of spurious warnings
420       return 0;
421     }
422   *buf = f->bptr;
423   return bavailr(f);
424 }
425
426 static inline void
427 bdirect_read_commit(struct fastbuf *f, byte *pos)
428 {
429   f->bptr = pos;
430 }
431
432 static inline void
433 bdirect_read_commit_modified(struct fastbuf *f, byte *pos)
434 {
435   f->bptr = pos;
436   f->buffer = pos;      /* Avoid seeking backwards in the buffer */
437 }
438
439 static inline uns
440 bdirect_write_prepare(struct fastbuf *f, byte **buf)
441 {
442   if (f->bptr == f->bufend)
443     f->spout(f);
444   *buf = f->bptr;
445   return bavailw(f);
446 }
447
448 static inline void
449 bdirect_write_commit(struct fastbuf *f, byte *pos)
450 {
451   f->bptr = pos;
452 }
453
454 /* Formatted output */
455
456 int bprintf(struct fastbuf *b, char *msg, ...) FORMAT_CHECK(printf,2,3);
457 int vbprintf(struct fastbuf *b, char *msg, va_list args);
458
459 #endif