]> mj.ucw.cz Git - libucw.git/blobdiff - lib/pagecache.c
Added functions for manipulating bit arrays. One day, an optimized
[libucw.git] / lib / pagecache.c
index b5636906bfaf1de75d0db9f4412624bb91b6e87b..4482b3ef5bb51119f90aa72ce5a1706fe4245338 100644 (file)
@@ -1,19 +1,22 @@
 /*
  *     Sherlock Library -- File Page Cache
  *
- *     (c) 1999 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
+ *     (c) 1999--2002 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
  */
 
+#include "lib/lib.h"
+#include "lib/pagecache.h"
+#include "lib/lfs.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
 
-#include "lib.h"
-#include "pagecache.h"
-#include "lfs.h"
-
 struct page_cache {
   list free_pages;                     /* LRU queue of free non-dirty pages */
   list locked_pages;                   /* List of locked pages (starts with dirty ones) */
@@ -39,10 +42,9 @@ struct page_cache {
 struct page_cache *
 pgc_open(uns page_size, uns max_pages)
 {
-  struct page_cache *c = xmalloc(sizeof(struct page_cache));
+  struct page_cache *c = xmalloc_zero(sizeof(struct page_cache));
   uns i;
 
-  bzero(c, sizeof(*c));
   init_list(&c->free_pages);
   init_list(&c->locked_pages);
   init_list(&c->dirty_pages);
@@ -65,8 +67,8 @@ pgc_close(struct page_cache *c)
   ASSERT(EMPTY_LIST(c->locked_pages));
   ASSERT(EMPTY_LIST(c->dirty_pages));
   ASSERT(EMPTY_LIST(c->free_pages));
-  free(c->hash_table);
-  free(c);
+  xfree(c->hash_table);
+  xfree(c);
 }
 
 static void
@@ -103,9 +105,9 @@ flush_page(struct page_cache *c, struct page *p)
 
   ASSERT(p->flags & PG_FLAG_DIRTY);
 #ifdef SHERLOCK_HAVE_PREAD
-  s = pwrite(p->fd, p->data, c->page_size, p->pos);
+  s = sh_pwrite(p->fd, p->data, c->page_size, p->pos);
 #else
-  if (c->pos != p->pos || c->pos_fd != p->fd)
+  if (c->pos != p->pos || c->pos_fd != (int) p->fd)
     sh_seek(p->fd, p->pos, SEEK_SET);
   s = write(p->fd, p->data, c->page_size);
   c->pos = p->pos + s;
@@ -114,7 +116,7 @@ flush_page(struct page_cache *c, struct page *p)
   if (s < 0)
     die("pgc_write(%d): %m", p->fd);
   if (s != (int) c->page_size)
-    die("pgc_write(%d): incomplete page (only %d of %d)", s, c->page_size);
+    die("pgc_write(%d): incomplete page (only %d of %d)", p->fd, s, c->page_size);
   p->flags &= ~PG_FLAG_DIRTY;
   c->stat_write++;
 }
@@ -140,7 +142,7 @@ static void
 flush_pages(struct page_cache *c, uns force)
 {
   uns cnt = 0;
-  uns max = force ? ~0 : c->free_count / 2; /* FIXME: Needs tuning */
+  uns max = force ? ~0U : c->free_count / 2; /* FIXME: Needs tuning */
   uns i;
   struct page *p, *q, **req, **rr;
 
@@ -251,7 +253,7 @@ pgc_cleanup(struct page_cache *c)
       rem_node(&p->hn);
       c->free_count--;
       c->total_count--;
-      free(p);
+      xfree(p);
     }
   ASSERT(!c->free_count);
 }
@@ -273,7 +275,6 @@ pgc_read(struct page_cache *c, int fd, sh_off_t pos)
   int s;
 
   ASSERT(!PAGE_OFFSET(pos));
-  ASSERT(!PAGE_NUMBER(fd));
   p = get_and_lock_page(c, pos, fd);
   if (p->flags & PG_FLAG_VALID)
     c->stat_hit++;
@@ -281,9 +282,9 @@ pgc_read(struct page_cache *c, int fd, sh_off_t pos)
     {
       c->stat_miss++;
 #ifdef SHERLOCK_HAVE_PREAD
-      s = pread(fd, p->data, c->page_size, pos);
+      s = sh_pread(fd, p->data, c->page_size, pos);
 #else
-      if (c->pos != pos || c->pos_fd != fd)
+      if (c->pos != pos || c->pos_fd != (int)fd)
        sh_seek(fd, pos, SEEK_SET);
       s = read(fd, p->data, c->page_size);
       c->pos = pos + s;
@@ -292,7 +293,7 @@ pgc_read(struct page_cache *c, int fd, sh_off_t pos)
       if (s < 0)
        die("pgc_read(%d): %m", fd);
       if (s != (int) c->page_size)
-       die("pgc_read(%d): incomplete page (only %d of %d)", s, c->page_size);
+       die("pgc_read(%d): incomplete page (only %d of %d)", p->fd, s, c->page_size);
       p->flags |= PG_FLAG_VALID;
     }
   return p;
@@ -304,7 +305,6 @@ pgc_get(struct page_cache *c, int fd, sh_off_t pos)
   struct page *p;
 
   ASSERT(!PAGE_OFFSET(pos));
-  ASSERT(!PAGE_NUMBER(fd));
   p = get_and_lock_page(c, pos, fd);
   p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;
   return p;
@@ -316,7 +316,6 @@ pgc_get_zero(struct page_cache *c, int fd, sh_off_t pos)
   struct page *p;
 
   ASSERT(!PAGE_OFFSET(pos));
-  ASSERT(!PAGE_NUMBER(fd));
   p = get_and_lock_page(c, pos, fd);
   bzero(p->data, c->page_size);
   p->flags |= PG_FLAG_VALID | PG_FLAG_DIRTY;