1 /* hash.c -- hash table routines
2 Copyright (C) 1993, 94 Free Software Foundation, Inc.
3 Written by Steve Chamberlain <sac@cygnus.com>
5 This file was lifted from BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
25 extern void free PARAMS ((PTR));
27 /* Obstack allocation and deallocation routines. */
28 #define obstack_chunk_alloc xmalloc
29 #define obstack_chunk_free free
31 extern char * xmalloc ();
33 /* The default number of entries to use when creating a hash table. */
34 #define DEFAULT_SIZE (1009)
40 /* Create a new hash table, given a number of entries. */
43 hash_table_init_n (table, newfunc, size)
44 struct hash_table *table;
45 struct hash_entry *(*newfunc) PARAMS ((struct hash_entry *,
52 alloc = size * sizeof (struct hash_entry *);
53 if (!obstack_begin (&table->memory, alloc))
58 table->table = ((struct hash_entry **)
59 obstack_alloc (&table->memory, alloc));
65 memset ((PTR) table->table, 0, alloc);
67 table->newfunc = newfunc;
71 /* Create a new hash table with the default number of entries. */
74 hash_table_init (table, newfunc)
75 struct hash_table *table;
76 struct hash_entry *(*newfunc) PARAMS ((struct hash_entry *,
80 return hash_table_init_n (table, newfunc, DEFAULT_SIZE);
83 /* Free a hash table. */
86 hash_table_free (table)
87 struct hash_table *table;
89 obstack_free (&table->memory, (PTR) NULL);
92 /* Look up a string in a hash table. */
95 hash_lookup (table, string, create, copy)
96 struct hash_table *table;
101 register const unsigned char *s;
102 register unsigned long hash;
103 register unsigned int c;
104 struct hash_entry *hashp;
110 s = (const unsigned char *) string;
111 while ((c = *s++) != '\0')
113 hash += c + (c << 17);
117 hash += len + (len << 17);
120 index = hash % table->size;
121 for (hashp = table->table[index];
122 hashp != (struct hash_entry *) NULL;
125 if (hashp->hash == hash
126 && strcmp (hashp->string, string) == 0)
131 return (struct hash_entry *) NULL;
133 hashp = (*table->newfunc) ((struct hash_entry *) NULL, table, string);
134 if (hashp == (struct hash_entry *) NULL)
135 return (struct hash_entry *) NULL;
140 new = (char *) obstack_alloc (&table->memory, len + 1);
144 return (struct hash_entry *) NULL;
146 strcpy (new, string);
149 hashp->string = string;
151 hashp->next = table->table[index];
152 table->table[index] = hashp;
157 /* Base method for creating a new hash table entry. */
161 hash_newfunc (entry, table, string)
162 struct hash_entry *entry;
163 struct hash_table *table;
166 if (entry == (struct hash_entry *) NULL)
167 entry = ((struct hash_entry *)
168 hash_allocate (table, sizeof (struct hash_entry)));
172 /* Allocate space in a hash table. */
175 hash_allocate (table, size)
176 struct hash_table *table;
181 ret = obstack_alloc (&table->memory, size);
182 if (ret == NULL && size != 0)
187 /* Traverse a hash table. */
190 hash_traverse (table, func, info)
191 struct hash_table *table;
192 boolean (*func) PARAMS ((struct hash_entry *, PTR));
197 for (i = 0; i < table->size; i++)
199 struct hash_entry *p;
201 for (p = table->table[i]; p != NULL; p = p->next)
203 if (! (*func) (p, info))