2001-07-07 Toon Moene <toon@moene.indiv.nluug.nl>
[official-gcc.git] / gcc / varray.c
blob7cf42f0b9114e717745e65388f6ec1265e54161e
1 /* Virtual array support.
2 Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions.
5 This file is part of GNU CC.
7 GNU CC 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 GNU CC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to the Free
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "bitmap.h"
27 #include "varray.h"
29 #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data))
31 /* Allocate a virtual array with NUM_ELEMENT elements, each of which is
32 ELEMENT_SIZE bytes long, named NAME. Array elements are zeroed. */
33 varray_type
34 varray_init (num_elements, element_size, name)
35 size_t num_elements;
36 size_t element_size;
37 const char *name;
39 size_t data_size = num_elements * element_size;
40 varray_type ptr = (varray_type) xcalloc (VARRAY_HDR_SIZE + data_size, 1);
42 ptr->num_elements = num_elements;
43 ptr->elements_used = 0;
44 ptr->element_size = element_size;
45 ptr->name = name;
46 return ptr;
49 /* Grow/shrink the virtual array VA to N elements. Zero any new elements
50 allocated. */
51 varray_type
52 varray_grow (va, n)
53 varray_type va;
54 size_t n;
56 size_t old_elements = va->num_elements;
58 if (n != old_elements)
60 size_t element_size = va->element_size;
61 size_t old_data_size = old_elements * element_size;
62 size_t data_size = n * element_size;
64 va = (varray_type) xrealloc ((char *)va, VARRAY_HDR_SIZE + data_size);
65 va->num_elements = n;
66 if (n > old_elements)
67 memset (&va->data.c[old_data_size], 0, data_size - old_data_size);
70 return va;
73 /* Check the bounds of a varray access. */
75 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
77 extern void error PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
79 void
80 varray_check_failed (va, n, file, line, function)
81 varray_type va;
82 size_t n;
83 const char *file;
84 int line;
85 const char *function;
87 internal_error ("Virtual array %s[%lu]: element %lu out of bounds in %s, at %s:%d",
88 va->name, (unsigned long) va->num_elements, (unsigned long) n,
89 function, trim_filename (file), line);
92 #endif