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