]> mj.ucw.cz Git - libucw.git/blob - lib/fb-mmap.c
d16458ef83b8e52de73a5afaab6b474d2cca9699
[libucw.git] / lib / fb-mmap.c
1 /*
2  *      UCW Library -- Fast Buffered I/O on Memory-Mapped Files
3  *
4  *      (c) 2002 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/fastbuf.h"
12 #include "lib/lfs.h"
13 #include "lib/conf.h"
14
15 #include <string.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18 #include <sys/mman.h>
19
20 static uns mmap_window_size = 16*CPU_PAGE_SIZE;
21 static uns mmap_extend_size = 4*CPU_PAGE_SIZE;
22
23 static struct cf_section fbmm_config = {
24   CF_ITEMS {
25     CF_UNS("WindowSize", &mmap_window_size),
26     CF_UNS("ExtendSize", &mmap_extend_size),
27     CF_END
28   }
29 };
30
31 static void CONSTRUCTOR fbmm_init_config(void)
32 {
33   cf_declare_section("FBMMap", &fbmm_config, 0);
34 }
35
36 struct fb_mmap {
37   struct fastbuf fb;
38   int fd;
39   int is_temp_file;
40   sh_off_t file_size;
41   sh_off_t file_extend;
42   sh_off_t window_pos;
43   uns window_size;
44   int mode;
45 };
46 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
47
48 static void
49 bfmm_map_window(struct fastbuf *f)
50 {
51   struct fb_mmap *F = FB_MMAP(f);
52   sh_off_t pos0 = f->pos & ~(sh_off_t)(CPU_PAGE_SIZE-1);
53   int l = MIN((sh_off_t)mmap_window_size, F->file_extend - pos0);
54   uns ll = ALIGN_TO(l, CPU_PAGE_SIZE);
55   int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
56
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 != F->window_size && f->buffer)
59     {
60       munmap(f->buffer, F->window_size);
61       f->buffer = NULL;
62     }
63   F->window_size = ll;
64   if (!f->buffer)
65     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
66   else
67     f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
68   if (f->buffer == (byte *) MAP_FAILED)
69     die("mmap(%s): %m", f->name);
70 #ifdef MADV_SEQUENTIAL
71   if (ll > CPU_PAGE_SIZE)
72     madvise(f->buffer, ll, MADV_SEQUENTIAL);
73 #endif
74   f->bufend = f->buffer + l;
75   f->bptr = f->buffer + (f->pos - pos0);
76   F->window_pos = pos0;
77 }
78
79 static int
80 bfmm_refill(struct fastbuf *f)
81 {
82   struct fb_mmap *F = FB_MMAP(f);
83
84   DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
85   if (f->pos >= F->file_size)
86     return 0;
87   if (f->bstop >= f->bufend)
88     bfmm_map_window(f);
89   if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
90     f->bstop = f->buffer + (F->file_size - F->window_pos);
91   else
92     f->bstop = f->bufend;
93   f->pos = F->window_pos + (f->bstop - f->buffer);
94   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
95   return 1;
96 }
97
98 static void
99 bfmm_spout(struct fastbuf *f)
100 {
101   struct fb_mmap *F = FB_MMAP(f);
102   sh_off_t end = f->pos + (f->bptr - f->bstop);
103
104   DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
105   if (end > F->file_size)
106     F->file_size = end;
107   if (f->bptr < f->bufend)
108     return;
109   f->pos = end;
110   if (f->pos >= F->file_extend)
111     {
112       F->file_extend = ALIGN_TO(F->file_extend + mmap_extend_size, (sh_off_t)CPU_PAGE_SIZE);
113       if (sh_ftruncate(F->fd, F->file_extend))
114         die("ftruncate(%s): %m", f->name);
115     }
116   bfmm_map_window(f);
117   f->bstop = f->bptr;
118   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
119 }
120
121 static int
122 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
123 {
124   if (whence == SEEK_END)
125     pos += FB_MMAP(f)->file_size;
126   else
127     ASSERT(whence == SEEK_SET);
128   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
129   f->pos = pos;
130   f->bptr = f->bstop = f->bufend = f->buffer;   /* force refill/spout call */
131   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
132   return 1;
133 }
134
135 static void
136 bfmm_close(struct fastbuf *f)
137 {
138   struct fb_mmap *F = FB_MMAP(f);
139
140   if (f->buffer)
141     munmap(f->buffer, F->window_size);
142   if (F->file_extend > F->file_size &&
143       sh_ftruncate(F->fd, F->file_size))
144     die("ftruncate(%s): %m", f->name);
145   switch (F->is_temp_file)
146     {
147     case 1:
148       if (unlink(f->name) < 0)
149         msg(L_ERROR, "unlink(%s): %m", f->name);
150     case 0:
151       if (close(F->fd))
152         die("close(%s): %m", f->name);
153     }
154   xfree(f);
155 }
156
157 static int
158 bfmm_config(struct fastbuf *f, uns item, int value)
159 {
160   switch (item)
161     {
162     case BCONFIG_IS_TEMP_FILE:
163       FB_MMAP(f)->is_temp_file = value;
164       return 0;
165     default:
166       return -1;
167     }
168 }
169
170 struct fastbuf *
171 bfmmopen_internal(int fd, const byte *name, uns mode)
172 {
173   int namelen = strlen(name) + 1;
174   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
175   struct fastbuf *f = &F->fb;
176
177   bzero(F, sizeof(*F));
178   f->name = (byte *)(F+1);
179   memcpy(f->name, name, namelen);
180   F->fd = fd;
181   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
182   if (F->file_size < 0)
183     die("seek(%s): %m", name);
184   if (mode & O_APPEND)
185     f->pos = F->file_size;
186   F->mode = mode;
187
188   f->refill = bfmm_refill;
189   f->spout = bfmm_spout;
190   f->seek = bfmm_seek;
191   f->close = bfmm_close;
192   f->config = bfmm_config;
193   return f;
194 }
195
196 #ifdef TEST
197
198 int main(int argc, char **argv)
199 {
200   struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
201   struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
202   int c;
203
204   DBG("Copying");
205   while ((c = bgetc(f)) >= 0)
206     bputc(g, c);
207   bclose(f);
208   DBG("Seek inside last block");
209   bsetpos(g, btell(g)-1333);
210   bputc(g, 13);
211   DBG("Seek to the beginning & write");
212   bsetpos(g, 1333);
213   bputc(g, 13);
214   DBG("flush");
215   bflush(g);
216   bputc(g, 13);
217   bflush(g);
218   DBG("Seek nearby & read");
219   bsetpos(g, 133);
220   bgetc(g);
221   DBG("Seek far & read");
222   bsetpos(g, 133333);
223   bgetc(g);
224   DBG("Closing");
225   bclose(g);
226
227   return 0;
228 }
229
230 #endif