2 * UCW 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"
20 static uns mmap_window_size = 16*CPU_PAGE_SIZE;
21 static uns mmap_extend_size = 4*CPU_PAGE_SIZE;
23 static struct cf_section fbmm_config = {
25 CF_UNS("WindowSize", &mmap_window_size),
26 CF_UNS("ExtendSize", &mmap_extend_size),
31 static void CONSTRUCTOR fbmm_init_config(void)
33 cf_declare_section("FBMMap", &fbmm_config, 0);
45 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
48 bfmm_map_window(struct fastbuf *f)
50 struct fb_mmap *F = FB_MMAP(f);
51 sh_off_t pos0 = f->pos & ~(sh_off_t)(CPU_PAGE_SIZE-1);
52 int l = MIN((sh_off_t)mmap_window_size, F->file_extend - pos0);
53 uns ll = ALIGN_TO(l, CPU_PAGE_SIZE);
54 uns oll = ALIGN_TO(f->bufend - f->buffer, CPU_PAGE_SIZE);
55 int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
57 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 if (ll != oll && f->buffer)
60 munmap(f->buffer, oll);
64 f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
66 f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
67 if (f->buffer == (byte *) MAP_FAILED)
68 die("mmap(%s): %m", f->name);
69 #ifdef MADV_SEQUENTIAL
70 if (ll > CPU_PAGE_SIZE)
71 madvise(f->buffer, ll, MADV_SEQUENTIAL);
73 f->bufend = f->buffer + l;
74 f->bptr = f->buffer + (f->pos - pos0);
79 bfmm_refill(struct fastbuf *f)
81 struct fb_mmap *F = FB_MMAP(f);
83 DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
84 if (f->pos >= F->file_size)
86 if (f->bstop >= f->bufend)
88 if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
89 f->bstop = f->buffer + (F->file_size - F->window_pos);
92 f->pos = F->window_pos + (f->bstop - f->buffer);
93 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
98 bfmm_spout(struct fastbuf *f)
100 struct fb_mmap *F = FB_MMAP(f);
101 sh_off_t end = f->pos + (f->bptr - f->bstop);
103 DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
104 if (end > F->file_size)
106 if (f->bptr < f->bufend)
109 if (f->pos >= F->file_extend)
111 F->file_extend = ALIGN_TO(F->file_extend + mmap_extend_size, (sh_off_t)CPU_PAGE_SIZE);
112 if (sh_ftruncate(F->fd, F->file_extend))
113 die("ftruncate(%s): %m", f->name);
117 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
121 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
123 if (whence == SEEK_END)
124 pos += FB_MMAP(f)->file_size;
126 ASSERT(whence == SEEK_SET);
127 ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
129 f->bptr = f->bstop = f->bufend; /* force refill/spout call */
130 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_TO(f->bufend-f->buffer, CPU_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;
169 bfmmopen_internal(int fd, byte *name, uns mode)
171 int namelen = strlen(name) + 1;
172 struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
173 struct fastbuf *f = &F->fb;
175 bzero(F, sizeof(*F));
176 f->name = (byte *)(F+1);
177 memcpy(f->name, name, namelen);
179 F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
181 f->pos = F->file_size;
184 f->refill = bfmm_refill;
185 f->spout = bfmm_spout;
187 f->close = bfmm_close;
188 f->config = bfmm_config;
194 int main(int argc, char **argv)
196 struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
197 struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
201 while ((c = bgetc(f)) >= 0)
204 DBG("Seek inside last block");
205 bsetpos(g, btell(g)-1333);
207 DBG("Seek to the beginning & write");
214 DBG("Seek nearby & read");
217 DBG("Seek far & read");