1 /* hash.c -- hash table routines
2 Copyright (C) 1993, 1994, 1998, 2001 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, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
28 /* Obstack allocation and deallocation routines. */
29 #define obstack_chunk_alloc xmalloc
30 #define obstack_chunk_free free
32 /* The default number of entries to use when creating a hash table. */
33 #define DEFAULT_SIZE 1009
35 /* Create a new hash table, given a number of entries. */
38 hash_table_init_n (table
, newfunc
, hash
, comp
, size
)
39 struct hash_table
*table
;
40 struct hash_entry
*(*newfunc
) PARAMS ((struct hash_entry
*,
43 unsigned long (*hash
) PARAMS ((hash_table_key
));
44 bool (*comp
) PARAMS ((hash_table_key
, hash_table_key
));
49 alloc
= size
* sizeof (struct hash_entry
*);
50 obstack_begin (&table
->memory
, alloc
);
51 table
->table
= ((struct hash_entry
**)
52 obstack_alloc (&table
->memory
, alloc
));
53 memset ((PTR
) table
->table
, 0, alloc
);
55 table
->newfunc
= newfunc
;
60 /* Create a new hash table with the default number of entries. */
63 hash_table_init (table
, newfunc
, hash
, comp
)
64 struct hash_table
*table
;
65 struct hash_entry
*(*newfunc
) PARAMS ((struct hash_entry
*,
68 unsigned long (*hash
) PARAMS ((hash_table_key
));
69 bool (*comp
) PARAMS ((hash_table_key
, hash_table_key
));
71 hash_table_init_n (table
, newfunc
, hash
, comp
, DEFAULT_SIZE
);
74 /* Free a hash table. */
77 hash_table_free (table
)
78 struct hash_table
*table
;
80 obstack_free (&table
->memory
, (PTR
) NULL
);
83 /* Look up KEY in TABLE. If CREATE is non-NULL a new entry is
84 created if one does not previously exist. */
87 hash_lookup (table
, key
, create
, copy
)
88 struct hash_table
*table
;
91 hash_table_key (*copy
) PARAMS ((struct obstack
* memory
,
95 struct hash_entry
*hashp
;
98 hash
= (*table
->hash
)(key
);
100 index
= hash
% table
->size
;
101 for (hashp
= table
->table
[index
]; hashp
!= 0; hashp
= hashp
->next
)
102 if (hashp
->hash
== hash
103 && (*table
->comp
)(hashp
->key
, key
))
109 hashp
= (*table
->newfunc
) ((struct hash_entry
*) NULL
, table
, key
);
114 key
= (*copy
) (&table
->memory
, key
);
118 hashp
->next
= table
->table
[index
];
119 table
->table
[index
] = hashp
;
124 /* Base method for creating a new hash table entry. */
127 hash_newfunc (entry
, table
, p
)
128 struct hash_entry
*entry
;
129 struct hash_table
*table
;
130 hash_table_key p ATTRIBUTE_UNUSED
;
133 entry
= ((struct hash_entry
*)
134 hash_allocate (table
, sizeof (struct hash_entry
)));
138 /* Allocate space in a hash table. */
141 hash_allocate (table
, size
)
142 struct hash_table
*table
;
145 return obstack_alloc (&table
->memory
, size
);
148 /* Traverse a hash table. */
151 hash_traverse (table
, func
, info
)
152 struct hash_table
*table
;
153 bool (*func
) PARAMS ((struct hash_entry
*, hash_table_key
));
157 struct hash_entry
*p
;
159 for (i
= 0; i
< table
->size
; i
++)
160 for (p
= table
->table
[i
]; p
!= 0; p
= p
->next
)
161 if (! (*func
) (p
, info
))
165 /* Hash a string. Return a hash-code for the string. */
171 const unsigned char *s
;
176 s
= (const unsigned char *) k
;
180 while ((c
= *s
++) != '\0')
182 hash
+= c
+ (c
<< 17);
187 hash
+= len
+ (len
<< 17);
193 /* Compare two strings. Return non-zero iff the two strings are
197 string_compare (k1
, k2
)
201 return (strcmp ((char*) k1
, (char*) k2
) == 0);
204 /* Copy K to OBSTACK. */
207 string_copy (memory
, k
)
208 struct obstack
*memory
;
212 char *string
= (char *) k
;
214 new = (char *) obstack_alloc (memory
, strlen (string
) + 1);
215 strcpy (new, string
);