]> mj.ucw.cz Git - libucw.git/blobdiff - lib/pagecache.c
Removed old KMP completely
[libucw.git] / lib / pagecache.c
index 9420ab3128a21c229c7eed6a52f19bbe9550e929..13ad36603fc7f69ea64c0aa9e527e74c88d8a7f5 100644 (file)
@@ -1,18 +1,22 @@
 /*
 /*
- *     Sherlock Library -- File Page Cache
+ *     UCW Library -- File Page Cache
  *
  *
- *     (c) 1999--2000 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 <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <fcntl.h>
 #include <unistd.h>
-
-#include "lib/lib.h"
-#include "lib/pagecache.h"
-#include "lib/lfs.h"
+#include <alloca.h>
 
 struct page_cache {
   list free_pages;                     /* LRU queue of free non-dirty pages */
 
 struct page_cache {
   list free_pages;                     /* LRU queue of free non-dirty pages */
@@ -27,7 +31,7 @@ struct page_cache {
   uns stat_miss;                       /* Number of cache misses */
   uns stat_write;                      /* Number of writes */
   list *hash_table;                    /* List heads corresponding to hash buckets */
   uns stat_miss;                       /* Number of cache misses */
   uns stat_write;                      /* Number of writes */
   list *hash_table;                    /* List heads corresponding to hash buckets */
-#ifndef SHERLOCK_HAVE_PREAD
+#ifndef HAVE_PREAD
   sh_off_t pos;                                /* Current position in the file */
   int pos_fd;                          /* FD the position corresponds to */
 #endif
   sh_off_t pos;                                /* Current position in the file */
   int pos_fd;                          /* FD the position corresponds to */
 #endif
@@ -39,10 +43,9 @@ struct page_cache {
 struct page_cache *
 pgc_open(uns page_size, uns max_pages)
 {
 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;
 
   uns i;
 
-  bzero(c, sizeof(*c));
   init_list(&c->free_pages);
   init_list(&c->locked_pages);
   init_list(&c->dirty_pages);
   init_list(&c->free_pages);
   init_list(&c->locked_pages);
   init_list(&c->dirty_pages);
@@ -52,7 +55,7 @@ pgc_open(uns page_size, uns max_pages)
   c->hash_table = xmalloc(sizeof(list) * c->hash_size);
   for(i=0; i<c->hash_size; i++)
     init_list(&c->hash_table[i]);
   c->hash_table = xmalloc(sizeof(list) * c->hash_size);
   for(i=0; i<c->hash_size; i++)
     init_list(&c->hash_table[i]);
-#ifndef SHERLOCK_HAVE_PREAD
+#ifndef HAVE_PREAD
   c->pos_fd = -1;
 #endif
   return c;
   c->pos_fd = -1;
 #endif
   return c;
@@ -65,8 +68,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));
   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
 }
 
 static void
@@ -102,10 +105,10 @@ flush_page(struct page_cache *c, struct page *p)
   int s;
 
   ASSERT(p->flags & PG_FLAG_DIRTY);
   int s;
 
   ASSERT(p->flags & PG_FLAG_DIRTY);
-#ifdef SHERLOCK_HAVE_PREAD
+#ifdef HAVE_PREAD
   s = sh_pwrite(p->fd, p->data, c->page_size, p->pos);
 #else
   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;
     sh_seek(p->fd, p->pos, SEEK_SET);
   s = write(p->fd, p->data, c->page_size);
   c->pos = p->pos + s;
@@ -140,7 +143,7 @@ static void
 flush_pages(struct page_cache *c, uns force)
 {
   uns cnt = 0;
 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;
   uns i;
   struct page *p, *q, **req, **rr;
 
   uns i;
   struct page *p, *q, **req, **rr;
 
@@ -251,7 +254,7 @@ pgc_cleanup(struct page_cache *c)
       rem_node(&p->hn);
       c->free_count--;
       c->total_count--;
       rem_node(&p->hn);
       c->free_count--;
       c->total_count--;
-      free(p);
+      xfree(p);
     }
   ASSERT(!c->free_count);
 }
     }
   ASSERT(!c->free_count);
 }
@@ -279,10 +282,10 @@ pgc_read(struct page_cache *c, int fd, sh_off_t pos)
   else
     {
       c->stat_miss++;
   else
     {
       c->stat_miss++;
-#ifdef SHERLOCK_HAVE_PREAD
+#ifdef HAVE_PREAD
       s = sh_pread(fd, p->data, c->page_size, pos);
 #else
       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;
        sh_seek(fd, pos, SEEK_SET);
       s = read(fd, p->data, c->page_size);
       c->pos = pos + s;
@@ -340,7 +343,7 @@ pgc_put(struct page_cache *c, struct page *p)
   else
     {
       rem_node(&p->hn);
   else
     {
       rem_node(&p->hn);
-      free(p);
+      xfree(p);
       c->total_count--;
     }
 }
       c->total_count--;
     }
 }