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 #include "gansidecl.h"
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)
37 /* Create a new hash table, given a number of entries. */
40 hash_table_init_n (table
, newfunc
, size
)
41 struct hash_table
*table
;
42 struct hash_entry
*(*newfunc
) PARAMS ((struct hash_entry
*,
49 alloc
= size
* sizeof (struct hash_entry
*);
50 if (!obstack_begin (&table
->memory
, alloc
))
55 table
->table
= ((struct hash_entry
**)
56 obstack_alloc (&table
->memory
, alloc
));
62 memset ((PTR
) table
->table
, 0, alloc
);
64 table
->newfunc
= newfunc
;
68 /* Create a new hash table with the default number of entries. */
71 hash_table_init (table
, newfunc
)
72 struct hash_table
*table
;
73 struct hash_entry
*(*newfunc
) PARAMS ((struct hash_entry
*,
77 return hash_table_init_n (table
, newfunc
, DEFAULT_SIZE
);
80 /* Free a hash table. */
83 hash_table_free (table
)
84 struct hash_table
*table
;
86 obstack_free (&table
->memory
, (PTR
) NULL
);
89 /* Look up a string in a hash table. */
92 hash_lookup (table
, string
, create
, copy
)
93 struct hash_table
*table
;
98 register const unsigned char *s
;
99 register unsigned long hash
;
100 register unsigned int c
;
101 struct hash_entry
*hashp
;
107 s
= (const unsigned char *) string
;
108 while ((c
= *s
++) != '\0')
110 hash
+= c
+ (c
<< 17);
114 hash
+= len
+ (len
<< 17);
117 index
= hash
% table
->size
;
118 for (hashp
= table
->table
[index
];
119 hashp
!= (struct hash_entry
*) NULL
;
122 if (hashp
->hash
== hash
123 && strcmp (hashp
->string
, string
) == 0)
128 return (struct hash_entry
*) NULL
;
130 hashp
= (*table
->newfunc
) ((struct hash_entry
*) NULL
, table
, string
);
131 if (hashp
== (struct hash_entry
*) NULL
)
132 return (struct hash_entry
*) NULL
;
137 new = (char *) obstack_alloc (&table
->memory
, len
+ 1);
141 return (struct hash_entry
*) NULL
;
143 strcpy (new, string
);
146 hashp
->string
= string
;
148 hashp
->next
= table
->table
[index
];
149 table
->table
[index
] = hashp
;
154 /* Base method for creating a new hash table entry. */
158 hash_newfunc (entry
, table
, string
)
159 struct hash_entry
*entry
;
160 struct hash_table
*table
;
163 if (entry
== (struct hash_entry
*) NULL
)
164 entry
= ((struct hash_entry
*)
165 hash_allocate (table
, sizeof (struct hash_entry
)));
169 /* Allocate space in a hash table. */
172 hash_allocate (table
, size
)
173 struct hash_table
*table
;
178 ret
= obstack_alloc (&table
->memory
, size
);
179 if (ret
== NULL
&& size
!= 0)
184 /* Traverse a hash table. */
187 hash_traverse (table
, func
, info
)
188 struct hash_table
*table
;
189 boolean (*func
) PARAMS ((struct hash_entry
*, PTR
));
194 for (i
= 0; i
< table
->size
; i
++)
196 struct hash_entry
*p
;
198 for (p
= table
->table
[i
]; p
!= NULL
; p
= p
->next
)
200 if (! (*func
) (p
, info
))