1 /* String pool for GCC.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* String text, identifier text and identifier node allocator. Strings
22 allocated by ggc_alloc_string are stored in an obstack which is
23 never shrunk. Identifiers are uniquely stored in a hash table.
25 We use cpplib's hash table implementation. libiberty's
26 hashtab.c is not used because it requires 100% average space
27 overhead per string, which is unacceptable. Also, this algorithm
32 #include "coretypes.h"
39 /* The "" allocated string. */
40 const char empty_string
[] = "";
42 /* Character strings, each containing a single decimal digit.
43 Written this way to save space. */
44 const char digit_vector
[] = {
45 '0', 0, '1', 0, '2', 0, '3', 0, '4', 0,
46 '5', 0, '6', 0, '7', 0, '8', 0, '9', 0
49 struct ht
*ident_hash
;
51 static hashnode
alloc_node (hash_table
*);
52 static int mark_ident (struct cpp_reader
*, hashnode
, const void *);
55 stringpool_ggc_alloc (size_t x
)
60 /* Initialize the string pool. */
62 init_stringpool (void)
64 /* Create with 16K (2^14) entries. */
65 ident_hash
= ht_create (14);
66 ident_hash
->alloc_node
= alloc_node
;
67 ident_hash
->alloc_subobject
= stringpool_ggc_alloc
;
70 /* Allocate a hash node. */
72 alloc_node (hash_table
*table ATTRIBUTE_UNUSED
)
74 return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE
));
77 /* Allocate and return a string constant of length LENGTH, containing
78 CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a
79 nul-terminated string, and the length is calculated using strlen.
80 If the same string constant has been allocated before, that copy is
81 returned this time too. */
84 ggc_alloc_string (const char *contents
, int length
)
89 length
= strlen (contents
);
93 if (length
== 1 && ISDIGIT (contents
[0]))
94 return digit_string (contents
[0] - '0');
96 result
= GGC_NEWVAR (char, length
+ 1);
97 memcpy (result
, contents
, length
+ 1);
98 return (const char *) result
;
101 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
102 If an identifier with that name has previously been referred to,
103 the same node is returned this time. */
105 #undef get_identifier
108 get_identifier (const char *text
)
110 hashnode ht_node
= ht_lookup (ident_hash
,
111 (const unsigned char *) text
,
112 strlen (text
), HT_ALLOC
);
114 /* ht_node can't be NULL here. */
115 return HT_IDENT_TO_GCC_IDENT (ht_node
);
118 /* Identical to get_identifier, except that the length is assumed
122 get_identifier_with_length (const char *text
, size_t length
)
124 hashnode ht_node
= ht_lookup (ident_hash
,
125 (const unsigned char *) text
,
128 /* ht_node can't be NULL here. */
129 return HT_IDENT_TO_GCC_IDENT (ht_node
);
132 /* If an identifier with the name TEXT (a null-terminated string) has
133 previously been referred to, return that node; otherwise return
137 maybe_get_identifier (const char *text
)
141 ht_node
= ht_lookup (ident_hash
, (const unsigned char *) text
,
142 strlen (text
), HT_NO_INSERT
);
144 return HT_IDENT_TO_GCC_IDENT (ht_node
);
149 /* Report some basic statistics about the string pool. */
152 stringpool_statistics (void)
154 ht_dump_statistics (ident_hash
);
157 /* Mark an identifier for GC. */
160 mark_ident (struct cpp_reader
*pfile ATTRIBUTE_UNUSED
, hashnode h
,
161 const void *v ATTRIBUTE_UNUSED
)
163 gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h
));
167 /* Return true if an identifier should be removed from the table. */
170 maybe_delete_ident (struct cpp_reader
*pfile ATTRIBUTE_UNUSED
, hashnode h
,
171 const void *v ATTRIBUTE_UNUSED
)
173 return !ggc_marked_p (HT_IDENT_TO_GCC_IDENT (h
));
176 /* Mark the trees hanging off the identifier node for GGC. These are
177 handled specially (not using gengtype) because identifiers are only
178 roots during one part of compilation. */
181 ggc_mark_stringpool (void)
183 ht_forall (ident_hash
, mark_ident
, NULL
);
186 /* Purge the identifier hash of identifiers which are no longer
190 ggc_purge_stringpool (void)
192 ht_purge (ident_hash
, maybe_delete_ident
, NULL
);
195 /* Pointer-walking routine for strings (not very interesting, since
196 strings don't contain pointers). */
199 gt_pch_p_S (void *obj ATTRIBUTE_UNUSED
, void *x ATTRIBUTE_UNUSED
,
200 gt_pointer_operator op ATTRIBUTE_UNUSED
,
201 void *cookie ATTRIBUTE_UNUSED
)
205 /* PCH pointer-walking routine for strings. */
208 gt_pch_n_S (const void *x
)
210 gt_pch_note_object (CONST_CAST (void *, x
), CONST_CAST (void *, x
),
211 >_pch_p_S
, gt_types_enum_last
);
214 /* Handle saving and restoring the string pool for PCH. */
216 /* SPD is saved in the PCH file and holds the information needed
217 to restore the string pool. */
219 struct string_pool_data
GTY(())
221 struct ht_identifier
* *
222 GTY((length ("%h.nslots"),
223 nested_ptr (union tree_node
, "%h ? GCC_IDENT_TO_HT_IDENT (%h) : NULL",
224 "%h ? HT_IDENT_TO_GCC_IDENT (%h) : NULL")))
227 unsigned int nelements
;
230 static GTY(()) struct string_pool_data
* spd
;
232 /* Save the stringpool data in SPD. */
235 gt_pch_save_stringpool (void)
237 spd
= GGC_NEW (struct string_pool_data
);
238 spd
->nslots
= ident_hash
->nslots
;
239 spd
->nelements
= ident_hash
->nelements
;
240 spd
->entries
= GGC_NEWVEC (struct ht_identifier
*, spd
->nslots
);
241 memcpy (spd
->entries
, ident_hash
->entries
,
242 spd
->nslots
* sizeof (spd
->entries
[0]));
245 /* Return the stringpool to its state before gt_pch_save_stringpool
249 gt_pch_fixup_stringpool (void)
253 /* A PCH file has been restored, which loaded SPD; fill the real hash table
257 gt_pch_restore_stringpool (void)
259 ht_load (ident_hash
, spd
->entries
, spd
->nslots
, spd
->nelements
, false);
263 #include "gt-stringpool.h"