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.
12 * - O_WRONLY ? (& generally processing of mode bits)
16 #define MMAP_WINDOW_SIZE 16*PAGE_SIZE
17 #define MMAP_EXTEND_SIZE 4*PAGE_SIZE
19 #define MMAP_WINDOW_SIZE 256*PAGE_SIZE
20 #define MMAP_EXTEND_SIZE 256*PAGE_SIZE
24 #include "lib/fastbuf.h"
43 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
46 bfmm_map_window(struct fastbuf *f)
48 struct fb_mmap *F = FB_MMAP(f);
49 sh_off_t pos0 = f->pos & ~(sh_off_t)(PAGE_SIZE-1);
50 int l = MIN(MMAP_WINDOW_SIZE, F->file_extend - pos0);
51 uns ll = ALIGN(l, PAGE_SIZE);
52 uns oll = ALIGN(f->bufend - f->buffer, PAGE_SIZE);
53 int prot = F->is_writeable ? (PROT_READ | PROT_WRITE) : PROT_READ;
55 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);
58 munmap(f->buffer, oll);
62 f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
64 f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
65 if (f->buffer == (byte *) MAP_FAILED)
66 die("mmap(%s): %m", f->name);
67 #ifdef MADV_SEQUENTIAL
69 madvise(f->buffer, ll, MADV_SEQUENTIAL);
71 f->bufend = f->buffer + l;
72 f->bptr = f->buffer + (f->pos - pos0);
73 F->window_pos = f->pos;
77 bfmm_refill(struct fastbuf *f)
79 struct fb_mmap *F = FB_MMAP(f);
81 DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
82 if (f->pos >= F->file_size)
84 if (f->bstop >= f->bufend)
86 if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
87 f->bstop = f->buffer + (F->file_size - F->window_pos);
90 f->pos = F->window_pos + (f->bstop - f->buffer);
91 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
96 bfmm_spout(struct fastbuf *f)
98 struct fb_mmap *F = FB_MMAP(f);
99 sh_off_t end = f->pos + (f->bptr - f->bstop);
101 DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
102 if (end > F->file_size)
104 if (f->bptr < f->bufend)
107 if (f->pos >= F->file_extend)
109 F->file_extend = ALIGN(F->file_extend + MMAP_EXTEND_SIZE, (sh_off_t)PAGE_SIZE);
110 if (sh_ftruncate(F->fd, F->file_extend))
111 die("ftruncate(%s): %m", f->name);
115 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
119 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
121 if (whence == SEEK_END)
122 pos += FB_MMAP(f)->file_size;
124 ASSERT(whence == SEEK_SET);
125 ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
127 f->bptr = f->bstop = f->bufend; /* force refill/spout call */
128 DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
132 bfmm_close(struct fastbuf *f)
134 struct fb_mmap *F = FB_MMAP(f);
137 munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
138 if (F->file_extend > F->file_size &&
139 sh_ftruncate(F->fd, F->file_size))
140 die("ftruncate(%s): %m", f->name);
141 switch (F->is_temp_file)
144 if (unlink(f->name) < 0)
145 log(L_ERROR, "unlink(%s): %m", f->name);
153 bfmm_config(struct fastbuf *f, uns item, int value)
157 case BCONFIG_IS_TEMP_FILE:
158 FB_MMAP(f)->is_temp_file = value;
165 static struct fastbuf *
166 bfmmopen_internal(int fd, byte *name, uns mode)
168 int namelen = strlen(name) + 1;
169 struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
170 struct fastbuf *f = &F->fb;
172 bzero(F, sizeof(*F));
173 f->name = (byte *)(F+1);
174 memcpy(f->name, name, namelen);
176 F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
178 f->pos = F->file_size;
180 if (mode & O_WRONLY || mode & O_RDWR)
183 f->refill = bfmm_refill;
184 f->spout = bfmm_spout;
186 f->close = bfmm_close;
187 f->config = bfmm_config;
192 bopen_mm(byte *name, uns mode)
194 int fd = sh_open(name, mode, 0666);
196 die("Unable to %s file %s: %m",
197 (mode & O_CREAT) ? "create" : "open", name);
198 return bfmmopen_internal(fd, name, mode);
203 int main(int argc, char **argv)
205 struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
206 struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
210 while ((c = bgetc(f)) >= 0)
213 DBG("Seek inside last block");
214 bsetpos(g, btell(g)-1333);
216 DBG("Seek to the beginning & write");
223 DBG("Seek nearby & read");
226 DBG("Seek far & read");