]> mj.ucw.cz Git - libucw.git/blob - lib/fb-mmap.c
Added bit_array_assign(), replaced BIT_ARRAY_ALLOC by functions.
[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 #include <sys/user.h>
20
21 static uns mmap_window_size = 16*PAGE_SIZE;
22 static uns mmap_extend_size = 4*PAGE_SIZE;
23
24 static struct cfitem fbmm_config[] = {
25   { "FBMMap",           CT_SECTION,     NULL },
26   { "WindowSize",       CT_INT,         &mmap_window_size },
27   { "ExtendSize",       CT_INT,         &mmap_extend_size },
28   { NULL,               CT_STOP,        NULL }
29 };
30
31 static void CONSTRUCTOR fbmm_init_config(void)
32 {
33   cf_register(fbmm_config);
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   int mode;
44 };
45 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
46
47 static void
48 bfmm_map_window(struct fastbuf *f)
49 {
50   struct fb_mmap *F = FB_MMAP(f);
51   sh_off_t pos0 = f->pos & ~(sh_off_t)(PAGE_SIZE-1);
52   int l = MIN((sh_off_t)mmap_window_size, F->file_extend - pos0);
53   uns ll = ALIGN(l, PAGE_SIZE);
54   uns oll = ALIGN(f->bufend - f->buffer, 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 != oll && f->buffer)
59     {
60       munmap(f->buffer, oll);
61       f->buffer = NULL;
62     }
63   if (!f->buffer)
64     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
65   else
66     f->buffer = sh_mmap(f->buffer, ll, prot, MAP_SHARED | MAP_FIXED, F->fd, pos0);
67   if (f->buffer == (byte *) MAP_FAILED)
68     die("mmap(%s): %m", f->name);
69 #ifdef MADV_SEQUENTIAL
70   if (ll > PAGE_SIZE)
71     madvise(f->buffer, ll, MADV_SEQUENTIAL);
72 #endif
73   f->bufend = f->buffer + l;
74   f->bptr = f->buffer + (f->pos - pos0);
75   F->window_pos = pos0;
76 }
77
78 static int
79 bfmm_refill(struct fastbuf *f)
80 {
81   struct fb_mmap *F = FB_MMAP(f);
82
83   DBG("Refill <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
84   if (f->pos >= F->file_size)
85     return 0;
86   if (f->bstop >= f->bufend)
87     bfmm_map_window(f);
88   if (F->window_pos + (f->bufend - f->buffer) > F->file_size)
89     f->bstop = f->buffer + (F->file_size - F->window_pos);
90   else
91     f->bstop = f->bufend;
92   f->pos = F->window_pos + (f->bstop - f->buffer);
93   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
94   return 1;
95 }
96
97 static void
98 bfmm_spout(struct fastbuf *f)
99 {
100   struct fb_mmap *F = FB_MMAP(f);
101   sh_off_t end = f->pos + (f->bptr - f->bstop);
102
103   DBG("Spout <- %p %p %p %p", f->buffer, f->bptr, f->bstop, f->bufend);
104   if (end > F->file_size)
105     F->file_size = end;
106   if (f->bptr < f->bufend)
107     return;
108   f->pos = end;
109   if (f->pos >= F->file_extend)
110     {
111       F->file_extend = ALIGN(F->file_extend + mmap_extend_size, (sh_off_t)PAGE_SIZE);
112       if (sh_ftruncate(F->fd, F->file_extend))
113         die("ftruncate(%s): %m", f->name);
114     }
115   bfmm_map_window(f);
116   f->bstop = f->bptr;
117   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
118 }
119
120 static void
121 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
122 {
123   if (whence == SEEK_END)
124     pos += FB_MMAP(f)->file_size;
125   else
126     ASSERT(whence == SEEK_SET);
127   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
128   f->pos = pos;
129   f->bptr = f->bstop = f->bufend;       /* force refill/spout call */
130   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
131 }
132
133 static void
134 bfmm_close(struct fastbuf *f)
135 {
136   struct fb_mmap *F = FB_MMAP(f);
137
138   if (f->buffer)
139     munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
140   if (F->file_extend > F->file_size &&
141       sh_ftruncate(F->fd, F->file_size))
142     die("ftruncate(%s): %m", f->name);
143   switch (F->is_temp_file)
144     {
145     case 1:
146       if (unlink(f->name) < 0)
147         log(L_ERROR, "unlink(%s): %m", f->name);
148     case 0:
149       close(F->fd);
150     }
151   xfree(f);
152 }
153
154 static int
155 bfmm_config(struct fastbuf *f, uns item, int value)
156 {
157   switch (item)
158     {
159     case BCONFIG_IS_TEMP_FILE:
160       FB_MMAP(f)->is_temp_file = value;
161       return 0;
162     default:
163       return -1;
164     }
165 }
166
167 static struct fastbuf *
168 bfmmopen_internal(int fd, byte *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 (mode & O_APPEND)
180     f->pos = F->file_size;
181   F->mode = mode;
182
183   f->refill = bfmm_refill;
184   f->spout = bfmm_spout;
185   f->seek = bfmm_seek;
186   f->close = bfmm_close;
187   f->config = bfmm_config;
188   return f;
189 }
190
191 struct fastbuf *
192 bopen_mm(byte *name, uns mode)
193 {
194   int fd;
195
196   if ((mode & O_ACCMODE) == O_WRONLY)
197     mode = (mode & ~O_ACCMODE) | O_RDWR;
198   fd = sh_open(name, mode, 0666);
199   if (fd < 0)
200     die("Unable to %s file %s: %m",
201         (mode & O_CREAT) ? "create" : "open", name);
202   return bfmmopen_internal(fd, name, mode);
203 }
204
205 #ifdef TEST
206
207 int main(int argc, char **argv)
208 {
209   struct fastbuf *f = bopen_mm(argv[1], O_RDONLY);
210   struct fastbuf *g = bopen_mm(argv[2], O_RDWR | O_CREAT | O_TRUNC);
211   int c;
212
213   DBG("Copying");
214   while ((c = bgetc(f)) >= 0)
215     bputc(g, c);
216   bclose(f);
217   DBG("Seek inside last block");
218   bsetpos(g, btell(g)-1333);
219   bputc(g, 13);
220   DBG("Seek to the beginning & write");
221   bsetpos(g, 1333);
222   bputc(g, 13);
223   DBG("flush");
224   bflush(g);
225   bputc(g, 13);
226   bflush(g);
227   DBG("Seek nearby & read");
228   bsetpos(g, 133);
229   bgetc(g);
230   DBG("Seek far & read");
231   bsetpos(g, 133333);
232   bgetc(g);
233   DBG("Closing");
234   bclose(g);
235
236   return 0;
237 }
238
239 #endif