]>
Commit | Line | Data |
---|---|---|
ada55151 | 1 | /* Vector API for GNU compiler. |
d1e082c2 | 2 | Copyright (C) 2004-2013 Free Software Foundation, Inc. |
ada55151 | 3 | Contributed by Nathan Sidwell <nathan@codesourcery.com> |
0823efed | 4 | Re-implemented in C++ by Diego Novillo <dnovillo@google.com> |
ada55151 NS |
5 | |
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 10 | Software Foundation; either version 3, or (at your option) any later |
ada55151 NS |
11 | version. |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
ada55151 NS |
21 | |
22 | #ifndef GCC_VEC_H | |
23 | #define GCC_VEC_H | |
24 | ||
9771b263 DN |
25 | /* FIXME - When compiling some of the gen* binaries, we cannot enable GC |
26 | support because the headers generated by gengtype are still not | |
27 | present. In particular, the header file gtype-desc.h is missing, | |
28 | so compilation may fail if we try to include ggc.h. | |
29 | ||
30 | Since we use some of those declarations, we need to provide them | |
31 | (even if the GC-based templates are not used). This is not a | |
32 | problem because the code that runs before gengtype is built will | |
33 | never need to use GC vectors. But it does force us to declare | |
34 | these functions more than once. */ | |
35 | #ifdef GENERATOR_FILE | |
36 | #define VEC_GC_ENABLED 0 | |
37 | #else | |
38 | #define VEC_GC_ENABLED 1 | |
39 | #endif // GENERATOR_FILE | |
40 | ||
41 | #include "statistics.h" // For CXX_MEM_STAT_INFO. | |
42 | ||
43 | #if VEC_GC_ENABLED | |
44 | #include "ggc.h" | |
45 | #else | |
46 | # ifndef GCC_GGC_H | |
47 | /* Even if we think that GC is not enabled, the test that sets it is | |
48 | weak. There are files compiled with -DGENERATOR_FILE that already | |
49 | include ggc.h. We only need to provide these definitions if ggc.h | |
50 | has not been included. Sigh. */ | |
51 | extern void ggc_free (void *); | |
52 | extern size_t ggc_round_alloc_size (size_t requested_size); | |
9771b263 DN |
53 | extern void *ggc_realloc_stat (void *, size_t MEM_STAT_DECL); |
54 | # endif // GCC_GGC_H | |
55 | #endif // VEC_GC_ENABLED | |
3e097227 | 56 | |
bd0c3bfd DN |
57 | /* Templated vector type and associated interfaces. |
58 | ||
59 | The interface functions are typesafe and use inline functions, | |
60 | sometimes backed by out-of-line generic functions. The vectors are | |
61 | designed to interoperate with the GTY machinery. | |
62 | ||
bd0c3bfd DN |
63 | There are both 'index' and 'iterate' accessors. The index accessor |
64 | is implemented by operator[]. The iterator returns a boolean | |
65 | iteration condition and updates the iteration variable passed by | |
66 | reference. Because the iterator will be inlined, the address-of | |
67 | can be optimized away. | |
9ba5ff0f | 68 | |
ada55151 NS |
69 | Each operation that increases the number of active elements is |
70 | available in 'quick' and 'safe' variants. The former presumes that | |
71 | there is sufficient allocated space for the operation to succeed | |
0e61db61 | 72 | (it dies if there is not). The latter will reallocate the |
ada55151 NS |
73 | vector, if needed. Reallocation causes an exponential increase in |
74 | vector size. If you know you will be adding N elements, it would | |
75 | be more efficient to use the reserve operation before adding the | |
d4e6fecb NS |
76 | elements with the 'quick' operation. This will ensure there are at |
77 | least as many elements as you ask for, it will exponentially | |
78 | increase if there are too few spare slots. If you want reserve a | |
79 | specific number of slots, but do not want the exponential increase | |
efb7e1e0 ILT |
80 | (for instance, you know this is the last allocation), use the |
81 | reserve_exact operation. You can also create a vector of a | |
d4e6fecb | 82 | specific size from the get go. |
ada55151 NS |
83 | |
84 | You should prefer the push and pop operations, as they append and | |
a064479c NS |
85 | remove from the end of the vector. If you need to remove several |
86 | items in one go, use the truncate operation. The insert and remove | |
ada55151 NS |
87 | operations allow you to change elements in the middle of the |
88 | vector. There are two remove operations, one which preserves the | |
89 | element ordering 'ordered_remove', and one which does not | |
90 | 'unordered_remove'. The latter function copies the end element | |
d4e6fecb NS |
91 | into the removed slot, rather than invoke a memmove operation. The |
92 | 'lower_bound' function will determine where to place an item in the | |
58152808 | 93 | array using insert that will maintain sorted order. |
9ba5ff0f | 94 | |
9771b263 DN |
95 | Vectors are template types with three arguments: the type of the |
96 | elements in the vector, the allocation strategy, and the physical | |
97 | layout to use | |
98 | ||
99 | Four allocation strategies are supported: | |
100 | ||
101 | - Heap: allocation is done using malloc/free. This is the | |
102 | default allocation strategy. | |
103 | ||
104 | - Stack: allocation is done using alloca. | |
105 | ||
106 | - GC: allocation is done using ggc_alloc/ggc_free. | |
107 | ||
108 | - GC atomic: same as GC with the exception that the elements | |
109 | themselves are assumed to be of an atomic type that does | |
110 | not need to be garbage collected. This means that marking | |
111 | routines do not need to traverse the array marking the | |
112 | individual elements. This increases the performance of | |
113 | GC activities. | |
114 | ||
115 | Two physical layouts are supported: | |
116 | ||
117 | - Embedded: The vector is structured using the trailing array | |
118 | idiom. The last member of the structure is an array of size | |
119 | 1. When the vector is initially allocated, a single memory | |
120 | block is created to hold the vector's control data and the | |
121 | array of elements. These vectors cannot grow without | |
122 | reallocation (see discussion on embeddable vectors below). | |
123 | ||
124 | - Space efficient: The vector is structured as a pointer to an | |
125 | embedded vector. This is the default layout. It means that | |
126 | vectors occupy a single word of storage before initial | |
127 | allocation. Vectors are allowed to grow (the internal | |
128 | pointer is reallocated but the main vector instance does not | |
129 | need to relocate). | |
130 | ||
131 | The type, allocation and layout are specified when the vector is | |
132 | declared. | |
b8698a0f | 133 | |
9ba5ff0f NS |
134 | If you need to directly manipulate a vector, then the 'address' |
135 | accessor will return the address of the start of the vector. Also | |
136 | the 'space' predicate will tell you whether there is spare capacity | |
137 | in the vector. You will not normally need to use these two functions. | |
b8698a0f | 138 | |
9771b263 DN |
139 | Notes on the different layout strategies |
140 | ||
141 | * Embeddable vectors (vec<T, A, vl_embed>) | |
142 | ||
143 | These vectors are suitable to be embedded in other data | |
144 | structures so that they can be pre-allocated in a contiguous | |
145 | memory block. | |
146 | ||
147 | Embeddable vectors are implemented using the trailing array | |
148 | idiom, thus they are not resizeable without changing the address | |
149 | of the vector object itself. This means you cannot have | |
150 | variables or fields of embeddable vector type -- always use a | |
151 | pointer to a vector. The one exception is the final field of a | |
152 | structure, which could be a vector type. | |
153 | ||
154 | You will have to use the embedded_size & embedded_init calls to | |
155 | create such objects, and they will not be resizeable (so the | |
156 | 'safe' allocation variants are not available). | |
157 | ||
158 | Properties of embeddable vectors: | |
159 | ||
160 | - The whole vector and control data are allocated in a single | |
161 | contiguous block. It uses the trailing-vector idiom, so | |
162 | allocation must reserve enough space for all the elements | |
163 | in the vector plus its control data. | |
164 | - The vector cannot be re-allocated. | |
165 | - The vector cannot grow nor shrink. | |
166 | - No indirections needed for access/manipulation. | |
167 | - It requires 2 words of storage (prior to vector allocation). | |
168 | ||
169 | ||
170 | * Space efficient vector (vec<T, A, vl_ptr>) | |
171 | ||
172 | These vectors can grow dynamically and are allocated together | |
173 | with their control data. They are suited to be included in data | |
174 | structures. Prior to initial allocation, they only take a single | |
175 | word of storage. | |
176 | ||
177 | These vectors are implemented as a pointer to embeddable vectors. | |
178 | The semantics allow for this pointer to be NULL to represent | |
179 | empty vectors. This way, empty vectors occupy minimal space in | |
180 | the structure containing them. | |
181 | ||
182 | Properties: | |
183 | ||
184 | - The whole vector and control data are allocated in a single | |
185 | contiguous block. | |
186 | - The whole vector may be re-allocated. | |
187 | - Vector data may grow and shrink. | |
188 | - Access and manipulation requires a pointer test and | |
189 | indirection. | |
190 | - It requires 1 word of storage (prior to vector allocation). | |
ada55151 NS |
191 | |
192 | An example of their use would be, | |
193 | ||
ada55151 | 194 | struct my_struct { |
9771b263 DN |
195 | // A space-efficient vector of tree pointers in GC memory. |
196 | vec<tree, va_gc, vl_ptr> v; | |
ada55151 NS |
197 | }; |
198 | ||
199 | struct my_struct *s; | |
200 | ||
9771b263 DN |
201 | if (s->v.length ()) { we have some contents } |
202 | s->v.safe_push (decl); // append some decl onto the end | |
203 | for (ix = 0; s->v.iterate (ix, &elt); ix++) | |
2a68a7de | 204 | { do something with elt } |
ada55151 NS |
205 | */ |
206 | ||
9771b263 DN |
207 | /* Support function for statistics. */ |
208 | extern void dump_vec_loc_statistics (void); | |
0823efed | 209 | |
0823efed | 210 | |
9771b263 DN |
211 | /* Control data for vectors. This contains the number of allocated |
212 | and used slots inside a vector. */ | |
0823efed | 213 | |
26da79f5 | 214 | struct vec_prefix |
0823efed | 215 | { |
38f2ca32 DN |
216 | /* FIXME - These fields should be private, but we need to cater to |
217 | compilers that have stricter notions of PODness for types. */ | |
26da79f5 | 218 | |
9771b263 | 219 | /* Memory allocation support routines in vec.c. */ |
26da79f5 JJ |
220 | void register_overhead (size_t, const char *, int, const char *); |
221 | void release_overhead (void); | |
222 | static unsigned calculate_allocation (vec_prefix *, unsigned, bool); | |
9771b263 DN |
223 | |
224 | /* Note that vec_prefix should be a base class for vec, but we use | |
225 | offsetof() on vector fields of tree structures (e.g., | |
226 | tree_binfo::base_binfos), and offsetof only supports base types. | |
227 | ||
228 | To compensate, we make vec_prefix a field inside vec and make | |
229 | vec a friend class of vec_prefix so it can access its fields. */ | |
18e1fd75 | 230 | template <typename, typename, typename> friend struct vec; |
9771b263 DN |
231 | |
232 | /* The allocator types also need access to our internals. */ | |
233 | friend struct va_gc; | |
234 | friend struct va_gc_atomic; | |
235 | friend struct va_heap; | |
236 | friend struct va_stack; | |
237 | ||
26da79f5 JJ |
238 | unsigned alloc_; |
239 | unsigned num_; | |
0823efed DN |
240 | }; |
241 | ||
18e1fd75 | 242 | template<typename, typename, typename> struct vec; |
bd0c3bfd | 243 | |
9771b263 | 244 | /* Valid vector layouts |
bd0c3bfd | 245 | |
9771b263 DN |
246 | vl_embed - Embeddable vector that uses the trailing array idiom. |
247 | vl_ptr - Space efficient vector that uses a pointer to an | |
248 | embeddable vector. */ | |
249 | struct vl_embed { }; | |
250 | struct vl_ptr { }; | |
bd0c3bfd | 251 | |
bd0c3bfd | 252 | |
9771b263 | 253 | /* Types of supported allocations |
bd0c3bfd | 254 | |
9771b263 DN |
255 | va_heap - Allocation uses malloc/free. |
256 | va_gc - Allocation uses ggc_alloc. | |
257 | va_gc_atomic - Same as GC, but individual elements of the array | |
258 | do not need to be marked during collection. | |
259 | va_stack - Allocation uses alloca. */ | |
bd0c3bfd | 260 | |
9771b263 DN |
261 | /* Allocator type for heap vectors. */ |
262 | struct va_heap | |
263 | { | |
264 | /* Heap vectors are frequently regular instances, so use the vl_ptr | |
265 | layout for them. */ | |
266 | typedef vl_ptr default_layout; | |
bd0c3bfd | 267 | |
9771b263 DN |
268 | template<typename T> |
269 | static void reserve (vec<T, va_heap, vl_embed> *&, unsigned, bool | |
270 | CXX_MEM_STAT_INFO); | |
bd0c3bfd | 271 | |
9771b263 DN |
272 | template<typename T> |
273 | static void release (vec<T, va_heap, vl_embed> *&); | |
274 | }; | |
bd0c3bfd | 275 | |
bd0c3bfd | 276 | |
9771b263 DN |
277 | /* Allocator for heap memory. Ensure there are at least RESERVE free |
278 | slots in V. If EXACT is true, grow exactly, else grow | |
279 | exponentially. As a special case, if the vector had not been | |
280 | allocated and and RESERVE is 0, no vector will be created. */ | |
bd0c3bfd | 281 | |
9771b263 DN |
282 | template<typename T> |
283 | inline void | |
284 | va_heap::reserve (vec<T, va_heap, vl_embed> *&v, unsigned reserve, bool exact | |
285 | MEM_STAT_DECL) | |
286 | { | |
26da79f5 JJ |
287 | unsigned alloc |
288 | = vec_prefix::calculate_allocation (v ? &v->vecpfx_ : 0, reserve, exact); | |
9771b263 DN |
289 | if (!alloc) |
290 | { | |
291 | release (v); | |
292 | return; | |
293 | } | |
bd0c3bfd | 294 | |
9771b263 | 295 | if (GATHER_STATISTICS && v) |
26da79f5 | 296 | v->vecpfx_.release_overhead (); |
bd0c3bfd | 297 | |
9771b263 DN |
298 | size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc); |
299 | unsigned nelem = v ? v->length () : 0; | |
300 | v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size)); | |
301 | v->embedded_init (alloc, nelem); | |
bd0c3bfd | 302 | |
9771b263 | 303 | if (GATHER_STATISTICS) |
26da79f5 | 304 | v->vecpfx_.register_overhead (size FINAL_PASS_MEM_STAT); |
9771b263 | 305 | } |
0823efed | 306 | |
bd0c3bfd | 307 | |
9771b263 | 308 | /* Free the heap space allocated for vector V. */ |
0823efed DN |
309 | |
310 | template<typename T> | |
311 | void | |
9771b263 | 312 | va_heap::release (vec<T, va_heap, vl_embed> *&v) |
0823efed | 313 | { |
38f2ca32 DN |
314 | if (v == NULL) |
315 | return; | |
316 | ||
9771b263 | 317 | if (GATHER_STATISTICS) |
26da79f5 | 318 | v->vecpfx_.release_overhead (); |
9771b263 DN |
319 | ::free (v); |
320 | v = NULL; | |
0823efed DN |
321 | } |
322 | ||
323 | ||
9771b263 DN |
324 | /* Allocator type for GC vectors. Notice that we need the structure |
325 | declaration even if GC is not enabled. */ | |
0823efed | 326 | |
9771b263 | 327 | struct va_gc |
0823efed | 328 | { |
9771b263 DN |
329 | /* Use vl_embed as the default layout for GC vectors. Due to GTY |
330 | limitations, GC vectors must always be pointers, so it is more | |
331 | efficient to use a pointer to the vl_embed layout, rather than | |
332 | using a pointer to a pointer as would be the case with vl_ptr. */ | |
333 | typedef vl_embed default_layout; | |
334 | ||
335 | template<typename T, typename A> | |
336 | static void reserve (vec<T, A, vl_embed> *&, unsigned, bool | |
337 | CXX_MEM_STAT_INFO); | |
338 | ||
339 | template<typename T, typename A> | |
340 | static void release (vec<T, A, vl_embed> *&v) { v = NULL; } | |
341 | }; | |
0823efed | 342 | |
0823efed | 343 | |
9771b263 DN |
344 | /* Allocator for GC memory. Ensure there are at least RESERVE free |
345 | slots in V. If EXACT is true, grow exactly, else grow | |
346 | exponentially. As a special case, if the vector had not been | |
347 | allocated and and RESERVE is 0, no vector will be created. */ | |
348 | ||
349 | template<typename T, typename A> | |
0823efed | 350 | void |
9771b263 DN |
351 | va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact |
352 | MEM_STAT_DECL) | |
0823efed | 353 | { |
26da79f5 JJ |
354 | unsigned alloc |
355 | = vec_prefix::calculate_allocation (v ? &v->vecpfx_ : 0, reserve, exact); | |
9771b263 DN |
356 | if (!alloc) |
357 | { | |
358 | ::ggc_free (v); | |
359 | v = NULL; | |
360 | return; | |
361 | } | |
0823efed | 362 | |
9771b263 DN |
363 | /* Calculate the amount of space we want. */ |
364 | size_t size = vec<T, A, vl_embed>::embedded_size (alloc); | |
0823efed | 365 | |
9771b263 | 366 | /* Ask the allocator how much space it will really give us. */ |
18e1fd75 | 367 | size = ::ggc_round_alloc_size (size); |
0823efed | 368 | |
9771b263 DN |
369 | /* Adjust the number of slots accordingly. */ |
370 | size_t vec_offset = sizeof (vec_prefix); | |
371 | size_t elt_size = sizeof (T); | |
372 | alloc = (size - vec_offset) / elt_size; | |
0823efed | 373 | |
9771b263 DN |
374 | /* And finally, recalculate the amount of space we ask for. */ |
375 | size = vec_offset + alloc * elt_size; | |
0823efed | 376 | |
9771b263 | 377 | unsigned nelem = v ? v->length () : 0; |
18e1fd75 DN |
378 | v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc_stat (v, size |
379 | PASS_MEM_STAT)); | |
9771b263 DN |
380 | v->embedded_init (alloc, nelem); |
381 | } | |
ada55151 | 382 | |
ada55151 | 383 | |
9771b263 DN |
384 | /* Allocator type for GC vectors. This is for vectors of types |
385 | atomics w.r.t. collection, so allocation and deallocation is | |
386 | completely inherited from va_gc. */ | |
387 | struct va_gc_atomic : va_gc | |
388 | { | |
389 | }; | |
9ba5ff0f | 390 | |
0823efed | 391 | |
9771b263 DN |
392 | /* Allocator type for stack vectors. */ |
393 | struct va_stack | |
0823efed | 394 | { |
9771b263 DN |
395 | /* Use vl_ptr as the default layout for stack vectors. */ |
396 | typedef vl_ptr default_layout; | |
ada55151 | 397 | |
9771b263 DN |
398 | template<typename T> |
399 | static void alloc (vec<T, va_stack, vl_ptr>&, unsigned, | |
400 | vec<T, va_stack, vl_embed> *); | |
4038c495 | 401 | |
9771b263 DN |
402 | template <typename T> |
403 | static void reserve (vec<T, va_stack, vl_embed> *&, unsigned, bool | |
404 | CXX_MEM_STAT_INFO); | |
4038c495 | 405 | |
9771b263 DN |
406 | template <typename T> |
407 | static void release (vec<T, va_stack, vl_embed> *&); | |
408 | }; | |
409 | ||
410 | /* Helper functions to keep track of vectors allocated on the stack. */ | |
411 | void register_stack_vec (void *); | |
412 | int stack_vec_register_index (void *); | |
413 | void unregister_stack_vec (unsigned); | |
4038c495 | 414 | |
9771b263 DN |
415 | /* Allocate a vector V which uses alloca for the initial allocation. |
416 | SPACE is space allocated using alloca. NELEMS is the number of | |
417 | entries allocated. */ | |
418 | ||
419 | template<typename T> | |
420 | void | |
421 | va_stack::alloc (vec<T, va_stack, vl_ptr> &v, unsigned nelems, | |
422 | vec<T, va_stack, vl_embed> *space) | |
423 | { | |
26da79f5 JJ |
424 | v.vec_ = space; |
425 | register_stack_vec (static_cast<void *> (v.vec_)); | |
426 | v.vec_->embedded_init (nelems, 0); | |
9771b263 | 427 | } |
0823efed | 428 | |
bd0c3bfd | 429 | |
9771b263 DN |
430 | /* Reserve NELEMS slots for a vector initially allocated on the stack. |
431 | When this happens, we switch back to heap allocation. We remove | |
432 | the vector from stack_vecs, if it is there, since we no longer need | |
433 | to avoid freeing it. If EXACT is true, grow exactly, otherwise | |
434 | grow exponentially. */ | |
bd0c3bfd | 435 | |
9771b263 DN |
436 | template<typename T> |
437 | void | |
438 | va_stack::reserve (vec<T, va_stack, vl_embed> *&v, unsigned nelems, bool exact | |
439 | MEM_STAT_DECL) | |
440 | { | |
441 | int ix = stack_vec_register_index (static_cast<void *> (v)); | |
442 | if (ix >= 0) | |
443 | unregister_stack_vec (ix); | |
444 | else | |
445 | { | |
446 | /* V is already on the heap. */ | |
447 | va_heap::reserve (reinterpret_cast<vec<T, va_heap, vl_embed> *&> (v), | |
18e1fd75 | 448 | nelems, exact PASS_MEM_STAT); |
9771b263 DN |
449 | return; |
450 | } | |
bd0c3bfd | 451 | |
9771b263 | 452 | /* Move VEC_ to the heap. */ |
26da79f5 | 453 | nelems += v->vecpfx_.num_; |
9771b263 DN |
454 | vec<T, va_stack, vl_embed> *oldvec = v; |
455 | v = NULL; | |
456 | va_heap::reserve (reinterpret_cast<vec<T, va_heap, vl_embed> *&>(v), nelems, | |
18e1fd75 | 457 | exact PASS_MEM_STAT); |
9771b263 DN |
458 | if (v && oldvec) |
459 | { | |
26da79f5 JJ |
460 | v->vecpfx_.num_ = oldvec->length (); |
461 | memcpy (v->vecdata_, | |
462 | oldvec->vecdata_, | |
38f2ca32 | 463 | oldvec->length () * sizeof (T)); |
9771b263 DN |
464 | } |
465 | } | |
bd0c3bfd | 466 | |
bd0c3bfd | 467 | |
9771b263 DN |
468 | /* Free a vector allocated on the stack. Don't actually free it if we |
469 | find it in the hash table. */ | |
bd0c3bfd | 470 | |
9771b263 DN |
471 | template<typename T> |
472 | void | |
473 | va_stack::release (vec<T, va_stack, vl_embed> *&v) | |
474 | { | |
38f2ca32 DN |
475 | if (v == NULL) |
476 | return; | |
477 | ||
9771b263 DN |
478 | int ix = stack_vec_register_index (static_cast<void *> (v)); |
479 | if (ix >= 0) | |
480 | { | |
481 | unregister_stack_vec (ix); | |
482 | v = NULL; | |
483 | } | |
484 | else | |
485 | { | |
486 | /* The vector was not on the list of vectors allocated on the stack, so it | |
487 | must be allocated on the heap. */ | |
488 | va_heap::release (reinterpret_cast<vec<T, va_heap, vl_embed> *&> (v)); | |
489 | } | |
490 | } | |
bd0c3bfd | 491 | |
bd0c3bfd | 492 | |
9771b263 DN |
493 | /* Generic vector template. Default values for A and L indicate the |
494 | most commonly used strategies. | |
bd0c3bfd | 495 | |
9771b263 DN |
496 | FIXME - Ideally, they would all be vl_ptr to encourage using regular |
497 | instances for vectors, but the existing GTY machinery is limited | |
498 | in that it can only deal with GC objects that are pointers | |
499 | themselves. | |
bd0c3bfd | 500 | |
9771b263 DN |
501 | This means that vector operations that need to deal with |
502 | potentially NULL pointers, must be provided as free | |
503 | functions (see the vec_safe_* functions above). */ | |
504 | template<typename T, | |
505 | typename A = va_heap, | |
506 | typename L = typename A::default_layout> | |
18e1fd75 | 507 | struct GTY((user)) vec |
9771b263 DN |
508 | { |
509 | }; | |
bd0c3bfd | 510 | |
6e1aa848 DN |
511 | /* Type to provide NULL values for vec<T, A, L>. This is used to |
512 | provide nil initializers for vec instances. Since vec must be | |
513 | a POD, we cannot have proper ctor/dtor for it. To initialize | |
514 | a vec instance, you can assign it the value vNULL. */ | |
515 | struct vnull | |
516 | { | |
517 | template <typename T, typename A, typename L> | |
518 | operator vec<T, A, L> () { return vec<T, A, L>(); } | |
519 | }; | |
520 | extern vnull vNULL; | |
521 | ||
bd0c3bfd | 522 | |
9771b263 DN |
523 | /* Embeddable vector. These vectors are suitable to be embedded |
524 | in other data structures so that they can be pre-allocated in a | |
525 | contiguous memory block. | |
bd0c3bfd | 526 | |
9771b263 DN |
527 | Embeddable vectors are implemented using the trailing array idiom, |
528 | thus they are not resizeable without changing the address of the | |
529 | vector object itself. This means you cannot have variables or | |
530 | fields of embeddable vector type -- always use a pointer to a | |
531 | vector. The one exception is the final field of a structure, which | |
532 | could be a vector type. | |
bd0c3bfd | 533 | |
9771b263 DN |
534 | You will have to use the embedded_size & embedded_init calls to |
535 | create such objects, and they will not be resizeable (so the 'safe' | |
536 | allocation variants are not available). | |
537 | ||
538 | Properties: | |
539 | ||
540 | - The whole vector and control data are allocated in a single | |
541 | contiguous block. It uses the trailing-vector idiom, so | |
542 | allocation must reserve enough space for all the elements | |
543 | in the vector plus its control data. | |
544 | - The vector cannot be re-allocated. | |
545 | - The vector cannot grow nor shrink. | |
546 | - No indirections needed for access/manipulation. | |
547 | - It requires 2 words of storage (prior to vector allocation). */ | |
548 | ||
549 | template<typename T, typename A> | |
18e1fd75 | 550 | struct GTY((user)) vec<T, A, vl_embed> |
9771b263 DN |
551 | { |
552 | public: | |
26da79f5 JJ |
553 | unsigned allocated (void) const { return vecpfx_.alloc_; } |
554 | unsigned length (void) const { return vecpfx_.num_; } | |
555 | bool is_empty (void) const { return vecpfx_.num_ == 0; } | |
556 | T *address (void) { return vecdata_; } | |
557 | const T *address (void) const { return vecdata_; } | |
9771b263 DN |
558 | const T &operator[] (unsigned) const; |
559 | T &operator[] (unsigned); | |
560 | T &last (void); | |
561 | bool space (unsigned) const; | |
562 | bool iterate (unsigned, T *) const; | |
563 | bool iterate (unsigned, T **) const; | |
18e1fd75 | 564 | vec *copy (ALONE_CXX_MEM_STAT_INFO) const; |
9771b263 DN |
565 | void splice (vec &); |
566 | void splice (vec *src); | |
567 | T *quick_push (const T &); | |
568 | T &pop (void); | |
569 | void truncate (unsigned); | |
570 | void quick_insert (unsigned, const T &); | |
571 | void ordered_remove (unsigned); | |
572 | void unordered_remove (unsigned); | |
573 | void block_remove (unsigned, unsigned); | |
574 | void qsort (int (*) (const void *, const void *)); | |
575 | unsigned lower_bound (T, bool (*)(const T &, const T &)) const; | |
576 | static size_t embedded_size (unsigned); | |
577 | void embedded_init (unsigned, unsigned = 0); | |
578 | void quick_grow (unsigned len); | |
579 | void quick_grow_cleared (unsigned len); | |
580 | ||
581 | /* vec class can access our internal data and functions. */ | |
18e1fd75 | 582 | template <typename, typename, typename> friend struct vec; |
9771b263 DN |
583 | |
584 | /* The allocator types also need access to our internals. */ | |
585 | friend struct va_gc; | |
586 | friend struct va_gc_atomic; | |
587 | friend struct va_heap; | |
588 | friend struct va_stack; | |
589 | ||
38f2ca32 DN |
590 | /* FIXME - These fields should be private, but we need to cater to |
591 | compilers that have stricter notions of PODness for types. */ | |
26da79f5 JJ |
592 | vec_prefix vecpfx_; |
593 | T vecdata_[1]; | |
9771b263 | 594 | }; |
bd0c3bfd | 595 | |
bd0c3bfd | 596 | |
9771b263 DN |
597 | /* Convenience wrapper functions to use when dealing with pointers to |
598 | embedded vectors. Some functionality for these vectors must be | |
599 | provided via free functions for these reasons: | |
0823efed | 600 | |
9771b263 | 601 | 1- The pointer may be NULL (e.g., before initial allocation). |
0823efed | 602 | |
9771b263 DN |
603 | 2- When the vector needs to grow, it must be reallocated, so |
604 | the pointer will change its value. | |
0823efed | 605 | |
9771b263 DN |
606 | Because of limitations with the current GC machinery, all vectors |
607 | in GC memory *must* be pointers. */ | |
0823efed | 608 | |
bd0c3bfd | 609 | |
9771b263 DN |
610 | /* If V contains no room for NELEMS elements, return false. Otherwise, |
611 | return true. */ | |
612 | template<typename T, typename A> | |
613 | inline bool | |
614 | vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems) | |
615 | { | |
616 | return v ? v->space (nelems) : nelems == 0; | |
617 | } | |
bd0c3bfd | 618 | |
0823efed | 619 | |
9771b263 DN |
620 | /* If V is NULL, return 0. Otherwise, return V->length(). */ |
621 | template<typename T, typename A> | |
bd0c3bfd | 622 | inline unsigned |
9771b263 | 623 | vec_safe_length (const vec<T, A, vl_embed> *v) |
0823efed | 624 | { |
9771b263 | 625 | return v ? v->length () : 0; |
0823efed | 626 | } |
4038c495 GB |
627 | |
628 | ||
9771b263 DN |
629 | /* If V is NULL, return NULL. Otherwise, return V->address(). */ |
630 | template<typename T, typename A> | |
631 | inline T * | |
632 | vec_safe_address (vec<T, A, vl_embed> *v) | |
633 | { | |
634 | return v ? v->address () : NULL; | |
635 | } | |
636 | ||
bd0c3bfd | 637 | |
9771b263 DN |
638 | /* If V is NULL, return true. Otherwise, return V->is_empty(). */ |
639 | template<typename T, typename A> | |
bd0c3bfd | 640 | inline bool |
9771b263 | 641 | vec_safe_is_empty (vec<T, A, vl_embed> *v) |
bd0c3bfd | 642 | { |
9771b263 | 643 | return v ? v->is_empty () : true; |
bd0c3bfd | 644 | } |
ada55151 | 645 | |
9ba5ff0f | 646 | |
9771b263 DN |
647 | /* If V does not have space for NELEMS elements, call |
648 | V->reserve(NELEMS, EXACT). */ | |
649 | template<typename T, typename A> | |
650 | inline bool | |
651 | vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false | |
18e1fd75 | 652 | CXX_MEM_STAT_INFO) |
9771b263 DN |
653 | { |
654 | bool extend = nelems ? !vec_safe_space (v, nelems) : false; | |
655 | if (extend) | |
656 | A::reserve (v, nelems, exact PASS_MEM_STAT); | |
657 | return extend; | |
658 | } | |
0823efed | 659 | |
9771b263 DN |
660 | template<typename T, typename A> |
661 | inline bool | |
18e1fd75 DN |
662 | vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems |
663 | CXX_MEM_STAT_INFO) | |
0823efed | 664 | { |
9771b263 | 665 | return vec_safe_reserve (v, nelems, true PASS_MEM_STAT); |
0823efed DN |
666 | } |
667 | ||
ada55151 | 668 | |
9771b263 DN |
669 | /* Allocate GC memory for V with space for NELEMS slots. If NELEMS |
670 | is 0, V is initialized to NULL. */ | |
bd0c3bfd | 671 | |
9771b263 DN |
672 | template<typename T, typename A> |
673 | inline void | |
18e1fd75 | 674 | vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO) |
bd0c3bfd | 675 | { |
9771b263 | 676 | v = NULL; |
18e1fd75 | 677 | vec_safe_reserve (v, nelems, false PASS_MEM_STAT); |
bd0c3bfd | 678 | } |
ada55151 | 679 | |
0823efed | 680 | |
9771b263 | 681 | /* Free the GC memory allocated by vector V and set it to NULL. */ |
0823efed | 682 | |
9771b263 DN |
683 | template<typename T, typename A> |
684 | inline void | |
685 | vec_free (vec<T, A, vl_embed> *&v) | |
0823efed | 686 | { |
9771b263 | 687 | A::release (v); |
0823efed DN |
688 | } |
689 | ||
9771b263 DN |
690 | |
691 | /* Grow V to length LEN. Allocate it, if necessary. */ | |
692 | template<typename T, typename A> | |
693 | inline void | |
18e1fd75 | 694 | vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO) |
0823efed | 695 | { |
9771b263 DN |
696 | unsigned oldlen = vec_safe_length (v); |
697 | gcc_checking_assert (len >= oldlen); | |
698 | vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT); | |
18e1fd75 | 699 | v->quick_grow (len); |
0823efed | 700 | } |
9ba5ff0f | 701 | |
ada55151 | 702 | |
9771b263 DN |
703 | /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */ |
704 | template<typename T, typename A> | |
705 | inline void | |
18e1fd75 | 706 | vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO) |
9771b263 DN |
707 | { |
708 | unsigned oldlen = vec_safe_length (v); | |
709 | vec_safe_grow (v, len PASS_MEM_STAT); | |
710 | memset (&(v->address()[oldlen]), 0, sizeof (T) * (len - oldlen)); | |
711 | } | |
ada55151 | 712 | |
0823efed | 713 | |
9771b263 DN |
714 | /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */ |
715 | template<typename T, typename A> | |
716 | inline bool | |
717 | vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr) | |
0823efed | 718 | { |
9771b263 DN |
719 | if (v) |
720 | return v->iterate (ix, ptr); | |
0823efed DN |
721 | else |
722 | { | |
723 | *ptr = 0; | |
724 | return false; | |
725 | } | |
726 | } | |
727 | ||
9771b263 DN |
728 | template<typename T, typename A> |
729 | inline bool | |
730 | vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr) | |
0823efed | 731 | { |
9771b263 DN |
732 | if (v) |
733 | return v->iterate (ix, ptr); | |
0823efed DN |
734 | else |
735 | { | |
736 | *ptr = 0; | |
737 | return false; | |
738 | } | |
739 | } | |
ada55151 | 740 | |
bd0c3bfd | 741 | |
9771b263 DN |
742 | /* If V has no room for one more element, reallocate it. Then call |
743 | V->quick_push(OBJ). */ | |
744 | template<typename T, typename A> | |
745 | inline T * | |
18e1fd75 | 746 | vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO) |
9771b263 DN |
747 | { |
748 | vec_safe_reserve (v, 1, false PASS_MEM_STAT); | |
18e1fd75 | 749 | return v->quick_push (obj); |
9771b263 | 750 | } |
ac47786e | 751 | |
ac47786e | 752 | |
9771b263 DN |
753 | /* if V has no room for one more element, reallocate it. Then call |
754 | V->quick_insert(IX, OBJ). */ | |
755 | template<typename T, typename A> | |
756 | inline void | |
757 | vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj | |
18e1fd75 | 758 | CXX_MEM_STAT_INFO) |
9771b263 DN |
759 | { |
760 | vec_safe_reserve (v, 1, false PASS_MEM_STAT); | |
761 | v->quick_insert (ix, obj); | |
762 | } | |
8ffa0351 | 763 | |
8ffa0351 | 764 | |
9771b263 DN |
765 | /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */ |
766 | template<typename T, typename A> | |
767 | inline void | |
768 | vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size) | |
769 | { | |
770 | if (v) | |
771 | v->truncate (size); | |
772 | } | |
c021f10b | 773 | |
c021f10b | 774 | |
9771b263 DN |
775 | /* If SRC is not NULL, return a pointer to a copy of it. */ |
776 | template<typename T, typename A> | |
777 | inline vec<T, A, vl_embed> * | |
778 | vec_safe_copy (vec<T, A, vl_embed> *src) | |
779 | { | |
780 | return src ? src->copy () : NULL; | |
781 | } | |
0823efed | 782 | |
9771b263 DN |
783 | /* Copy the elements from SRC to the end of DST as if by memcpy. |
784 | Reallocate DST, if necessary. */ | |
785 | template<typename T, typename A> | |
786 | inline void | |
787 | vec_safe_splice (vec<T, A, vl_embed> *&dst, vec<T, A, vl_embed> *src | |
18e1fd75 | 788 | CXX_MEM_STAT_INFO) |
9771b263 DN |
789 | { |
790 | unsigned src_len = vec_safe_length (src); | |
791 | if (src_len) | |
792 | { | |
18e1fd75 DN |
793 | vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len |
794 | PASS_MEM_STAT); | |
9771b263 DN |
795 | dst->splice (*src); |
796 | } | |
797 | } | |
0823efed | 798 | |
0823efed | 799 | |
9771b263 DN |
800 | /* Index into vector. Return the IX'th element. IX must be in the |
801 | domain of the vector. */ | |
0823efed | 802 | |
9771b263 DN |
803 | template<typename T, typename A> |
804 | inline const T & | |
805 | vec<T, A, vl_embed>::operator[] (unsigned ix) const | |
806 | { | |
26da79f5 JJ |
807 | gcc_checking_assert (ix < vecpfx_.num_); |
808 | return vecdata_[ix]; | |
9771b263 | 809 | } |
0823efed | 810 | |
9771b263 DN |
811 | template<typename T, typename A> |
812 | inline T & | |
813 | vec<T, A, vl_embed>::operator[] (unsigned ix) | |
0823efed | 814 | { |
26da79f5 JJ |
815 | gcc_checking_assert (ix < vecpfx_.num_); |
816 | return vecdata_[ix]; | |
0823efed DN |
817 | } |
818 | ||
bd0c3bfd | 819 | |
9771b263 | 820 | /* Get the final element of the vector, which must not be empty. */ |
0823efed | 821 | |
9771b263 DN |
822 | template<typename T, typename A> |
823 | inline T & | |
824 | vec<T, A, vl_embed>::last (void) | |
0823efed | 825 | { |
26da79f5 JJ |
826 | gcc_checking_assert (vecpfx_.num_ > 0); |
827 | return (*this)[vecpfx_.num_ - 1]; | |
0823efed DN |
828 | } |
829 | ||
830 | ||
9771b263 DN |
831 | /* If this vector has space for NELEMS additional entries, return |
832 | true. You usually only need to use this if you are doing your | |
833 | own vector reallocation, for instance on an embedded vector. This | |
834 | returns true in exactly the same circumstances that vec::reserve | |
835 | will. */ | |
bd0c3bfd | 836 | |
9771b263 DN |
837 | template<typename T, typename A> |
838 | inline bool | |
839 | vec<T, A, vl_embed>::space (unsigned nelems) const | |
840 | { | |
26da79f5 | 841 | return vecpfx_.alloc_ - vecpfx_.num_ >= nelems; |
9771b263 | 842 | } |
bd0c3bfd | 843 | |
bd0c3bfd | 844 | |
9771b263 DN |
845 | /* Return iteration condition and update PTR to point to the IX'th |
846 | element of this vector. Use this to iterate over the elements of a | |
847 | vector as follows, | |
bd0c3bfd | 848 | |
9771b263 DN |
849 | for (ix = 0; vec<T, A>::iterate(v, ix, &ptr); ix++) |
850 | continue; */ | |
bd0c3bfd | 851 | |
9771b263 DN |
852 | template<typename T, typename A> |
853 | inline bool | |
854 | vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const | |
0823efed | 855 | { |
26da79f5 | 856 | if (ix < vecpfx_.num_) |
9771b263 | 857 | { |
26da79f5 | 858 | *ptr = vecdata_[ix]; |
9771b263 DN |
859 | return true; |
860 | } | |
861 | else | |
862 | { | |
863 | *ptr = 0; | |
864 | return false; | |
865 | } | |
0823efed DN |
866 | } |
867 | ||
9ba5ff0f | 868 | |
9771b263 DN |
869 | /* Return iteration condition and update *PTR to point to the |
870 | IX'th element of this vector. Use this to iterate over the | |
871 | elements of a vector as follows, | |
ada55151 | 872 | |
9771b263 DN |
873 | for (ix = 0; v->iterate(ix, &ptr); ix++) |
874 | continue; | |
4c254e68 | 875 | |
9771b263 DN |
876 | This variant is for vectors of objects. */ |
877 | ||
878 | template<typename T, typename A> | |
879 | inline bool | |
880 | vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const | |
0823efed | 881 | { |
26da79f5 | 882 | if (ix < vecpfx_.num_) |
9771b263 | 883 | { |
26da79f5 | 884 | *ptr = CONST_CAST (T *, &vecdata_[ix]); |
9771b263 DN |
885 | return true; |
886 | } | |
887 | else | |
0823efed | 888 | { |
9771b263 DN |
889 | *ptr = 0; |
890 | return false; | |
0823efed | 891 | } |
0823efed | 892 | } |
9ba5ff0f | 893 | |
9ba5ff0f | 894 | |
9771b263 | 895 | /* Return a pointer to a copy of this vector. */ |
4038c495 | 896 | |
9771b263 DN |
897 | template<typename T, typename A> |
898 | inline vec<T, A, vl_embed> * | |
899 | vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const | |
0823efed | 900 | { |
9771b263 DN |
901 | vec<T, A, vl_embed> *new_vec = NULL; |
902 | unsigned len = length (); | |
bd0c3bfd | 903 | if (len) |
0823efed | 904 | { |
9771b263 | 905 | vec_alloc (new_vec, len PASS_MEM_STAT); |
bd0c3bfd | 906 | new_vec->embedded_init (len, len); |
26da79f5 | 907 | memcpy (new_vec->address(), vecdata_, sizeof (T) * len); |
0823efed | 908 | } |
bd0c3bfd DN |
909 | return new_vec; |
910 | } | |
b8698a0f | 911 | |
9ba5ff0f | 912 | |
9771b263 DN |
913 | /* Copy the elements from SRC to the end of this vector as if by memcpy. |
914 | The vector must have sufficient headroom available. */ | |
9ba5ff0f | 915 | |
9771b263 DN |
916 | template<typename T, typename A> |
917 | inline void | |
918 | vec<T, A, vl_embed>::splice (vec<T, A, vl_embed> &src) | |
919 | { | |
920 | unsigned len = src.length(); | |
921 | if (len) | |
922 | { | |
923 | gcc_checking_assert (space (len)); | |
924 | memcpy (address() + length(), src.address(), len * sizeof (T)); | |
26da79f5 | 925 | vecpfx_.num_ += len; |
9771b263 DN |
926 | } |
927 | } | |
928 | ||
929 | template<typename T, typename A> | |
930 | inline void | |
931 | vec<T, A, vl_embed>::splice (vec<T, A, vl_embed> *src) | |
0823efed | 932 | { |
9771b263 DN |
933 | if (src) |
934 | splice (*src); | |
0823efed DN |
935 | } |
936 | ||
ada55151 | 937 | |
9771b263 DN |
938 | /* Push OBJ (a new element) onto the end of the vector. There must be |
939 | sufficient space in the vector. Return a pointer to the slot | |
940 | where OBJ was inserted. */ | |
9ba5ff0f | 941 | |
9771b263 DN |
942 | template<typename T, typename A> |
943 | inline T * | |
944 | vec<T, A, vl_embed>::quick_push (const T &obj) | |
0823efed | 945 | { |
9771b263 | 946 | gcc_checking_assert (space (1)); |
26da79f5 | 947 | T *slot = &vecdata_[vecpfx_.num_++]; |
9771b263 DN |
948 | *slot = obj; |
949 | return slot; | |
0823efed DN |
950 | } |
951 | ||
ada55151 | 952 | |
9771b263 | 953 | /* Pop and return the last element off the end of the vector. */ |
efb7e1e0 | 954 | |
9771b263 DN |
955 | template<typename T, typename A> |
956 | inline T & | |
957 | vec<T, A, vl_embed>::pop (void) | |
0823efed | 958 | { |
9771b263 | 959 | gcc_checking_assert (length () > 0); |
26da79f5 | 960 | return vecdata_[--vecpfx_.num_]; |
9771b263 | 961 | } |
0823efed | 962 | |
0823efed | 963 | |
9771b263 DN |
964 | /* Set the length of the vector to SIZE. The new length must be less |
965 | than or equal to the current length. This is an O(1) operation. */ | |
966 | ||
967 | template<typename T, typename A> | |
968 | inline void | |
969 | vec<T, A, vl_embed>::truncate (unsigned size) | |
970 | { | |
971 | gcc_checking_assert (length () >= size); | |
26da79f5 | 972 | vecpfx_.num_ = size; |
0823efed DN |
973 | } |
974 | ||
efb7e1e0 | 975 | |
9771b263 DN |
976 | /* Insert an element, OBJ, at the IXth position of this vector. There |
977 | must be sufficient space. */ | |
0823efed | 978 | |
9771b263 DN |
979 | template<typename T, typename A> |
980 | inline void | |
981 | vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj) | |
0823efed | 982 | { |
9771b263 DN |
983 | gcc_checking_assert (length () < allocated ()); |
984 | gcc_checking_assert (ix <= length ()); | |
26da79f5 JJ |
985 | T *slot = &vecdata_[ix]; |
986 | memmove (slot + 1, slot, (vecpfx_.num_++ - ix) * sizeof (T)); | |
9771b263 | 987 | *slot = obj; |
0823efed DN |
988 | } |
989 | ||
989ea525 | 990 | |
9771b263 DN |
991 | /* Remove an element from the IXth position of this vector. Ordering of |
992 | remaining elements is preserved. This is an O(N) operation due to | |
993 | memmove. */ | |
989ea525 | 994 | |
9771b263 DN |
995 | template<typename T, typename A> |
996 | inline void | |
997 | vec<T, A, vl_embed>::ordered_remove (unsigned ix) | |
0823efed | 998 | { |
9771b263 | 999 | gcc_checking_assert (ix < length()); |
26da79f5 JJ |
1000 | T *slot = &vecdata_[ix]; |
1001 | memmove (slot, slot + 1, (--vecpfx_.num_ - ix) * sizeof (T)); | |
9771b263 DN |
1002 | } |
1003 | ||
1004 | ||
1005 | /* Remove an element from the IXth position of this vector. Ordering of | |
1006 | remaining elements is destroyed. This is an O(1) operation. */ | |
1007 | ||
1008 | template<typename T, typename A> | |
1009 | inline void | |
1010 | vec<T, A, vl_embed>::unordered_remove (unsigned ix) | |
1011 | { | |
1012 | gcc_checking_assert (ix < length()); | |
26da79f5 | 1013 | vecdata_[ix] = vecdata_[--vecpfx_.num_]; |
9771b263 DN |
1014 | } |
1015 | ||
1016 | ||
1017 | /* Remove LEN elements starting at the IXth. Ordering is retained. | |
1018 | This is an O(N) operation due to memmove. */ | |
1019 | ||
1020 | template<typename T, typename A> | |
1021 | inline void | |
1022 | vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len) | |
1023 | { | |
1024 | gcc_checking_assert (ix + len <= length()); | |
26da79f5 JJ |
1025 | T *slot = &vecdata_[ix]; |
1026 | vecpfx_.num_ -= len; | |
1027 | memmove (slot, slot + len, (vecpfx_.num_ - ix) * sizeof (T)); | |
9771b263 DN |
1028 | } |
1029 | ||
1030 | ||
1031 | /* Sort the contents of this vector with qsort. CMP is the comparison | |
1032 | function to pass to qsort. */ | |
1033 | ||
1034 | template<typename T, typename A> | |
1035 | inline void | |
1036 | vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *)) | |
1037 | { | |
1038 | ::qsort (address(), length(), sizeof (T), cmp); | |
1039 | } | |
1040 | ||
1041 | ||
1042 | /* Find and return the first position in which OBJ could be inserted | |
1043 | without changing the ordering of this vector. LESSTHAN is a | |
1044 | function that returns true if the first argument is strictly less | |
1045 | than the second. */ | |
1046 | ||
1047 | template<typename T, typename A> | |
1048 | unsigned | |
1049 | vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &)) | |
1050 | const | |
1051 | { | |
1052 | unsigned int len = length (); | |
1053 | unsigned int half, middle; | |
1054 | unsigned int first = 0; | |
1055 | while (len > 0) | |
0823efed | 1056 | { |
9771b263 DN |
1057 | half = len / 2; |
1058 | middle = first; | |
1059 | middle += half; | |
1060 | T middle_elem = (*this)[middle]; | |
1061 | if (lessthan (middle_elem, obj)) | |
1062 | { | |
1063 | first = middle; | |
1064 | ++first; | |
1065 | len = len - half - 1; | |
1066 | } | |
1067 | else | |
1068 | len = half; | |
0823efed | 1069 | } |
9771b263 | 1070 | return first; |
0823efed DN |
1071 | } |
1072 | ||
0823efed | 1073 | |
9771b263 DN |
1074 | /* Return the number of bytes needed to embed an instance of an |
1075 | embeddable vec inside another data structure. | |
bd0c3bfd | 1076 | |
9771b263 DN |
1077 | Use these methods to determine the required size and initialization |
1078 | of a vector V of type T embedded within another structure (as the | |
1079 | final member): | |
1080 | ||
1081 | size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc); | |
1082 | void v->embedded_init(unsigned alloc, unsigned num); | |
1083 | ||
1084 | These allow the caller to perform the memory allocation. */ | |
1085 | ||
1086 | template<typename T, typename A> | |
1087 | inline size_t | |
1088 | vec<T, A, vl_embed>::embedded_size (unsigned alloc) | |
0823efed | 1089 | { |
9771b263 | 1090 | typedef vec<T, A, vl_embed> vec_embedded; |
26da79f5 | 1091 | return offsetof (vec_embedded, vecdata_) + alloc * sizeof (T); |
0823efed DN |
1092 | } |
1093 | ||
ada55151 | 1094 | |
9771b263 DN |
1095 | /* Initialize the vector to contain room for ALLOC elements and |
1096 | NUM active elements. */ | |
bd0c3bfd | 1097 | |
9771b263 DN |
1098 | template<typename T, typename A> |
1099 | inline void | |
1100 | vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num) | |
0823efed | 1101 | { |
26da79f5 JJ |
1102 | vecpfx_.alloc_ = alloc; |
1103 | vecpfx_.num_ = num; | |
0823efed DN |
1104 | } |
1105 | ||
ada55151 | 1106 | |
9771b263 DN |
1107 | /* Grow the vector to a specific length. LEN must be as long or longer than |
1108 | the current length. The new elements are uninitialized. */ | |
0823efed | 1109 | |
9771b263 DN |
1110 | template<typename T, typename A> |
1111 | inline void | |
1112 | vec<T, A, vl_embed>::quick_grow (unsigned len) | |
0823efed | 1113 | { |
26da79f5 JJ |
1114 | gcc_checking_assert (length () <= len && len <= vecpfx_.alloc_); |
1115 | vecpfx_.num_ = len; | |
0823efed DN |
1116 | } |
1117 | ||
ada55151 | 1118 | |
9771b263 DN |
1119 | /* Grow the vector to a specific length. LEN must be as long or longer than |
1120 | the current length. The new elements are initialized to zero. */ | |
0823efed | 1121 | |
9771b263 DN |
1122 | template<typename T, typename A> |
1123 | inline void | |
1124 | vec<T, A, vl_embed>::quick_grow_cleared (unsigned len) | |
0823efed | 1125 | { |
9771b263 DN |
1126 | unsigned oldlen = length (); |
1127 | quick_grow (len); | |
1128 | memset (&(address()[oldlen]), 0, sizeof (T) * (len - oldlen)); | |
0823efed DN |
1129 | } |
1130 | ||
d4e6fecb | 1131 | |
9771b263 | 1132 | /* Garbage collection support for vec<T, A, vl_embed>. */ |
d4e6fecb | 1133 | |
bd0c3bfd | 1134 | template<typename T> |
bd0c3bfd | 1135 | void |
9771b263 | 1136 | gt_ggc_mx (vec<T, va_gc> *v) |
0823efed | 1137 | { |
9771b263 DN |
1138 | extern void gt_ggc_mx (T &); |
1139 | for (unsigned i = 0; i < v->length (); i++) | |
1140 | gt_ggc_mx ((*v)[i]); | |
0823efed DN |
1141 | } |
1142 | ||
bd0c3bfd | 1143 | template<typename T> |
bd0c3bfd | 1144 | void |
9771b263 | 1145 | gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED) |
0823efed | 1146 | { |
9771b263 DN |
1147 | /* Nothing to do. Vectors of atomic types wrt GC do not need to |
1148 | be traversed. */ | |
0823efed DN |
1149 | } |
1150 | ||
a590ac65 | 1151 | |
9771b263 | 1152 | /* PCH support for vec<T, A, vl_embed>. */ |
0823efed | 1153 | |
9771b263 | 1154 | template<typename T, typename A> |
bd0c3bfd | 1155 | void |
9771b263 | 1156 | gt_pch_nx (vec<T, A, vl_embed> *v) |
0823efed | 1157 | { |
9771b263 DN |
1158 | extern void gt_pch_nx (T &); |
1159 | for (unsigned i = 0; i < v->length (); i++) | |
1160 | gt_pch_nx ((*v)[i]); | |
0823efed DN |
1161 | } |
1162 | ||
9771b263 DN |
1163 | template<typename T, typename A> |
1164 | void | |
1165 | gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie) | |
1166 | { | |
1167 | for (unsigned i = 0; i < v->length (); i++) | |
1168 | op (&((*v)[i]), cookie); | |
1169 | } | |
ada55151 | 1170 | |
9771b263 | 1171 | template<typename T, typename A> |
bd0c3bfd | 1172 | void |
9771b263 | 1173 | gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie) |
bd0c3bfd | 1174 | { |
9771b263 DN |
1175 | extern void gt_pch_nx (T *, gt_pointer_operator, void *); |
1176 | for (unsigned i = 0; i < v->length (); i++) | |
1177 | gt_pch_nx (&((*v)[i]), op, cookie); | |
bd0c3bfd | 1178 | } |
9ba5ff0f | 1179 | |
bd0c3bfd | 1180 | |
9771b263 DN |
1181 | /* Space efficient vector. These vectors can grow dynamically and are |
1182 | allocated together with their control data. They are suited to be | |
1183 | included in data structures. Prior to initial allocation, they | |
1184 | only take a single word of storage. | |
1185 | ||
1186 | These vectors are implemented as a pointer to an embeddable vector. | |
1187 | The semantics allow for this pointer to be NULL to represent empty | |
1188 | vectors. This way, empty vectors occupy minimal space in the | |
1189 | structure containing them. | |
1190 | ||
1191 | Properties: | |
1192 | ||
1193 | - The whole vector and control data are allocated in a single | |
1194 | contiguous block. | |
1195 | - The whole vector may be re-allocated. | |
1196 | - Vector data may grow and shrink. | |
1197 | - Access and manipulation requires a pointer test and | |
1198 | indirection. | |
1199 | - It requires 1 word of storage (prior to vector allocation). | |
1200 | ||
1201 | ||
1202 | Limitations: | |
1203 | ||
1204 | These vectors must be PODs because they are stored in unions. | |
1205 | (http://en.wikipedia.org/wiki/Plain_old_data_structures). | |
1206 | As long as we use C++03, we cannot have constructors nor | |
1207 | destructors in classes that are stored in unions. */ | |
1208 | ||
1209 | template<typename T, typename A> | |
18e1fd75 | 1210 | struct vec<T, A, vl_ptr> |
9771b263 DN |
1211 | { |
1212 | public: | |
1213 | /* Memory allocation and deallocation for the embedded vector. | |
1214 | Needed because we cannot have proper ctors/dtors defined. */ | |
1215 | void create (unsigned nelems CXX_MEM_STAT_INFO); | |
1216 | void release (void); | |
1217 | ||
1218 | /* Vector operations. */ | |
1219 | bool exists (void) const | |
26da79f5 | 1220 | { return vec_ != NULL; } |
9771b263 DN |
1221 | |
1222 | bool is_empty (void) const | |
26da79f5 | 1223 | { return vec_ ? vec_->is_empty() : true; } |
9771b263 DN |
1224 | |
1225 | unsigned length (void) const | |
26da79f5 | 1226 | { return vec_ ? vec_->length() : 0; } |
9771b263 DN |
1227 | |
1228 | T *address (void) | |
26da79f5 | 1229 | { return vec_ ? vec_->vecdata_ : NULL; } |
9771b263 DN |
1230 | |
1231 | const T *address (void) const | |
26da79f5 | 1232 | { return vec_ ? vec_->vecdata_ : NULL; } |
9771b263 DN |
1233 | |
1234 | const T &operator[] (unsigned ix) const | |
26da79f5 | 1235 | { return (*vec_)[ix]; } |
9771b263 DN |
1236 | |
1237 | bool operator!=(const vec &other) const | |
1238 | { return !(*this == other); } | |
1239 | ||
1240 | bool operator==(const vec &other) const | |
1241 | { return address() == other.address(); } | |
1242 | ||
1243 | T &operator[] (unsigned ix) | |
26da79f5 | 1244 | { return (*vec_)[ix]; } |
9771b263 DN |
1245 | |
1246 | T &last (void) | |
26da79f5 | 1247 | { return vec_->last(); } |
9771b263 DN |
1248 | |
1249 | bool space (int nelems) const | |
26da79f5 | 1250 | { return vec_ ? vec_->space (nelems) : nelems == 0; } |
9771b263 DN |
1251 | |
1252 | bool iterate (unsigned ix, T *p) const; | |
1253 | bool iterate (unsigned ix, T **p) const; | |
1254 | vec copy (ALONE_CXX_MEM_STAT_INFO) const; | |
1255 | bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO); | |
1256 | bool reserve_exact (unsigned CXX_MEM_STAT_INFO); | |
1257 | void splice (vec &); | |
1258 | void safe_splice (vec & CXX_MEM_STAT_INFO); | |
1259 | T *quick_push (const T &); | |
1260 | T *safe_push (const T &CXX_MEM_STAT_INFO); | |
1261 | T &pop (void); | |
1262 | void truncate (unsigned); | |
1263 | void safe_grow (unsigned CXX_MEM_STAT_INFO); | |
1264 | void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO); | |
1265 | void quick_grow (unsigned); | |
1266 | void quick_grow_cleared (unsigned); | |
1267 | void quick_insert (unsigned, const T &); | |
1268 | void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO); | |
1269 | void ordered_remove (unsigned); | |
1270 | void unordered_remove (unsigned); | |
1271 | void block_remove (unsigned, unsigned); | |
1272 | void qsort (int (*) (const void *, const void *)); | |
1273 | unsigned lower_bound (T, bool (*)(const T &, const T &)) const; | |
1274 | ||
1275 | template<typename T1> | |
1276 | friend void va_stack::alloc(vec<T1, va_stack, vl_ptr>&, unsigned, | |
1277 | vec<T1, va_stack, vl_embed> *); | |
1278 | ||
38f2ca32 DN |
1279 | /* FIXME - This field should be private, but we need to cater to |
1280 | compilers that have stricter notions of PODness for types. */ | |
26da79f5 | 1281 | vec<T, A, vl_embed> *vec_; |
9771b263 DN |
1282 | }; |
1283 | ||
1284 | ||
1285 | /* Empty specialization for GC allocation. This will prevent GC | |
1286 | vectors from using the vl_ptr layout. FIXME: This is needed to | |
1287 | circumvent limitations in the GTY machinery. */ | |
bd0c3bfd | 1288 | |
0823efed | 1289 | template<typename T> |
18e1fd75 | 1290 | struct vec<T, va_gc, vl_ptr> |
0823efed | 1291 | { |
9771b263 | 1292 | }; |
0823efed | 1293 | |
ada55151 | 1294 | |
9771b263 DN |
1295 | /* Allocate heap memory for pointer V and create the internal vector |
1296 | with space for NELEMS elements. If NELEMS is 0, the internal | |
1297 | vector is initialized to empty. */ | |
9ba5ff0f | 1298 | |
0823efed | 1299 | template<typename T> |
9771b263 | 1300 | inline void |
18e1fd75 | 1301 | vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO) |
0823efed | 1302 | { |
9771b263 DN |
1303 | v = new vec<T>; |
1304 | v->create (nelems PASS_MEM_STAT); | |
0823efed DN |
1305 | } |
1306 | ||
ada55151 | 1307 | |
9771b263 | 1308 | /* Conditionally allocate heap memory for VEC and its internal vector. */ |
9ba5ff0f | 1309 | |
0823efed | 1310 | template<typename T> |
9771b263 | 1311 | inline void |
18e1fd75 | 1312 | vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO) |
0823efed | 1313 | { |
9771b263 DN |
1314 | if (!vec) |
1315 | vec_alloc (vec, nelems PASS_MEM_STAT); | |
0823efed DN |
1316 | } |
1317 | ||
ada55151 | 1318 | |
9771b263 | 1319 | /* Free the heap memory allocated by vector V and set it to NULL. */ |
9e28024a | 1320 | |
0823efed | 1321 | template<typename T> |
9771b263 DN |
1322 | inline void |
1323 | vec_free (vec<T> *&v) | |
0823efed | 1324 | { |
9771b263 DN |
1325 | if (v == NULL) |
1326 | return; | |
1327 | ||
1328 | v->release (); | |
1329 | delete v; | |
1330 | v = NULL; | |
0823efed | 1331 | } |
9ba5ff0f | 1332 | |
aaaa46d2 | 1333 | |
9771b263 DN |
1334 | /* Allocate a new stack vector with space for exactly NELEMS objects. |
1335 | If NELEMS is zero, NO vector is created. | |
b8698a0f | 1336 | |
9771b263 DN |
1337 | For the stack allocator, no memory is really allocated. The vector |
1338 | is initialized to be at address SPACE and contain NELEMS slots. | |
1339 | Memory allocation actually occurs in the expansion of VEC_alloc. | |
58152808 | 1340 | |
9771b263 DN |
1341 | Usage notes: |
1342 | ||
1343 | * This does not allocate an instance of vec<T, A>. It allocates the | |
1344 | actual vector of elements (i.e., vec<T, A, vl_embed>) inside a | |
1345 | vec<T, A> instance. | |
1346 | ||
1347 | * This allocator must always be a macro: | |
1348 | ||
1349 | We support a vector which starts out with space on the stack and | |
1350 | switches to heap space when forced to reallocate. This works a | |
1351 | little differently. In the case of stack vectors, vec_alloc will | |
1352 | expand to a call to vec_alloc_1 that calls XALLOCAVAR to request | |
1353 | the initial allocation. This uses alloca to get the initial | |
1354 | space. Since alloca can not be usefully called in an inline | |
1355 | function, vec_alloc must always be a macro. | |
1356 | ||
1357 | Important limitations of stack vectors: | |
1358 | ||
1359 | - Only the initial allocation will be made using alloca, so pass | |
1360 | a reasonable estimate that doesn't use too much stack space; | |
1361 | don't pass zero. | |
1362 | ||
1363 | - Don't return a stack-allocated vector from the function which | |
1364 | allocated it. */ | |
1365 | ||
1366 | #define vec_stack_alloc(T,V,N) \ | |
1367 | do { \ | |
1368 | typedef vec<T, va_stack, vl_embed> stackv; \ | |
1369 | va_stack::alloc (V, N, XALLOCAVAR (stackv, stackv::embedded_size (N)));\ | |
1370 | } while (0) | |
1371 | ||
1372 | ||
1373 | /* Return iteration condition and update PTR to point to the IX'th | |
1374 | element of this vector. Use this to iterate over the elements of a | |
1375 | vector as follows, | |
1376 | ||
1377 | for (ix = 0; v.iterate(ix, &ptr); ix++) | |
1378 | continue; */ | |
1379 | ||
1380 | template<typename T, typename A> | |
1381 | inline bool | |
1382 | vec<T, A, vl_ptr>::iterate (unsigned ix, T *ptr) const | |
fc64b448 | 1383 | { |
26da79f5 JJ |
1384 | if (vec_) |
1385 | return vec_->iterate (ix, ptr); | |
9771b263 | 1386 | else |
0823efed | 1387 | { |
9771b263 DN |
1388 | *ptr = 0; |
1389 | return false; | |
0823efed | 1390 | } |
a0ef884f NS |
1391 | } |
1392 | ||
bd0c3bfd | 1393 | |
9771b263 DN |
1394 | /* Return iteration condition and update *PTR to point to the |
1395 | IX'th element of this vector. Use this to iterate over the | |
1396 | elements of a vector as follows, | |
1397 | ||
1398 | for (ix = 0; v->iterate(ix, &ptr); ix++) | |
1399 | continue; | |
c2569604 | 1400 | |
9771b263 | 1401 | This variant is for vectors of objects. */ |
c2569604 | 1402 | |
9771b263 DN |
1403 | template<typename T, typename A> |
1404 | inline bool | |
1405 | vec<T, A, vl_ptr>::iterate (unsigned ix, T **ptr) const | |
0823efed | 1406 | { |
26da79f5 JJ |
1407 | if (vec_) |
1408 | return vec_->iterate (ix, ptr); | |
9771b263 | 1409 | else |
bd0c3bfd | 1410 | { |
9771b263 DN |
1411 | *ptr = 0; |
1412 | return false; | |
bd0c3bfd | 1413 | } |
9771b263 DN |
1414 | } |
1415 | ||
1416 | ||
1417 | /* Convenience macro for forward iteration. */ | |
1418 | #define FOR_EACH_VEC_ELT(V, I, P) \ | |
1419 | for (I = 0; (V).iterate ((I), &(P)); ++(I)) | |
1420 | ||
1421 | #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \ | |
1422 | for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I)) | |
1423 | ||
1424 | /* Likewise, but start from FROM rather than 0. */ | |
1425 | #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \ | |
1426 | for (I = (FROM); (V).iterate ((I), &(P)); ++(I)) | |
bd0c3bfd | 1427 | |
9771b263 DN |
1428 | /* Convenience macro for reverse iteration. */ |
1429 | #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \ | |
1430 | for (I = (V).length () - 1; \ | |
1431 | (V).iterate ((I), &(P)); \ | |
1432 | (I)--) | |
1433 | ||
1434 | #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \ | |
1435 | for (I = vec_safe_length (V) - 1; \ | |
1436 | vec_safe_iterate ((V), (I), &(P)); \ | |
1437 | (I)--) | |
1438 | ||
1439 | ||
1440 | /* Return a copy of this vector. */ | |
1441 | ||
1442 | template<typename T, typename A> | |
1443 | inline vec<T, A, vl_ptr> | |
1444 | vec<T, A, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const | |
1445 | { | |
6e1aa848 | 1446 | vec<T, A, vl_ptr> new_vec = vNULL; |
9771b263 | 1447 | if (length ()) |
26da79f5 | 1448 | new_vec.vec_ = vec_->copy (); |
9771b263 | 1449 | return new_vec; |
c2569604 ILT |
1450 | } |
1451 | ||
c2569604 | 1452 | |
9771b263 DN |
1453 | /* Ensure that the vector has at least RESERVE slots available (if |
1454 | EXACT is false), or exactly RESERVE slots available (if EXACT is | |
1455 | true). | |
c2569604 | 1456 | |
9771b263 DN |
1457 | This may create additional headroom if EXACT is false. |
1458 | ||
1459 | Note that this can cause the embedded vector to be reallocated. | |
1460 | Returns true iff reallocation actually occurred. */ | |
1461 | ||
1462 | template<typename T, typename A> | |
1463 | inline bool | |
1464 | vec<T, A, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL) | |
0823efed | 1465 | { |
9771b263 DN |
1466 | bool extend = nelems ? !space (nelems) : false; |
1467 | if (extend) | |
26da79f5 | 1468 | A::reserve (vec_, nelems, exact PASS_MEM_STAT); |
9771b263 DN |
1469 | return extend; |
1470 | } | |
1471 | ||
bd0c3bfd | 1472 | |
9771b263 DN |
1473 | /* Ensure that this vector has exactly NELEMS slots available. This |
1474 | will not create additional headroom. Note this can cause the | |
1475 | embedded vector to be reallocated. Returns true iff reallocation | |
1476 | actually occurred. */ | |
bd0c3bfd | 1477 | |
9771b263 DN |
1478 | template<typename T, typename A> |
1479 | inline bool | |
1480 | vec<T, A, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL) | |
1481 | { | |
1482 | return reserve (nelems, true PASS_MEM_STAT); | |
1483 | } | |
1484 | ||
1485 | ||
1486 | /* Create the internal vector and reserve NELEMS for it. This is | |
1487 | exactly like vec::reserve, but the internal vector is | |
1488 | unconditionally allocated from scratch. The old one, if it | |
1489 | existed, is lost. */ | |
1490 | ||
1491 | template<typename T, typename A> | |
1492 | inline void | |
1493 | vec<T, A, vl_ptr>::create (unsigned nelems MEM_STAT_DECL) | |
1494 | { | |
26da79f5 | 1495 | vec_ = NULL; |
9771b263 DN |
1496 | if (nelems > 0) |
1497 | reserve_exact (nelems PASS_MEM_STAT); | |
1498 | } | |
1499 | ||
1500 | ||
1501 | /* Free the memory occupied by the embedded vector. */ | |
1502 | ||
1503 | template<typename T, typename A> | |
1504 | inline void | |
1505 | vec<T, A, vl_ptr>::release (void) | |
1506 | { | |
26da79f5 JJ |
1507 | if (vec_) |
1508 | A::release (vec_); | |
9771b263 DN |
1509 | } |
1510 | ||
1511 | ||
1512 | /* Copy the elements from SRC to the end of this vector as if by memcpy. | |
1513 | SRC and this vector must be allocated with the same memory | |
1514 | allocation mechanism. This vector is assumed to have sufficient | |
1515 | headroom available. */ | |
1516 | ||
1517 | template<typename T, typename A> | |
1518 | inline void | |
1519 | vec<T, A, vl_ptr>::splice (vec<T, A, vl_ptr> &src) | |
1520 | { | |
26da79f5 JJ |
1521 | if (src.vec_) |
1522 | vec_->splice (*(src.vec_)); | |
9771b263 DN |
1523 | } |
1524 | ||
1525 | ||
1526 | /* Copy the elements in SRC to the end of this vector as if by memcpy. | |
1527 | SRC and this vector must be allocated with the same mechanism. | |
1528 | If there is not enough headroom in this vector, it will be reallocated | |
1529 | as needed. */ | |
1530 | ||
1531 | template<typename T, typename A> | |
1532 | inline void | |
1533 | vec<T, A, vl_ptr>::safe_splice (vec<T, A, vl_ptr> &src MEM_STAT_DECL) | |
1534 | { | |
1535 | if (src.length()) | |
0823efed | 1536 | { |
9771b263 DN |
1537 | reserve_exact (src.length()); |
1538 | splice (src); | |
0823efed | 1539 | } |
9771b263 DN |
1540 | } |
1541 | ||
1542 | ||
1543 | /* Push OBJ (a new element) onto the end of the vector. There must be | |
1544 | sufficient space in the vector. Return a pointer to the slot | |
1545 | where OBJ was inserted. */ | |
1546 | ||
1547 | template<typename T, typename A> | |
1548 | inline T * | |
1549 | vec<T, A, vl_ptr>::quick_push (const T &obj) | |
1550 | { | |
26da79f5 | 1551 | return vec_->quick_push (obj); |
9771b263 DN |
1552 | } |
1553 | ||
1554 | ||
1555 | /* Push a new element OBJ onto the end of this vector. Reallocates | |
1556 | the embedded vector, if needed. Return a pointer to the slot where | |
1557 | OBJ was inserted. */ | |
1558 | ||
1559 | template<typename T, typename A> | |
1560 | inline T * | |
1561 | vec<T, A, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL) | |
1562 | { | |
1563 | reserve (1, false PASS_MEM_STAT); | |
1564 | return quick_push (obj); | |
1565 | } | |
1566 | ||
bd0c3bfd | 1567 | |
9771b263 DN |
1568 | /* Pop and return the last element off the end of the vector. */ |
1569 | ||
1570 | template<typename T, typename A> | |
1571 | inline T & | |
1572 | vec<T, A, vl_ptr>::pop (void) | |
1573 | { | |
26da79f5 | 1574 | return vec_->pop (); |
9771b263 DN |
1575 | } |
1576 | ||
1577 | ||
1578 | /* Set the length of the vector to LEN. The new length must be less | |
1579 | than or equal to the current length. This is an O(1) operation. */ | |
1580 | ||
1581 | template<typename T, typename A> | |
1582 | inline void | |
1583 | vec<T, A, vl_ptr>::truncate (unsigned size) | |
1584 | { | |
26da79f5 JJ |
1585 | if (vec_) |
1586 | vec_->truncate (size); | |
9771b263 DN |
1587 | else |
1588 | gcc_checking_assert (size == 0); | |
1589 | } | |
1590 | ||
1591 | ||
1592 | /* Grow the vector to a specific length. LEN must be as long or | |
1593 | longer than the current length. The new elements are | |
1594 | uninitialized. Reallocate the internal vector, if needed. */ | |
1595 | ||
1596 | template<typename T, typename A> | |
1597 | inline void | |
1598 | vec<T, A, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL) | |
1599 | { | |
1600 | unsigned oldlen = length (); | |
1601 | gcc_checking_assert (oldlen <= len); | |
1602 | reserve_exact (len - oldlen PASS_MEM_STAT); | |
26da79f5 | 1603 | vec_->quick_grow (len); |
9771b263 DN |
1604 | } |
1605 | ||
1606 | ||
1607 | /* Grow the embedded vector to a specific length. LEN must be as | |
1608 | long or longer than the current length. The new elements are | |
1609 | initialized to zero. Reallocate the internal vector, if needed. */ | |
1610 | ||
1611 | template<typename T, typename A> | |
1612 | inline void | |
1613 | vec<T, A, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL) | |
1614 | { | |
1615 | unsigned oldlen = length (); | |
1616 | safe_grow (len PASS_MEM_STAT); | |
1617 | memset (&(address()[oldlen]), 0, sizeof (T) * (len - oldlen)); | |
1618 | } | |
1619 | ||
1620 | ||
1621 | /* Same as vec::safe_grow but without reallocation of the internal vector. | |
1622 | If the vector cannot be extended, a runtime assertion will be triggered. */ | |
1623 | ||
1624 | template<typename T, typename A> | |
1625 | inline void | |
1626 | vec<T, A, vl_ptr>::quick_grow (unsigned len) | |
1627 | { | |
26da79f5 JJ |
1628 | gcc_checking_assert (vec_); |
1629 | vec_->quick_grow (len); | |
9771b263 DN |
1630 | } |
1631 | ||
1632 | ||
1633 | /* Same as vec::quick_grow_cleared but without reallocation of the | |
1634 | internal vector. If the vector cannot be extended, a runtime | |
1635 | assertion will be triggered. */ | |
1636 | ||
1637 | template<typename T, typename A> | |
1638 | inline void | |
1639 | vec<T, A, vl_ptr>::quick_grow_cleared (unsigned len) | |
1640 | { | |
26da79f5 JJ |
1641 | gcc_checking_assert (vec_); |
1642 | vec_->quick_grow_cleared (len); | |
9771b263 DN |
1643 | } |
1644 | ||
1645 | ||
1646 | /* Insert an element, OBJ, at the IXth position of this vector. There | |
1647 | must be sufficient space. */ | |
1648 | ||
1649 | template<typename T, typename A> | |
1650 | inline void | |
1651 | vec<T, A, vl_ptr>::quick_insert (unsigned ix, const T &obj) | |
1652 | { | |
26da79f5 | 1653 | vec_->quick_insert (ix, obj); |
9771b263 DN |
1654 | } |
1655 | ||
1656 | ||
1657 | /* Insert an element, OBJ, at the IXth position of the vector. | |
1658 | Reallocate the embedded vector, if necessary. */ | |
1659 | ||
1660 | template<typename T, typename A> | |
1661 | inline void | |
1662 | vec<T, A, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL) | |
1663 | { | |
1664 | reserve (1, false PASS_MEM_STAT); | |
1665 | quick_insert (ix, obj); | |
1666 | } | |
1667 | ||
1668 | ||
1669 | /* Remove an element from the IXth position of this vector. Ordering of | |
1670 | remaining elements is preserved. This is an O(N) operation due to | |
1671 | a memmove. */ | |
1672 | ||
1673 | template<typename T, typename A> | |
1674 | inline void | |
1675 | vec<T, A, vl_ptr>::ordered_remove (unsigned ix) | |
1676 | { | |
26da79f5 | 1677 | vec_->ordered_remove (ix); |
9771b263 DN |
1678 | } |
1679 | ||
1680 | ||
1681 | /* Remove an element from the IXth position of this vector. Ordering | |
1682 | of remaining elements is destroyed. This is an O(1) operation. */ | |
1683 | ||
1684 | template<typename T, typename A> | |
1685 | inline void | |
1686 | vec<T, A, vl_ptr>::unordered_remove (unsigned ix) | |
1687 | { | |
26da79f5 | 1688 | vec_->unordered_remove (ix); |
9771b263 DN |
1689 | } |
1690 | ||
1691 | ||
1692 | /* Remove LEN elements starting at the IXth. Ordering is retained. | |
1693 | This is an O(N) operation due to memmove. */ | |
1694 | ||
1695 | template<typename T, typename A> | |
1696 | inline void | |
1697 | vec<T, A, vl_ptr>::block_remove (unsigned ix, unsigned len) | |
1698 | { | |
26da79f5 | 1699 | vec_->block_remove (ix, len); |
9771b263 DN |
1700 | } |
1701 | ||
1702 | ||
1703 | /* Sort the contents of this vector with qsort. CMP is the comparison | |
1704 | function to pass to qsort. */ | |
1705 | ||
1706 | template<typename T, typename A> | |
1707 | inline void | |
1708 | vec<T, A, vl_ptr>::qsort (int (*cmp) (const void *, const void *)) | |
1709 | { | |
26da79f5 JJ |
1710 | if (vec_) |
1711 | vec_->qsort (cmp); | |
9771b263 DN |
1712 | } |
1713 | ||
1714 | ||
1715 | /* Find and return the first position in which OBJ could be inserted | |
1716 | without changing the ordering of this vector. LESSTHAN is a | |
1717 | function that returns true if the first argument is strictly less | |
1718 | than the second. */ | |
1719 | ||
1720 | template<typename T, typename A> | |
1721 | inline unsigned | |
38f2ca32 DN |
1722 | vec<T, A, vl_ptr>::lower_bound (T obj, bool (*lessthan)(const T &, const T &)) |
1723 | const | |
9771b263 | 1724 | { |
26da79f5 | 1725 | return vec_ ? vec_->lower_bound (obj, lessthan) : 0; |
c2569604 ILT |
1726 | } |
1727 | ||
26da79f5 JJ |
1728 | #if (GCC_VERSION >= 3000) |
1729 | # pragma GCC poison vec_ vecpfx_ vecdata_ | |
1730 | #endif | |
1731 | ||
9771b263 | 1732 | #endif // GCC_VEC_H |