]> mj.ucw.cz Git - moe.git/blob - lib/fb-mmap.c
Added libucw documentation regarding configuration to doc/ucw/.
[moe.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 #undef LOCAL_DEBUG
11
12 #include "lib/lib.h"
13 #include "lib/fastbuf.h"
14 #include "lib/lfs.h"
15 #include "lib/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 static struct cf_section fbmm_config = {
26   CF_ITEMS {
27     CF_UNS("WindowSize", &mmap_window_size),
28     CF_UNS("ExtendSize", &mmap_extend_size),
29     CF_END
30   }
31 };
32
33 static void CONSTRUCTOR fbmm_init_config(void)
34 {
35   cf_declare_section("FBMMap", &fbmm_config, 0);
36 }
37
38 struct fb_mmap {
39   struct fastbuf fb;
40   int fd;
41   int is_temp_file;
42   sh_off_t file_size;
43   sh_off_t file_extend;
44   sh_off_t window_pos;
45   uns window_size;
46   int mode;
47 };
48 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
49
50 static void
51 bfmm_map_window(struct fastbuf *f)
52 {
53   struct fb_mmap *F = FB_MMAP(f);
54   sh_off_t pos0 = f->pos & ~(sh_off_t)(CPU_PAGE_SIZE-1);
55   int l = MIN((sh_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);
58
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)
61     {
62       munmap(f->buffer, F->window_size);
63       f->buffer = NULL;
64     }
65   F->window_size = ll;
66   if (!f->buffer)
67     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
68   else
69     f->buffer = sh_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);
75 #endif
76   f->bufend = f->buffer + l;
77   f->bptr = f->buffer + (f->pos - pos0);
78   F->window_pos = pos0;
79 }
80
81 static int
82 bfmm_refill(struct fastbuf *f)
83 {
84   struct fb_mmap *F = FB_MMAP(f);
85
86   DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
87   if (f->pos >= F->file_size)
88     return 0;
89   if (f->bstop >= f->bufend)
90     bfmm_map_window(f);
91   if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
92     f->bstop = f->buffer + (F->file_size - F->window_pos);
93   else
94     f->bstop = f->bufend;
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);
97   return 1;
98 }
99
100 static void
101 bfmm_spout(struct fastbuf *f)
102 {
103   struct fb_mmap *F = FB_MMAP(f);
104   sh_off_t end = f->pos + (f->bptr - f->bstop);
105
106   DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
107   if (end > F->file_size)
108     F->file_size = end;
109   if (f->bptr < f->bufend)
110     return;
111   f->pos = end;
112   if (f->pos >= F->file_extend)
113     {
114       F->file_extend = ALIGN_TO(F->file_extend + mmap_extend_size, (sh_off_t)CPU_PAGE_SIZE);
115       if (sh_ftruncate(F->fd, F->file_extend))
116         die("ftruncate(%s): %m", f->name);
117     }
118   bfmm_map_window(f);
119   f->bstop = f->bptr;
120   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
121 }
122
123 static int
124 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
125 {
126   if (whence == SEEK_END)
127     pos += FB_MMAP(f)->file_size;
128   else
129     ASSERT(whence == SEEK_SET);
130   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
131   f->pos = pos;
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);
134   return 1;
135 }
136
137 static void
138 bfmm_close(struct fastbuf *f)
139 {
140   struct fb_mmap *F = FB_MMAP(f);
141
142   if (f->buffer)
143     munmap(f->buffer, F->window_size);
144   if (F->file_extend > F->file_size &&
145       sh_ftruncate(F->fd, F->file_size))
146     die("ftruncate(%s): %m", f->name);
147   bclose_file_helper(f, F->fd, F->is_temp_file);
148   xfree(f);
149 }
150
151 static int
152 bfmm_config(struct fastbuf *f, uns item, int value)
153 {
154   int orig;
155
156   switch (item)
157     {
158     case BCONFIG_IS_TEMP_FILE:
159       orig = FB_MMAP(f)->is_temp_file;
160       FB_MMAP(f)->is_temp_file = value;
161       return orig;
162     default:
163       return -1;
164     }
165 }
166
167 struct fastbuf *
168 bfmmopen_internal(int fd, const char *name, uns mode)
169 {
170   int namelen = strlen(name) + 1;
171   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
172   struct fastbuf *f = &F->fb;
173
174   bzero(F, sizeof(*F));
175   f->name = (byte *)(F+1);
176   memcpy(f->name, name, namelen);
177   F->fd = fd;
178   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
179   if (F->file_size < 0)
180     die("seek(%s): %m", name);
181   if (mode & O_APPEND)
182     f->pos = F->file_size;
183   F->mode = mode;
184
185   f->refill = bfmm_refill;
186   f->spout = bfmm_spout;
187   f->seek = bfmm_seek;
188   f->close = bfmm_close;
189   f->config = bfmm_config;
190   return f;
191 }
192
193 #ifdef TEST
194
195 int main(int argc, char **argv)
196 {
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);
200   int c;
201
202   DBG("Copying");
203   while ((c = bgetc(f)) >= 0)
204     bputc(g, c);
205   bclose(f);
206   DBG("Seek inside last block");
207   bsetpos(g, btell(g)-1333);
208   bputc(g, 13);
209   DBG("Seek to the beginning & write");
210   bsetpos(g, 1333);
211   bputc(g, 13);
212   DBG("flush");
213   bflush(g);
214   bputc(g, 13);
215   bflush(g);
216   DBG("Seek nearby & read");
217   bsetpos(g, 133);
218   bgetc(g);
219   DBG("Seek far & read");
220   bsetpos(g, 133333);
221   bgetc(g);
222   DBG("Closing");
223   bclose(g);
224
225   return 0;
226 }
227
228 #endif