History

fixdep: refactor hash table lookup

Change the hash table code so it will be easier to add the second table.

Masahiro Yamada committed 3 years ago

Commit 871d6571 parent 21852421 file · +25 −22Browse files

Showing the changes to scripts/basic (1 of 1 file). Show all

Unified · Split

scripts/basic/fixdep.c+25−22
@@ -113,7 +113,7 @@ struct item { · 112 unmodified lines
113113 };
114114
115115 #define HASHSZ 256
116−static struct item *hashtab[HASHSZ];
116+static struct item *config_hashtab[HASHSZ];
117117
118118 static unsigned int strhash(const char *str, unsigned int sz)
119119 {
@@ -125,25 +125,11 @@ static unsigned int strhash(const char *str, unsigned int sz) · 5 unmodified lines
125125 return hash;
126126 }
127127
128−/*
129− * Lookup a value in the configuration string.
130− */
131−static int is_defined_config(const char *name, int len, unsigned int hash)
132−{
133− struct item *aux;
134−
135− for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
136− if (aux->hash == hash && aux->len == len &&
137− memcmp(aux->name, name, len) == 0)
138− return 1;
139− }
140− return 0;
141−}
142−
143128 /*
144129 * Add a new value to the configuration string.
145130 */
146−static void define_config(const char *name, int len, unsigned int hash)
131+static void add_to_hashtable(const char *name, int len, unsigned int hash,
132+ struct item *hashtab[])
147133 {
148134 struct item *aux = malloc(sizeof(*aux) + len);
149135
@@ -158,17 +144,34 @@ static void define_config(const char *name, int len, unsigned int hash) · 8 unmodified lines
158144 hashtab[hash % HASHSZ] = aux;
159145 }
160146
147+/*
148+ * Lookup a string in the hash table. If found, just return true.
149+ * If not, add it to the hashtable and return false.
150+ */
151+static bool in_hashtable(const char *name, int len, struct item *hashtab[])
152+{
153+ struct item *aux;
154+ unsigned int hash = strhash(name, len);
155+
156+ for (aux = hashtab[hash % HASHSZ]; aux; aux = aux->next) {
157+ if (aux->hash == hash && aux->len == len &&
158+ memcmp(aux->name, name, len) == 0)
159+ return true;
160+ }
161+
162+ add_to_hashtable(name, len, hash, hashtab);
163+
164+ return false;
165+}
166+
161167 /*
162168 * Record the use of a CONFIG_* word.
163169 */
164170 static void use_config(const char *m, int slen)
165171 {
166− unsigned int hash = strhash(m, slen);
167−
168− if (is_defined_config(m, slen, hash))
169− return;
172+ if (in_hashtable(m, slen, config_hashtab))
173+ return;
170174
171− define_config(m, slen, hash);
172175 /* Print out a dependency path from a symbol name. */
173176 printf(" $(wildcard include/config/%.*s) \\\n", slen, m);
174177 }