]> mj.ucw.cz Git - libucw.git/blobdiff - lib/partmap.c
First bits of installation of .pc files.
[libucw.git] / lib / partmap.c
index 756fdb635aeb826e1b40121af6efe27bfb6fd37a..2b70f1d916f6e16e0235687f0f51007aac44f849 100644 (file)
@@ -1,8 +1,8 @@
 /*
- *     Sherlock Library -- Mapping of File Parts
+ *     UCW Library -- Mapping of File Parts
  *
- *     (c) 2003 Martin Mares <mj@ucw.cz>
- *     (c) 2003 Robert Spalek <robert@ucw.cz>
+ *     (c) 2003--2006 Martin Mares <mj@ucw.cz>
+ *     (c) 2003--2005 Robert Spalek <robert@ucw.cz>
  *
  *     This software may be freely distributed and used according to the terms
  *     of the GNU Lesser General Public License.
@@ -10,6 +10,7 @@
 
 #include "lib/lib.h"
 #include "lib/lfs.h"
+#include "lib/partmap.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
 #include <sys/mman.h>
 
-struct partmap {
-  int fd;
-  sh_off_t file_size;
-  sh_off_t start_off, end_off;
-  byte *start_map;
-  int writeable;
-};
-
+#ifdef CONFIG_PARTMAP_IS_MMAP
+#define PARTMAP_WINDOW ~(size_t)0
+#else
 #ifdef TEST
 #define PARTMAP_WINDOW 4096
 #else
 #define PARTMAP_WINDOW 16777216
 #endif
-
-static uns page_size;
+#endif
 
 struct partmap *
-partmap_open(byte *name, int writeable)
+partmap_open(char *name, int writeable)
 {
-  if (!page_size)
-    page_size = getpagesize();
-
   struct partmap *p = xmalloc_zero(sizeof(struct partmap));
 
   p->fd = sh_open(name, writeable ? O_RDWR : O_RDONLY);
@@ -48,6 +40,9 @@ partmap_open(byte *name, int writeable)
   if ((p->file_size = sh_seek(p->fd, 0, SEEK_END)) < 0)
     die("lseek(%s): %m", name);
   p->writeable = writeable;
+#ifdef CONFIG_PARTMAP_IS_MMAP
+  partmap_load(p, 0, p->file_size);
+#endif
   return p;
 }
 
@@ -66,28 +61,24 @@ partmap_close(struct partmap *p)
   xfree(p);
 }
 
-void *
-partmap_map(struct partmap *p, sh_off_t start, uns size)
+void
+partmap_load(struct partmap *p, sh_off_t start, uns size)
 {
-  if (!p->start_map || start < p->start_off || start+size > p->end_off)
-    {
-      if (p->start_map)
-       munmap(p->start_map, p->end_off - p->start_off);
-      uns win = PARTMAP_WINDOW;
-      ASSERT(win >= size);
-      sh_off_t end = start + size;
-      start = start/page_size * page_size;
-      if (start+win > p->file_size)
-       win = p->file_size - start;
-      if (start+win < end)
-       die("cannot mmap, the window is too small");
-      p->start_map = sh_mmap(NULL, win, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, start);
-      if (p->start_map == MAP_FAILED)
-       die("mmap failed at position %Ld: %m", (long long)start);
-      p->start_off = start;
-      p->end_off = start+win;
-    }
-  return p->start_map + (start - p->start_off);
+  if (p->start_map)
+    munmap(p->start_map, p->end_off - p->start_off);
+  sh_off_t end = start + size;
+  sh_off_t win_start = start/CPU_PAGE_SIZE * CPU_PAGE_SIZE;
+  size_t win_len = PARTMAP_WINDOW;
+  if ((sh_off_t) (win_start+win_len) > p->file_size)
+    win_len = ALIGN_TO(p->file_size - win_start, CPU_PAGE_SIZE);
+  if ((sh_off_t) (win_start+win_len) < end)
+    die("partmap_map: Window is too small for mapping %d bytes", size);
+  p->start_map = sh_mmap(NULL, win_len, p->writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, p->fd, win_start);
+  if (p->start_map == MAP_FAILED)
+    die("mmap failed at position %lld: %m", (long long)win_start);
+  p->start_off = win_start;
+  p->end_off = win_start+win_len;
+  madvise(p->start_map, win_len, MADV_SEQUENTIAL);
 }
 
 #ifdef TEST