2003-04-11 Eric Christopher <echristo@redhat.com>
[official-gcc.git] / gcc / varray.c
blob8eb6a6235203536638b1de09929e703b40ad1426
1 /* Virtual array support.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
20 MA 02111-1307, USA. */
22 #include "config.h"
23 #include "errors.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "varray.h"
28 #include "ggc.h"
30 #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data))
32 /* Do not add any more non-GC items here. Please either remove or GC those items that
33 are not GCed. */
35 static const struct {
36 unsigned char size;
37 bool uses_ggc;
38 } element[NUM_VARRAY_DATA] = {
39 { sizeof (char), 1 },
40 { sizeof (unsigned char), 1 },
41 { sizeof (short), 1 },
42 { sizeof (unsigned short), 1 },
43 { sizeof (int), 1 },
44 { sizeof (unsigned int), 1 },
45 { sizeof (long), 1 },
46 { sizeof (unsigned long), 1 },
47 { sizeof (HOST_WIDE_INT), 1 },
48 { sizeof (unsigned HOST_WIDE_INT), 1 },
49 { sizeof (PTR), 1 },
50 { sizeof (char *), 1 },
51 { sizeof (struct rtx_def *), 1 },
52 { sizeof (struct rtvec_def *), 1 },
53 { sizeof (union tree_node *), 1 },
54 { sizeof (struct bitmap_head_def *), 1 },
55 { sizeof (struct reg_info_def *), 0 },
56 { sizeof (struct const_equiv_data), 0 },
57 { sizeof (struct basic_block_def *), 0 },
58 { sizeof (struct elt_list *), 1 },
61 /* Allocate a virtual array with NUM_ELEMENT elements, each of which is
62 ELEMENT_SIZE bytes long, named NAME. Array elements are zeroed. */
63 varray_type
64 varray_init (num_elements, element_kind, name)
65 size_t num_elements;
66 enum varray_data_enum element_kind;
67 const char *name;
69 size_t data_size = num_elements * element[element_kind].size;
70 varray_type ptr;
71 if (element[element_kind].uses_ggc)
72 ptr = (varray_type) ggc_alloc_cleared (VARRAY_HDR_SIZE + data_size);
73 else
74 ptr = (varray_type) xcalloc (VARRAY_HDR_SIZE + data_size, 1);
76 ptr->num_elements = num_elements;
77 ptr->elements_used = 0;
78 ptr->type = element_kind;
79 ptr->name = name;
80 return ptr;
83 /* Grow/shrink the virtual array VA to N elements. Zero any new elements
84 allocated. */
85 varray_type
86 varray_grow (va, n)
87 varray_type va;
88 size_t n;
90 size_t old_elements = va->num_elements;
92 if (n != old_elements)
94 size_t elem_size = element[va->type].size;
95 size_t old_data_size = old_elements * elem_size;
96 size_t data_size = n * elem_size;
98 if (element[va->type].uses_ggc)
99 va = (varray_type) ggc_realloc (va, VARRAY_HDR_SIZE + data_size);
100 else
101 va = (varray_type) xrealloc ((char *) va, VARRAY_HDR_SIZE + data_size);
102 va->num_elements = n;
103 if (n > old_elements)
104 memset (&va->data.c[old_data_size], 0, data_size - old_data_size);
107 return va;
110 /* Reset a varray to its original state. */
111 void
112 varray_clear (va)
113 varray_type va;
115 size_t data_size = element[va->type].size * va->num_elements;
117 memset (va->data.c, 0, data_size);
118 va->elements_used = 0;
121 /* Check the bounds of a varray access. */
123 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
125 extern void error PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
127 void
128 varray_check_failed (va, n, file, line, function)
129 varray_type va;
130 size_t n;
131 const char *file;
132 int line;
133 const char *function;
135 internal_error ("virtual array %s[%lu]: element %lu out of bounds in %s, at %s:%d",
136 va->name, (unsigned long) va->num_elements, (unsigned long) n,
137 function, trim_filename (file), line);
140 #endif