OSDN Git Service

2006-08-17 Paolo Bonzini <bonzini@gnu.org>
[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 "tree.h"
35 #include "toplev.h"
36
37 struct vec_prefix 
38 {
39   unsigned num;
40   unsigned alloc;
41   void *vec[1];
42 };
43
44 /* Calculate the new ALLOC value, making sure that abs(RESERVE) slots
45    are free.  If RESERVE < 0 grow exactly, otherwise grow
46    exponentially.  */
47
48 static inline unsigned
49 calculate_allocation (const struct vec_prefix *pfx, int reserve)
50 {
51   unsigned alloc = 0;
52   unsigned num = 0;
53
54   if (pfx)
55     {
56       alloc = pfx->alloc;
57       num = pfx->num;
58     }
59   else if (!reserve)
60     /* If there's no prefix, and we've not requested anything, then we
61        will create a NULL vector.  */
62     return 0;
63   
64   /* We must have run out of room.  */
65   gcc_assert (alloc - num < (unsigned)(reserve < 0 ? -reserve : reserve));
66   
67   if (reserve < 0)
68     /* Exact size.  */
69     alloc = num + -reserve;
70   else
71     {
72       /* Exponential growth. */
73       if (!alloc)
74         alloc = 4;
75       else if (alloc < 16)
76         /* Double when small.  */
77         alloc = alloc * 2;
78       else
79         /* Grow slower when large.  */
80         alloc = (alloc * 3 / 2);
81       
82       /* If this is still too small, set it to the right size. */
83       if (alloc < num + reserve)
84         alloc = num + reserve;
85     }
86   return alloc;
87 }
88
89 /* Ensure there are at least abs(RESERVE) free slots in VEC.  If
90    RESERVE < 0 grow exactly, else grow exponentially.  As a special
91    case, if VEC is NULL, and RESERVE is 0, no vector will be created. */
92
93 void *
94 vec_gc_p_reserve (void *vec, int reserve MEM_STAT_DECL)
95 {
96   return vec_gc_o_reserve (vec, reserve,
97                            offsetof (struct vec_prefix, vec), sizeof (void *)
98                            PASS_MEM_STAT);
99 }
100
101 /* As vec_gc_p_reserve, but for object vectors.  The vector's trailing
102    array is at VEC_OFFSET offset and consists of ELT_SIZE sized
103    elements.  */
104
105 void *
106 vec_gc_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size
107                    MEM_STAT_DECL)
108 {
109   struct vec_prefix *pfx = vec;
110   unsigned alloc = alloc = calculate_allocation (pfx, reserve);
111   
112   if (!alloc)
113     return NULL;
114   
115   vec = ggc_realloc_stat (vec, vec_offset + alloc * elt_size PASS_MEM_STAT);
116   ((struct vec_prefix *)vec)->alloc = alloc;
117   if (!pfx)
118     ((struct vec_prefix *)vec)->num = 0;
119   
120   return vec;
121 }
122
123 /* As for vec_gc_p_reserve, but for heap allocated vectors.  */
124
125 void *
126 vec_heap_p_reserve (void *vec, int reserve MEM_STAT_DECL)
127 {
128   return vec_heap_o_reserve (vec, reserve,
129                              offsetof (struct vec_prefix, vec), sizeof (void *)
130                              PASS_MEM_STAT);
131 }
132
133 /* As for vec_gc_o_reserve, but for heap allocated vectors.  */
134
135 void *
136 vec_heap_o_reserve (void *vec, int reserve, size_t vec_offset, size_t elt_size
137                     MEM_STAT_DECL)
138 {
139   struct vec_prefix *pfx = vec;
140   unsigned alloc = calculate_allocation (pfx, reserve);
141
142   if (!alloc)
143     return NULL;
144   
145   vec = xrealloc (vec, vec_offset + alloc * elt_size);
146   ((struct vec_prefix *)vec)->alloc = alloc;
147   if (!pfx)
148     ((struct vec_prefix *)vec)->num = 0;
149   
150   return vec;
151 }
152
153 #if ENABLE_CHECKING
154 /* Issue a vector domain error, and then fall over.  */
155
156 void
157 vec_assert_fail (const char *op, const char *struct_name,
158                  const char *file, unsigned int line, const char *function)
159 {
160   internal_error ("vector %s %s domain error, in %s at %s:%u",
161                   struct_name, op, function, trim_filename (file), line);
162 }
163 #endif