1 /* CYGNUS LOCAL: whole file jason */
2 /* hash.c -- hash table routines
3 Copyright (C) 1993, 94 Free Software Foundation, Inc.
4 Written by Steve Chamberlain <sac@cygnus.com>
6 This file was lifted from BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
26 extern void free PARAMS ((PTR));
28 /* Obstack allocation and deallocation routines. */
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free free
32 extern char * xmalloc ();
34 /* The default number of entries to use when creating a hash table. */
35 #define DEFAULT_SIZE (1009)
41 /* Create a new hash table, given a number of entries. */
44 hash_table_init_n (table, newfunc, size)
45 struct hash_table *table;
46 struct hash_entry *(*newfunc) PARAMS ((struct hash_entry *,
53 alloc = size * sizeof (struct hash_entry *);
54 if (!obstack_begin (&table->memory, alloc))
59 table->table = ((struct hash_entry **)
60 obstack_alloc (&table->memory, alloc));
66 memset ((PTR) table->table, 0, alloc);
68 table->newfunc = newfunc;
72 /* Create a new hash table with the default number of entries. */
75 hash_table_init (table, newfunc)
76 struct hash_table *table;
77 struct hash_entry *(*newfunc) PARAMS ((struct hash_entry *,
81 return hash_table_init_n (table, newfunc, DEFAULT_SIZE);
84 /* Free a hash table. */
87 hash_table_free (table)
88 struct hash_table *table;
90 obstack_free (&table->memory, (PTR) NULL);
93 /* Look up a string in a hash table. */
96 hash_lookup (table, string, create, copy)
97 struct hash_table *table;
102 register const unsigned char *s;
103 register unsigned long hash;
104 register unsigned int c;
105 struct hash_entry *hashp;
111 s = (const unsigned char *) string;
112 while ((c = *s++) != '\0')
114 hash += c + (c << 17);
118 hash += len + (len << 17);
121 index = hash % table->size;
122 for (hashp = table->table[index];
123 hashp != (struct hash_entry *) NULL;
126 if (hashp->hash == hash
127 && strcmp (hashp->string, string) == 0)
132 return (struct hash_entry *) NULL;
134 hashp = (*table->newfunc) ((struct hash_entry *) NULL, table, string);
135 if (hashp == (struct hash_entry *) NULL)
136 return (struct hash_entry *) NULL;
141 new = (char *) obstack_alloc (&table->memory, len + 1);
145 return (struct hash_entry *) NULL;
147 strcpy (new, string);
150 hashp->string = string;
152 hashp->next = table->table[index];
153 table->table[index] = hashp;
158 /* Base method for creating a new hash table entry. */
162 hash_newfunc (entry, table, string)
163 struct hash_entry *entry;
164 struct hash_table *table;
167 if (entry == (struct hash_entry *) NULL)
168 entry = ((struct hash_entry *)
169 hash_allocate (table, sizeof (struct hash_entry)));
173 /* Allocate space in a hash table. */
176 hash_allocate (table, size)
177 struct hash_table *table;
182 ret = obstack_alloc (&table->memory, size);
183 if (ret == NULL && size != 0)
188 /* Traverse a hash table. */
191 hash_traverse (table, func, info)
192 struct hash_table *table;
193 boolean (*func) PARAMS ((struct hash_entry *, PTR));
198 for (i = 0; i < table->size; i++)
200 struct hash_entry *p;
202 for (p = table->table[i]; p != NULL; p = p->next)
204 if (! (*func) (p, info))