source: trunk/src/gcc/gcc/varray.c@ 1392

Last change on this file since 1392 was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

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