2 * Sherlock Library -- Fast Buffered I/O on Memory-Mapped Files
4 * (c) 2002 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/fastbuf.h"
22 static uns mmap_window_size = 16*PAGE_SIZE;
23 static uns mmap_extend_size = 4*PAGE_SIZE;
25 static struct cfitem fbmm_config[] = {
26 { "FBMMap", CT_SECTION, NULL },
27 { "WindowSize", CT_INT, &mmap_window_size },
28 { "ExtendSize", CT_INT, &mmap_extend_size },
29 { NULL, CT_STOP, NULL }
32 static void CONSTRUCTOR fbmm_init_config(void)
34 cf_register(fbmm_config);
46 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
49 bfmm_map_window(struct fastbuf *f)
51 struct fb_mmap *F = FB_MMAP(f);
52 sh_off_t pos0 = f->pos & ~(sh_off_t)(PAGE_SIZE-1);
53 int l = MIN((sh_off_t)mmap_window_size, F->file_extend - pos0);
54 uns ll = ALIGN(l, PAGE_SIZE);
55 uns oll = ALIGN(f->bufend - f->buffer, PAGE_SIZE);
56 int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
58 DBG(" ... Mapping %x(%x)+%x(%x) len=%x extend=%x", (int)pos0, (int)f->pos, ll, l, (int)F->file_size, (int)F->file_extend);
59 if (ll != oll && f->buffer)
61 munmap(f->buffer, oll);
65 f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
67 f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
68 if (f->buffer == (byte *) MAP_FAILED)
69 die("mmap(%s): %m", f->name);
70 #ifdef MADV_SEQUENTIAL
72 madvise(f->buffer, ll, MADV_SEQUENTIAL);
74 f->bufend = f->buffer + l;
75 f->bptr = f->buffer + (f->pos - pos0);
80 bfmm_refill(struct fastbuf *f)
82 struct fb_mmap *F = FB_MMAP(f);
84 DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
85 if (f->pos >= F->file_size)
87 if (f->bstop >= f->bufend)
89 if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
90 f->bstop = f->buffer + (F->file_size - F->window_pos);
93 f->pos = F->window_pos + (f->bstop - f->buffer);
94 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
99 bfmm_spout(struct fastbuf *f)
101 struct fb_mmap *F = FB_MMAP(f);
102 sh_off_t end = f->pos + (f->bptr - f->bstop);
104 DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
105 if (end > F->file_size)
107 if (f->bptr < f->bufend)
110 if (f->pos >= F->file_extend)
112 F->file_extend = ALIGN(F->file_extend + mmap_extend_size, (sh_off_t)PAGE_SIZE);
113 if (sh_ftruncate(F->fd, F->file_extend))
114 die("ftruncate(%s): %m", f->name);
118 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
122 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
124 if (whence == SEEK_END)
125 pos += FB_MMAP(f)->file_size;
127 ASSERT(whence == SEEK_SET);
128 ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
130 f->bptr = f->bstop = f->bufend; /* force refill/spout call */
131 DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
135 bfmm_close(struct fastbuf *f)
137 struct fb_mmap *F = FB_MMAP(f);
140 munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
141 if (F->file_extend > F->file_size &&
142 sh_ftruncate(F->fd, F->file_size))
143 die("ftruncate(%s): %m", f->name);
144 switch (F->is_temp_file)
147 if (unlink(f->name) < 0)
148 log(L_ERROR, "unlink(%s): %m", f->name);
156 bfmm_config(struct fastbuf *f, uns item, int value)
160 case BCONFIG_IS_TEMP_FILE:
161 FB_MMAP(f)->is_temp_file = value;
163 case BCONFIG_CAN_OVERWRITE:
164 return 0; /* cannot use 1, because the pages would become dirty */
170 static struct fastbuf *
171 bfmmopen_internal(int fd, byte *name, uns mode)
173 int namelen = strlen(name) + 1;
174 struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
175 struct fastbuf *f = &F->fb;
177 bzero(F, sizeof(*F));
178 f->name = (byte *)(F+1);
179 memcpy(f->name, name, namelen);
181 F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
183 f->pos = F->file_size;
186 f->refill = bfmm_refill;
187 f->spout = bfmm_spout;
189 f->close = bfmm_close;
190 f->config = bfmm_config;
195 bopen_mm(byte *name, uns mode)
199 if ((mode & O_ACCMODE) == O_WRONLY)
200 mode = (mode & ~O_ACCMODE) | O_RDWR;
201 fd = sh_open(name, mode, 0666);
203 die("Unable to %s file %s: %m",
204 (mode & O_CREAT) ? "create" : "open", name);
205 return bfmmopen_internal(fd, name, mode);
210 int main(int argc, char **argv)
212 struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
213 struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
217 while ((c = bgetc(f)) >= 0)
220 DBG("Seek inside last block");
221 bsetpos(g, btell(g)-1333);
223 DBG("Seek to the beginning & write");
230 DBG("Seek nearby & read");
233 DBG("Seek far & read");