]> git.saurik.com Git - bison.git/blob - src/uniqstr.c
(nullable_compute): Do not subtract from the returned value of malloc.
[bison.git] / src / uniqstr.c
1 /* Keeping a unique copy of strings.
2 Copyright (C) 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "system.h"
22
23 #include <error.h>
24 #include <hash.h>
25 #include <quotearg.h>
26
27 #include "uniqstr.h"
28
29 /*-----------------------.
30 | A uniqstr hash table. |
31 `-----------------------*/
32
33 /* Initial capacity of uniqstr hash table. */
34 #define HT_INITIAL_CAPACITY 257
35
36 static struct hash_table *uniqstrs_table = NULL;
37
38 /*-------------------------------------.
39 | Create the uniqstr for S if needed. |
40 `-------------------------------------*/
41
42 uniqstr
43 uniqstr_new (const char *s)
44 {
45 uniqstr res = hash_lookup (uniqstrs_table, s);
46 if (!res)
47 {
48 /* First insertion in the hash. */
49 res = xstrdup (s);
50 hash_insert (uniqstrs_table, res);
51 }
52 return res;
53 }
54
55
56 /*------------------------------.
57 | Abort if S is not a uniqstr. |
58 `------------------------------*/
59
60 void
61 uniqstr_assert (const char *s)
62 {
63 if (!hash_lookup (uniqstrs_table, s))
64 {
65 error (0, 0, "not a uniqstr: %s", quotearg (s));
66 abort ();
67 }
68 }
69
70
71 /*--------------------.
72 | Print the uniqstr. |
73 `--------------------*/
74
75 static bool
76 uniqstr_print (const uniqstr s)
77 {
78 fprintf (stderr, "%s\n", s);
79 return true;
80 }
81
82 \f
83 /*-----------------------.
84 | A uniqstr hash table. |
85 `-----------------------*/
86
87 static bool
88 hash_compare_uniqstr (const uniqstr m1, const uniqstr m2)
89 {
90 return strcmp (m1, m2) == 0;
91 }
92
93 static unsigned int
94 hash_uniqstr (const uniqstr m, unsigned int tablesize)
95 {
96 return hash_string (m, tablesize);
97 }
98
99 /* A function to apply to each symbol. */
100 typedef bool (*uniqstr_processor) (const uniqstr);
101
102 /*----------------------------.
103 | Create the uniqstrs table. |
104 `----------------------------*/
105
106 void
107 uniqstrs_new (void)
108 {
109 uniqstrs_table = hash_initialize (HT_INITIAL_CAPACITY,
110 NULL,
111 (Hash_hasher) hash_uniqstr,
112 (Hash_comparator) hash_compare_uniqstr,
113 (Hash_data_freer) free);
114 }
115
116
117 /*-------------------------------------.
118 | Perform a task on all the uniqstrs. |
119 `-------------------------------------*/
120
121 static void
122 uniqstrs_do (uniqstr_processor processor, void *processor_data)
123 {
124 hash_do_for_each (uniqstrs_table,
125 (Hash_processor) processor,
126 processor_data);
127 }
128
129
130 /*-----------------.
131 | Print them all. |
132 `-----------------*/
133
134 void
135 uniqstrs_print (void)
136 {
137 uniqstrs_do (uniqstr_print, NULL);
138 }
139
140
141 /*--------------------.
142 | Free the uniqstrs. |
143 `--------------------*/
144
145 void
146 uniqstrs_free (void)
147 {
148 hash_free (uniqstrs_table);
149 }