2 * UCW Library -- Fast Buffered I/O on Files
4 * (c) 1997--2007 Martin Mares <mj@ucw.cz>
5 * (c) 2007 Pavel Charvat <pchar@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "ucw/fastbuf.h"
22 int fd; /* File descriptor */
24 int keep_back_buf; /* Optimize for backwards reading */
25 ucw_off_t wpos; /* Real file position */
26 uns wlen; /* Window size */
28 #define FB_FILE(f) ((struct fb_file *)(f)->is_fastbuf)
29 #define FB_BUFFER(f) (byte *)(FB_FILE(f) + 1)
32 bfd_refill(struct fastbuf *f)
34 struct fb_file *F = FB_FILE(f);
35 byte *read_ptr = (f->buffer = FB_BUFFER(f));
36 uns blen = f->bufend - f->buffer, back = F->keep_back_buf ? blen >> 2 : 0, read_len = blen;
37 /* Forward or no seek */
38 if (F->wpos <= f->pos)
40 ucw_off_t diff = f->pos - F->wpos;
41 /* Formula for long forward seeks (prefer lseek()) */
42 if (diff > ((ucw_off_t)blen << 2))
45 f->bptr = f->buffer + back;
46 f->bstop = f->buffer + blen;
49 /* Short forward seek (prefer read() to skip data )*/
50 else if ((uns)diff >= back)
52 uns skip = diff - back;
56 int l = read(F->fd, f->buffer, MIN(skip, blen));
59 die("Error reading %s: %m", f->name);
68 /* Reuse part of the previous window and append new data (also F->wpos == f->pos) */
71 uns keep = back - (uns)diff;
73 back = diff + (keep = F->wlen);
75 memmove(f->buffer, f->buffer + F->wlen - keep, keep);
79 f->bptr = f->buffer + back;
80 f->bstop = f->buffer + blen;
85 ucw_off_t diff = F->wpos - f->pos;
86 /* Formula for long backwards seeks (keep smaller backbuffer than for shorter seeks ) */
87 if (diff > ((ucw_off_t)blen << 1))
89 if ((ucw_off_t)back > f->pos)
93 /* Seek into previous window (do nothing... for example brewind) */
94 else if ((uns)diff <= F->wlen)
96 f->bstop = f->buffer + F->wlen;
97 f->bptr = f->bstop - diff;
102 if ((ucw_off_t)back > f->pos)
104 f->bptr = f->buffer + back;
106 f->bstop = f->buffer + read_len;
107 /* Reuse part of previous window */
108 if (F->wlen && read_len <= back + diff && read_len > back + diff - F->wlen)
110 uns keep = read_len + F->wlen - back - diff;
111 memmove(f->buffer + read_len - keep, f->buffer, keep);
115 F->wpos = f->pos + (f->buffer - f->bptr);
116 if (ucw_seek(F->fd, F->wpos, SEEK_SET) < 0)
117 die("Error seeking %s: %m", f->name);
119 /* Read (part of) buffer */
122 int l = read(F->fd, read_ptr, read_len);
124 die("Error reading %s: %m", f->name);
126 if (unlikely(read_ptr < f->bptr))
129 break; /* Incomplete read because of EOF */
134 while (read_ptr <= f->bptr);
137 f->pos += f->bstop - f->bptr;
138 F->wlen = f->bstop - f->buffer;
139 return f->bstop - f->bptr;
141 /* Seeked behind EOF */
142 f->bptr = f->bstop = f->buffer;
148 bfd_spout(struct fastbuf *f)
150 /* Do delayed lseek() if needed */
151 if (FB_FILE(f)->wpos != f->pos && ucw_seek(FB_FILE(f)->fd, f->pos, SEEK_SET) < 0)
152 die("Error seeking %s: %m", f->name);
154 int l = f->bptr - f->buffer;
157 /* Write the buffer */
158 FB_FILE(f)->wpos = (f->pos += l);
159 FB_FILE(f)->wlen = 0;
162 int z = write(FB_FILE(f)->fd, c, l);
164 die("Error writing %s: %m", f->name);
168 f->bptr = f->buffer = FB_BUFFER(f);
172 bfd_seek(struct fastbuf *f, ucw_off_t pos, int whence)
174 /* Delay the seek for the next refill() or spout() call (if whence != SEEK_END). */
183 if ((pos > 0) ^ (l > f->pos))
188 l = ucw_seek(FB_FILE(f)->fd, pos, SEEK_END);
191 FB_FILE(f)->wpos = f->pos = l;
192 FB_FILE(f)->wlen = 0;
200 bfd_close(struct fastbuf *f)
202 bclose_file_helper(f, FB_FILE(f)->fd, FB_FILE(f)->is_temp_file);
207 bfd_config(struct fastbuf *f, uns item, int value)
213 case BCONFIG_IS_TEMP_FILE:
214 orig = FB_FILE(f)->is_temp_file;
215 FB_FILE(f)->is_temp_file = value;
217 case BCONFIG_KEEP_BACK_BUF:
218 orig = FB_FILE(f)->keep_back_buf;
219 FB_FILE(f)->keep_back_buf = value;
227 bfdopen_internal(int fd, const char *name, uns buflen)
230 int namelen = strlen(name) + 1;
231 struct fb_file *F = xmalloc_zero(sizeof(struct fb_file) + buflen + namelen);
232 struct fastbuf *f = &F->fb;
234 bzero(F, sizeof(*F));
235 f->buffer = (byte *)(F+1);
236 f->bptr = f->bstop = f->buffer;
237 f->bufend = f->buffer + buflen;
239 memcpy(f->name, name, namelen);
241 f->refill = bfd_refill;
242 f->spout = bfd_spout;
244 f->close = bfd_close;
245 f->config = bfd_config;
246 f->can_overwrite_buffer = 2;
251 bfilesync(struct fastbuf *b)
254 if (fsync(FB_FILE(b)->fd) < 0)
255 msg(L_ERROR, "fsync(%s) failed: %m", b->name);
262 struct fastbuf *f, *t;
264 t = bfdopen_shared(1, 13);
265 for (uns i = 0; i < 16; i++)
266 bwrite(f, "<hello>", 7);
267 bprintf(t, "%d\n", (int)btell(f));
270 bprintf(t, "\n%d %d\n", (int)btell(f), (int)btell(t));