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
113};113};
114 114
115#define HASHSZ 256115#define HASHSZ 256
116static struct item *hashtab[HASHSZ];116static struct item *config_hashtab[HASHSZ];
117 117
118static unsigned int strhash(const char *str, unsigned int sz)118static unsigned int strhash(const char *str, unsigned int sz)
119{119{
@@ -125,25 +125,11 @@ static unsigned int strhash(const char *str, unsigned int sz) · 5 unmodified lines
125 return hash;125 return hash;
126}126}
127 127
128/*
129 * Lookup a value in the configuration string.
130 */
131static 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
143/*128/*
144 * Add a new value to the configuration string.129 * Add a new value to the configuration string.
145 */130 */
146static void define_config(const char *name, int len, unsigned int hash)131static void add_to_hashtable(const char *name, int len, unsigned int hash,
132 struct item *hashtab[])
147{133{
148 struct item *aux = malloc(sizeof(*aux) + len);134 struct item *aux = malloc(sizeof(*aux) + len);
149 135
@@ -158,17 +144,34 @@ static void define_config(const char *name, int len, unsigned int hash) · 8 unmodified lines
158 hashtab[hash % HASHSZ] = aux;144 hashtab[hash % HASHSZ] = aux;
159}145}
160 146
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 */
151static 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
161/*167/*
162 * Record the use of a CONFIG_* word.168 * Record the use of a CONFIG_* word.
163 */169 */
164static void use_config(const char *m, int slen)170static void use_config(const char *m, int slen)
165{171{
166 unsigned int hash = strhash(m, slen);172 if (in_hashtable(m, slen, config_hashtab))
167 173 return;
168 if (is_defined_config(m, slen, hash))
169 return;
170 174
171 define_config(m, slen, hash);
172 /* Print out a dependency path from a symbol name. */175 /* Print out a dependency path from a symbol name. */
173 printf(" $(wildcard include/config/%.*s) \\\n", slen, m);176 printf(" $(wildcard include/config/%.*s) \\\n", slen, m);
174}177}