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.
13 #include "ucw/fastbuf.h"
22 static uns mmap_window_size = 16*CPU_PAGE_SIZE;
23 static uns mmap_extend_size = 4*CPU_PAGE_SIZE;
25 static struct cf_section fbmm_config = {
27 CF_UNS("WindowSize", &mmap_window_size),
28 CF_UNS("ExtendSize", &mmap_extend_size),
33 static void CONSTRUCTOR fbmm_init_config(void)
35 cf_declare_section("FBMMap", &fbmm_config, 0);
43 ucw_off_t file_extend;
48 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
51 bfmm_map_window(struct fastbuf *f)
53 struct fb_mmap *F = FB_MMAP(f);
54 ucw_off_t pos0 = f->pos & ~(ucw_off_t)(CPU_PAGE_SIZE-1);
55 int l = MIN((ucw_off_t)mmap_window_size, F->file_extend - pos0);
56 uns ll = ALIGN_TO(l, CPU_PAGE_SIZE);
57 int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
59 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);
60 if (ll != F->window_size && f->buffer)
62 munmap(f->buffer, F->window_size);
67 f->buffer = ucw_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
69 f->buffer = ucw_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
70 if (f->buffer == (byte *) MAP_FAILED)
71 die("mmap(%s): %m", f->name);
72 #ifdef MADV_SEQUENTIAL
73 if (ll > CPU_PAGE_SIZE)
74 madvise(f->buffer, ll, MADV_SEQUENTIAL);
76 f->bufend = f->buffer + l;
77 f->bptr = f->buffer + (f->pos - pos0);
82 bfmm_refill(struct fastbuf *f)
84 struct fb_mmap *F = FB_MMAP(f);
86 DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
87 if (f->pos >= F->file_size)
89 if (f->bstop >= f->bufend)
91 if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
92 f->bstop = f->buffer + (F->file_size - F->window_pos);
95 f->pos = F->window_pos + (f->bstop - f->buffer);
96 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
101 bfmm_spout(struct fastbuf *f)
103 struct fb_mmap *F = FB_MMAP(f);
104 ucw_off_t end = f->pos + (f->bptr - f->bstop);
106 DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
107 if (end > F->file_size)
109 if (f->bptr < f->bufend)
112 if (f->pos >= F->file_extend)
114 F->file_extend = ALIGN_TO(F->file_extend + mmap_extend_size, (ucw_off_t)CPU_PAGE_SIZE);
115 if (ucw_ftruncate(F->fd, F->file_extend))
116 die("ftruncate(%s): %m", f->name);
120 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
124 bfmm_seek(struct fastbuf *f, ucw_off_t pos, int whence)
126 if (whence == SEEK_END)
127 pos += FB_MMAP(f)->file_size;
129 ASSERT(whence == SEEK_SET);
130 ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
132 f->bptr = f->bstop = f->bufend = f->buffer; /* force refill/spout call */
133 DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
138 bfmm_close(struct fastbuf *f)
140 struct fb_mmap *F = FB_MMAP(f);
143 munmap(f->buffer, F->window_size);
144 if (F->file_extend > F->file_size &&
145 ucw_ftruncate(F->fd, F->file_size))
146 die("ftruncate(%s): %m", f->name);
147 bclose_file_helper(f, F->fd, F->is_temp_file);
152 bfmm_config(struct fastbuf *f, uns item, int value)
158 case BCONFIG_IS_TEMP_FILE:
159 orig = FB_MMAP(f)->is_temp_file;
160 FB_MMAP(f)->is_temp_file = value;
168 bfmmopen_internal(int fd, const char *name, uns mode)
170 int namelen = strlen(name) + 1;
171 struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
172 struct fastbuf *f = &F->fb;
174 bzero(F, sizeof(*F));
175 f->name = (byte *)(F+1);
176 memcpy(f->name, name, namelen);
178 F->file_extend = F->file_size = ucw_seek(fd, 0, SEEK_END);
179 if (F->file_size < 0)
180 die("seek(%s): %m", name);
182 f->pos = F->file_size;
185 f->refill = bfmm_refill;
186 f->spout = bfmm_spout;
188 f->close = bfmm_close;
189 f->config = bfmm_config;
195 int main(int argc, char **argv)
197 struct fb_params par = { .type = FB_MMAP };
198 struct fastbuf *f = bopen_file(argv[1], O_RDONLY, &par);
199 struct fastbuf *g = bopen_file(argv[2], O_RDWR | O_CREAT | O_TRUNC, &par);
203 while ((c = bgetc(f)) >= 0)
206 DBG("Seek inside last block");
207 bsetpos(g, btell(g)-1333);
209 DBG("Seek to the beginning & write");
216 DBG("Seek nearby & read");
219 DBG("Seek far & read");