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;
26 static struct cf_section fbmm_config = {
28 CF_UNS("WindowSize", &mmap_window_size),
29 CF_UNS("ExtendSize", &mmap_extend_size),
34 static void CONSTRUCTOR fbmm_init_config(void)
36 cf_declare_section("FBMMap", &fbmm_config, 0);
45 ucw_off_t file_extend;
50 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
53 bfmm_map_window(struct fastbuf *f)
55 struct fb_mmap *F = FB_MMAP(f);
56 ucw_off_t pos0 = f->pos & ~(ucw_off_t)(CPU_PAGE_SIZE-1);
57 int l = MIN((ucw_off_t)mmap_window_size, F->file_extend - pos0);
58 uns ll = ALIGN_TO(l, CPU_PAGE_SIZE);
59 int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
61 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);
62 if (ll != F->window_size && f->buffer)
64 munmap(f->buffer, F->window_size);
69 f->buffer = ucw_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
71 f->buffer = ucw_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
72 if (f->buffer == (byte *) MAP_FAILED)
73 die("mmap(%s): %m", f->name);
74 #ifdef MADV_SEQUENTIAL
75 if (ll > CPU_PAGE_SIZE)
76 madvise(f->buffer, ll, MADV_SEQUENTIAL);
78 f->bufend = f->buffer + l;
79 f->bptr = f->buffer + (f->pos - pos0);
84 bfmm_refill(struct fastbuf *f)
86 struct fb_mmap *F = FB_MMAP(f);
88 DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
89 if (f->pos >= F->file_size)
91 if (f->bstop >= f->bufend)
93 if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
94 f->bstop = f->buffer + (F->file_size - F->window_pos);
97 f->pos = F->window_pos + (f->bstop - f->buffer);
98 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
103 bfmm_spout(struct fastbuf *f)
105 struct fb_mmap *F = FB_MMAP(f);
106 ucw_off_t end = f->pos + (f->bptr - f->bstop);
108 DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
109 if (end > F->file_size)
111 if (f->bptr < f->bufend)
114 if (f->pos >= F->file_extend)
116 F->file_extend = ALIGN_TO(F->file_extend + mmap_extend_size, (ucw_off_t)CPU_PAGE_SIZE);
117 if (ucw_ftruncate(F->fd, F->file_extend))
118 die("ftruncate(%s): %m", f->name);
122 DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
126 bfmm_seek(struct fastbuf *f, ucw_off_t pos, int whence)
128 if (whence == SEEK_END)
129 pos += FB_MMAP(f)->file_size;
131 ASSERT(whence == SEEK_SET);
132 ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
134 f->bptr = f->bstop = f->bufend = f->buffer; /* force refill/spout call */
135 DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
140 bfmm_close(struct fastbuf *f)
142 struct fb_mmap *F = FB_MMAP(f);
145 munmap(f->buffer, F->window_size);
146 if (F->file_extend > F->file_size &&
147 ucw_ftruncate(F->fd, F->file_size))
148 die("ftruncate(%s): %m", f->name);
149 bclose_file_helper(f, F->fd, F->is_temp_file);
154 bfmm_config(struct fastbuf *f, uns item, int value)
160 case BCONFIG_IS_TEMP_FILE:
161 orig = FB_MMAP(f)->is_temp_file;
162 FB_MMAP(f)->is_temp_file = value;
170 bfmmopen_internal(int fd, const char *name, uns mode)
172 int namelen = strlen(name) + 1;
173 struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
174 struct fastbuf *f = &F->fb;
176 bzero(F, sizeof(*F));
177 f->name = (byte *)(F+1);
178 memcpy(f->name, name, namelen);
180 F->file_extend = F->file_size = ucw_seek(fd, 0, SEEK_END);
181 if (F->file_size < 0)
182 die("seek(%s): %m", name);
184 f->pos = F->file_size;
187 f->refill = bfmm_refill;
188 f->spout = bfmm_spout;
190 f->close = bfmm_close;
191 f->config = bfmm_config;
197 int main(int UNUSED argc, char **argv)
199 struct fb_params par = { .type = FB_MMAP };
200 struct fastbuf *f = bopen_file(argv[1], O_RDONLY, &par);
201 struct fastbuf *g = bopen_file(argv[2], O_RDWR | O_CREAT | O_TRUNC, &par);
205 while ((c = bgetc(f)) >= 0)
208 DBG("Seek inside last block");
209 bsetpos(g, btell(g)-1333);
211 DBG("Seek to the beginning & write");
218 DBG("Seek nearby & read");
221 DBG("Seek far & read");