1 /* hash.c -- hash table routines for BFD
2 Copyright 1993 Free Software Foundation, Inc.
3 Written by Steve Chamberlain <sac@cygnus.com>
5 This file is part of GLD, the Gnu Linker.
7 GLD 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, or (at your option)
12 GLD 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 GLD; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 /* Obstack allocation and deallocation routines. */
27 #define obstack_chunk_alloc bfd_xmalloc_by_size_t
28 #define obstack_chunk_free free
30 /* The default number of entries to use when creating a hash table. */
31 #define DEFAULT_SIZE (4051)
33 /* Create a new hash table, given a number of entries. */
36 bfd_hash_table_init_n (table, newfunc, size)
37 struct bfd_hash_table *table;
38 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
39 struct bfd_hash_table *,
45 alloc = size * sizeof (struct bfd_hash_entry *);
46 obstack_begin (&table->memory, alloc);
47 table->table = ((struct bfd_hash_entry **)
48 obstack_alloc (&table->memory, alloc));
49 memset ((PTR) table->table, 0, alloc);
51 table->newfunc = newfunc;
55 /* Create a new hash table with the default number of entries. */
58 bfd_hash_table_init (table, newfunc)
59 struct bfd_hash_table *table;
60 struct bfd_hash_entry *(*newfunc) PARAMS ((struct bfd_hash_entry *,
61 struct bfd_hash_table *,
64 return bfd_hash_table_init_n (table, newfunc, DEFAULT_SIZE);
67 /* Free a hash table. */
70 bfd_hash_table_free (table)
71 struct bfd_hash_table *table;
73 obstack_free (&table->memory, (PTR) NULL);
76 /* Look up a string in a hash table. */
78 struct bfd_hash_entry *
79 bfd_hash_lookup (table, string, create, copy)
80 struct bfd_hash_table *table;
85 register const unsigned char *s;
86 register unsigned long hash;
87 register unsigned int c;
88 struct bfd_hash_entry *hashp;
94 s = (const unsigned char *) string;
95 while ((c = *s++) != '\0')
97 hash += c + (c << 17);
101 hash += len + (len << 17);
104 index = hash % table->size;
105 for (hashp = table->table[index];
106 hashp != (struct bfd_hash_entry *) NULL;
109 if (hashp->hash == hash
110 && strcmp (hashp->string, string) == 0)
115 return (struct bfd_hash_entry *) NULL;
117 hashp = (*table->newfunc) ((struct bfd_hash_entry *) NULL, table, string);
118 if (hashp == (struct bfd_hash_entry *) NULL)
119 return (struct bfd_hash_entry *) NULL;
124 new = (char *) obstack_alloc (&table->memory, len + 1);
125 strcpy (new, string);
128 hashp->string = string;
130 hashp->next = table->table[index];
131 table->table[index] = hashp;
136 /* Base method for creating a new hash table entry. */
139 struct bfd_hash_entry *
140 bfd_hash_newfunc (entry, table, string)
141 struct bfd_hash_entry *entry;
142 struct bfd_hash_table *table;
145 if (entry == (struct bfd_hash_entry *) NULL)
146 entry = ((struct bfd_hash_entry *)
147 bfd_hash_allocate (table, sizeof (struct bfd_hash_entry)));
151 /* Allocate space in a hash table. */
154 bfd_hash_allocate (table, size)
155 struct bfd_hash_table *table;
158 return obstack_alloc (&table->memory, size);
161 /* Traverse a hash table. */
164 bfd_hash_traverse (table, func, info)
165 struct bfd_hash_table *table;
166 boolean (*func) PARAMS ((struct bfd_hash_entry *, PTR));
171 for (i = 0; i < table->size; i++)
173 struct bfd_hash_entry *p;
175 for (p = table->table[i]; p != NULL; p = p->next)
177 if (! (*func) (p, info))