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)
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. */
26 #include "coretypes.h"
32 #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data))
34 #ifdef GATHER_STATISTICS
36 /* Store infromation about each particular varray. */
37 struct varray_descriptor
46 /* Hashtable mapping varray names to descriptors. */
47 static htab_t varray_hash;
49 /* Hashtable helpers. */
51 hash_descriptor (const void *p)
53 const struct varray_descriptor *d = p;
54 return htab_hash_pointer (d->name);
57 eq_descriptor (const void *p1, const void *p2)
59 const struct varray_descriptor *d = p1;
63 /* For given name, return descriptor, create new if needed. */
64 static struct varray_descriptor *
65 varray_descriptor (const char *name)
67 struct varray_descriptor **slot;
70 varray_hash = htab_create (10, hash_descriptor, eq_descriptor, NULL);
72 slot = (struct varray_descriptor **)
73 htab_find_slot_with_hash (varray_hash, name,
74 htab_hash_pointer (name),
78 *slot = xcalloc (sizeof (**slot), 1);
84 /* Do not add any more non-GC items here. Please either remove or GC
85 those items that are not GCed. */
90 } element[NUM_VARRAY_DATA] = {
92 { sizeof (unsigned char), 1 },
93 { sizeof (short), 1 },
94 { sizeof (unsigned short), 1 },
96 { sizeof (unsigned int), 1 },
98 { sizeof (unsigned long), 1 },
99 { sizeof (HOST_WIDE_INT), 1 },
100 { sizeof (unsigned HOST_WIDE_INT), 1 },
101 { sizeof (void *), 1 },
102 { sizeof (char *), 1 },
103 { sizeof (struct rtx_def *), 1 },
104 { sizeof (struct rtvec_def *), 1 },
105 { sizeof (union tree_node *), 1 },
106 { sizeof (struct bitmap_head_def *), 1 },
107 { sizeof (struct reg_info_def *), 0 },
108 { sizeof (struct const_equiv_data), 0 },
109 { sizeof (struct basic_block_def *), 0 },
110 { sizeof (struct elt_list *), 1 },
113 /* Allocate a virtual array with NUM_ELEMENT elements, each of which is
114 ELEMENT_SIZE bytes long, named NAME. Array elements are zeroed. */
116 varray_init (size_t num_elements, enum varray_data_enum element_kind,
119 size_t data_size = num_elements * element[element_kind].size;
121 #ifdef GATHER_STATISTICS
122 struct varray_descriptor *desc = varray_descriptor (name);
125 desc->allocated += data_size + VARRAY_HDR_SIZE;
127 if (element[element_kind].uses_ggc)
128 ptr = ggc_alloc_cleared (VARRAY_HDR_SIZE + data_size);
130 ptr = xcalloc (VARRAY_HDR_SIZE + data_size, 1);
132 ptr->num_elements = num_elements;
133 ptr->elements_used = 0;
134 ptr->type = element_kind;
139 /* Grow/shrink the virtual array VA to N elements. Zero any new elements
142 varray_grow (varray_type va, size_t n)
144 size_t old_elements = va->num_elements;
145 if (n != old_elements)
147 size_t elem_size = element[va->type].size;
148 size_t old_data_size = old_elements * elem_size;
149 size_t data_size = n * elem_size;
150 #ifdef GATHER_STATISTICS
151 struct varray_descriptor *desc = varray_descriptor (va->name);
152 varray_type oldva = va;
154 if (data_size > old_data_size)
155 desc->allocated += data_size - old_data_size;
160 if (element[va->type].uses_ggc)
161 va = ggc_realloc (va, VARRAY_HDR_SIZE + data_size);
163 va = xrealloc (va, VARRAY_HDR_SIZE + data_size);
164 va->num_elements = n;
165 if (n > old_elements)
166 memset (&va->data.c[old_data_size], 0, data_size - old_data_size);
167 #ifdef GATHER_STATISTICS
176 /* Reset a varray to its original state. */
178 varray_clear (varray_type va)
180 size_t data_size = element[va->type].size * va->num_elements;
182 memset (va->data.c, 0, data_size);
183 va->elements_used = 0;
186 /* Check the bounds of a varray access. */
188 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
191 varray_check_failed (varray_type va, size_t n, const char *file, int line,
192 const char *function)
194 internal_error ("virtual array %s[%lu]: element %lu out of bounds "
196 va->name, (unsigned long) va->num_elements, (unsigned long) n,
197 function, trim_filename (file), line);
201 varray_underflow (varray_type va, const char *file, int line,
202 const char *function)
204 internal_error ("underflowed virtual array %s in %s, at %s:%d",
205 va->name, function, trim_filename (file), line);
210 /* Output per-varray statistics. */
211 #ifdef GATHER_STATISTICS
218 print_statistics (void **slot, void *b)
220 struct varray_descriptor *d = (struct varray_descriptor *) *slot;
221 struct output_info *i = (struct output_info *) b;
225 fprintf (stderr, "%-21s %6d %10d %7d %7d\n", d->name,
226 d->created, d->allocated, d->resized, d->copied);
227 i->size += d->allocated;
228 i->count += d->created;
233 void dump_varray_statistics (void)
235 #ifdef GATHER_STATISTICS
236 struct output_info info;
238 fprintf (stderr, "\nVARRAY Kind Count Bytes Resized copied\n");
239 fprintf (stderr, "-------------------------------------------------------\n");
242 htab_traverse (varray_hash, print_statistics, &info);
243 fprintf (stderr, "-------------------------------------------------------\n");
244 fprintf (stderr, "%-20s %7d %10d\n",
245 "Total", info.count, info.size);
246 fprintf (stderr, "-------------------------------------------------------\n");