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