OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / vec.c
1 /* Vector API for GNU compiler.
2    Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3    Contributed by Nathan Sidwell <nathan@codesourcery.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 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 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.  */
21
22 /* This file is compiled twice: once for the generator programs
23    once for the compiler.  */
24 #ifdef GENERATOR_FILE
25 #include "bconfig.h"
26 #else
27 #include "config.h"
28 #endif
29
30 #include "system.h"
31 #include "ggc.h"
32 #include "vec.h"
33 #include "coretypes.h"
34 #include "toplev.h"
35
36 struct vec_prefix 
37 {
38   unsigned num;
39   unsigned alloc;
40   void *vec[1];
41 };
42
43 /* Calculate the new ALLOC value, making sure that abs(RESERVE) slots
44    are free.  If RESERVE < 0 grow exactly, otherwise grow
45    exponentially.  */
46
47 static inline unsigned
48 calculate_allocation (const struct vec_prefix *pfx, int reserve)
49 {
50   unsigned alloc = 0;
51   unsigned num = 0;
52
53   if (pfx)
54     {
55       alloc = pfx->alloc;
56       num = pfx->num;
57     }
58   else if (!reserve)
59     /* If there's no prefix, and we've not requested anything, then we
60        will create a NULL vector.  */
61     return 0;
62   
63   /* We must have run out of room.  */
64   gcc_assert (alloc - num < (unsigned)(reserve < 0 ? -reserve : reserve));
65   
66   if (reserve < 0)
67     /* Exact size.  */
68     alloc = num + -reserve;
69   else
70     {
71       /* Exponential growth. */
72       if (!alloc)
73         alloc = 4;
74       else if (alloc < 16)
75         /* Double when small.  */
76         alloc = alloc * 2;
77       else
78         /* Grow slower when large.  */
79         alloc = (alloc * 3 / 2);
80       
81       /* If this is still too small, set it to the right size. */
82       if (alloc < num + reserve)
83         alloc = num + reserve;
84     }
85   return alloc;
86 }
87
88 /* Ensure there are at least abs(RESERVE) free slots in VEC.  If
89    RESERVE < 0 grow exactly, else grow exponentially.  As a special
90    case, if VEC is NULL, and RESERVE is 0, no vector will be created. */
91
92 void *
93 vec_gc_p_reserve (void *vec, int reserve MEM_STAT_DECL)
94 {
95   return vec_gc_o_reserve (vec, reserve,
96                            offsetof (struct vec_prefix, vec), sizeof (void *)
97                            PASS_MEM_STAT);
98 }
99
100 /* As vec_gc_p_reserve, but for object vectors.  The vector's trailing
101    array is at VEC_OFFSET offset and consists of ELT_SIZE sized
102    elements.  */
103
104 void *
105 vec_gc_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size
106                    MEM_STAT_DECL)
107 {
108   struct vec_prefix *pfx = vec;
109   unsigned alloc = alloc = calculate_allocation (pfx, reserve);
110   
111   if (!alloc)
112     return NULL;
113   
114   vec = ggc_realloc_stat (vec, vec_offset + alloc * elt_size PASS_MEM_STAT);
115   ((struct vec_prefix *)vec)->alloc = alloc;
116   if (!pfx)
117     ((struct vec_prefix *)vec)->num = 0;
118   
119   return vec;
120 }
121
122 /* As for vec_gc_p_reserve, but for heap allocated vectors.  */
123
124 void *
125 vec_heap_p_reserve (void *vec, int reserve MEM_STAT_DECL)
126 {
127   return vec_heap_o_reserve (vec, reserve,
128                              offsetof (struct vec_prefix, vec), sizeof (void *)
129                              PASS_MEM_STAT);
130 }
131
132 /* As for vec_gc_o_reserve, but for heap allocated vectors.  */
133
134 void *
135 vec_heap_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size
136                     MEM_STAT_DECL)
137 {
138   struct vec_prefix *pfx = vec;
139   unsigned alloc = calculate_allocation (pfx, reserve);
140
141   if (!alloc)
142     return NULL;
143   
144   vec = xrealloc (vec, vec_offset + alloc * elt_size);
145   ((struct vec_prefix *)vec)->alloc = alloc;
146   if (!pfx)
147     ((struct vec_prefix *)vec)->num = 0;
148   
149   return vec;
150 }
151
152 #if ENABLE_CHECKING
153 /* Issue a vector domain error, and then fall over.  */
154
155 void
156 vec_assert_fail (const char *op, const char *struct_name,
157                  const char *file, unsigned int line, const char *function)
158 {
159   internal_error ("vector %s %s domain error, in %s at %s:%u",
160                   struct_name, op, function, trim_filename (file), line);
161 }
162 #endif