]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-mmap.c
fastbufs: byte * -> char *
[libucw.git] / lib / fb-mmap.c
index 6e0105f22005773be8cc88ae6fbed1f77b5ea285..3a82a92accec38459d934f85538d227daf98b691 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *     Sherlock Library -- Fast Buffered I/O on Memory-Mapped Files
+ *     UCW Library -- Fast Buffered I/O on Memory-Mapped Files
  *
  *     (c) 2002 Martin Mares <mj@ucw.cz>
  *
@@ -7,29 +7,31 @@
  *     of the GNU Lesser General Public License.
  */
 
-/*
- *  FIXME:
- *  - O_WRONLY ? (& generally processing of mode bits)
- */
-
-#ifdef TEST
-#define MMAP_WINDOW_SIZE 16*PAGE_SIZE
-#define MMAP_EXTEND_SIZE 4*PAGE_SIZE
-#else
-#define MMAP_WINDOW_SIZE 256*PAGE_SIZE
-#define MMAP_EXTEND_SIZE 256*PAGE_SIZE
-#endif
-
 #include "lib/lib.h"
 #include "lib/fastbuf.h"
 #include "lib/lfs.h"
+#include "lib/conf.h"
 
-#include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <sys/mman.h>
-#include <sys/user.h>
+
+static uns mmap_window_size = 16*CPU_PAGE_SIZE;
+static uns mmap_extend_size = 4*CPU_PAGE_SIZE;
+
+static struct cf_section fbmm_config = {
+  CF_ITEMS {
+    CF_UNS("WindowSize", &mmap_window_size),
+    CF_UNS("ExtendSize", &mmap_extend_size),
+    CF_END
+  }
+};
+
+static void CONSTRUCTOR fbmm_init_config(void)
+{
+  cf_declare_section("FBMMap", &fbmm_config, 0);
+}
 
 struct fb_mmap {
   struct fastbuf fb;
@@ -38,7 +40,7 @@ struct fb_mmap {
   sh_off_t file_size;
   sh_off_t file_extend;
   sh_off_t window_pos;
-  int is_writeable;
+  int mode;
 };
 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
 
@@ -46,14 +48,14 @@ static void
 bfmm_map_window(struct fastbuf *f)
 {
   struct fb_mmap *F = FB_MMAP(f);
-  sh_off_t pos0 = f->pos & ~(sh_off_t)(PAGE_SIZE-1);
-  int l = MIN((sh_off_t)MMAP_WINDOW_SIZE, F->file_extend - pos0);
-  uns ll = ALIGN(l, PAGE_SIZE);
-  uns oll = ALIGN(f->bufend - f->buffer, PAGE_SIZE);
-  int prot = F->is_writeable ? (PROT_READ | PROT_WRITE) : PROT_READ;
+  sh_off_t pos0 = f->pos & ~(sh_off_t)(CPU_PAGE_SIZE-1);
+  int l = MIN((sh_off_t)mmap_window_size, F->file_extend - pos0);
+  uns ll = ALIGN_TO(l, CPU_PAGE_SIZE);
+  uns oll = ALIGN_TO(f->bufend - f->buffer, CPU_PAGE_SIZE);
+  int prot = ((F->mode & O_ACCMODE) == O_RDONLY) ? PROT_READ : (PROT_READ | PROT_WRITE);
 
   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);
-  if (ll != oll)
+  if (ll != oll && f->buffer)
     {
       munmap(f->buffer, oll);
       f->buffer = NULL;
@@ -65,12 +67,12 @@ bfmm_map_window(struct fastbuf *f)
   if (f->buffer == (byte *) MAP_FAILED)
     die("mmap(%s): %m", f->name);
 #ifdef MADV_SEQUENTIAL
-  if (ll > PAGE_SIZE)
+  if (ll > CPU_PAGE_SIZE)
     madvise(f->buffer, ll, MADV_SEQUENTIAL);
 #endif
   f->bufend = f->buffer + l;
   f->bptr = f->buffer + (f->pos - pos0);
-  F->window_pos = f->pos;
+  F->window_pos = pos0;
 }
 
 static int
@@ -106,7 +108,7 @@ bfmm_spout(struct fastbuf *f)
   f->pos = end;
   if (f->pos >= F->file_extend)
     {
-      F->file_extend = ALIGN(F->file_extend + MMAP_EXTEND_SIZE, (sh_off_t)PAGE_SIZE);
+      F->file_extend = ALIGN_TO(F->file_extend + mmap_extend_size, (sh_off_t)CPU_PAGE_SIZE);
       if (sh_ftruncate(F->fd, F->file_extend))
        die("ftruncate(%s): %m", f->name);
     }
@@ -115,7 +117,7 @@ bfmm_spout(struct fastbuf *f)
   DBG(" -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
 }
 
-static void
+static int
 bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
 {
   if (whence == SEEK_END)
@@ -126,6 +128,7 @@ bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
   f->pos = pos;
   f->bptr = f->bstop = f->bufend;      /* force refill/spout call */
   DBG("Seek -> %p %p %p(%x) %p", f->buffer, f->bptr, f->bstop, (int)f->pos, f->bufend);
+  return 1;
 }
 
 static void
@@ -134,7 +137,7 @@ bfmm_close(struct fastbuf *f)
   struct fb_mmap *F = FB_MMAP(f);
 
   if (f->buffer)
-    munmap(f->buffer, ALIGN(f->bufend-f->buffer, PAGE_SIZE));
+    munmap(f->buffer, ALIGN_TO(f->bufend-f->buffer, CPU_PAGE_SIZE));
   if (F->file_extend > F->file_size &&
       sh_ftruncate(F->fd, F->file_size))
     die("ftruncate(%s): %m", f->name);
@@ -142,7 +145,7 @@ bfmm_close(struct fastbuf *f)
     {
     case 1:
       if (unlink(f->name) < 0)
-       log(L_ERROR, "unlink(%s): %m", f->name);
+       msg(L_ERROR, "unlink(%s): %m", f->name);
     case 0:
       close(F->fd);
     }
@@ -163,7 +166,7 @@ bfmm_config(struct fastbuf *f, uns item, int value)
 }
 
 static struct fastbuf *
-bfmmopen_internal(int fd, byte *name, uns mode)
+bfmmopen_internal(int fd, const char *name, uns mode)
 {
   int namelen = strlen(name) + 1;
   struct fb_mmap *F = xmalloc(sizeof(struct fb_mmap) + namelen);
@@ -176,9 +179,7 @@ bfmmopen_internal(int fd, byte *name, uns mode)
   F->file_extend = F->file_size = sh_seek(fd, 0, SEEK_END);
   if (mode & O_APPEND)
     f->pos = F->file_size;
-  mode &= ~O_APPEND;
-  if (mode & O_WRONLY || mode & O_RDWR)
-    F->is_writeable = 1;
+  F->mode = mode;
 
   f->refill = bfmm_refill;
   f->spout = bfmm_spout;
@@ -189,9 +190,13 @@ bfmmopen_internal(int fd, byte *name, uns mode)
 }
 
 struct fastbuf *
-bopen_mm(byte *name, uns mode)
+bopen_mm(const char *name, uns mode)
 {
-  int fd = sh_open(name, mode, 0666);
+  int fd;
+
+  if ((mode & O_ACCMODE) == O_WRONLY)
+    mode = (mode & ~O_ACCMODE) | O_RDWR;
+  fd = sh_open(name, mode, 0666);
   if (fd < 0)
     die("Unable to %s file %s: %m",
        (mode & O_CREAT) ? "create" : "open", name);