Change the hash table code so it will be easier to add the second table.
Showing the changes to scripts/basic (1 of 1 file). Show all
Unified · Split
| @@ -113,7 +113,7 @@ struct item { · 112 unmodified lines | ||
| 113 | 113 | }; |
| 114 | 114 | |
| 115 | 115 | #define HASHSZ 256 |
| 116 | −static struct item *hashtab[HASHSZ]; | |
| 116 | +static struct item *config_hashtab[HASHSZ]; | |
| 117 | 117 | |
| 118 | 118 | static 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 | 125 | return hash; |
| 126 | 126 | } |
| 127 | 127 | |
| 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 | − | |
| 143 | 128 | /* |
| 144 | 129 | * Add a new value to the configuration string. |
| 145 | 130 | */ |
| 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[]) | |
| 147 | 133 | { |
| 148 | 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 | 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 | + */ | |
| 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 | + | |
| 161 | 167 | /* |
| 162 | 168 | * Record the use of a CONFIG_* word. |
| 163 | 169 | */ |
| 164 | 170 | static void use_config(const char *m, int slen) |
| 165 | 171 | { |
| 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; | |
| 170 | 174 | |
| 171 | − define_config(m, slen, hash); | |
| 172 | 175 | /* Print out a dependency path from a symbol name. */ |
| 173 | 176 | printf(" $(wildcard include/config/%.*s) \\\n", slen, m); |
| 174 | 177 | } |