2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / varray.c
blob7901471bd798034b750a85a9e42da4a91a12c724
1 /* Virtual array support.
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003
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 #include "config.h"
24 #include "errors.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "varray.h"
29 #include "ggc.h"
31 #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data))
33 /* Do not add any more non-GC items here. Please either remove or GC those items that
34 are not GCed. */
36 static const struct {
37 unsigned char size;
38 bool uses_ggc;
39 } element[NUM_VARRAY_DATA] = {
40 { sizeof (char), 1 },
41 { sizeof (unsigned char), 1 },
42 { sizeof (short), 1 },
43 { sizeof (unsigned short), 1 },
44 { sizeof (int), 1 },
45 { sizeof (unsigned int), 1 },
46 { sizeof (long), 1 },
47 { sizeof (unsigned long), 1 },
48 { sizeof (HOST_WIDE_INT), 1 },
49 { sizeof (unsigned HOST_WIDE_INT), 1 },
50 { sizeof (void *), 1 },
51 { sizeof (char *), 1 },
52 { sizeof (struct rtx_def *), 1 },
53 { sizeof (struct rtvec_def *), 1 },
54 { sizeof (union tree_node *), 1 },
55 { sizeof (struct bitmap_head_def *), 1 },
56 { sizeof (struct reg_info_def *), 0 },
57 { sizeof (struct const_equiv_data), 0 },
58 { sizeof (struct basic_block_def *), 0 },
59 { sizeof (struct elt_list *), 1 },
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
65 varray_init (size_t num_elements, enum varray_data_enum element_kind,
66 const char *name)
68 size_t data_size = num_elements * element[element_kind].size;
69 varray_type ptr;
70 if (element[element_kind].uses_ggc)
71 ptr = ggc_alloc_cleared (VARRAY_HDR_SIZE + data_size);
72 else
73 ptr = xcalloc (VARRAY_HDR_SIZE + data_size, 1);
75 ptr->num_elements = num_elements;
76 ptr->elements_used = 0;
77 ptr->type = element_kind;
78 ptr->name = name;
79 return ptr;
82 /* Grow/shrink the virtual array VA to N elements. Zero any new elements
83 allocated. */
84 varray_type
85 varray_grow (varray_type va, size_t n)
87 size_t old_elements = va->num_elements;
89 if (n != old_elements)
91 size_t elem_size = element[va->type].size;
92 size_t old_data_size = old_elements * elem_size;
93 size_t data_size = n * elem_size;
95 if (element[va->type].uses_ggc)
96 va = ggc_realloc (va, VARRAY_HDR_SIZE + data_size);
97 else
98 va = xrealloc (va, VARRAY_HDR_SIZE + data_size);
99 va->num_elements = n;
100 if (n > old_elements)
101 memset (&va->data.c[old_data_size], 0, data_size - old_data_size);
104 return va;
107 /* Reset a varray to its original state. */
108 void
109 varray_clear (varray_type va)
111 size_t data_size = element[va->type].size * va->num_elements;
113 memset (va->data.c, 0, data_size);
114 va->elements_used = 0;
117 /* Check the bounds of a varray access. */
119 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
121 extern void error (const char *, ...) ATTRIBUTE_PRINTF_1;
123 void
124 varray_check_failed (varray_type va, size_t n, const char *file, int line,
125 const char *function)
127 internal_error ("virtual array %s[%lu]: element %lu out of bounds in %s, at %s:%d",
128 va->name, (unsigned long) va->num_elements, (unsigned long) n,
129 function, trim_filename (file), line);
132 #endif