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))
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 bthrow(f, "read", "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 bthrow(f, "read", "Error seeking %s: %m", f->name);
119 /* Read (part of) buffer */
122 int l = read(F->fd, read_ptr, read_len);
124 bthrow(f, "read", "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 bthrow(f, "write", "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 bthrow(f, "write", "Error writing %s: %m", f->name);
168 f->bptr = f->bstop = f->buffer = FB_BUFFER(f);
172 bfd_seek(struct fastbuf *f, ucw_off_t pos, int whence)
174 ASSERT(f->bptr == f->bstop);
175 /* Delay the seek for the next refill() or spout() call (if whence != SEEK_END). */
182 ucw_off_t l = ucw_seek(FB_FILE(f)->fd, pos, SEEK_END);
184 bthrow(f, "seek", "Error seeking %s: %m", f->name);
185 FB_FILE(f)->wpos = f->pos = l;
186 FB_FILE(f)->wlen = 0;
194 bfd_close(struct fastbuf *f)
196 bclose_file_helper(f, FB_FILE(f)->fd, FB_FILE(f)->is_temp_file);
201 bfd_config(struct fastbuf *f, uns item, int value)
207 case BCONFIG_IS_TEMP_FILE:
208 orig = FB_FILE(f)->is_temp_file;
209 FB_FILE(f)->is_temp_file = value;
211 case BCONFIG_KEEP_BACK_BUF:
212 orig = FB_FILE(f)->keep_back_buf;
213 FB_FILE(f)->keep_back_buf = value;
221 bfdopen_internal(int fd, const char *name, uns buflen)
224 int namelen = strlen(name) + 1;
225 struct fb_file *F = xmalloc_zero(sizeof(struct fb_file) + buflen + namelen);
226 struct fastbuf *f = &F->fb;
228 bzero(F, sizeof(*F));
229 f->buffer = (byte *)(F+1);
230 f->bptr = f->bstop = f->buffer;
231 f->bufend = f->buffer + buflen;
233 memcpy(f->name, name, namelen);
235 f->refill = bfd_refill;
236 f->spout = bfd_spout;
238 f->close = bfd_close;
239 f->config = bfd_config;
240 f->can_overwrite_buffer = 2;
245 bfilesync(struct fastbuf *b)
248 if (fsync(FB_FILE(b)->fd) < 0)
249 msg(L_ERROR, "fsync(%s) failed: %m", b->name);
256 struct fastbuf *f, *t;
258 t = bfdopen_shared(1, 13);
259 for (uns i = 0; i < 16; i++)
260 bwrite(f, "<hello>", 7);
261 bprintf(t, "%d\n", (int)btell(f));
264 bprintf(t, "\n%d %d\n", (int)btell(f), (int)btell(t));