]>
Commit | Line | Data |
---|---|---|
96872bc2 LT |
1 | /* |
2 | * name-hash.c | |
3 | * | |
4 | * Hashing names in the index state | |
5 | * | |
6 | * Copyright (C) 2008 Linus Torvalds | |
7 | */ | |
8 | #define NO_THE_INDEX_COMPATIBILITY_MACROS | |
9 | #include "cache.h" | |
10 | ||
2092678c | 11 | struct dir_entry { |
e05881a4 | 12 | struct hashmap_entry ent; |
2092678c | 13 | struct dir_entry *parent; |
2092678c KB |
14 | int nr; |
15 | unsigned int namelen; | |
41284eb0 | 16 | char name[FLEX_ARRAY]; |
2092678c KB |
17 | }; |
18 | ||
7663cdc8 SB |
19 | static int dir_entry_cmp(const void *unused_cmp_data, |
20 | const struct dir_entry *e1, | |
21 | const struct dir_entry *e2, | |
22 | const char *name) | |
e05881a4 | 23 | { |
41284eb0 DT |
24 | return e1->namelen != e2->namelen || strncasecmp(e1->name, |
25 | name ? name : e2->name, e1->namelen); | |
e05881a4 KB |
26 | } |
27 | ||
846df809 JH |
28 | static struct dir_entry *find_dir_entry__hash(struct index_state *istate, |
29 | const char *name, unsigned int namelen, unsigned int hash) | |
2092678c | 30 | { |
e05881a4 | 31 | struct dir_entry key; |
846df809 | 32 | hashmap_entry_init(&key, hash); |
e05881a4 KB |
33 | key.namelen = namelen; |
34 | return hashmap_get(&istate->dir_hash, &key, name); | |
2092678c KB |
35 | } |
36 | ||
846df809 JH |
37 | static struct dir_entry *find_dir_entry(struct index_state *istate, |
38 | const char *name, unsigned int namelen) | |
39 | { | |
40 | return find_dir_entry__hash(istate, name, namelen, memihash(name, namelen)); | |
41 | } | |
42 | ||
2092678c KB |
43 | static struct dir_entry *hash_dir_entry(struct index_state *istate, |
44 | struct cache_entry *ce, int namelen) | |
5102c617 JJ |
45 | { |
46 | /* | |
47 | * Throw each directory component in the hash for quick lookup | |
d28eec26 | 48 | * during a git status. Directory components are stored without their |
5102c617 | 49 | * closing slash. Despite submodules being a directory, they never |
d28eec26 | 50 | * reach this point, because they are stored |
2092678c | 51 | * in index_state.name_hash (as ordinary cache_entries). |
5102c617 | 52 | */ |
2092678c KB |
53 | struct dir_entry *dir; |
54 | ||
55 | /* get length of parent directory */ | |
56 | while (namelen > 0 && !is_dir_sep(ce->name[namelen - 1])) | |
57 | namelen--; | |
58 | if (namelen <= 0) | |
59 | return NULL; | |
d28eec26 | 60 | namelen--; |
2092678c KB |
61 | |
62 | /* lookup existing entry for that directory */ | |
63 | dir = find_dir_entry(istate, ce->name, namelen); | |
64 | if (!dir) { | |
65 | /* not found, create it and add to hash table */ | |
96ffc06f | 66 | FLEX_ALLOC_MEM(dir, name, ce->name, namelen); |
e05881a4 | 67 | hashmap_entry_init(dir, memihash(ce->name, namelen)); |
2092678c | 68 | dir->namelen = namelen; |
e05881a4 | 69 | hashmap_add(&istate->dir_hash, dir); |
2092678c KB |
70 | |
71 | /* recursively add missing parent directories */ | |
d28eec26 | 72 | dir->parent = hash_dir_entry(istate, ce, namelen); |
5102c617 | 73 | } |
2092678c KB |
74 | return dir; |
75 | } | |
76 | ||
77 | static void add_dir_entry(struct index_state *istate, struct cache_entry *ce) | |
78 | { | |
79 | /* Add reference to the directory entry (and parents if 0). */ | |
80 | struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce)); | |
81 | while (dir && !(dir->nr++)) | |
82 | dir = dir->parent; | |
83 | } | |
84 | ||
85 | static void remove_dir_entry(struct index_state *istate, struct cache_entry *ce) | |
86 | { | |
87 | /* | |
1c8cca19 KB |
88 | * Release reference to the directory entry. If 0, remove and continue |
89 | * with parent directory. | |
2092678c KB |
90 | */ |
91 | struct dir_entry *dir = hash_dir_entry(istate, ce, ce_namelen(ce)); | |
1c8cca19 KB |
92 | while (dir && !(--dir->nr)) { |
93 | struct dir_entry *parent = dir->parent; | |
94 | hashmap_remove(&istate->dir_hash, dir, NULL); | |
95 | free(dir); | |
96 | dir = parent; | |
97 | } | |
5102c617 JJ |
98 | } |
99 | ||
96872bc2 LT |
100 | static void hash_index_entry(struct index_state *istate, struct cache_entry *ce) |
101 | { | |
96872bc2 LT |
102 | if (ce->ce_flags & CE_HASHED) |
103 | return; | |
104 | ce->ce_flags |= CE_HASHED; | |
8b013788 KB |
105 | hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce))); |
106 | hashmap_add(&istate->name_hash, ce); | |
5102c617 | 107 | |
419a597f | 108 | if (ignore_case) |
2092678c | 109 | add_dir_entry(istate, ce); |
96872bc2 LT |
110 | } |
111 | ||
7663cdc8 SB |
112 | static int cache_entry_cmp(const void *unused_cmp_data, |
113 | const struct cache_entry *ce1, | |
114 | const struct cache_entry *ce2, | |
115 | const void *remove) | |
419a597f KB |
116 | { |
117 | /* | |
118 | * For remove_name_hash, find the exact entry (pointer equality); for | |
7b359ea6 | 119 | * index_file_exists, find all entries with matching hash code and |
419a597f KB |
120 | * decide whether the entry matches in same_name. |
121 | */ | |
122 | return remove ? !(ce1 == ce2) : 0; | |
123 | } | |
124 | ||
846df809 JH |
125 | static int lazy_try_threaded = 1; |
126 | static int lazy_nr_dir_threads; | |
127 | ||
128 | #ifdef NO_PTHREADS | |
129 | ||
130 | static inline int lookup_lazy_params(struct index_state *istate) | |
96872bc2 | 131 | { |
846df809 JH |
132 | return 0; |
133 | } | |
134 | ||
135 | static inline void threaded_lazy_init_name_hash( | |
136 | struct index_state *istate) | |
137 | { | |
138 | } | |
139 | ||
140 | #else | |
141 | ||
142 | #include "thread-utils.h" | |
143 | ||
144 | /* | |
145 | * Set a minimum number of cache_entries that we will handle per | |
146 | * thread and use that to decide how many threads to run (upto | |
147 | * the number on the system). | |
148 | * | |
149 | * For guidance setting the lower per-thread bound, see: | |
150 | * t/helper/test-lazy-init-name-hash --analyze | |
151 | */ | |
152 | #define LAZY_THREAD_COST (2000) | |
153 | ||
154 | /* | |
155 | * We use n mutexes to guard n partitions of the "istate->dir_hash" | |
156 | * hashtable. Since "find" and "insert" operations will hash to a | |
157 | * particular bucket and modify/search a single chain, we can say | |
158 | * that "all chains mod n" are guarded by the same mutex -- rather | |
159 | * than having a single mutex to guard the entire table. (This does | |
160 | * require that we disable "rehashing" on the hashtable.) | |
161 | * | |
162 | * So, a larger value here decreases the probability of a collision | |
163 | * and the time that each thread must wait for the mutex. | |
164 | */ | |
165 | #define LAZY_MAX_MUTEX (32) | |
166 | ||
167 | static pthread_mutex_t *lazy_dir_mutex_array; | |
168 | ||
169 | /* | |
170 | * An array of lazy_entry items is used by the n threads in | |
171 | * the directory parse (first) phase to (lock-free) store the | |
172 | * intermediate results. These values are then referenced by | |
173 | * the 2 threads in the second phase. | |
174 | */ | |
175 | struct lazy_entry { | |
176 | struct dir_entry *dir; | |
177 | unsigned int hash_dir; | |
178 | unsigned int hash_name; | |
179 | }; | |
180 | ||
181 | /* | |
182 | * Decide if we want to use threads (if available) to load | |
183 | * the hash tables. We set "lazy_nr_dir_threads" to zero when | |
184 | * it is not worth it. | |
185 | */ | |
186 | static int lookup_lazy_params(struct index_state *istate) | |
187 | { | |
188 | int nr_cpus; | |
189 | ||
190 | lazy_nr_dir_threads = 0; | |
191 | ||
192 | if (!lazy_try_threaded) | |
193 | return 0; | |
194 | ||
195 | /* | |
196 | * If we are respecting case, just use the original | |
197 | * code to build the "istate->name_hash". We don't | |
198 | * need the complexity here. | |
199 | */ | |
200 | if (!ignore_case) | |
201 | return 0; | |
202 | ||
203 | nr_cpus = online_cpus(); | |
204 | if (nr_cpus < 2) | |
205 | return 0; | |
206 | ||
207 | if (istate->cache_nr < 2 * LAZY_THREAD_COST) | |
208 | return 0; | |
96872bc2 | 209 | |
846df809 JH |
210 | if (istate->cache_nr < nr_cpus * LAZY_THREAD_COST) |
211 | nr_cpus = istate->cache_nr / LAZY_THREAD_COST; | |
212 | lazy_nr_dir_threads = nr_cpus; | |
213 | return lazy_nr_dir_threads; | |
214 | } | |
215 | ||
216 | /* | |
217 | * Initialize n mutexes for use when searching and inserting | |
218 | * into "istate->dir_hash". All "dir" threads are trying | |
219 | * to insert partial pathnames into the hash as they iterate | |
220 | * over their portions of the index, so lock contention is | |
221 | * high. | |
222 | * | |
223 | * However, the hashmap is going to put items into bucket | |
224 | * chains based on their hash values. Use that to create n | |
225 | * mutexes and lock on mutex[bucket(hash) % n]. This will | |
226 | * decrease the collision rate by (hopefully) by a factor of n. | |
227 | */ | |
228 | static void init_dir_mutex(void) | |
229 | { | |
230 | int j; | |
231 | ||
232 | lazy_dir_mutex_array = xcalloc(LAZY_MAX_MUTEX, sizeof(pthread_mutex_t)); | |
233 | ||
234 | for (j = 0; j < LAZY_MAX_MUTEX; j++) | |
235 | init_recursive_mutex(&lazy_dir_mutex_array[j]); | |
236 | } | |
237 | ||
238 | static void cleanup_dir_mutex(void) | |
239 | { | |
240 | int j; | |
241 | ||
242 | for (j = 0; j < LAZY_MAX_MUTEX; j++) | |
243 | pthread_mutex_destroy(&lazy_dir_mutex_array[j]); | |
244 | ||
245 | free(lazy_dir_mutex_array); | |
246 | } | |
247 | ||
248 | static void lock_dir_mutex(int j) | |
249 | { | |
250 | pthread_mutex_lock(&lazy_dir_mutex_array[j]); | |
251 | } | |
252 | ||
253 | static void unlock_dir_mutex(int j) | |
254 | { | |
255 | pthread_mutex_unlock(&lazy_dir_mutex_array[j]); | |
256 | } | |
257 | ||
258 | static inline int compute_dir_lock_nr( | |
259 | const struct hashmap *map, | |
260 | unsigned int hash) | |
261 | { | |
262 | return hashmap_bucket(map, hash) % LAZY_MAX_MUTEX; | |
263 | } | |
264 | ||
265 | static struct dir_entry *hash_dir_entry_with_parent_and_prefix( | |
266 | struct index_state *istate, | |
267 | struct dir_entry *parent, | |
268 | struct strbuf *prefix) | |
269 | { | |
270 | struct dir_entry *dir; | |
271 | unsigned int hash; | |
272 | int lock_nr; | |
273 | ||
274 | /* | |
275 | * Either we have a parent directory and path with slash(es) | |
276 | * or the directory is an immediate child of the root directory. | |
277 | */ | |
278 | assert((parent != NULL) ^ (strchr(prefix->buf, '/') == NULL)); | |
279 | ||
280 | if (parent) | |
281 | hash = memihash_cont(parent->ent.hash, | |
282 | prefix->buf + parent->namelen, | |
283 | prefix->len - parent->namelen); | |
284 | else | |
285 | hash = memihash(prefix->buf, prefix->len); | |
286 | ||
287 | lock_nr = compute_dir_lock_nr(&istate->dir_hash, hash); | |
288 | lock_dir_mutex(lock_nr); | |
289 | ||
290 | dir = find_dir_entry__hash(istate, prefix->buf, prefix->len, hash); | |
291 | if (!dir) { | |
292 | FLEX_ALLOC_MEM(dir, name, prefix->buf, prefix->len); | |
293 | hashmap_entry_init(dir, hash); | |
294 | dir->namelen = prefix->len; | |
295 | dir->parent = parent; | |
296 | hashmap_add(&istate->dir_hash, dir); | |
297 | ||
298 | if (parent) { | |
299 | unlock_dir_mutex(lock_nr); | |
300 | ||
301 | /* All I really need here is an InterlockedIncrement(&(parent->nr)) */ | |
302 | lock_nr = compute_dir_lock_nr(&istate->dir_hash, parent->ent.hash); | |
303 | lock_dir_mutex(lock_nr); | |
304 | parent->nr++; | |
305 | } | |
306 | } | |
307 | ||
308 | unlock_dir_mutex(lock_nr); | |
309 | ||
310 | return dir; | |
311 | } | |
312 | ||
313 | /* | |
314 | * handle_range_1() and handle_range_dir() are derived from | |
315 | * clear_ce_flags_1() and clear_ce_flags_dir() in unpack-trees.c | |
316 | * and handle the iteration over the entire array of index entries. | |
317 | * They use recursion for adjacent entries in the same parent | |
318 | * directory. | |
319 | */ | |
320 | static int handle_range_1( | |
321 | struct index_state *istate, | |
322 | int k_start, | |
323 | int k_end, | |
324 | struct dir_entry *parent, | |
325 | struct strbuf *prefix, | |
326 | struct lazy_entry *lazy_entries); | |
327 | ||
328 | static int handle_range_dir( | |
329 | struct index_state *istate, | |
330 | int k_start, | |
331 | int k_end, | |
332 | struct dir_entry *parent, | |
333 | struct strbuf *prefix, | |
334 | struct lazy_entry *lazy_entries, | |
335 | struct dir_entry **dir_new_out) | |
336 | { | |
337 | int rc, k; | |
338 | int input_prefix_len = prefix->len; | |
339 | struct dir_entry *dir_new; | |
340 | ||
341 | dir_new = hash_dir_entry_with_parent_and_prefix(istate, parent, prefix); | |
342 | ||
343 | strbuf_addch(prefix, '/'); | |
344 | ||
345 | /* | |
346 | * Scan forward in the index array for index entries having the same | |
347 | * path prefix (that are also in this directory). | |
348 | */ | |
2a1bd45b KW |
349 | if (k_start + 1 >= k_end) |
350 | k = k_end; | |
351 | else if (strncmp(istate->cache[k_start + 1]->name, prefix->buf, prefix->len) > 0) | |
846df809 JH |
352 | k = k_start + 1; |
353 | else if (strncmp(istate->cache[k_end - 1]->name, prefix->buf, prefix->len) == 0) | |
354 | k = k_end; | |
355 | else { | |
356 | int begin = k_start; | |
357 | int end = k_end; | |
358 | while (begin < end) { | |
359 | int mid = (begin + end) >> 1; | |
360 | int cmp = strncmp(istate->cache[mid]->name, prefix->buf, prefix->len); | |
361 | if (cmp == 0) /* mid has same prefix; look in second part */ | |
362 | begin = mid + 1; | |
363 | else if (cmp > 0) /* mid is past group; look in first part */ | |
364 | end = mid; | |
365 | else | |
366 | die("cache entry out of order"); | |
367 | } | |
368 | k = begin; | |
369 | } | |
370 | ||
371 | /* | |
372 | * Recurse and process what we can of this subset [k_start, k). | |
373 | */ | |
374 | rc = handle_range_1(istate, k_start, k, dir_new, prefix, lazy_entries); | |
375 | ||
376 | strbuf_setlen(prefix, input_prefix_len); | |
377 | ||
378 | *dir_new_out = dir_new; | |
379 | return rc; | |
380 | } | |
381 | ||
382 | static int handle_range_1( | |
383 | struct index_state *istate, | |
384 | int k_start, | |
385 | int k_end, | |
386 | struct dir_entry *parent, | |
387 | struct strbuf *prefix, | |
388 | struct lazy_entry *lazy_entries) | |
389 | { | |
390 | int input_prefix_len = prefix->len; | |
391 | int k = k_start; | |
392 | ||
393 | while (k < k_end) { | |
394 | struct cache_entry *ce_k = istate->cache[k]; | |
395 | const char *name, *slash; | |
396 | ||
397 | if (prefix->len && strncmp(ce_k->name, prefix->buf, prefix->len)) | |
398 | break; | |
399 | ||
400 | name = ce_k->name + prefix->len; | |
401 | slash = strchr(name, '/'); | |
402 | ||
403 | if (slash) { | |
404 | int len = slash - name; | |
405 | int processed; | |
406 | struct dir_entry *dir_new; | |
407 | ||
408 | strbuf_add(prefix, name, len); | |
409 | processed = handle_range_dir(istate, k, k_end, parent, prefix, lazy_entries, &dir_new); | |
410 | if (processed) { | |
411 | k += processed; | |
412 | strbuf_setlen(prefix, input_prefix_len); | |
413 | continue; | |
414 | } | |
415 | ||
416 | strbuf_addch(prefix, '/'); | |
417 | processed = handle_range_1(istate, k, k_end, dir_new, prefix, lazy_entries); | |
418 | k += processed; | |
419 | strbuf_setlen(prefix, input_prefix_len); | |
420 | continue; | |
421 | } | |
422 | ||
423 | /* | |
424 | * It is too expensive to take a lock to insert "ce_k" | |
425 | * into "istate->name_hash" and increment the ref-count | |
426 | * on the "parent" dir. So we defer actually updating | |
427 | * permanent data structures until phase 2 (where we | |
428 | * can change the locking requirements) and simply | |
429 | * accumulate our current results into the lazy_entries | |
430 | * data array). | |
431 | * | |
432 | * We do not need to lock the lazy_entries array because | |
433 | * we have exclusive access to the cells in the range | |
434 | * [k_start,k_end) that this thread was given. | |
435 | */ | |
436 | lazy_entries[k].dir = parent; | |
437 | if (parent) { | |
438 | lazy_entries[k].hash_name = memihash_cont( | |
439 | parent->ent.hash, | |
440 | ce_k->name + parent->namelen, | |
441 | ce_namelen(ce_k) - parent->namelen); | |
442 | lazy_entries[k].hash_dir = parent->ent.hash; | |
443 | } else { | |
444 | lazy_entries[k].hash_name = memihash(ce_k->name, ce_namelen(ce_k)); | |
445 | } | |
446 | ||
447 | k++; | |
448 | } | |
449 | ||
450 | return k - k_start; | |
451 | } | |
452 | ||
453 | struct lazy_dir_thread_data { | |
454 | pthread_t pthread; | |
455 | struct index_state *istate; | |
456 | struct lazy_entry *lazy_entries; | |
457 | int k_start; | |
458 | int k_end; | |
459 | }; | |
460 | ||
461 | static void *lazy_dir_thread_proc(void *_data) | |
462 | { | |
463 | struct lazy_dir_thread_data *d = _data; | |
464 | struct strbuf prefix = STRBUF_INIT; | |
465 | handle_range_1(d->istate, d->k_start, d->k_end, NULL, &prefix, d->lazy_entries); | |
466 | strbuf_release(&prefix); | |
467 | return NULL; | |
468 | } | |
469 | ||
470 | struct lazy_name_thread_data { | |
471 | pthread_t pthread; | |
472 | struct index_state *istate; | |
473 | struct lazy_entry *lazy_entries; | |
474 | }; | |
475 | ||
476 | static void *lazy_name_thread_proc(void *_data) | |
477 | { | |
478 | struct lazy_name_thread_data *d = _data; | |
479 | int k; | |
480 | ||
481 | for (k = 0; k < d->istate->cache_nr; k++) { | |
482 | struct cache_entry *ce_k = d->istate->cache[k]; | |
483 | ce_k->ce_flags |= CE_HASHED; | |
484 | hashmap_entry_init(ce_k, d->lazy_entries[k].hash_name); | |
485 | hashmap_add(&d->istate->name_hash, ce_k); | |
486 | } | |
487 | ||
488 | return NULL; | |
489 | } | |
490 | ||
491 | static inline void lazy_update_dir_ref_counts( | |
492 | struct index_state *istate, | |
493 | struct lazy_entry *lazy_entries) | |
494 | { | |
495 | int k; | |
496 | ||
497 | for (k = 0; k < istate->cache_nr; k++) { | |
498 | if (lazy_entries[k].dir) | |
499 | lazy_entries[k].dir->nr++; | |
500 | } | |
501 | } | |
502 | ||
503 | static void threaded_lazy_init_name_hash( | |
504 | struct index_state *istate) | |
505 | { | |
506 | int nr_each; | |
507 | int k_start; | |
508 | int t; | |
509 | struct lazy_entry *lazy_entries; | |
510 | struct lazy_dir_thread_data *td_dir; | |
511 | struct lazy_name_thread_data *td_name; | |
512 | ||
513 | k_start = 0; | |
514 | nr_each = DIV_ROUND_UP(istate->cache_nr, lazy_nr_dir_threads); | |
515 | ||
516 | lazy_entries = xcalloc(istate->cache_nr, sizeof(struct lazy_entry)); | |
517 | td_dir = xcalloc(lazy_nr_dir_threads, sizeof(struct lazy_dir_thread_data)); | |
518 | td_name = xcalloc(1, sizeof(struct lazy_name_thread_data)); | |
519 | ||
520 | init_dir_mutex(); | |
521 | ||
522 | /* | |
523 | * Phase 1: | |
524 | * Build "istate->dir_hash" using n "dir" threads (and a read-only index). | |
525 | */ | |
526 | for (t = 0; t < lazy_nr_dir_threads; t++) { | |
527 | struct lazy_dir_thread_data *td_dir_t = td_dir + t; | |
528 | td_dir_t->istate = istate; | |
529 | td_dir_t->lazy_entries = lazy_entries; | |
530 | td_dir_t->k_start = k_start; | |
531 | k_start += nr_each; | |
532 | if (k_start > istate->cache_nr) | |
533 | k_start = istate->cache_nr; | |
534 | td_dir_t->k_end = k_start; | |
535 | if (pthread_create(&td_dir_t->pthread, NULL, lazy_dir_thread_proc, td_dir_t)) | |
536 | die("unable to create lazy_dir_thread"); | |
537 | } | |
538 | for (t = 0; t < lazy_nr_dir_threads; t++) { | |
539 | struct lazy_dir_thread_data *td_dir_t = td_dir + t; | |
540 | if (pthread_join(td_dir_t->pthread, NULL)) | |
541 | die("unable to join lazy_dir_thread"); | |
542 | } | |
543 | ||
544 | /* | |
545 | * Phase 2: | |
546 | * Iterate over all index entries and add them to the "istate->name_hash" | |
547 | * using a single "name" background thread. | |
548 | * (Testing showed it wasn't worth running more than 1 thread for this.) | |
549 | * | |
550 | * Meanwhile, finish updating the parent directory ref-counts for each | |
551 | * index entry using the current thread. (This step is very fast and | |
552 | * doesn't need threading.) | |
553 | */ | |
554 | td_name->istate = istate; | |
555 | td_name->lazy_entries = lazy_entries; | |
556 | if (pthread_create(&td_name->pthread, NULL, lazy_name_thread_proc, td_name)) | |
557 | die("unable to create lazy_name_thread"); | |
558 | ||
559 | lazy_update_dir_ref_counts(istate, lazy_entries); | |
560 | ||
561 | if (pthread_join(td_name->pthread, NULL)) | |
562 | die("unable to join lazy_name_thread"); | |
563 | ||
564 | cleanup_dir_mutex(); | |
565 | ||
566 | free(td_name); | |
567 | free(td_dir); | |
568 | free(lazy_entries); | |
569 | } | |
570 | ||
571 | #endif | |
572 | ||
573 | static void lazy_init_name_hash(struct index_state *istate) | |
574 | { | |
96872bc2 LT |
575 | if (istate->name_hash_initialized) |
576 | return; | |
419a597f | 577 | hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp, |
7663cdc8 | 578 | NULL, istate->cache_nr); |
16f9b4cd | 579 | hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp, |
7663cdc8 | 580 | NULL, istate->cache_nr); |
846df809 JH |
581 | |
582 | if (lookup_lazy_params(istate)) { | |
583 | hashmap_disallow_rehash(&istate->dir_hash, 1); | |
584 | threaded_lazy_init_name_hash(istate); | |
585 | hashmap_disallow_rehash(&istate->dir_hash, 0); | |
586 | } else { | |
587 | int nr; | |
588 | for (nr = 0; nr < istate->cache_nr; nr++) | |
589 | hash_index_entry(istate, istate->cache[nr]); | |
590 | } | |
591 | ||
96872bc2 LT |
592 | istate->name_hash_initialized = 1; |
593 | } | |
594 | ||
846df809 JH |
595 | /* |
596 | * A test routine for t/helper/ sources. | |
597 | * | |
598 | * Returns the number of threads used or 0 when | |
599 | * the non-threaded code path was used. | |
600 | * | |
601 | * Requesting threading WILL NOT override guards | |
602 | * in lookup_lazy_params(). | |
603 | */ | |
604 | int test_lazy_init_name_hash(struct index_state *istate, int try_threaded) | |
605 | { | |
606 | lazy_nr_dir_threads = 0; | |
607 | lazy_try_threaded = try_threaded; | |
608 | ||
609 | lazy_init_name_hash(istate); | |
610 | ||
611 | return lazy_nr_dir_threads; | |
612 | } | |
613 | ||
96872bc2 LT |
614 | void add_name_hash(struct index_state *istate, struct cache_entry *ce) |
615 | { | |
96872bc2 LT |
616 | if (istate->name_hash_initialized) |
617 | hash_index_entry(istate, ce); | |
618 | } | |
619 | ||
2092678c KB |
620 | void remove_name_hash(struct index_state *istate, struct cache_entry *ce) |
621 | { | |
419a597f KB |
622 | if (!istate->name_hash_initialized || !(ce->ce_flags & CE_HASHED)) |
623 | return; | |
624 | ce->ce_flags &= ~CE_HASHED; | |
625 | hashmap_remove(&istate->name_hash, ce, ce); | |
2092678c | 626 | |
419a597f KB |
627 | if (ignore_case) |
628 | remove_dir_entry(istate, ce); | |
2092678c KB |
629 | } |
630 | ||
cd2fef59 LT |
631 | static int slow_same_name(const char *name1, int len1, const char *name2, int len2) |
632 | { | |
633 | if (len1 != len2) | |
634 | return 0; | |
635 | ||
636 | while (len1) { | |
637 | unsigned char c1 = *name1++; | |
638 | unsigned char c2 = *name2++; | |
639 | len1--; | |
640 | if (c1 != c2) { | |
641 | c1 = toupper(c1); | |
642 | c2 = toupper(c2); | |
643 | if (c1 != c2) | |
644 | return 0; | |
645 | } | |
646 | } | |
647 | return 1; | |
648 | } | |
649 | ||
650 | static int same_name(const struct cache_entry *ce, const char *name, int namelen, int icase) | |
651 | { | |
652 | int len = ce_namelen(ce); | |
653 | ||
654 | /* | |
655 | * Always do exact compare, even if we want a case-ignoring comparison; | |
656 | * we do the quick exact one first, because it will be the common case. | |
657 | */ | |
be99ec97 | 658 | if (len == namelen && !memcmp(name, ce->name, len)) |
cd2fef59 LT |
659 | return 1; |
660 | ||
5102c617 JJ |
661 | if (!icase) |
662 | return 0; | |
663 | ||
2092678c | 664 | return slow_same_name(name, namelen, ce->name, len); |
cd2fef59 LT |
665 | } |
666 | ||
41284eb0 | 667 | int index_dir_exists(struct index_state *istate, const char *name, int namelen) |
db5360f3 | 668 | { |
db5360f3 ES |
669 | struct dir_entry *dir; |
670 | ||
671 | lazy_init_name_hash(istate); | |
672 | dir = find_dir_entry(istate, name, namelen); | |
41284eb0 DT |
673 | return dir && dir->nr; |
674 | } | |
db5360f3 | 675 | |
41284eb0 DT |
676 | void adjust_dirname_case(struct index_state *istate, char *name) |
677 | { | |
678 | const char *startPtr = name; | |
679 | const char *ptr = startPtr; | |
db5360f3 | 680 | |
41284eb0 DT |
681 | lazy_init_name_hash(istate); |
682 | while (*ptr) { | |
683 | while (*ptr && *ptr != '/') | |
684 | ptr++; | |
685 | ||
686 | if (*ptr == '/') { | |
687 | struct dir_entry *dir; | |
688 | ||
689 | ptr++; | |
690 | dir = find_dir_entry(istate, name, ptr - name + 1); | |
691 | if (dir) { | |
692 | memcpy((void *)startPtr, dir->name + (startPtr - name), ptr - startPtr); | |
693 | startPtr = ptr; | |
694 | } | |
695 | } | |
696 | } | |
db5360f3 ES |
697 | } |
698 | ||
699 | struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase) | |
96872bc2 | 700 | { |
96872bc2 LT |
701 | struct cache_entry *ce; |
702 | ||
703 | lazy_init_name_hash(istate); | |
96872bc2 | 704 | |
ab73a9d1 KB |
705 | ce = hashmap_get_from_hash(&istate->name_hash, |
706 | memihash(name, namelen), NULL); | |
96872bc2 | 707 | while (ce) { |
419a597f KB |
708 | if (same_name(ce, name, namelen, icase)) |
709 | return ce; | |
8b013788 | 710 | ce = hashmap_get_next(&istate->name_hash, ce); |
96872bc2 | 711 | } |
df292c79 | 712 | return NULL; |
96872bc2 | 713 | } |
2092678c | 714 | |
2092678c KB |
715 | void free_name_hash(struct index_state *istate) |
716 | { | |
717 | if (!istate->name_hash_initialized) | |
718 | return; | |
719 | istate->name_hash_initialized = 0; | |
2092678c | 720 | |
8b013788 | 721 | hashmap_free(&istate->name_hash, 0); |
e05881a4 | 722 | hashmap_free(&istate->dir_hash, 1); |
2092678c | 723 | } |