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