1 /* Vector API for GNU compiler.
2 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
5 This file is part of GCC.
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
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
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
22 /* This file is compiled twice: once for the generator programs
23 once for the compiler. */
33 #include "coretypes.h"
44 /* Calculate the new ALLOC value, making sure that RESERVE slots are
45 free. If EXACT grow exactly, otherwise grow exponentially. */
47 static inline unsigned
48 calculate_allocation (const struct vec_prefix
*pfx
, int reserve
, bool exact
)
53 gcc_assert (reserve
>= 0);
61 /* If there's no prefix, and we've not requested anything, then we
62 will create a NULL vector. */
65 /* We must have run out of room. */
66 gcc_assert (alloc
- num
< (unsigned) reserve
);
70 alloc
= num
+ reserve
;
73 /* Exponential growth. */
77 /* Double when small. */
80 /* Grow slower when large. */
81 alloc
= (alloc
* 3 / 2);
83 /* If this is still too small, set it to the right size. */
84 if (alloc
< num
+ reserve
)
85 alloc
= num
+ reserve
;
90 /* Ensure there are at least RESERVE free slots in VEC. If EXACT grow
91 exactly, else grow exponentially. As a special case, if VEC is
92 NULL and RESERVE is 0, no vector will be created. The vector's
93 trailing array is at VEC_OFFSET offset and consists of ELT_SIZE
97 vec_gc_o_reserve_1 (void *vec
, int reserve
, size_t vec_offset
, size_t elt_size
,
98 bool exact MEM_STAT_DECL
)
100 struct vec_prefix
*pfx
= vec
;
101 unsigned alloc
= alloc
= calculate_allocation (pfx
, reserve
, exact
);
106 vec
= ggc_realloc_stat (vec
, vec_offset
+ alloc
* elt_size PASS_MEM_STAT
);
107 ((struct vec_prefix
*)vec
)->alloc
= alloc
;
109 ((struct vec_prefix
*)vec
)->num
= 0;
114 /* Ensure there are at least RESERVE free slots in VEC, growing
115 exponentially. If RESERVE < 0 grow exactly, else grow
116 exponentially. As a special case, if VEC is NULL, and RESERVE is
117 0, no vector will be created. */
120 vec_gc_p_reserve (void *vec
, int reserve MEM_STAT_DECL
)
122 return vec_gc_o_reserve_1 (vec
, reserve
,
123 offsetof (struct vec_prefix
, vec
),
124 sizeof (void *), false
128 /* Ensure there are at least RESERVE free slots in VEC, growing
129 exactly. If RESERVE < 0 grow exactly, else grow exponentially. As
130 a special case, if VEC is NULL, and RESERVE is 0, no vector will be
134 vec_gc_p_reserve_exact (void *vec
, int reserve MEM_STAT_DECL
)
136 return vec_gc_o_reserve_1 (vec
, reserve
,
137 offsetof (struct vec_prefix
, vec
),
138 sizeof (void *), true
142 /* As for vec_gc_p_reserve, but for object vectors. The vector's
143 trailing array is at VEC_OFFSET offset and consists of ELT_SIZE
147 vec_gc_o_reserve (void *vec
, int reserve
, size_t vec_offset
, size_t elt_size
150 return vec_gc_o_reserve_1 (vec
, reserve
, vec_offset
, elt_size
, false
154 /* As for vec_gc_p_reserve_exact, but for object vectors. The
155 vector's trailing array is at VEC_OFFSET offset and consists of
156 ELT_SIZE sized elements. */
159 vec_gc_o_reserve_exact (void *vec
, int reserve
, size_t vec_offset
,
160 size_t elt_size MEM_STAT_DECL
)
162 return vec_gc_o_reserve_1 (vec
, reserve
, vec_offset
, elt_size
, true
166 /* As for vec_gc_o_reserve_1, but for heap allocated vectors. */
169 vec_heap_o_reserve_1 (void *vec
, int reserve
, size_t vec_offset
,
170 size_t elt_size
, bool exact MEM_STAT_DECL
)
172 struct vec_prefix
*pfx
= vec
;
173 unsigned alloc
= calculate_allocation (pfx
, reserve
, exact
);
178 vec
= xrealloc (vec
, vec_offset
+ alloc
* elt_size
);
179 ((struct vec_prefix
*)vec
)->alloc
= alloc
;
181 ((struct vec_prefix
*)vec
)->num
= 0;
186 /* As for vec_gc_p_reserve, but for heap allocated vectors. */
189 vec_heap_p_reserve (void *vec
, int reserve MEM_STAT_DECL
)
191 return vec_heap_o_reserve_1 (vec
, reserve
,
192 offsetof (struct vec_prefix
, vec
),
193 sizeof (void *), false
197 /* As for vec_gc_p_reserve_exact, but for heap allocated vectors. */
200 vec_heap_p_reserve_exact (void *vec
, int reserve MEM_STAT_DECL
)
202 return vec_heap_o_reserve_1 (vec
, reserve
,
203 offsetof (struct vec_prefix
, vec
),
204 sizeof (void *), true
208 /* As for vec_gc_o_reserve, but for heap allocated vectors. */
211 vec_heap_o_reserve (void *vec
, int reserve
, size_t vec_offset
, size_t elt_size
214 return vec_heap_o_reserve_1 (vec
, reserve
, vec_offset
, elt_size
, false
218 /* As for vec_gc_o_reserve_exact, but for heap allocated vectors. */
221 vec_heap_o_reserve_exact (void *vec
, int reserve
, size_t vec_offset
,
222 size_t elt_size MEM_STAT_DECL
)
224 return vec_heap_o_reserve_1 (vec
, reserve
, vec_offset
, elt_size
, true
229 /* Issue a vector domain error, and then fall over. */
232 vec_assert_fail (const char *op
, const char *struct_name
,
233 const char *file
, unsigned int line
, const char *function
)
235 internal_error ("vector %s %s domain error, in %s at %s:%u",
236 struct_name
, op
, function
, trim_filename (file
), line
);