]>
Commit | Line | Data |
---|---|---|
ac72f3e9 PE |
1 | /* Keep a unique copy of strings. |
2 | ||
04098407 | 3 | Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. |
5bab03fc PE |
4 | |
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | Bison 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) | |
10 | any later version. | |
11 | ||
12 | Bison 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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Bison; see the file COPYING. If not, write to | |
0fb669f9 PE |
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
20 | Boston, MA 02110-1301, USA. */ | |
5bab03fc | 21 | |
2cec9080 | 22 | #include <config.h> |
5bab03fc PE |
23 | #include "system.h" |
24 | ||
25 | #include <error.h> | |
26 | #include <hash.h> | |
27 | #include <quotearg.h> | |
28 | ||
29 | #include "uniqstr.h" | |
30 | ||
31 | /*-----------------------. | |
32 | | A uniqstr hash table. | | |
33 | `-----------------------*/ | |
34 | ||
35 | /* Initial capacity of uniqstr hash table. */ | |
36 | #define HT_INITIAL_CAPACITY 257 | |
37 | ||
38 | static struct hash_table *uniqstrs_table = NULL; | |
39 | ||
40 | /*-------------------------------------. | |
41 | | Create the uniqstr for S if needed. | | |
42 | `-------------------------------------*/ | |
43 | ||
44 | uniqstr | |
a737b216 | 45 | uniqstr_new (char const *str) |
5bab03fc | 46 | { |
a737b216 | 47 | uniqstr res = hash_lookup (uniqstrs_table, str); |
5bab03fc PE |
48 | if (!res) |
49 | { | |
50 | /* First insertion in the hash. */ | |
a737b216 | 51 | res = xstrdup (str); |
5bab03fc PE |
52 | hash_insert (uniqstrs_table, res); |
53 | } | |
54 | return res; | |
55 | } | |
56 | ||
57 | ||
58 | /*------------------------------. | |
59 | | Abort if S is not a uniqstr. | | |
60 | `------------------------------*/ | |
61 | ||
62 | void | |
a737b216 | 63 | uniqstr_assert (char const *str) |
5bab03fc | 64 | { |
a737b216 | 65 | if (!hash_lookup (uniqstrs_table, str)) |
5bab03fc | 66 | { |
04098407 PE |
67 | error (0, 0, |
68 | "not a uniqstr: %s", quotearg (str)); | |
5bab03fc PE |
69 | abort (); |
70 | } | |
71 | } | |
72 | ||
73 | ||
74 | /*--------------------. | |
75 | | Print the uniqstr. | | |
76 | `--------------------*/ | |
77 | ||
ac72f3e9 | 78 | static inline bool |
a737b216 | 79 | uniqstr_print (uniqstr ustr) |
5bab03fc | 80 | { |
a737b216 | 81 | fprintf (stderr, "%s\n", ustr); |
5bab03fc PE |
82 | return true; |
83 | } | |
84 | ||
ac72f3e9 | 85 | static bool |
a737b216 | 86 | uniqstr_print_processor (void *ustr, void *null ATTRIBUTE_UNUSED) |
ac72f3e9 | 87 | { |
a737b216 | 88 | return uniqstr_print (ustr); |
ac72f3e9 PE |
89 | } |
90 | ||
5bab03fc PE |
91 | \f |
92 | /*-----------------------. | |
93 | | A uniqstr hash table. | | |
94 | `-----------------------*/ | |
95 | ||
96 | static bool | |
ac72f3e9 | 97 | hash_compare_uniqstr (void const *m1, void const *m2) |
5bab03fc PE |
98 | { |
99 | return strcmp (m1, m2) == 0; | |
100 | } | |
101 | ||
233a88ad PE |
102 | static size_t |
103 | hash_uniqstr (void const *m, size_t tablesize) | |
5bab03fc PE |
104 | { |
105 | return hash_string (m, tablesize); | |
106 | } | |
107 | ||
5bab03fc PE |
108 | /*----------------------------. |
109 | | Create the uniqstrs table. | | |
110 | `----------------------------*/ | |
111 | ||
112 | void | |
113 | uniqstrs_new (void) | |
114 | { | |
115 | uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY, | |
116 | NULL, | |
ac72f3e9 PE |
117 | hash_uniqstr, |
118 | hash_compare_uniqstr, | |
119 | free); | |
5bab03fc PE |
120 | } |
121 | ||
122 | ||
123 | /*-------------------------------------. | |
124 | | Perform a task on all the uniqstrs. | | |
125 | `-------------------------------------*/ | |
126 | ||
127 | static void | |
ac72f3e9 | 128 | uniqstrs_do (Hash_processor processor, void *processor_data) |
5bab03fc | 129 | { |
ac72f3e9 | 130 | hash_do_for_each (uniqstrs_table, processor, processor_data); |
5bab03fc PE |
131 | } |
132 | ||
133 | ||
134 | /*-----------------. | |
135 | | Print them all. | | |
136 | `-----------------*/ | |
137 | ||
138 | void | |
139 | uniqstrs_print (void) | |
140 | { | |
ac72f3e9 | 141 | uniqstrs_do (uniqstr_print_processor, NULL); |
5bab03fc PE |
142 | } |
143 | ||
144 | ||
145 | /*--------------------. | |
146 | | Free the uniqstrs. | | |
147 | `--------------------*/ | |
148 | ||
149 | void | |
150 | uniqstrs_free (void) | |
151 | { | |
152 | hash_free (uniqstrs_table); | |
153 | } |