* configure.ac: Don't test for [build] __cxa_atexit when building a
[official-gcc.git] / gcc / varray.c
blobc5c3a8ec4201df123553e473ceeedc01b0eb375a
1 /* Virtual array support.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Solutions.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
23 /* This file is compiled twice: once for the generator programs
24 once for the compiler. */
25 #ifdef GENERATOR_FILE
26 #include "bconfig.h"
27 #else
28 #include "config.h"
29 #endif
31 #include "system.h"
32 #include "coretypes.h"
33 #include "tm.h"
34 #include "errors.h"
35 #include "varray.h"
36 #include "ggc.h"
37 #include "hashtab.h"
39 #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data))
41 #ifdef GATHER_STATISTICS
43 /* Store information about each particular varray. */
44 struct varray_descriptor
46 const char *name;
47 int allocated;
48 int created;
49 int resized;
50 int copied;
53 /* Hashtable mapping varray names to descriptors. */
54 static htab_t varray_hash;
56 /* Hashtable helpers. */
57 static hashval_t
58 hash_descriptor (const void *p)
60 const struct varray_descriptor *d = p;
61 return htab_hash_pointer (d->name);
63 static int
64 eq_descriptor (const void *p1, const void *p2)
66 const struct varray_descriptor *d = p1;
67 return d->name == p2;
70 /* For given name, return descriptor, create new if needed. */
71 static struct varray_descriptor *
72 varray_descriptor (const char *name)
74 struct varray_descriptor **slot;
76 if (!varray_hash)
77 varray_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
79 slot = (struct varray_descriptor **)
80 htab_find_slot_with_hash (varray_hash, name,
81 htab_hash_pointer (name),
82 1);
83 if (*slot)
84 return *slot;
85 *slot = xcalloc (sizeof (**slot), 1);
86 (*slot)->name = name;
87 return *slot;
89 #endif
91 /* Do not add any more non-GC items here. Please either remove or GC
92 those items that are not GCed. */
94 static const struct {
95 unsigned char size;
96 bool uses_ggc;
97 } element[NUM_VARRAY_DATA] = {
98 { sizeof (char), 1 },
99 { sizeof (unsigned char), 1 },
100 { sizeof (short), 1 },
101 { sizeof (unsigned short), 1 },
102 { sizeof (int), 1 },
103 { sizeof (unsigned int), 1 },
104 { sizeof (long), 1 },
105 { sizeof (unsigned long), 1 },
106 { sizeof (HOST_WIDE_INT), 1 },
107 { sizeof (unsigned HOST_WIDE_INT), 1 },
108 { sizeof (void *), 1 },
109 { sizeof (void *), 0 },
110 { sizeof (char *), 1 },
111 { sizeof (struct rtx_def *), 1 },
112 { sizeof (struct rtvec_def *), 1 },
113 { sizeof (union tree_node *), 1 },
114 { sizeof (struct bitmap_head_def *), 1 },
115 { sizeof (struct reg_info_def *), 0 },
116 { sizeof (struct const_equiv_data), 0 },
117 { sizeof (struct basic_block_def *), 1 },
118 { sizeof (struct elt_list *), 1 },
119 { sizeof (struct edge_def *), 1 },
120 { sizeof (tree *), 1 },
123 /* Allocate a virtual array with NUM_ELEMENT elements, each of which is
124 ELEMENT_SIZE bytes long, named NAME. Array elements are zeroed. */
125 varray_type
126 varray_init (size_t num_elements, enum varray_data_enum element_kind,
127 const char *name)
129 size_t data_size = num_elements * element[element_kind].size;
130 varray_type ptr;
131 #ifdef GATHER_STATISTICS
132 struct varray_descriptor *desc = varray_descriptor (name);
134 desc->created++;
135 desc->allocated += data_size + VARRAY_HDR_SIZE;
136 #endif
137 if (element[element_kind].uses_ggc)
138 ptr = ggc_alloc_cleared (VARRAY_HDR_SIZE + data_size);
139 else
140 ptr = xcalloc (VARRAY_HDR_SIZE + data_size, 1);
142 ptr->num_elements = num_elements;
143 ptr->elements_used = 0;
144 ptr->type = element_kind;
145 ptr->name = name;
146 return ptr;
149 /* Grow/shrink the virtual array VA to N elements. Zero any new elements
150 allocated. */
151 varray_type
152 varray_grow (varray_type va, size_t n)
154 size_t old_elements = va->num_elements;
155 if (n != old_elements)
157 size_t elem_size = element[va->type].size;
158 size_t old_data_size = old_elements * elem_size;
159 size_t data_size = n * elem_size;
160 #ifdef GATHER_STATISTICS
161 struct varray_descriptor *desc = varray_descriptor (va->name);
162 varray_type oldva = va;
164 if (data_size > old_data_size)
165 desc->allocated += data_size - old_data_size;
166 desc->resized ++;
167 #endif
170 if (element[va->type].uses_ggc)
171 va = ggc_realloc (va, VARRAY_HDR_SIZE + data_size);
172 else
173 va = xrealloc (va, VARRAY_HDR_SIZE + data_size);
174 va->num_elements = n;
175 if (n > old_elements)
176 memset (&va->data.c[old_data_size], 0, data_size - old_data_size);
177 #ifdef GATHER_STATISTICS
178 if (oldva != va)
179 desc->copied++;
180 #endif
183 return va;
186 /* Reset a varray to its original state. */
187 void
188 varray_clear (varray_type va)
190 size_t data_size = element[va->type].size * va->num_elements;
192 memset (va->data.c, 0, data_size);
193 va->elements_used = 0;
196 /* Check the bounds of a varray access. */
198 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
200 void
201 varray_check_failed (varray_type va, size_t n, const char *file, int line,
202 const char *function)
204 internal_error ("virtual array %s[%lu]: element %lu out of bounds "
205 "in %s, at %s:%d",
206 va->name, (unsigned long) va->num_elements, (unsigned long) n,
207 function, trim_filename (file), line);
210 void
211 varray_underflow (varray_type va, const char *file, int line,
212 const char *function)
214 internal_error ("underflowed virtual array %s in %s, at %s:%d",
215 va->name, function, trim_filename (file), line);
218 #endif
221 /* Copy varray V2 into varray V1. Both arrays must be the same size
222 and type. */
224 void
225 varray_copy (varray_type v1, varray_type v2)
227 size_t data_size;
229 if (v1->type != v2->type)
230 abort ();
232 if (v1->num_elements != v2->num_elements)
233 abort ();
235 data_size = element[v2->type].size * v2->num_elements;
236 memcpy (v1->data.c, v2->data.c, data_size);
237 v1->elements_used = v2->elements_used;
240 /* Output per-varray statistics. */
241 #ifdef GATHER_STATISTICS
243 /* Used to accumulate statistics about varray sizes. */
244 struct output_info
246 int count;
247 int size;
250 /* Called via htab_traverse. Output varray descriptor pointed out by SLOT
251 and update statistics. */
252 static int
253 print_statistics (void **slot, void *b)
255 struct varray_descriptor *d = (struct varray_descriptor *) *slot;
256 struct output_info *i = (struct output_info *) b;
258 if (d->allocated)
260 fprintf (stderr, "%-21s %6d %10d %7d %7d\n", d->name,
261 d->created, d->allocated, d->resized, d->copied);
262 i->size += d->allocated;
263 i->count += d->created;
265 return 1;
267 #endif
269 /* Output per-varray memory usage statistics. */
270 void dump_varray_statistics (void)
272 #ifdef GATHER_STATISTICS
273 struct output_info info;
275 fprintf (stderr, "\nVARRAY Kind Count Bytes Resized copied\n");
276 fprintf (stderr, "-------------------------------------------------------\n");
277 info.count = 0;
278 info.size = 0;
279 htab_traverse (varray_hash, print_statistics, &info);
280 fprintf (stderr, "-------------------------------------------------------\n");
281 fprintf (stderr, "%-20s %7d %10d\n",
282 "Total", info.count, info.size);
283 fprintf (stderr, "-------------------------------------------------------\n");
284 #endif