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

Last change on this file since 2 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 2.7 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 "rtl.h"
26#include "tree.h"
27#include "bitmap.h"
28#include "varray.h"
29
30#define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data))
31
32/* Allocate a virtual array with NUM_ELEMENT elements, each of which is
33 ELEMENT_SIZE bytes long, named NAME. Array elements are zeroed. */
34varray_type
35varray_init (num_elements, element_size, name)
36 size_t num_elements;
37 size_t element_size;
38 const char *name;
39{
40 size_t data_size = num_elements * element_size;
41 varray_type ptr = (varray_type) xcalloc (VARRAY_HDR_SIZE + data_size, 1);
42
43 ptr->num_elements = num_elements;
44 ptr->elements_used = 0;
45 ptr->element_size = element_size;
46 ptr->name = name;
47 return ptr;
48}
49
50/* Grow/shrink the virtual array VA to N elements. Zero any new elements
51 allocated. */
52varray_type
53varray_grow (va, n)
54 varray_type va;
55 size_t n;
56{
57 size_t old_elements = va->num_elements;
58
59 if (n != old_elements)
60 {
61 size_t element_size = va->element_size;
62 size_t old_data_size = old_elements * element_size;
63 size_t data_size = n * element_size;
64
65 va = (varray_type) xrealloc ((char *) va, VARRAY_HDR_SIZE + data_size);
66 va->num_elements = n;
67 if (n > old_elements)
68 memset (&va->data.c[old_data_size], 0, data_size - old_data_size);
69 }
70
71 return va;
72}
73
74/* Check the bounds of a varray access. */
75
76#if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
77
78extern void error PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
79
80void
81varray_check_failed (va, n, file, line, function)
82 varray_type va;
83 size_t n;
84 const char *file;
85 int line;
86 const char *function;
87{
88 internal_error ("virtual array %s[%lu]: element %lu out of bounds in %s, at %s:%d",
89 va->name, (unsigned long) va->num_elements, (unsigned long) n,
90 function, trim_filename (file), line);
91}
92
93#endif
Note: See TracBrowser for help on using the repository browser.