]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/lib/htable.c
Fix compiler warning noted in bug #2309
[bacula/bacula] / bacula / src / lib / htable.c
index c08527e7b9be799cbe6fc8a9f5e2cde11cd6227b..8f4624a6729f2bef03d45f31dc6cad37471641a1 100644 (file)
@@ -1,29 +1,20 @@
 /*
-   Bacula® - The Network Backup Solution
-
-   Copyright (C) 2003-2008 Free Software Foundation Europe e.V.
-
-   The main author of Bacula is Kern Sibbald, with contributions from
-   many others, a complete list can be found in the file AUTHORS.
-   This program is Free Software; you can redistribute it and/or
-   modify it under the terms of version two of the GNU General Public
-   License as published by the Free Software Foundation and included
-   in the file LICENSE.
-
-   This program is distributed in the hope that it will be useful, but
-   WITHOUT ANY WARRANTY; without even the implied warranty of
-   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-   General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-   02110-1301, USA.
-
-   Bacula® is a registered trademark of Kern Sibbald.
-   The licensor of Bacula is the Free Software Foundation Europe
-   (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
-   Switzerland, email:ftf@fsfeurope.org.
+   Bacula(R) - The Network Backup Solution
+
+   Copyright (C) 2000-2017 Kern Sibbald
+
+   The original author of Bacula is Kern Sibbald, with contributions
+   from many others, a complete list can be found in the file AUTHORS.
+
+   You may use this file and others of this release according to the
+   license defined in the LICENSE file, which includes the Affero General
+   Public License, v3.0 ("AGPLv3") and some additional permissions and
+   terms pursuant to its AGPLv3 Section 7.
+
+   This notice must be preserved when any source code is 
+   conveyed and/or propagated.
+
+   Bacula(R) is a registered trademark of Kern Sibbald.
 */
 /*
  *  Bacula hash table routines
  *
  *   Kern Sibbald, July MMIII
  *
- *   Version $Id$
- *
  */
 
 #include "bacula.h"
-
 #include "htable.h"
 
+#define lli long long int
+static const int dbglvl = 500;
+
 /* ===================================================================
  *    htable
  */
@@ -132,14 +123,21 @@ void htable::hash_index(char *key)
 {
    hash = 0;
    for (char *p=key; *p; p++) {
-//    hash += (hash << 3) + (uint32_t)*p;
       hash +=  ((hash << 5) | (hash >> (sizeof(hash)*8-5))) + (uint32_t)*p;
    }
    /* Multiply by large prime number, take top bits, mask for remainder */
-   index = ((hash * 1103515249) >> rshift) & mask;
-   Dmsg2(500, "Leave hash_index hash=0x%x index=%d\n", hash, index);
-}
-
+   index = ((hash * 1103515249LL) >> rshift) & mask;
+   Dmsg2(dbglvl, "Leave hash_index hash=0x%x index=%d\n", hash, index);
+} 
+void htable::hash_index(uint64_t ikey)
+{ 
+   hash = ikey;           /* already have starting binary hash */
+   /* Same algorithm as for char * */
+   index = ((hash * 1103515249LL) >> rshift) & mask;
+   Dmsg2(dbglvl, "Leave hash_index hash=0x%x index=%d\n", hash, index);
+} 
 /*
  * tsize is the estimated number of entries in the hash table
  */
@@ -152,7 +150,7 @@ void htable::init(void *item, void *link, int tsize)
 {
    int pwr;
 
-   memset(this, 0, sizeof(htable));
+   bmemzero(this, sizeof(htable));
    if (tsize < 31) {
       tsize = 31;
    }
@@ -166,10 +164,10 @@ void htable::init(void *item, void *link, int tsize)
    buckets = 1<<pwr;                  /* hash table size -- power of two */
    max_items = buckets * 4;           /* allow average 4 entries per chain */
    table = (hlink **)malloc(buckets * sizeof(hlink *));
-   memset(table, 0, buckets * sizeof(hlink *));
+   bmemzero(table, buckets * sizeof(hlink *));
 #ifdef BIG_MALLOC
    malloc_big_buf(1000000);   /* ***FIXME*** need variable or some estimate */
-#endif
+#endif /* BIG_MALLOC */
 }
 
 uint32_t htable::size()
@@ -217,7 +215,7 @@ void htable::stats()
    printf("buckets=%d num_items=%d max_items=%d\n", buckets, num_items, max_items);
    printf("max hits in a bucket = %d\n", max);
 #ifdef BIG_MALLOC
-   printf("total bytes malloced = %d\n", total_size);
+   printf("total bytes malloced = %lld\n", (lli)total_size);
    printf("total blocks malloced = %d\n", blocks);
 #endif
 }
@@ -236,7 +234,7 @@ void htable::grow_table()
    big->max_items = big->buckets * 4;
    /* Create a bigger hash table */
    big->table = (hlink **)malloc(big->buckets * sizeof(hlink *));
-   memset(big->table, 0, big->buckets * sizeof(hlink *));
+   bmemzero(big->table, big->buckets * sizeof(hlink *));
    big->walkptr = NULL;
    big->walk_index = 0;
    /* Insert all the items in the new hash table */
@@ -250,8 +248,14 @@ void htable::grow_table()
     */
    for (void *item=first(); item; ) {
       void *ni = ((hlink *)((char *)item+loffset))->next;  /* save link overwritten by insert */
-      Dmsg1(100, "Grow insert: %s\n", ((hlink *)((char *)item+loffset))->key);
-      big->insert(((hlink *)((char *)item+loffset))->key, item);
+      hlink *hp = (hlink *)((char *)item+loffset);
+      if (hp->is_ikey) {
+         Dmsg1(100, "Grow insert: %lld\n", hp->key.ikey);
+         big->insert(hp->key.ikey, item);
+      } else {
+         Dmsg1(100, "Grow insert: %s\n", hp->key.key);
+         big->insert(hp->key.key, item);
+      }  
       if (ni) {
          item = (void *)((char *)ni-loffset);
       } else {
@@ -276,77 +280,119 @@ bool htable::insert(char *key, void *item)
       return false;                   /* already exists */
    }
    ASSERT(index < buckets);
-   Dmsg2(500, "Insert: hash=%p index=%d\n", hash, index);
+   Dmsg2(dbglvl, "Insert: hash=%p index=%d\n", hash, index);
    hp = (hlink *)(((char *)item)+loffset);
-   Dmsg4(500, "Insert hp=%p index=%d item=%p offset=%u\n", hp,
+   Dmsg4(dbglvl, "Insert hp=%p index=%d item=%p offset=%u\n", hp,
       index, item, loffset);
    hp->next = table[index];
    hp->hash = hash;
-   hp->key = key;
+   hp->key.key = key;
+   hp->is_ikey = false;
    table[index] = hp;
-   Dmsg3(500, "Insert hp->next=%p hp->hash=0x%x hp->key=%s\n",
-      hp->next, hp->hash, hp->key);
+   Dmsg3(dbglvl, "Insert hp->next=%p hp->hash=0x%x hp->key=%s\n",
+      hp->next, hp->hash, hp->key.key);
 
    if (++num_items >= max_items) {
-      Dmsg2(500, "num_items=%d max_items=%d\n", num_items, max_items);
+      Dmsg2(dbglvl, "num_items=%d max_items=%d\n", num_items, max_items);
       grow_table();
    }
-   Dmsg3(500, "Leave insert index=%d num_items=%d key=%s\n", index, num_items, key);
+   Dmsg3(dbglvl, "Leave insert index=%d num_items=%d key=%s\n", index, num_items, key);
    return true;
 }
 
 void *htable::lookup(char *key)
-{
-   hash_index(key);
-   for (hlink *hp=table[index]; hp; hp=(hlink *)hp->next) {
-//    Dmsg2(100, "hp=%p key=%s\n", hp, hp->key);
-      if (hash == hp->hash && strcmp(key, hp->key) == 0) {
-         Dmsg1(500, "lookup return %p\n", ((char *)hp)-loffset);
-         return ((char *)hp)-loffset;
-      }
-   }
-   return NULL;
-}
-
+{ 
+   hash_index(key); 
+   for (hlink *hp=table[index]; hp; hp=(hlink *)hp->next) { 
+//    Dmsg2(100, "hp=%p key=%s\n", hp, hp->key.key);
+      if (hash == hp->hash && strcmp(key, hp->key.key) == 0) {
+         Dmsg1(dbglvl, "lookup return %p\n", ((char *)hp)-loffset); 
+         return ((char *)hp)-loffset; 
+      } 
+   } 
+   return NULL; 
+} 
+bool htable::insert(uint64_t ikey, void *item)
+{ 
+   hlink *hp; 
+   if (lookup(ikey)) {
+      return false;                  /* already exists */
+   } 
+   ASSERT(index < buckets); 
+   Dmsg2(dbglvl, "Insert: hash=%p index=%d\n", hash, index); 
+   hp = (hlink *)(((char *)item)+loffset); 
+   Dmsg4(dbglvl, "Insert hp=%p index=%d item=%p offset=%u\n", hp, index, 
+     item, loffset);
+   hp->next = table[index]; 
+   hp->hash = hash; 
+   hp->key.ikey = ikey;
+   hp->is_ikey = true;
+   table[index] = hp; 
+   Dmsg3(dbglvl, "Insert hp->next=%p hp->hash=0x%x hp->ikey=%lld\n", hp->next, 
+      hp->hash, hp->key.ikey);
+   if (++num_items >= max_items) { 
+      Dmsg2(dbglvl, "num_items=%d max_items=%d\n", num_items, max_items); 
+      grow_table(); 
+   } 
+   Dmsg3(dbglvl, "Leave insert index=%d num_items=%d key=%lld\n",
+      index, num_items, ikey);
+   return true;  
+} 
+void *htable::lookup(uint64_t ikey)
+{ 
+   hash_index(ikey);
+   for (hlink *hp=table[index]; hp; hp=(hlink *)hp->next) { 
+//    Dmsg2(100, "hp=%p key=%lld\n", hp, hp->key.ikey);
+      if (hash == hp->hash && ikey == hp->key.ikey) {
+         Dmsg1(dbglvl, "lookup return %p\n", ((char *)hp)-loffset); 
+         return ((char *)hp)-loffset; 
+      } 
+   } 
+   return NULL; 
+} 
 void *htable::next()
 {
-   Dmsg1(500, "Enter next: walkptr=%p\n", walkptr);
+   Dmsg1(dbglvl, "Enter next: walkptr=%p\n", walkptr);
    if (walkptr) {
       walkptr = (hlink *)(walkptr->next);
    }
    while (!walkptr && walk_index < buckets) {
       walkptr = table[walk_index++];
       if (walkptr) {
-         Dmsg3(500, "new walkptr=%p next=%p inx=%d\n", walkptr,
+         Dmsg3(dbglvl, "new walkptr=%p next=%p inx=%d\n", walkptr,
             walkptr->next, walk_index-1);
       }
    }
    if (walkptr) {
-      Dmsg2(500, "next: rtn %p walk_index=%d\n",
+      Dmsg2(dbglvl, "next: rtn %p walk_index=%d\n",
          ((char *)walkptr)-loffset, walk_index);
       return ((char *)walkptr)-loffset;
    }
-   Dmsg0(500, "next: return NULL\n");
+   Dmsg0(dbglvl, "next: return NULL\n");
    return NULL;
 }
 
 void *htable::first()
 {
-   Dmsg0(500, "Enter first\n");
+   Dmsg0(dbglvl, "Enter first\n");
    walkptr = table[0];                /* get first bucket */
    walk_index = 1;                    /* Point to next index */
    while (!walkptr && walk_index < buckets) {
       walkptr = table[walk_index++];  /* go to next bucket */
       if (walkptr) {
-         Dmsg3(500, "first new walkptr=%p next=%p inx=%d\n", walkptr,
+         Dmsg3(dbglvl, "first new walkptr=%p next=%p inx=%d\n", walkptr,
             walkptr->next, walk_index-1);
       }
    }
    if (walkptr) {
-      Dmsg1(500, "Leave first walkptr=%p\n", walkptr);
+      Dmsg1(dbglvl, "Leave first walkptr=%p\n", walkptr);
       return ((char *)walkptr)-loffset;
    }
-   Dmsg0(500, "Leave first walkptr=NULL\n");
+   Dmsg0(dbglvl, "Leave first walkptr=NULL\n");
    return NULL;
 }
 
@@ -391,7 +437,7 @@ int main()
    int count = 0;
 
    jcrtbl = (htable *)malloc(sizeof(htable));
-   jcrtbl->init(jcr, &jcr->link, NITEMS);
+   jcrtbl->init(jcr, &jcr->link, NITEMS); 
 
    Dmsg1(000, "Inserting %d items\n", NITEMS);
    for (int i=0; i<NITEMS; i++) {
@@ -430,7 +476,7 @@ int main()
    free(jcrtbl);
    printf("Freed jcrtbl\n");
 
-   sm_dump(false);
+   sm_dump(false);   /* unit test */
 
 }
 #endif