summaryrefslogtreecommitdiff
path: root/Build/source/texk/kpathsea/win32hash.c
blob: 1c1252b3797bd496297284cdf577d23186849461 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
/* hash.c: hash table operations.

Copyright (C) 1994, 95, 96, 97, 98, 99, 2000 Karl Berry & Olaf Weber.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

#include <kpathsea/config.h>
#include <kpathsea/c-ctype.h>

#include <kpathsea/hash.h>
#include <kpathsea/str-list.h>

#ifdef HAVE_GETURL
#include <kpathsea/remote.h>
#endif

/* The hash function.  We go for simplicity here.  */

/* All our hash tables are related to filenames.  */
#ifdef MONOCASE_FILENAMES
#if defined(WIN32) && !defined(__i386_pc_gnu__)
/* This is way faster under Win32. */
#define TRANSFORM(x) ((unsigned)CharLower((LPTSTR)(BYTE)(x)))
#else
#define TRANSFORM(x) (tolower(x))
#endif
#else
#define TRANSFORM(x) (x)
#endif

#define MAX_HASH_TABLES 64

#define HASH_NULL(ptr) (ptr == hash_memory)
#define HASH_PTR(ptr) ((char *)hash_memory + (unsigned long)(ptr))
#define HASH_REL(rel) ((unsigned long)((char *)(rel) - (char *)hash_memory))
#define HASH_BUCKETS(table, n) (*((hash_element_type **)((char *)hash_memory + (unsigned long)table->buckets + (n) * sizeof(hash_element_type *))))
/* #define HASH_BUCKETS(table, n)  *((hash_element_type **)(HASH_PTR((unsigned long) ((hash_element_type **)HASH_PTR(table->buckets))[n]))) */
HANDLE hash_handle = 0;
void *hash_memory = NULL;
static unsigned long hash_memory_allocated = 0;
unsigned long hash_memory_size = 8L*1024L*1024L;

typedef struct _hash_memory_description {
  unsigned long sig;
  unsigned long memory_size;
  unsigned long first_free_address;
  unsigned long hash_offsets[MAX_HASH_TABLES];
  unsigned long db_dir_list_len;
  unsigned long db_dir_list;
} hash_memory_description;

hash_memory_description *hash_bloc;

/* We need to protect initialisation and insert / lookup */
static HANDLE hash_mutex;
static boolean hash_init_done = false;

extern str_list_type db_dir_list;

void hash_restore_str_list(unsigned long ptr, unsigned len, str_list_type *l);
void hash_save_str_list(str_list_type l);

#if 0
extern unsigned hash P2H(hash_table_type *table,  const_string key);
#ifdef NORMALIZED
extern unsigned hash_normalized P2H(hash_table_type *table,  const_string key);
#endif
#else
static unsigned
hash P2C(hash_table_type *, table,  const_string, key)
{
  unsigned n = 0;

  // fprintf(stderr, "Hashing %s to ", key);

  /* Our keys aren't often anagrams of each other, so no point in
     weighting the characters.  */
  while (*key != 0)
    n = (n + n + TRANSFORM (*key++)) % table->size;
  
  // fprintf(stderr, "%d\n", n);

  return n;
}

#ifdef NORMALIZED
/* Identical has function as above, but does not normalize keys. */
static unsigned
hash_normalized P2C(hash_table_type *, table,  const_string, key)
{
  unsigned n = 0;

  // fprintf(stderr, "Hashing (normalized) %s to ", key);

  /* Our keys aren't often anagrams of each other, so no point in
     weighting the characters.  */
  while (*key != 0)
    n = (n + n +  (*key++)) % table->size;

  // fprintf(stderr, "%d\n", n);
  
  return n;
}
#endif
#endif

/*
  This  function will be  registered by  atexit()  to  be called  upon
  program termination.     It   will free resources     allocated  for
  hash-tables.  It has to be registered only once, when the very first
  hash-table is allocated.
*/
void CDECL
hash_cleanup (void)
{
#ifdef HAVE_GETURL
  cleanup_remote_files();
#endif

  if (hash_memory != NULL) {
    if (UnmapViewOfFile(hash_memory) == 0) {
      /* Report an error */
      WARNING2("Can't UnMapViewOfFile(%x) (Error %d)!\n", hash_memory, GetLastError());
    }
    hash_memory = NULL;
  }

  if (hash_handle) {
    if (CloseHandle(hash_handle) == 0) {
      /* Report an error */
      WARNING2("Can't CloseHandle(%x) (Error %d)!\n", hash_handle, GetLastError());
    }
    hash_handle = NULL;
  }

  if (hash_mutex) {
    if (CloseHandle(hash_mutex) == 0) {
      /* Report an error */
      WARNING2("Can't CloseHandle(%x) (Error %d)!\n", hash_mutex, GetLastError());
    }
    hash_mutex = NULL;
  }
    
#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_SHM))
    DEBUGF1("Cleaned up shared hash tables, %ld memory was allocated.\n", hash_memory_allocated);
#endif
}

KPSEDLL boolean hash_init_needed = false;

boolean
hash_lock(boolean force)
{
  int dwWaitResult;

  if (!force && !hash_init_done)
    return true;

  /* Request ownership of mutex. */
  dwWaitResult = WaitForSingleObject(hash_mutex, 25000L);
 
  switch (dwWaitResult) {
    /* The thread got mutex ownership. */
  case WAIT_OBJECT_0: 
    return true;
    
    /* Cannot get mutex ownership due to time-out. */
  case WAIT_TIMEOUT: 
    WARNING("Timeout waiting to lock shared memory access!\n");
    return false; 

    /* Got ownership of the abandoned mutex object. */
  case WAIT_ABANDONED: 
    WARNING("Waiting to lock shared memory access abandoned!\n");
    return false; 
  }

  return false;
}

void
hash_unlock(void)
{
  if (! hash_init_done)
    return;

  if (ReleaseMutex(hash_mutex) == 0) {
    FATAL2("%s: Can't ReleaseMutex() access to shared memory databases (Error %d)!\n", kpse_program_name, GetLastError());
  }
}

static void
hash_start_init()
{
#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_SHM))
    DEBUGF1("%s: Start of shared hash tables initialisation.\n", kpse_program_name);
#endif
  hash_lock(true);
}

void
hash_end_init()
{
  hash_init_done = true;
  hash_unlock();

#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_SHM))
    DEBUGF1("%s: End of shared hash tables initialisation.\n", kpse_program_name);
#endif
}

void
hash_init (void)
{
  SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };

  hash_init_done = false;
  if ((hash_mutex = CreateMutex(NULL, FALSE, "kpse_initialization")) == 0) {
    FATAL1("Can't CreateMutex() for shared memory locking (Error %d)!\n", GetLastError());
  }

  if ((hash_handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, TRUE, HASH_MAPPING_NAME)) == 0) {

    hash_start_init();

    /* First call, need to allocate memory. */
    if ((hash_handle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE,
					 0, hash_memory_size, HASH_MAPPING_NAME)) == 0) {
      /* Report an error. */
      FATAL2("Can't CreateFileMapping() of size %ul (Error %d)!\n", hash_memory_size, GetLastError());
    };
    atexit(hash_cleanup);
    hash_init_needed = true;
  }

  if ((hash_memory = MapViewOfFile(hash_handle, FILE_MAP_ALL_ACCESS, 0, 0, 0)) == NULL) {
    /* Report an error. */
      FATAL2("Can't MapViewOfFile(%x) (Error %d)!\n", hash_handle, GetLastError());
  }

  hash_bloc = (hash_memory_description *)hash_memory;
  if (hash_init_needed) {
    /* Is it more efficient to zero the whole block first ? */
    ZeroMemory(hash_memory, sizeof(hash_bloc));
    hash_bloc->sig = sizeof(hash_memory_description);
    hash_bloc->memory_size = hash_memory_size;
    hash_bloc->first_free_address = sizeof(hash_memory_description);
  }
  else {
    extern boolean first_search;

    hash_start_init();

#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_SHM))
    DEBUGF1("Reusing shared hash tables at %x.\n", hash_memory);
#endif

    assert(hash_bloc->sig == sizeof(hash_memory_description));
    /*
      We could be a little bit cleaner here, just
      relying on the hashtable_instances enumeration.
    */
    cnf_hash = (hash_table_type *)hash_exists_p(hashtable_cnf);
    db = (hash_table_type *)hash_exists_p(hashtable_db);
    alias_db = (hash_table_type *)hash_exists_p(hashtable_alias);
    map = (hash_table_type *)hash_exists_p(hashtable_map);
    link_table = (hash_table_type *)hash_exists_p(hashtable_links);
    remote_db = (hash_table_type *)hash_exists_p(hashtable_remote);

    db_dir_list = str_list_init();
    /* Awful kludge ! */
    first_search = false;
    hash_restore_str_list(hash_bloc->db_dir_list, hash_bloc->db_dir_list_len, &db_dir_list);
    
  }
}

void
hash_reinit()
{
  if (!hash_init_needed) {
    fprintf(stderr, "%s: your `ls-R' database files have been modified\n", kpse_program_name);
    fprintf(stderr, "while kpathsea Dll was loaded by other programs.\n");
    fprintf(stderr, "It is recommended that you terminate all of them.\n");
    fprintf(stderr, "in order to reinitialize the shared databases.\n");
  }
}

boolean
hash_extend_memory ()
{
  /* We need to allocate a new memory bloc twice the size
     of the previous one, copy everything and free the previous block.
  */
  SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, TRUE };
  HANDLE new_hash_handle = 0;
  void *new_hash_memory = NULL;
  unsigned long new_hash_memory_size = 2 * hash_memory_size;

  assert(hash_memory != NULL);

  /* First, create some private unnamed bloc */  
  if ((new_hash_handle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE,
					   0, new_hash_memory_size, NULL)) == 0) {
    /* Report an error. */
    FATAL2("Can't CreateFileMapping() of size %ul (Error %d)!\n", new_hash_memory_size, GetLastError());
  };

  if ((new_hash_memory = MapViewOfFile(new_hash_handle, FILE_MAP_ALL_ACCESS, 0, 0, 0)) == NULL) {
    /* Report an error. */
      FATAL2("Can't MapViewOfFile(%x) (Error %d)!\n", new_hash_handle, GetLastError());
  }
  /* Copy the data */
  CopyMemory(new_hash_memory, hash_memory, hash_memory_size);
  /* Cleanup handles */
  hash_cleanup();
  /* make the values point to the new bloc */
  hash_memory_size = new_hash_memory_size;
  hash_memory = new_hash_memory;
  hash_handle = new_hash_handle;
  
  /* Now, create the actual named, shareable bloc */
  if ((new_hash_handle = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE,
					   0, hash_memory_size, HASH_MAPPING_NAME)) == 0) {
    /* Report an error. */
    FATAL2("Can't CreateFileMapping() of size %ul (Error %d)!\n", new_hash_memory_size, GetLastError());
  };

  if ((new_hash_memory = MapViewOfFile(new_hash_handle, FILE_MAP_ALL_ACCESS, 0, 0, 0)) == NULL) {
    /* Report an error. */
      FATAL2("Can't MapViewOfFile(%x) (Error %d)!\n", new_hash_handle, GetLastError());
  }
  /* Copy the data */
  CopyMemory(new_hash_memory, hash_memory, hash_memory_size);
  /* Cleanup the handles */
  hash_cleanup();

  hash_memory = new_hash_memory;
  hash_handle = new_hash_handle;
  hash_bloc = (hash_memory_description *)hash_memory;

  return true;
}

unsigned long
hash_malloc P1C(unsigned, size)
{
  unsigned long ret = hash_bloc->first_free_address;
  hash_bloc->first_free_address += size;
  
  if ((unsigned long)hash_bloc->first_free_address > (unsigned long)hash_bloc->memory_size) {
    if (! hash_extend_memory()) {
      fprintf(stderr, "Memory exhausted: base = %x, first_free_address = %x, size = %x\n",
	      hash_memory, hash_bloc->first_free_address, hash_bloc->memory_size);
    }
  }
  assert((unsigned long)hash_bloc->first_free_address < (unsigned long)hash_bloc->memory_size);

  hash_memory_allocated += size;

  return ret;
}

unsigned long
hash_strdup P1C(const_string, p)
{
  unsigned long q = hash_malloc(strlen(p)+1);
  memcpy(HASH_PTR(q), p, strlen(p)+1);
  return q;
}

void
hash_free P1C(void *, address)
{
  /* Don't want to play with this for the moment.
     Hash-Tables just grow up, no need to remove 
     anything from them. The `hash_remove' function
     is only called from contrib/variables.c .
  */
}

void
hash_save_str_list(str_list_type l)
{
  unsigned int i;
  unsigned long *q;
  assert(hash_bloc != NULL);
  hash_bloc->db_dir_list_len = STR_LIST_LENGTH(l);

  if (hash_bloc->db_dir_list_len == 0) return;

  hash_bloc->db_dir_list = hash_malloc(hash_bloc->db_dir_list_len * sizeof(unsigned long));
  for (i = 0, q = (unsigned long *)HASH_PTR(hash_bloc->db_dir_list); 
       i < hash_bloc->db_dir_list_len; 
       i++, q++) {
    *q = hash_strdup(STR_LIST_ELT(l, i));
  }
}

void
hash_restore_str_list(unsigned long ptr, unsigned len, str_list_type *l)
{
  int i;
  unsigned long *q;
  for (i = 0, q = (unsigned long *)HASH_PTR(ptr); 
       i < len; 
       i++, q++) {
      str_list_add (l, HASH_PTR(*q));
  }
#if 0
  fprintf(stderr, "Restored db_dir_list :\n");
  for (i = 0; i < STR_LIST_LENGTH(*l); i++) {
    fprintf(stderr, "\t%s\n", STR_LIST_ELT(*l, i));
  }
#endif
}

hash_table_type *
hash_exists_p P1C(hashtable_instances, hash_number)
{
  unsigned long rel;

  assert(hash_memory != NULL);
  assert(hash_bloc != NULL);
  
#if 0
  fprintf(stderr, "Looking up for table %d ... ", hash_number);
#endif
  if (hash_bloc) {
    if (rel = hash_bloc->hash_offsets[hash_number]) {
#if 0
      fprintf(stderr, "found (%x) for %d\n", (char *)hash_memory + rel, hash_number);
#endif
      return (hash_table_type *)((char *)hash_memory + rel);
    }
  }
#if 0
  fprintf(stderr, "found (null) for %d\n", hash_number);
#endif
  return NULL;
}

hash_table_type *
hash_create P2C(unsigned, size, hashtable_instances, hash_number) 
{
  /* hash_table_type ret; changed into "static ..." to work around gcc
     optimizer bug for Alpha.  */
  static hash_table_type *ret;
  unsigned b;

  if (hash_memory == NULL) {
    hash_init();
  }

  if (ret = hash_exists_p(hash_number)) {
    assert(size == ret->size);
    return ret;
  }

  hash_lock(false);

#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_SHM))
    DEBUGF2("Hash: creating shared hash table %u of size %u\n", hash_number, size);
#endif

  hash_bloc->hash_offsets[hash_number] = hash_malloc(sizeof(hash_table_type));
  ret = (hash_table_type *)HASH_PTR(hash_bloc->hash_offsets[hash_number]);

  ret->buckets = (hash_element_type **)hash_malloc(size * sizeof(hash_element_type *));
  ret->size = size;
  
  /* calloc's zeroes aren't necessarily NULL, so be safe.  */
  for (b = 0; b <ret->size; b++)
    HASH_BUCKETS(ret, b) = NULL;

  hash_unlock();

  return ret;
}

#if 0
/* Not really usable. */
hash_table_type *
hash_reset P1C(hashtable_instances, hash_number) 
{
  /* hash_table_type ret; changed into "static ..." to work around gcc
     optimizer bug for Alpha.  */
  static hash_table_type *ret;
  unsigned b;

  if (hash_memory == NULL
      || (ret = hash_exists_p(hash_number)) == NULL) {
    return NULL;
  }

  hash_lock(false);

#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_SHM))
    DEBUGF1("Hash: resetting shared hash table %u\n", hash_number);
#endif

  /* calloc's zeroes aren't necessarily NULL, so be safe.  */
  for (b = 0; b <ret->size; b++)
    HASH_BUCKETS(ret, b) = NULL;

  hash_unlock();

  return ret;
}

#endif
/* WARNING: the comment below is frightening !
   However, it does not seem to be the case that the strings
   are changed after being inserted in the map.
   IHMO, this should be strictly forbidden.
*/
/* Whether or not KEY is already in MAP, insert it and VALUE.  Do not
   duplicate the strings, in case they're being purposefully shared.  */

void
hash_insert P3C(hash_table_type *, table,  const_string, key,
                const_string, value)
{

  unsigned n = hash (table, key);
  unsigned long new_elt;

  hash_lock(false);

  new_elt = hash_malloc(sizeof(hash_element_type));

  ((hash_element_type *)(HASH_PTR(new_elt)))->key = (const_string)hash_strdup(key);
  ((hash_element_type *)(HASH_PTR(new_elt)))->value = (const_string)hash_strdup(value);
  ((hash_element_type *)(HASH_PTR(new_elt)))->next = NULL;
  
  /* Insert the new element at the end of the list.  */
  if (! HASH_BUCKETS(table, n))
    /* first element in bucket is a special case.  */
    HASH_BUCKETS(table, n) = (hash_element_type *)new_elt;
  else
    {
      hash_element_type *loc = (hash_element_type *)HASH_PTR(HASH_BUCKETS(table, n));
      while (loc->next)		/* Find the last element.  */
        loc = (hash_element_type *)HASH_PTR(loc->next);
      loc->next = (hash_element_type *)new_elt;	/* Insert the new one after.  */
    }

  hash_unlock();
}

#ifdef NORMALIZED
/* Same as above, for normalized keys. */
void
hash_insert_normalized P3C(hash_table_type *, table,
                           const_string, key,
                           const_string, value)
{
  unsigned n = hash_normalized (table, key);
  unsigned long new_elt;

  hash_lock(false);

  new_elt = hash_malloc(sizeof(hash_element_type));

  ((hash_element_type *)(HASH_PTR(new_elt)))->key = (const_string)hash_strdup(key);
  ((hash_element_type *)(HASH_PTR(new_elt)))->value = (const_string)hash_strdup(value);
  ((hash_element_type *)(HASH_PTR(new_elt)))->next = NULL;
  
  /* Insert the new element at the end of the list.  */
  if (! HASH_BUCKETS(table, n))
    /* first element in bucket is a special case.  */
    HASH_BUCKETS(table, n) = (hash_element_type *)new_elt;
  else
    {
      hash_element_type *loc = (hash_element_type *)HASH_PTR(HASH_BUCKETS(table, n));
      while (loc->next)		/* Find the last element.  */
        loc = (hash_element_type *)HASH_PTR(loc->next);
      loc->next = (hash_element_type *)new_elt;	/* Insert the new one after.  */
    }

  hash_unlock();
}
#endif /* WIN32*/

/* Remove a (KEY, VALUE) pair.  */

void
hash_remove P3C(hash_table_type *, table,  const_string, key,
                const_string, value)
{
  hash_element_type *p;
  hash_element_type *q;
  unsigned n = hash (table, key);

  hash_lock(false);

  /* Find pair.  */
  for (q = NULL, p = (hash_element_type *)HASH_PTR(HASH_BUCKETS(table, n)); ! HASH_NULL(p); q = p, p = (hash_element_type *)HASH_PTR(p->next))
    if (FILESTRCASEEQ (key, HASH_PTR(p->key)) && STREQ (value, HASH_PTR(p->value)))
      break;
  if (p) {
    /* We found something, remove it from the chain.  */
    if (q) q->next = p->next; else HASH_BUCKETS(table, n) = p->next;
    /* We cannot dispose of the contents.  */
    hash_free ((void *)HASH_REL(p));
  }

  hash_unlock();
}

/* Look up STR in MAP.  Return a (dynamically-allocated) list of the
   corresponding strings or NULL if no match.  */ 

#ifdef KPSE_DEBUG
/* Print the hash values as integers if this is nonzero.  */
boolean kpse_debug_hash_lookup_int = false; 
#endif

string *
hash_lookup P2C(hash_table_type *, table,  const_string, key)
{
  hash_element_type *p;
  str_list_type ret;
  unsigned n = hash (table, key);

  ret = str_list_init ();

  hash_lock(false);

  /* Look at everything in this bucket.  */
  for (p = (hash_element_type *)HASH_PTR(HASH_BUCKETS(table, n)); ! HASH_NULL(p); p = (hash_element_type *)HASH_PTR(p->next))
    if (FILESTRCASEEQ (key, HASH_PTR(p->key)))
      /* Cast because the general str_list_type shouldn't force const data.  */
      str_list_add (&ret, (string) xstrdup((string)HASH_PTR(p->value)));
  
  /* If we found anything, mark end of list with null.  */
  if (STR_LIST (ret))
    str_list_add (&ret, NULL);

#ifdef KPSE_DEBUG
  if (KPSE_DEBUG_P (KPSE_DEBUG_HASH))
    {
      DEBUGF1 ("hash_shared_lookup(%s) =>", key);
      if (!STR_LIST (ret))
        fputs (" (nil)\n", stderr);
      else
        {
          string *r;
          for (r = STR_LIST (ret); *r; r++)
            {
              putc (' ', stderr);
              if (kpse_debug_hash_lookup_int)
                fprintf (stderr, "%ld", (long) *r);
              else
                fputs (*r, stderr);
            }
          putc ('\n', stderr);
        }
      fflush (stderr);
    }
#endif

  hash_unlock();

  return STR_LIST (ret);
}

/* We only print nonempty buckets, to decrease output volume.  */

void
hash_print P2C(hash_table_type *, table,  boolean, summary_only)
{
  unsigned b;
  unsigned total_elements = 0, total_buckets = 0;
  
  if (table == NULL) return;

  hash_lock(false);

  for (b = 0; b < table->size; b++) {
    hash_element_type *bucket = (hash_element_type *)HASH_PTR(HASH_BUCKETS(table, b));

    if (! HASH_NULL(bucket)) {
      unsigned len = 1;
      hash_element_type *tb;

      total_buckets++;
      if (!summary_only) fprintf (stderr, "%4d ", b);

      for (tb = (hash_element_type *)HASH_PTR(bucket->next); ! HASH_NULL(tb); tb = (hash_element_type *)HASH_PTR(tb->next))
        len++;
      if (!summary_only) fprintf (stderr, ":%-5d", len);
      total_elements += len;

      if (!summary_only) {
        for (tb = bucket; ! HASH_NULL(tb); tb = (hash_element_type *)HASH_PTR(tb->next))
          fprintf (stderr, " %s=>%s", HASH_PTR(tb->key), HASH_PTR(tb->value));
        putc ('\n', stderr);
      }
    }
  }

  hash_unlock();

  fprintf (stderr,
          "%u buckets, %u nonempty (%u%%); %u entries, average chain %.1f.\n",
          table->size,
          total_buckets,
          100 * total_buckets / table->size,
          total_elements,
          total_buckets ? total_elements / (double) total_buckets : 0.0);
}

/* Simple iterator over hash-table elements */
void
hash_iter P2C(hash_table_type *, table, hash_iter_fn, fn)
{
  unsigned b;

  if (! table) return;

  hash_lock(false);

  for (b = 0; b < table->size; b++) {
    hash_element_type *bucket = (hash_element_type *)HASH_PTR(HASH_BUCKETS(table, b));
    if (! HASH_NULL(bucket)) {
      unsigned len = 1;
      hash_element_type *tb;
      for (tb = bucket; ! HASH_NULL(tb); tb = (hash_element_type *)HASH_PTR(tb->next)) {
#ifdef KPSE_DEBUG
	if (KPSE_DEBUG_P (KPSE_DEBUG_HASH))
	  {
	    DEBUGF2("applying (shared) fn to %s%s\n", HASH_PTR(tb->value), HASH_PTR(tb->key));
	  }	
#endif
	(*fn)(HASH_PTR(tb->key), HASH_PTR(tb->value));
      }
    }
  }

  hash_unlock();
}