fixdep: refactor hash table lookup
[linux-block.git] / scripts / basic / fixdep.c
index f5a51770eb74fb5fcf3243b62b9e402a0b6dbcda..74f90a0deeb946f6fbf4ccb0804d3fcbb3efe1cc 100644 (file)
@@ -113,7 +113,7 @@ struct item {
 };
 
 #define HASHSZ 256
-static struct item *hashtab[HASHSZ];
+static struct item *config_hashtab[HASHSZ];
 
 static unsigned int strhash(const char *str, unsigned int sz)
 {
@@ -125,25 +125,11 @@ static unsigned int strhash(const char *str, unsigned int sz)
        return hash;
 }
 
-/*
- * Lookup a value in the configuration string.
- */
-static int is_defined_config(const char *name, int len, unsigned int hash)
-{
-       struct item *aux;
-
-       for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
-               if (aux->hash == hash && aux->len == len &&
-                   memcmp(aux->name, name, len) == 0)
-                       return 1;
-       }
-       return 0;
-}
-
 /*
  * Add a new value to the configuration string.
  */
-static void define_config(const char *name, int len, unsigned int hash)
+static void add_to_hashtable(const char *name, int len, unsigned int hash,
+                            struct item *hashtab[])
 {
        struct item *aux = malloc(sizeof(*aux) + len);
 
@@ -158,17 +144,34 @@ static void define_config(const char *name, int len, unsigned int hash)
        hashtab[hash % HASHSZ] = aux;
 }
 
+/*
+ * Lookup a string in the hash table. If found, just return true.
+ * If not, add it to the hashtable and return false.
+ */
+static bool in_hashtable(const char *name, int len, struct item *hashtab[])
+{
+       struct item *aux;
+       unsigned int hash = strhash(name, len);
+
+       for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
+               if (aux->hash == hash && aux->len == len &&
+                   memcmp(aux->name, name, len) == 0)
+                       return true;
+       }
+
+       add_to_hashtable(name, len, hash, hashtab);
+
+       return false;
+}
+
 /*
  * Record the use of a CONFIG_* word.
  */
 static void use_config(const char *m, int slen)
 {
-       unsigned int hash = strhash(m, slen);
-
-       if (is_defined_config(m, slen, hash))
-           return;
+       if (in_hashtable(m, slen, config_hashtab))
+               return;
 
-       define_config(m, slen, hash);
        /* Print out a dependency path from a symbol name. */
        printf("    $(wildcard include/config/%.*s) \\\n", slen, m);
 }