]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/hash.c
4149f3a83869f8778abac7aca32efa71fdefe05a
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. */
26 /* Obstack allocation and deallocation routines. */
27 #define obstack_chunk_alloc xmalloc
28 #define obstack_chunk_free free
30 extern char * xmalloc ();
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
, size
)
39 struct hash_table
*table
;
40 struct hash_entry
*(*newfunc
) PARAMS ((struct hash_entry
*,
47 alloc
= size
* sizeof (struct hash_entry
*);
48 if (!obstack_begin (&table
->memory
, alloc
))
53 table
->table
= ((struct hash_entry
**)
54 obstack_alloc (&table
->memory
, alloc
));
60 memset ((PTR
) table
->table
, 0, alloc
);
62 table
->newfunc
= newfunc
;
66 /* Create a new hash table with the default number of entries. */
69 hash_table_init (table
, newfunc
)
70 struct hash_table
*table
;
71 struct hash_entry
*(*newfunc
) PARAMS ((struct hash_entry
*,
75 return hash_table_init_n (table
, newfunc
, DEFAULT_SIZE
);
78 /* Free a hash table. */
81 hash_table_free (table
)
82 struct hash_table
*table
;
84 obstack_free (&table
->memory
, (PTR
) NULL
);
87 /* Look up a string in a hash table. */
90 hash_lookup (table
, string
, create
, copy
)
91 struct hash_table
*table
;
96 register const unsigned char *s
;
97 register unsigned long hash
;
98 register unsigned int c
;
99 struct hash_entry
*hashp
;
105 s
= (const unsigned char *) string
;
106 while ((c
= *s
++) != '\0')
108 hash
+= c
+ (c
<< 17);
112 hash
+= len
+ (len
<< 17);
115 index
= hash
% table
->size
;
116 for (hashp
= table
->table
[index
];
117 hashp
!= (struct hash_entry
*) NULL
;
120 if (hashp
->hash
== hash
121 && strcmp (hashp
->string
, string
) == 0)
126 return (struct hash_entry
*) NULL
;
128 hashp
= (*table
->newfunc
) ((struct hash_entry
*) NULL
, table
, string
);
129 if (hashp
== (struct hash_entry
*) NULL
)
130 return (struct hash_entry
*) NULL
;
135 new = (char *) obstack_alloc (&table
->memory
, len
+ 1);
139 return (struct hash_entry
*) NULL
;
141 strcpy (new, string
);
144 hashp
->string
= string
;
146 hashp
->next
= table
->table
[index
];
147 table
->table
[index
] = hashp
;
152 /* Base method for creating a new hash table entry. */
156 hash_newfunc (entry
, table
, string
)
157 struct hash_entry
*entry
;
158 struct hash_table
*table
;
161 if (entry
== (struct hash_entry
*) NULL
)
162 entry
= ((struct hash_entry
*)
163 hash_allocate (table
, sizeof (struct hash_entry
)));
167 /* Allocate space in a hash table. */
170 hash_allocate (table
, size
)
171 struct hash_table
*table
;
176 ret
= obstack_alloc (&table
->memory
, size
);
177 if (ret
== NULL
&& size
!= 0)
182 /* Traverse a hash table. */
185 hash_traverse (table
, func
, info
)
186 struct hash_table
*table
;
187 boolean (*func
) PARAMS ((struct hash_entry
*, PTR
));
192 for (i
= 0; i
< table
->size
; i
++)
194 struct hash_entry
*p
;
196 for (p
= table
->table
[i
]; p
!= NULL
; p
= p
->next
)
198 if (! (*func
) (p
, info
))