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