1 /* String pool for GCC.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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 have our own private 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"
36 #include "hashtable.h"
38 /* The "" allocated string. */
39 const char empty_string
[] = "";
41 /* Character strings, each containing a single decimal digit.
42 Written this way to save space. */
43 const char digit_vector
[] = {
44 '0', 0, '1', 0, '2', 0, '3', 0, '4', 0,
45 '5', 0, '6', 0, '7', 0, '8', 0, '9', 0
48 struct ht
*ident_hash
;
49 static struct obstack string_stack
;
51 static hashnode alloc_node
PARAMS ((hash_table
*));
52 static int mark_ident
PARAMS ((struct cpp_reader
*, hashnode
, const PTR
));
54 /* Initialize the string pool. */
58 /* Create with 16K (2^14) entries. */
59 ident_hash
= ht_create (14);
60 ident_hash
->alloc_node
= alloc_node
;
61 gcc_obstack_init (&string_stack
);
64 /* Allocate a hash node. */
67 hash_table
*table ATTRIBUTE_UNUSED
;
69 return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE
));
72 /* Allocate and return a string constant of length LENGTH, containing
73 CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a
74 nul-terminated string, and the length is calculated using strlen.
75 If the same string constant has been allocated before, that copy is
76 returned this time too. */
79 ggc_alloc_string (contents
, length
)
84 length
= strlen (contents
);
88 if (length
== 1 && ISDIGIT (contents
[0]))
89 return digit_string (contents
[0] - '0');
91 obstack_grow0 (&string_stack
, contents
, length
);
92 return obstack_finish (&string_stack
);
95 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
96 If an identifier with that name has previously been referred to,
97 the same node is returned this time. */
100 get_identifier (text
)
103 hashnode ht_node
= ht_lookup (ident_hash
,
104 (const unsigned char *) text
,
105 strlen (text
), HT_ALLOC
);
107 /* ht_node can't be NULL here. */
108 return HT_IDENT_TO_GCC_IDENT (ht_node
);
111 /* Identical to get_identifier, except that the length is assumed
115 get_identifier_with_length (text
, length
)
119 hashnode ht_node
= ht_lookup (ident_hash
,
120 (const unsigned char *) text
,
123 /* ht_node can't be NULL here. */
124 return HT_IDENT_TO_GCC_IDENT (ht_node
);
127 /* If an identifier with the name TEXT (a null-terminated string) has
128 previously been referred to, return that node; otherwise return
132 maybe_get_identifier (text
)
137 ht_node
= ht_lookup (ident_hash
, (const unsigned char *) text
,
138 strlen (text
), HT_NO_INSERT
);
140 return HT_IDENT_TO_GCC_IDENT (ht_node
);
145 /* Report some basic statistics about the string pool. */
148 stringpool_statistics ()
150 ht_dump_statistics (ident_hash
);
153 /* Mark an identifier for GC. */
156 mark_ident (pfile
, h
, v
)
157 struct cpp_reader
*pfile ATTRIBUTE_UNUSED
;
159 const PTR v ATTRIBUTE_UNUSED
;
161 gt_ggc_m_9tree_node (HT_IDENT_TO_GCC_IDENT (h
));
165 /* Mark the trees hanging off the identifier node for GGC. These are
166 handled specially (not using gengtype) because of the special
167 treatment for strings. */
170 ggc_mark_stringpool ()
172 ht_forall (ident_hash
, mark_ident
, NULL
);
175 /* Strings are _not_ GCed, but this routine exists so that a separate
176 roots table isn't needed for the few global variables that refer
181 void *x ATTRIBUTE_UNUSED
;
185 /* Pointer-walking routine for strings (not very interesting, since
186 strings don't contain pointers). */
189 gt_pch_p_S (obj
, x
, op
, cookie
)
190 void *obj ATTRIBUTE_UNUSED
;
191 void *x ATTRIBUTE_UNUSED
;
192 gt_pointer_operator op ATTRIBUTE_UNUSED
;
193 void *cookie ATTRIBUTE_UNUSED
;
197 /* PCH pointer-walking routine for strings. */
203 gt_pch_note_object ((void *)x
, (void *)x
, >_pch_p_S
);
206 /* Handle saving and restoring the string pool for PCH. */
208 struct string_pool_data
GTY(())
210 tree
* GTY((length ("%h.nslots"))) entries
;
212 unsigned int nelements
;
215 static GTY(()) struct string_pool_data
* spd
;
218 gt_pch_save_stringpool ()
222 spd
= ggc_alloc (sizeof (*spd
));
223 spd
->nslots
= ident_hash
->nslots
;
224 spd
->nelements
= ident_hash
->nelements
;
225 spd
->entries
= ggc_alloc (sizeof (tree
*) * spd
->nslots
);
226 for (i
= 0; i
< spd
->nslots
; i
++)
227 if (ident_hash
->entries
[i
] != NULL
)
228 spd
->entries
[i
] = HT_IDENT_TO_GCC_IDENT (ident_hash
->entries
[i
]);
230 spd
->entries
[i
] = NULL
;
234 gt_pch_restore_stringpool ()
238 ident_hash
->nslots
= spd
->nslots
;
239 ident_hash
->nelements
= spd
->nelements
;
240 ident_hash
->entries
= xrealloc (ident_hash
->entries
,
241 sizeof (hashnode
) * spd
->nslots
);
242 for (i
= 0; i
< spd
->nslots
; i
++)
243 if (spd
->entries
[i
] != NULL
)
244 ident_hash
->entries
[i
] = GCC_IDENT_TO_HT_IDENT (spd
->entries
[i
]);
246 ident_hash
->entries
[i
] = NULL
;
251 #include "gt-stringpool.h"