00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "config.h"
00023 #include "errors.h"
00024 #include "system.h"
00025 #include "varray.h"
00026 #include "ggc.h"
00027
00028 #define VARRAY_HDR_SIZE (sizeof (struct varray_head_tag) - sizeof (varray_data))
00029
00030 static const size_t element_size[NUM_VARRAY_DATA] = {
00031 sizeof (char),
00032 sizeof (unsigned char),
00033 sizeof (short),
00034 sizeof (unsigned short),
00035 sizeof (int),
00036 sizeof (unsigned int),
00037 sizeof (long),
00038 sizeof (unsigned long),
00039 sizeof (HOST_WIDE_INT),
00040 sizeof (unsigned HOST_WIDE_INT),
00041 sizeof (PTR),
00042 sizeof (char *),
00043 sizeof (struct rtx_def *),
00044 sizeof (struct rtvec_def *),
00045 sizeof (union tree_node *),
00046 sizeof (struct bitmap_head_def *),
00047 sizeof (struct reg_info_def *),
00048 sizeof (struct const_equiv_data),
00049 sizeof (struct basic_block_def *),
00050 sizeof (struct elt_list *)
00051 };
00052
00053 static const int uses_ggc[NUM_VARRAY_DATA] = {
00054 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
00055 1,
00056 1, 1, 1, 1, 1,
00057 0, 0, 0, 1
00058 };
00059
00060
00061
00062 varray_type
00063 varray_init (num_elements, element_kind, name)
00064 size_t num_elements;
00065 enum varray_data_enum element_kind;
00066 const char *name;
00067 {
00068 size_t data_size = num_elements * element_size[element_kind];
00069 varray_type ptr;
00070 if (uses_ggc [element_kind])
00071 ptr = (varray_type) ggc_alloc_cleared (VARRAY_HDR_SIZE + data_size);
00072 else
00073 ptr = (varray_type) xcalloc (VARRAY_HDR_SIZE + data_size, 1);
00074
00075 ptr->num_elements = num_elements;
00076 ptr->elements_used = 0;
00077 ptr->type = element_kind;
00078 ptr->name = name;
00079 return ptr;
00080 }
00081
00082
00083
00084 varray_type
00085 varray_grow (va, n)
00086 varray_type va;
00087 size_t n;
00088 {
00089 size_t old_elements = va->num_elements;
00090
00091 if (n != old_elements)
00092 {
00093 size_t elem_size = element_size[va->type];
00094 size_t old_data_size = old_elements * elem_size;
00095 size_t data_size = n * elem_size;
00096
00097 if (uses_ggc[va->type])
00098 va = (varray_type) ggc_realloc (va, VARRAY_HDR_SIZE + data_size);
00099 else
00100 va = (varray_type) xrealloc ((char *) va, VARRAY_HDR_SIZE + data_size);
00101 va->num_elements = n;
00102 if (n > old_elements)
00103 memset (&va->data.c[old_data_size], 0, data_size - old_data_size);
00104 }
00105
00106 return va;
00107 }
00108
00109
00110 void
00111 varray_clear (va)
00112 varray_type va;
00113 {
00114 size_t data_size = element_size[va->type] * va->num_elements;
00115
00116 memset (va->data.c, 0, data_size);
00117 va->elements_used = 0;
00118 }
00119
00120
00121
00122 #if defined ENABLE_CHECKING && (GCC_VERSION >= 2007)
00123
00124 extern void error PARAMS ((const char *, ...)) ATTRIBUTE_PRINTF_1;
00125
00126 void
00127 varray_check_failed (va, n, file, line, function)
00128 varray_type va;
00129 size_t n;
00130 const char *file;
00131 int line;
00132 const char *function;
00133 {
00134 internal_error ("virtual array %s[%lu]: element %lu out of bounds in %s, at %s:%d",
00135 va->name, (unsigned long) va->num_elements, (unsigned long) n,
00136 function, trim_filename (file), line);
00137 }
00138
00139 #endif