Imported Upstream version 0.7.20
[platform/upstream/libsolv.git] / src / strpool.c
index 364f096..b4a09a5 100644 (file)
@@ -56,11 +56,7 @@ stringpool_freehash(Stringpool *ss)
 void
 stringpool_init_empty(Stringpool *ss)
 {
-  const char *emptystrs[] = {
-    "<NULL>",
-    "",
-    0,
-  };
+  static const char *emptystrs[] = { "<NULL>", "", 0 };
   stringpool_init(ss, emptystrs);
 }
 
@@ -76,11 +72,39 @@ stringpool_clone(Stringpool *ss, Stringpool *from)
   ss->sstrings = from->sstrings;
 }
 
+void
+stringpool_resize_hash(Stringpool *ss, int numnew)
+{
+  Hashval h, hh, hashmask;
+  Hashtable hashtbl;
+  int i;
+
+  if (numnew <= 0)
+    return;
+  hashmask = mkmask(ss->nstrings + numnew);
+  if (hashmask <= ss->stringhashmask)
+    return;    /* same as before */
+
+  /* realloc hash table */
+  ss->stringhashmask = hashmask;
+  solv_free(ss->stringhashtbl);
+  ss->stringhashtbl = hashtbl = (Hashtable)solv_calloc(hashmask + 1, sizeof(Id));
+  
+  /* rehash all strings into new hashtable */
+  for (i = 1; i < ss->nstrings; i++)
+    {
+      h = strhash(ss->stringspace + ss->strings[i]) & hashmask;
+      hh = HASHCHAIN_START;
+      while (hashtbl[h] != 0)
+       h = HASHCHAIN_NEXT(h, hh, hashmask);
+      hashtbl[h] = i;
+    }
+}
+
 Id
 stringpool_strn2id(Stringpool *ss, const char *str, unsigned int len, int create)
 {
   Hashval h, hh, hashmask, oldhashmask;
-  int i;
   Id id;
   Hashtable hashtbl;
 
@@ -90,27 +114,13 @@ stringpool_strn2id(Stringpool *ss, const char *str, unsigned int len, int create
     return STRID_EMPTY;
 
   hashmask = oldhashmask = ss->stringhashmask;
-  hashtbl = ss->stringhashtbl;
-
   /* expand hashtable if needed */
-  if (ss->nstrings * 2 > hashmask)
+  if ((Hashval)ss->nstrings * 2 > hashmask)
     {
-      solv_free(hashtbl);
-
-      /* realloc hash table */
-      ss->stringhashmask = hashmask = mkmask(ss->nstrings + STRING_BLOCK);
-      ss->stringhashtbl = hashtbl = (Hashtable)solv_calloc(hashmask + 1, sizeof(Id));
-
-      /* rehash all strings into new hashtable */
-      for (i = 1; i < ss->nstrings; i++)
-       {
-         h = strhash(ss->stringspace + ss->strings[i]) & hashmask;
-         hh = HASHCHAIN_START;
-         while (hashtbl[h] != 0)
-           h = HASHCHAIN_NEXT(h, hh, hashmask);
-         hashtbl[h] = i;
-       }
+      stringpool_resize_hash(ss, STRING_BLOCK);
+      hashmask = ss->stringhashmask;
     }
+  hashtbl = ss->stringhashtbl;
 
   /* compute hash and check for match */
   h = strnhash(str, len) & hashmask;
@@ -125,7 +135,7 @@ stringpool_strn2id(Stringpool *ss, const char *str, unsigned int len, int create
   if (id || !create)    /* exit here if string found */
     return id;
 
-  /* this should be a test for a flag that tells us if the 
+  /* this should be a test for a flag that tells us if the
    * correct blocking is used, but adding a flag would break
    * the ABI. So we use the existance of the hash area as
    * indication instead */