]>
Commit | Line | Data |
---|---|---|
8e97a030 | 1 | /* Virtual array support. |
85db5591 | 2 | Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
8e97a030 | 3 | Contributed by Cygnus Solutions. |
4 | ||
f12b58b3 | 5 | This file is part of GCC. |
8e97a030 | 6 | |
f12b58b3 | 7 | GCC is free software; you can redistribute it and/or modify it |
8e97a030 | 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. | |
11 | ||
f12b58b3 | 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. | |
8e97a030 | 16 | |
17 | You should have received a copy of the GNU General Public License | |
f12b58b3 | 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. */ | |
8e97a030 | 21 | |
22 | #include "config.h" | |
45523651 | 23 | #include "errors.h" |
8e97a030 | 24 | #include "system.h" |
805e22b2 | 25 | #include "coretypes.h" |
26 | #include "tm.h" | |
8e97a030 | 27 | #include "varray.h" |
1f3233d1 | 28 | #include "ggc.h" |
8e97a030 | 29 | |
30 | #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data)) | |
31 | ||
1f3233d1 | 32 | static const size_t element_size[NUM_VARRAY_DATA] = { |
33 | sizeof (char), | |
34 | sizeof (unsigned char), | |
35 | sizeof (short), | |
36 | sizeof (unsigned short), | |
37 | sizeof (int), | |
38 | sizeof (unsigned int), | |
39 | sizeof (long), | |
40 | sizeof (unsigned long), | |
41 | sizeof (HOST_WIDE_INT), | |
42 | sizeof (unsigned HOST_WIDE_INT), | |
43 | sizeof (PTR), | |
44 | sizeof (char *), | |
45 | sizeof (struct rtx_def *), | |
46 | sizeof (struct rtvec_def *), | |
47 | sizeof (union tree_node *), | |
48 | sizeof (struct bitmap_head_def *), | |
49 | sizeof (struct reg_info_def *), | |
50 | sizeof (struct const_equiv_data), | |
51 | sizeof (struct basic_block_def *), | |
52 | sizeof (struct elt_list *) | |
53 | }; | |
54 | ||
55 | static const int uses_ggc[NUM_VARRAY_DATA] = { | |
56 | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* unsigned HOST_WIDE_INT */ | |
57 | 1, /* PTR */ | |
58 | 1, 1, 1, 1, 1, /* bitmap_head_def */ | |
59 | 0, 0, 0, 1 | |
60 | }; | |
61 | ||
8e97a030 | 62 | /* Allocate a virtual array with NUM_ELEMENT elements, each of which is |
63 | ELEMENT_SIZE bytes long, named NAME. Array elements are zeroed. */ | |
64 | varray_type | |
1f3233d1 | 65 | varray_init (num_elements, element_kind, name) |
8e97a030 | 66 | size_t num_elements; |
1f3233d1 | 67 | enum varray_data_enum element_kind; |
8e97a030 | 68 | const char *name; |
69 | { | |
1f3233d1 | 70 | size_t data_size = num_elements * element_size[element_kind]; |
71 | varray_type ptr; | |
72 | if (uses_ggc [element_kind]) | |
73 | ptr = (varray_type) ggc_alloc_cleared (VARRAY_HDR_SIZE + data_size); | |
74 | else | |
75 | ptr = (varray_type) xcalloc (VARRAY_HDR_SIZE + data_size, 1); | |
8e97a030 | 76 | |
77 | ptr->num_elements = num_elements; | |
395adca1 | 78 | ptr->elements_used = 0; |
1f3233d1 | 79 | ptr->type = element_kind; |
395adca1 | 80 | ptr->name = name; |
8e97a030 | 81 | return ptr; |
82 | } | |
83 | ||
84 | /* Grow/shrink the virtual array VA to N elements. Zero any new elements | |
85 | allocated. */ | |
86 | varray_type | |
87 | varray_grow (va, n) | |
88 | varray_type va; | |
89 | size_t n; | |
90 | { | |
91 | size_t old_elements = va->num_elements; | |
92 | ||
93 | if (n != old_elements) | |
94 | { | |
1f3233d1 | 95 | size_t elem_size = element_size[va->type]; |
96 | size_t old_data_size = old_elements * elem_size; | |
97 | size_t data_size = n * elem_size; | |
98 | ||
99 | if (uses_ggc[va->type]) | |
100 | va = (varray_type) ggc_realloc (va, VARRAY_HDR_SIZE + data_size); | |
101 | else | |
102 | va = (varray_type) xrealloc ((char *) va, VARRAY_HDR_SIZE + data_size); | |
8e97a030 | 103 | va->num_elements = n; |
104 | if (n > old_elements) | |
93d3b7de | 105 | memset (&va->data.c[old_data_size], 0, data_size - old_data_size); |
8e97a030 | 106 | } |
107 | ||
108 | return va; | |
109 | } | |
dda90815 | 110 | |
1f3233d1 | 111 | /* Reset a varray to its original state. */ |
112 | void | |
113 | varray_clear (va) | |
114 | varray_type va; | |
115 | { | |
116 | size_t data_size = element_size[va->type] * va->num_elements; | |
117 | ||
118 | memset (va->data.c, 0, data_size); | |
119 | va->elements_used = 0; | |
120 | } | |
121 | ||
dda90815 | 122 | /* Check the bounds of a varray access. */ |
123 | ||
e6e27f6e | 124 | #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007) |
dda90815 | 125 | |
e0909be9 | 126 | extern void error PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1; |
dda90815 | 127 | |
128 | void | |
129 | varray_check_failed (va, n, file, line, function) | |
130 | varray_type va; | |
131 | size_t n; | |
132 | const char *file; | |
133 | int line; | |
134 | const char *function; | |
135 | { | |
cb8bacb6 | 136 | internal_error ("virtual array %s[%lu]: element %lu out of bounds in %s, at %s:%d", |
5df84ab2 | 137 | va->name, (unsigned long) va->num_elements, (unsigned long) n, |
138 | function, trim_filename (file), line); | |
dda90815 | 139 | } |
140 | ||
141 | #endif |