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 * - problems with temp files
13 * - O_WRONLY ? (& generally processing of mode bits)
17 #define MMAP_WINDOW_SIZE 16*PAGE_SIZE
18 #define MMAP_EXTEND_SIZE 4*PAGE_SIZE
20 #define MMAP_WINDOW_SIZE 256*PAGE_SIZE
21 #define MMAP_EXTEND_SIZE 256*PAGE_SIZE
25 #include "lib/fastbuf.h"
38 int dummy; /* FIXME: dirty hack for is_temp_file, remove */
44 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
47 bfmm_map_window(struct fastbuf *f)
49 struct fb_mmap *F = FB_MMAP(f);
50 sh_off_t pos0 = f->pos & ~(sh_off_t)(PAGE_SIZE-1);
51 int l = MIN(MMAP_WINDOW_SIZE, F->file_extend - pos0);
52 uns ll = ALIGN(l, PAGE_SIZE);
53 uns oll = ALIGN(f->bufend - f->buffer, PAGE_SIZE);
54 int prot = F->is_writeable ? (PROT_READ | PROT_WRITE) : PROT_READ;
56 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 munmap(f->buffer, oll);
63 f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
65 f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
66 if (f->buffer == (byte *) MAP_FAILED)
67 die("mmap(%s): %m", f->name);
69 madvise(f->buffer, ll, MADV_SEQUENTIAL);
70 f->bufend = f->buffer + l;
71 f->bptr = f->buffer + (f->pos - pos0);
72 F->window_pos = f->pos;
76 bfmm_refill(struct fastbuf *f)
78 struct fb_mmap *F = FB_MMAP(f);
80 DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
81 if (f->pos >= F->file_size)
83 if (f->bstop >= f->bufend)
85 if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
86 f->bstop = f->buffer + (F->file_size - F->window_pos);
89 f->pos = F->window_pos + (f->bstop - f->buffer);
90 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
95 bfmm_spout(struct fastbuf *f)
97 struct fb_mmap *F = FB_MMAP(f);
98 sh_off_t end = f->pos + (f->bptr - f->bstop);
100 DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
101 if (end > F->file_size)
103 if (f->bptr < f->bufend)
106 if (f->pos >= F->file_extend)
108 F->file_extend = ALIGN(F->file_extend + MMAP_EXTEND_SIZE, (sh_off_t)PAGE_SIZE);
109 if (sh_ftruncate(F->fd, F->file_extend))
110 die("ftruncate(%s): %m", f->name);
114 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
118 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
120 if (whence == SEEK_END)
121 pos += FB_MMAP(f)->file_size;
123 ASSERT(whence == SEEK_SET);
124 ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
126 f->bptr = f->bstop = f->bufend; /* force refill/spout call */
127 DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
131 bfmm_close(struct fastbuf *f)
133 struct fb_mmap *F = FB_MMAP(f);
136 munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
137 if (F->file_extend > F->file_size &&
138 sh_ftruncate(F->fd, F->file_size))
139 die("ftruncate(%s): %m", f->name);
144 static struct fastbuf *
145 bfmmopen_internal(int fd, byte *name, uns mode)
147 int namelen = strlen(name) + 1;
148 struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
149 struct fastbuf *f = &F->fb;
151 bzero(F, sizeof(*F));
152 f->name = (byte *)(F+1);
153 memcpy(f->name, name, namelen);
155 F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
157 f->pos = F->file_size;
159 if (mode & O_WRONLY || mode & O_RDWR)
162 f->refill = bfmm_refill;
163 f->spout = bfmm_spout;
165 f->close = bfmm_close;
170 bopen_mm(byte *name, uns mode)
172 int fd = sh_open(name, mode, 0666);
174 die("Unable to %s file %s: %m",
175 (mode & O_CREAT) ? "create" : "open", name);
176 return bfmmopen_internal(fd, name, mode);
181 int main(int argc, char **argv)
183 struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
184 struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
188 while ((c = bgetc(f)) >= 0)
191 DBG("Seek inside last block");
192 bsetpos(g, btell(g)-1333);
194 DBG("Seek to the beginning & write");
201 DBG("Seek nearby & read");
204 DBG("Seek far & read");