configure.in (GLIBCPP_ENABLE_CXX_FLAGS): Do not pass arguments, let the defaults...
[official-gcc.git] / gcc / stringpool.c
blobf3b34b139cfd59ce7627c0d8d949cc64827bf9a8
1 /* String pool for GCC.
2 Copyright (C) 2000, 2001 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
9 version.
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
14 for more details.
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
19 02111-1307, USA. */
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
28 is faster. */
30 #include "config.h"
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "ggc.h"
35 #include "tree.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));
53 static void mark_ident_hash PARAMS ((void *));
55 /* Initialize the string pool. */
56 void
57 init_stringpool ()
59 /* Create with 16K (2^14) entries. */
60 ident_hash = ht_create (14);
61 ident_hash->alloc_node = alloc_node;
62 gcc_obstack_init (&string_stack);
63 ggc_add_root (&ident_hash, 1, sizeof ident_hash, mark_ident_hash);
66 /* Allocate a hash node. */
67 static hashnode
68 alloc_node (table)
69 hash_table *table ATTRIBUTE_UNUSED;
71 return GCC_IDENT_TO_HT_IDENT (make_node (IDENTIFIER_NODE));
74 /* Allocate and return a string constant of length LENGTH, containing
75 CONTENTS. If LENGTH is -1, CONTENTS is assumed to be a
76 nul-terminated string, and the length is calculated using strlen.
77 If the same string constant has been allocated before, that copy is
78 returned this time too. */
80 const char *
81 ggc_alloc_string (contents, length)
82 const char *contents;
83 int length;
85 if (length == -1)
86 length = strlen (contents);
88 if (length == 0)
89 return empty_string;
90 if (length == 1 && ISDIGIT (contents[0]))
91 return digit_string (contents[0] - '0');
93 obstack_grow0 (&string_stack, contents, length);
94 return obstack_finish (&string_stack);
97 /* Return an IDENTIFIER_NODE whose name is TEXT (a null-terminated string).
98 If an identifier with that name has previously been referred to,
99 the same node is returned this time. */
101 tree
102 get_identifier (text)
103 const char *text;
105 hashnode ht_node = ht_lookup (ident_hash,
106 (const unsigned char *) text,
107 strlen (text), HT_ALLOC);
109 /* ht_node can't be NULL here. */
110 return HT_IDENT_TO_GCC_IDENT (ht_node);
113 /* Identical to get_identifier, except that the length is assumed
114 known. */
116 tree
117 get_identifier_with_length (text, length)
118 const char *text;
119 unsigned int length;
121 hashnode ht_node = ht_lookup (ident_hash,
122 (const unsigned char *) text,
123 length, HT_ALLOC);
125 /* ht_node can't be NULL here. */
126 return HT_IDENT_TO_GCC_IDENT (ht_node);
129 /* If an identifier with the name TEXT (a null-terminated string) has
130 previously been referred to, return that node; otherwise return
131 NULL_TREE. */
133 tree
134 maybe_get_identifier (text)
135 const char *text;
137 hashnode ht_node;
139 ht_node = ht_lookup (ident_hash, (const unsigned char *) text,
140 strlen (text), HT_NO_INSERT);
141 if (ht_node)
142 return HT_IDENT_TO_GCC_IDENT (ht_node);
144 return NULL_TREE;
147 /* Report some basic statistics about the string pool. */
149 void
150 stringpool_statistics ()
152 ht_dump_statistics (ident_hash);
155 /* Mark an identifier for GC. */
157 static int
158 mark_ident (pfile, h, v)
159 struct cpp_reader *pfile ATTRIBUTE_UNUSED;
160 hashnode h;
161 const PTR v ATTRIBUTE_UNUSED;
163 ggc_mark_tree (HT_IDENT_TO_GCC_IDENT (h));
164 return 1;
167 /* Mark all identifiers for GC. */
169 static void
170 mark_ident_hash (arg)
171 PTR arg ATTRIBUTE_UNUSED;
173 ht_forall (ident_hash, mark_ident, NULL);