]> mj.ucw.cz Git - moe.git/blob - ucw/fb-mmap.c
Merge commit '700824d3e9bce9219819e4e5096ab94da301d44b' from branch bernard/master
[moe.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/lfs.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 uns mmap_window_size = 16*CPU_PAGE_SIZE;
23 static uns mmap_extend_size = 4*CPU_PAGE_SIZE;
24
25 #ifndef TEST
26 static struct cf_section fbmm_config = {
27   CF_ITEMS {
28     CF_UNS("WindowSize", &mmap_window_size),
29     CF_UNS("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   uns window_size;
48   int mode;
49 };
50 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
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   uns 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     die("mmap(%s): %m", f->name);
74 #ifdef MADV_SEQUENTIAL
75   if (ll > CPU_PAGE_SIZE)
76     madvise(f->buffer, ll, MADV_SEQUENTIAL);
77 #endif
78   f->bufend = f->buffer + l;
79   f->bptr = f->buffer + (f->pos - pos0);
80   F->window_pos = pos0;
81 }
82
83 static int
84 bfmm_refill(struct fastbuf *f)
85 {
86   struct fb_mmap *F = FB_MMAP(f);
87
88   DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
89   if (f->pos >= F->file_size)
90     return 0;
91   if (f->bstop >= f->bufend)
92     bfmm_map_window(f);
93   if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
94     f->bstop = f->buffer + (F->file_size - F->window_pos);
95   else
96     f->bstop = f->bufend;
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);
99   return 1;
100 }
101
102 static void
103 bfmm_spout(struct fastbuf *f)
104 {
105   struct fb_mmap *F = FB_MMAP(f);
106   ucw_off_t end = f->pos + (f->bptr - f->bstop);
107
108   DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
109   if (end > F->file_size)
110     F->file_size = end;
111   if (f->bptr < f->bufend)
112     return;
113   f->pos = end;
114   if (f->pos >= F->file_extend)
115     {
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);
119     }
120   bfmm_map_window(f);
121   f->bstop = f->bptr;
122   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
123 }
124
125 static int
126 bfmm_seek(struct fastbuf *f, ucw_off_t pos, int whence)
127 {
128   if (whence == SEEK_END)
129     pos += FB_MMAP(f)->file_size;
130   else
131     ASSERT(whence == SEEK_SET);
132   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
133   f->pos = pos;
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);
136   return 1;
137 }
138
139 static void
140 bfmm_close(struct fastbuf *f)
141 {
142   struct fb_mmap *F = FB_MMAP(f);
143
144   if (f->buffer)
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);
150   xfree(f);
151 }
152
153 static int
154 bfmm_config(struct fastbuf *f, uns item, int value)
155 {
156   int orig;
157
158   switch (item)
159     {
160     case BCONFIG_IS_TEMP_FILE:
161       orig = FB_MMAP(f)->is_temp_file;
162       FB_MMAP(f)->is_temp_file = value;
163       return orig;
164     default:
165       return -1;
166     }
167 }
168
169 struct fastbuf *
170 bfmmopen_internal(int fd, const char *name, uns mode)
171 {
172   int namelen = strlen(name) + 1;
173   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
174   struct fastbuf *f = &F->fb;
175
176   bzero(F, sizeof(*F));
177   f->name = (byte *)(F+1);
178   memcpy(f->name, name, namelen);
179   F->fd = fd;
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);
183   if (mode & O_APPEND)
184     f->pos = F->file_size;
185   F->mode = mode;
186
187   f->refill = bfmm_refill;
188   f->spout = bfmm_spout;
189   f->seek = bfmm_seek;
190   f->close = bfmm_close;
191   f->config = bfmm_config;
192   return f;
193 }
194
195 #ifdef TEST
196
197 int main(int UNUSED argc, char **argv)
198 {
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);
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