]> mj.ucw.cz Git - libucw.git/blobdiff - lib/fb-mmap.c
lib: added {big,page}_alloc_zero routines
[libucw.git] / lib / fb-mmap.c
index 462fdaea80bcbdfee254c516fd9d39782447ba1d..1e610c9c9d18b99bf2eba577f850d765d7fc8a24 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>
  *
 #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*PAGE_SIZE;
-static uns mmap_extend_size = 4*PAGE_SIZE;
+static uns mmap_window_size = 16*CPU_PAGE_SIZE;
+static uns mmap_extend_size = 4*CPU_PAGE_SIZE;
 
-static struct cfitem obuck_config[] = {
-  { "FBMMap",          CT_SECTION,     NULL },
-  { "WindowSize",      CT_INT,         &mmap_window_size },
-  { "ExtendSize",      CT_INT,         &mmap_extend_size },
-  { NULL,              CT_STOP,        NULL }
+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 obuck_init_config(void)
+static void CONSTRUCTOR fbmm_init_config(void)
 {
-  cf_register(obuck_config);
+  cf_declare_section("FBMMap", &fbmm_config, 0);
 }
 
 struct fb_mmap {
@@ -41,6 +40,7 @@ struct fb_mmap {
   sh_off_t file_size;
   sh_off_t file_extend;
   sh_off_t window_pos;
+  uns window_size;
   int mode;
 };
 #define FB_MMAP(f) ((struct fb_mmap *)(f)->is_fastbuf)
@@ -49,18 +49,18 @@ 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);
+  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(l, PAGE_SIZE);
-  uns oll = ALIGN(f->bufend - f->buffer, PAGE_SIZE);
+  uns ll = ALIGN_TO(l, 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 && f->buffer)
+  if (ll != F->window_size && f->buffer)
     {
-      munmap(f->buffer, oll);
+      munmap(f->buffer, F->window_size);
       f->buffer = NULL;
     }
+  F->window_size = ll;
   if (!f->buffer)
     f->buffer = sh_mmap(NULL, ll, prot, MAP_SHARED, F->fd, pos0);
   else
@@ -68,7 +68,7 @@ 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;
@@ -109,7 +109,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);
     }
@@ -118,7 +118,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)
@@ -127,8 +127,9 @@ bfmm_seek(struct fastbuf *f, sh_off_t pos, int whence)
     ASSERT(whence == SEEK_SET);
   ASSERT(pos >= 0 && pos <= FB_MMAP(f)->file_size);
   f->pos = pos;
-  f->bptr = f->bstop = f->bufend;      /* force refill/spout call */
+  f->bptr = f->bstop = f->bufend = f->buffer;  /* 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
@@ -137,7 +138,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, F->window_size);
   if (F->file_extend > F->file_size &&
       sh_ftruncate(F->fd, F->file_size))
     die("ftruncate(%s): %m", f->name);
@@ -165,7 +166,7 @@ bfmm_config(struct fastbuf *f, uns item, int value)
     }
 }
 
-static struct fastbuf *
+struct fastbuf *
 bfmmopen_internal(int fd, byte *name, uns mode)
 {
   int namelen = strlen(name) + 1;
@@ -189,20 +190,6 @@ bfmmopen_internal(int fd, byte *name, uns mode)
   return f;
 }
 
-struct fastbuf *
-bopen_mm(byte *name, uns mode)
-{
-  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);
-  return bfmmopen_internal(fd, name, mode);
-}
-
 #ifdef TEST
 
 int main(int argc, char **argv)