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. */
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)
36 /* Create a new hash table, given a number of entries. */
39 hash_table_init_n (table, newfunc, hash, comp, size)
40 struct hash_table *table;
41 struct hash_entry *(*newfunc) PARAMS ((struct hash_entry *,
44 unsigned long (*hash) PARAMS ((hash_table_key));
45 boolean (*comp) PARAMS ((hash_table_key, hash_table_key));
50 alloc = size * sizeof (struct hash_entry *);
51 if (!obstack_begin (&table->memory, alloc))
56 table->table = ((struct hash_entry **)
57 obstack_alloc (&table->memory, alloc));
63 memset ((PTR) table->table, 0, alloc);
65 table->newfunc = newfunc;
71 /* Create a new hash table with the default number of entries. */
74 hash_table_init (table, newfunc, hash, comp)
75 struct hash_table *table;
76 struct hash_entry *(*newfunc) PARAMS ((struct hash_entry *,
79 unsigned long (*hash) PARAMS ((hash_table_key));
80 boolean (*comp) PARAMS ((hash_table_key, hash_table_key));
82 return hash_table_init_n (table, newfunc, hash, comp, DEFAULT_SIZE);
85 /* Free a hash table. */
88 hash_table_free (table)
89 struct hash_table *table;
91 obstack_free (&table->memory, (PTR) NULL);
94 /* Look up KEY in TABLE. If CREATE is non-NULL a new entry is
95 created if one does not previously exist. */
98 hash_lookup (table, key, create, copy)
99 struct hash_table *table;
102 hash_table_key (*copy) PARAMS ((struct obstack* memory,
103 hash_table_key key));
105 register unsigned long hash;
106 struct hash_entry *hashp;
109 hash = (*table->hash)(key);
111 index = hash % table->size;
112 for (hashp = table->table[index];
113 hashp != (struct hash_entry *) NULL;
116 if (hashp->hash == hash
117 && (*table->comp)(hashp->key, key))
122 return (struct hash_entry *) NULL;
124 hashp = (*table->newfunc) ((struct hash_entry *) NULL, table, key);
125 if (hashp == (struct hash_entry *) NULL)
126 return (struct hash_entry *) NULL;
128 key = (*copy) (&table->memory, key);
131 hashp->next = table->table[index];
132 table->table[index] = hashp;
137 /* Base method for creating a new hash table entry. */
141 hash_newfunc (entry, table, p)
142 struct hash_entry *entry;
143 struct hash_table *table;
146 if (entry == (struct hash_entry *) NULL)
147 entry = ((struct hash_entry *)
148 hash_allocate (table, sizeof (struct hash_entry)));
152 /* Allocate space in a hash table. */
155 hash_allocate (table, size)
156 struct hash_table *table;
161 ret = obstack_alloc (&table->memory, size);
162 if (ret == NULL && size != 0)
167 /* Traverse a hash table. */
170 hash_traverse (table, func, info)
171 struct hash_table *table;
172 boolean (*func) PARAMS ((struct hash_entry *, hash_table_key));
177 for (i = 0; i < table->size; i++)
179 struct hash_entry *p;
181 for (p = table->table[i]; p != NULL; p = p->next)
183 if (! (*func) (p, info))
189 /* Hash a string. Return a hash-code for the string. */
195 const unsigned char *s;
200 s = (const unsigned char *) k;
204 while ((c = *s++) != '\0')
206 hash += c + (c << 17);
210 hash += len + (len << 17);
216 /* Compare two strings. Return non-zero iff the two strings are
220 string_compare (k1, k2)
224 return (strcmp ((char*) k1, (char*) k2) == 0);
227 /* Copy K to OBSTACK. */
230 string_copy (memory, k)
231 struct obstack* memory;
235 char *string = (char*) k;
237 new = (char *) obstack_alloc (memory, strlen (string) + 1);
243 strcpy (new, string);