Daily bump.
[gcc.git] / gcc / vec.h
1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2016 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. */
414 struct vnull
415 {
416 template <typename T, typename A, typename L>
417 operator vec<T, A, L> () { return vec<T, A, L>(); }
418 };
419 extern vnull vNULL;
420
421
422 /* Embeddable vector. These vectors are suitable to be embedded
423 in other data structures so that they can be pre-allocated in a
424 contiguous memory block.
425
426 Embeddable vectors are implemented using the trailing array idiom,
427 thus they are not resizeable without changing the address of the
428 vector object itself. This means you cannot have variables or
429 fields of embeddable vector type -- always use a pointer to a
430 vector. The one exception is the final field of a structure, which
431 could be a vector type.
432
433 You will have to use the embedded_size & embedded_init calls to
434 create such objects, and they will not be resizeable (so the 'safe'
435 allocation variants are not available).
436
437 Properties:
438
439 - The whole vector and control data are allocated in a single
440 contiguous block. It uses the trailing-vector idiom, so
441 allocation must reserve enough space for all the elements
442 in the vector plus its control data.
443 - The vector cannot be re-allocated.
444 - The vector cannot grow nor shrink.
445 - No indirections needed for access/manipulation.
446 - It requires 2 words of storage (prior to vector allocation). */
447
448 template<typename T, typename A>
449 struct GTY((user)) vec<T, A, vl_embed>
450 {
451 public:
452 unsigned allocated (void) const { return m_vecpfx.m_alloc; }
453 unsigned length (void) const { return m_vecpfx.m_num; }
454 bool is_empty (void) const { return m_vecpfx.m_num == 0; }
455 T *address (void) { return m_vecdata; }
456 const T *address (void) const { return m_vecdata; }
457 T *begin () { return address (); }
458 const T *begin () const { return address (); }
459 T *end () { return address () + length (); }
460 const T *end () const { return address () + length (); }
461 const T &operator[] (unsigned) const;
462 T &operator[] (unsigned);
463 T &last (void);
464 bool space (unsigned) const;
465 bool iterate (unsigned, T *) const;
466 bool iterate (unsigned, T **) const;
467 vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
468 void splice (const vec &);
469 void splice (const vec *src);
470 T *quick_push (const T &);
471 T &pop (void);
472 void truncate (unsigned);
473 void quick_insert (unsigned, const T &);
474 void ordered_remove (unsigned);
475 void unordered_remove (unsigned);
476 void block_remove (unsigned, unsigned);
477 void qsort (int (*) (const void *, const void *));
478 T *bsearch (const void *key, int (*compar)(const void *, const void *));
479 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
480 bool contains (const T &search) const;
481 static size_t embedded_size (unsigned);
482 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
483 void quick_grow (unsigned len);
484 void quick_grow_cleared (unsigned len);
485
486 /* vec class can access our internal data and functions. */
487 template <typename, typename, typename> friend struct vec;
488
489 /* The allocator types also need access to our internals. */
490 friend struct va_gc;
491 friend struct va_gc_atomic;
492 friend struct va_heap;
493
494 /* FIXME - These fields should be private, but we need to cater to
495 compilers that have stricter notions of PODness for types. */
496 vec_prefix m_vecpfx;
497 T m_vecdata[1];
498 };
499
500
501 /* Convenience wrapper functions to use when dealing with pointers to
502 embedded vectors. Some functionality for these vectors must be
503 provided via free functions for these reasons:
504
505 1- The pointer may be NULL (e.g., before initial allocation).
506
507 2- When the vector needs to grow, it must be reallocated, so
508 the pointer will change its value.
509
510 Because of limitations with the current GC machinery, all vectors
511 in GC memory *must* be pointers. */
512
513
514 /* If V contains no room for NELEMS elements, return false. Otherwise,
515 return true. */
516 template<typename T, typename A>
517 inline bool
518 vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
519 {
520 return v ? v->space (nelems) : nelems == 0;
521 }
522
523
524 /* If V is NULL, return 0. Otherwise, return V->length(). */
525 template<typename T, typename A>
526 inline unsigned
527 vec_safe_length (const vec<T, A, vl_embed> *v)
528 {
529 return v ? v->length () : 0;
530 }
531
532
533 /* If V is NULL, return NULL. Otherwise, return V->address(). */
534 template<typename T, typename A>
535 inline T *
536 vec_safe_address (vec<T, A, vl_embed> *v)
537 {
538 return v ? v->address () : NULL;
539 }
540
541
542 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
543 template<typename T, typename A>
544 inline bool
545 vec_safe_is_empty (vec<T, A, vl_embed> *v)
546 {
547 return v ? v->is_empty () : true;
548 }
549
550 /* If V does not have space for NELEMS elements, call
551 V->reserve(NELEMS, EXACT). */
552 template<typename T, typename A>
553 inline bool
554 vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
555 CXX_MEM_STAT_INFO)
556 {
557 bool extend = nelems ? !vec_safe_space (v, nelems) : false;
558 if (extend)
559 A::reserve (v, nelems, exact PASS_MEM_STAT);
560 return extend;
561 }
562
563 template<typename T, typename A>
564 inline bool
565 vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
566 CXX_MEM_STAT_INFO)
567 {
568 return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
569 }
570
571
572 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
573 is 0, V is initialized to NULL. */
574
575 template<typename T, typename A>
576 inline void
577 vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
578 {
579 v = NULL;
580 vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
581 }
582
583
584 /* Free the GC memory allocated by vector V and set it to NULL. */
585
586 template<typename T, typename A>
587 inline void
588 vec_free (vec<T, A, vl_embed> *&v)
589 {
590 A::release (v);
591 }
592
593
594 /* Grow V to length LEN. Allocate it, if necessary. */
595 template<typename T, typename A>
596 inline void
597 vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
598 {
599 unsigned oldlen = vec_safe_length (v);
600 gcc_checking_assert (len >= oldlen);
601 vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT);
602 v->quick_grow (len);
603 }
604
605
606 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
607 template<typename T, typename A>
608 inline void
609 vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
610 {
611 unsigned oldlen = vec_safe_length (v);
612 vec_safe_grow (v, len PASS_MEM_STAT);
613 memset (&(v->address ()[oldlen]), 0, sizeof (T) * (len - oldlen));
614 }
615
616
617 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
618 template<typename T, typename A>
619 inline bool
620 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
621 {
622 if (v)
623 return v->iterate (ix, ptr);
624 else
625 {
626 *ptr = 0;
627 return false;
628 }
629 }
630
631 template<typename T, typename A>
632 inline bool
633 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
634 {
635 if (v)
636 return v->iterate (ix, ptr);
637 else
638 {
639 *ptr = 0;
640 return false;
641 }
642 }
643
644
645 /* If V has no room for one more element, reallocate it. Then call
646 V->quick_push(OBJ). */
647 template<typename T, typename A>
648 inline T *
649 vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
650 {
651 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
652 return v->quick_push (obj);
653 }
654
655
656 /* if V has no room for one more element, reallocate it. Then call
657 V->quick_insert(IX, OBJ). */
658 template<typename T, typename A>
659 inline void
660 vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
661 CXX_MEM_STAT_INFO)
662 {
663 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
664 v->quick_insert (ix, obj);
665 }
666
667
668 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
669 template<typename T, typename A>
670 inline void
671 vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
672 {
673 if (v)
674 v->truncate (size);
675 }
676
677
678 /* If SRC is not NULL, return a pointer to a copy of it. */
679 template<typename T, typename A>
680 inline vec<T, A, vl_embed> *
681 vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
682 {
683 return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
684 }
685
686 /* Copy the elements from SRC to the end of DST as if by memcpy.
687 Reallocate DST, if necessary. */
688 template<typename T, typename A>
689 inline void
690 vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
691 CXX_MEM_STAT_INFO)
692 {
693 unsigned src_len = vec_safe_length (src);
694 if (src_len)
695 {
696 vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
697 PASS_MEM_STAT);
698 dst->splice (*src);
699 }
700 }
701
702 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
703 size of the vector and so should be used with care. */
704
705 template<typename T, typename A>
706 inline bool
707 vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
708 {
709 return v ? v->contains (search) : false;
710 }
711
712 /* Index into vector. Return the IX'th element. IX must be in the
713 domain of the vector. */
714
715 template<typename T, typename A>
716 inline const T &
717 vec<T, A, vl_embed>::operator[] (unsigned ix) const
718 {
719 gcc_checking_assert (ix < m_vecpfx.m_num);
720 return m_vecdata[ix];
721 }
722
723 template<typename T, typename A>
724 inline T &
725 vec<T, A, vl_embed>::operator[] (unsigned ix)
726 {
727 gcc_checking_assert (ix < m_vecpfx.m_num);
728 return m_vecdata[ix];
729 }
730
731
732 /* Get the final element of the vector, which must not be empty. */
733
734 template<typename T, typename A>
735 inline T &
736 vec<T, A, vl_embed>::last (void)
737 {
738 gcc_checking_assert (m_vecpfx.m_num > 0);
739 return (*this)[m_vecpfx.m_num - 1];
740 }
741
742
743 /* If this vector has space for NELEMS additional entries, return
744 true. You usually only need to use this if you are doing your
745 own vector reallocation, for instance on an embedded vector. This
746 returns true in exactly the same circumstances that vec::reserve
747 will. */
748
749 template<typename T, typename A>
750 inline bool
751 vec<T, A, vl_embed>::space (unsigned nelems) const
752 {
753 return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
754 }
755
756
757 /* Return iteration condition and update PTR to point to the IX'th
758 element of this vector. Use this to iterate over the elements of a
759 vector as follows,
760
761 for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
762 continue; */
763
764 template<typename T, typename A>
765 inline bool
766 vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
767 {
768 if (ix < m_vecpfx.m_num)
769 {
770 *ptr = m_vecdata[ix];
771 return true;
772 }
773 else
774 {
775 *ptr = 0;
776 return false;
777 }
778 }
779
780
781 /* Return iteration condition and update *PTR to point to the
782 IX'th element of this vector. Use this to iterate over the
783 elements of a vector as follows,
784
785 for (ix = 0; v->iterate (ix, &ptr); ix++)
786 continue;
787
788 This variant is for vectors of objects. */
789
790 template<typename T, typename A>
791 inline bool
792 vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
793 {
794 if (ix < m_vecpfx.m_num)
795 {
796 *ptr = CONST_CAST (T *, &m_vecdata[ix]);
797 return true;
798 }
799 else
800 {
801 *ptr = 0;
802 return false;
803 }
804 }
805
806
807 /* Return a pointer to a copy of this vector. */
808
809 template<typename T, typename A>
810 inline vec<T, A, vl_embed> *
811 vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
812 {
813 vec<T, A, vl_embed> *new_vec = NULL;
814 unsigned len = length ();
815 if (len)
816 {
817 vec_alloc (new_vec, len PASS_MEM_STAT);
818 new_vec->embedded_init (len, len);
819 memcpy (new_vec->address (), m_vecdata, sizeof (T) * len);
820 }
821 return new_vec;
822 }
823
824
825 /* Copy the elements from SRC to the end of this vector as if by memcpy.
826 The vector must have sufficient headroom available. */
827
828 template<typename T, typename A>
829 inline void
830 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
831 {
832 unsigned len = src.length ();
833 if (len)
834 {
835 gcc_checking_assert (space (len));
836 memcpy (address () + length (), src.address (), len * sizeof (T));
837 m_vecpfx.m_num += len;
838 }
839 }
840
841 template<typename T, typename A>
842 inline void
843 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
844 {
845 if (src)
846 splice (*src);
847 }
848
849
850 /* Push OBJ (a new element) onto the end of the vector. There must be
851 sufficient space in the vector. Return a pointer to the slot
852 where OBJ was inserted. */
853
854 template<typename T, typename A>
855 inline T *
856 vec<T, A, vl_embed>::quick_push (const T &obj)
857 {
858 gcc_checking_assert (space (1));
859 T *slot = &m_vecdata[m_vecpfx.m_num++];
860 *slot = obj;
861 return slot;
862 }
863
864
865 /* Pop and return the last element off the end of the vector. */
866
867 template<typename T, typename A>
868 inline T &
869 vec<T, A, vl_embed>::pop (void)
870 {
871 gcc_checking_assert (length () > 0);
872 return m_vecdata[--m_vecpfx.m_num];
873 }
874
875
876 /* Set the length of the vector to SIZE. The new length must be less
877 than or equal to the current length. This is an O(1) operation. */
878
879 template<typename T, typename A>
880 inline void
881 vec<T, A, vl_embed>::truncate (unsigned size)
882 {
883 gcc_checking_assert (length () >= size);
884 m_vecpfx.m_num = size;
885 }
886
887
888 /* Insert an element, OBJ, at the IXth position of this vector. There
889 must be sufficient space. */
890
891 template<typename T, typename A>
892 inline void
893 vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
894 {
895 gcc_checking_assert (length () < allocated ());
896 gcc_checking_assert (ix <= length ());
897 T *slot = &m_vecdata[ix];
898 memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
899 *slot = obj;
900 }
901
902
903 /* Remove an element from the IXth position of this vector. Ordering of
904 remaining elements is preserved. This is an O(N) operation due to
905 memmove. */
906
907 template<typename T, typename A>
908 inline void
909 vec<T, A, vl_embed>::ordered_remove (unsigned ix)
910 {
911 gcc_checking_assert (ix < length ());
912 T *slot = &m_vecdata[ix];
913 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
914 }
915
916
917 /* Remove an element from the IXth position of this vector. Ordering of
918 remaining elements is destroyed. This is an O(1) operation. */
919
920 template<typename T, typename A>
921 inline void
922 vec<T, A, vl_embed>::unordered_remove (unsigned ix)
923 {
924 gcc_checking_assert (ix < length ());
925 m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
926 }
927
928
929 /* Remove LEN elements starting at the IXth. Ordering is retained.
930 This is an O(N) operation due to memmove. */
931
932 template<typename T, typename A>
933 inline void
934 vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
935 {
936 gcc_checking_assert (ix + len <= length ());
937 T *slot = &m_vecdata[ix];
938 m_vecpfx.m_num -= len;
939 memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
940 }
941
942
943 /* Sort the contents of this vector with qsort. CMP is the comparison
944 function to pass to qsort. */
945
946 template<typename T, typename A>
947 inline void
948 vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *))
949 {
950 if (length () > 1)
951 ::qsort (address (), length (), sizeof (T), cmp);
952 }
953
954
955 /* Search the contents of the sorted vector with a binary search.
956 CMP is the comparison function to pass to bsearch. */
957
958 template<typename T, typename A>
959 inline T *
960 vec<T, A, vl_embed>::bsearch (const void *key,
961 int (*compar) (const void *, const void *))
962 {
963 const void *base = this->address ();
964 size_t nmemb = this->length ();
965 size_t size = sizeof (T);
966 /* The following is a copy of glibc stdlib-bsearch.h. */
967 size_t l, u, idx;
968 const void *p;
969 int comparison;
970
971 l = 0;
972 u = nmemb;
973 while (l < u)
974 {
975 idx = (l + u) / 2;
976 p = (const void *) (((const char *) base) + (idx * size));
977 comparison = (*compar) (key, p);
978 if (comparison < 0)
979 u = idx;
980 else if (comparison > 0)
981 l = idx + 1;
982 else
983 return (T *)const_cast<void *>(p);
984 }
985
986 return NULL;
987 }
988
989 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
990 size of the vector and so should be used with care. */
991
992 template<typename T, typename A>
993 inline bool
994 vec<T, A, vl_embed>::contains (const T &search) const
995 {
996 unsigned int len = length ();
997 for (unsigned int i = 0; i < len; i++)
998 if ((*this)[i] == search)
999 return true;
1000
1001 return false;
1002 }
1003
1004 /* Find and return the first position in which OBJ could be inserted
1005 without changing the ordering of this vector. LESSTHAN is a
1006 function that returns true if the first argument is strictly less
1007 than the second. */
1008
1009 template<typename T, typename A>
1010 unsigned
1011 vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
1012 const
1013 {
1014 unsigned int len = length ();
1015 unsigned int half, middle;
1016 unsigned int first = 0;
1017 while (len > 0)
1018 {
1019 half = len / 2;
1020 middle = first;
1021 middle += half;
1022 T middle_elem = (*this)[middle];
1023 if (lessthan (middle_elem, obj))
1024 {
1025 first = middle;
1026 ++first;
1027 len = len - half - 1;
1028 }
1029 else
1030 len = half;
1031 }
1032 return first;
1033 }
1034
1035
1036 /* Return the number of bytes needed to embed an instance of an
1037 embeddable vec inside another data structure.
1038
1039 Use these methods to determine the required size and initialization
1040 of a vector V of type T embedded within another structure (as the
1041 final member):
1042
1043 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1044 void v->embedded_init (unsigned alloc, unsigned num);
1045
1046 These allow the caller to perform the memory allocation. */
1047
1048 template<typename T, typename A>
1049 inline size_t
1050 vec<T, A, vl_embed>::embedded_size (unsigned alloc)
1051 {
1052 typedef vec<T, A, vl_embed> vec_embedded;
1053 return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
1054 }
1055
1056
1057 /* Initialize the vector to contain room for ALLOC elements and
1058 NUM active elements. */
1059
1060 template<typename T, typename A>
1061 inline void
1062 vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
1063 {
1064 m_vecpfx.m_alloc = alloc;
1065 m_vecpfx.m_using_auto_storage = aut;
1066 m_vecpfx.m_num = num;
1067 }
1068
1069
1070 /* Grow the vector to a specific length. LEN must be as long or longer than
1071 the current length. The new elements are uninitialized. */
1072
1073 template<typename T, typename A>
1074 inline void
1075 vec<T, A, vl_embed>::quick_grow (unsigned len)
1076 {
1077 gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
1078 m_vecpfx.m_num = len;
1079 }
1080
1081
1082 /* Grow the vector to a specific length. LEN must be as long or longer than
1083 the current length. The new elements are initialized to zero. */
1084
1085 template<typename T, typename A>
1086 inline void
1087 vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
1088 {
1089 unsigned oldlen = length ();
1090 quick_grow (len);
1091 memset (&(address ()[oldlen]), 0, sizeof (T) * (len - oldlen));
1092 }
1093
1094
1095 /* Garbage collection support for vec<T, A, vl_embed>. */
1096
1097 template<typename T>
1098 void
1099 gt_ggc_mx (vec<T, va_gc> *v)
1100 {
1101 extern void gt_ggc_mx (T &);
1102 for (unsigned i = 0; i < v->length (); i++)
1103 gt_ggc_mx ((*v)[i]);
1104 }
1105
1106 template<typename T>
1107 void
1108 gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
1109 {
1110 /* Nothing to do. Vectors of atomic types wrt GC do not need to
1111 be traversed. */
1112 }
1113
1114
1115 /* PCH support for vec<T, A, vl_embed>. */
1116
1117 template<typename T, typename A>
1118 void
1119 gt_pch_nx (vec<T, A, vl_embed> *v)
1120 {
1121 extern void gt_pch_nx (T &);
1122 for (unsigned i = 0; i < v->length (); i++)
1123 gt_pch_nx ((*v)[i]);
1124 }
1125
1126 template<typename T, typename A>
1127 void
1128 gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1129 {
1130 for (unsigned i = 0; i < v->length (); i++)
1131 op (&((*v)[i]), cookie);
1132 }
1133
1134 template<typename T, typename A>
1135 void
1136 gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1137 {
1138 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
1139 for (unsigned i = 0; i < v->length (); i++)
1140 gt_pch_nx (&((*v)[i]), op, cookie);
1141 }
1142
1143
1144 /* Space efficient vector. These vectors can grow dynamically and are
1145 allocated together with their control data. They are suited to be
1146 included in data structures. Prior to initial allocation, they
1147 only take a single word of storage.
1148
1149 These vectors are implemented as a pointer to an embeddable vector.
1150 The semantics allow for this pointer to be NULL to represent empty
1151 vectors. This way, empty vectors occupy minimal space in the
1152 structure containing them.
1153
1154 Properties:
1155
1156 - The whole vector and control data are allocated in a single
1157 contiguous block.
1158 - The whole vector may be re-allocated.
1159 - Vector data may grow and shrink.
1160 - Access and manipulation requires a pointer test and
1161 indirection.
1162 - It requires 1 word of storage (prior to vector allocation).
1163
1164
1165 Limitations:
1166
1167 These vectors must be PODs because they are stored in unions.
1168 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1169 As long as we use C++03, we cannot have constructors nor
1170 destructors in classes that are stored in unions. */
1171
1172 template<typename T>
1173 struct vec<T, va_heap, vl_ptr>
1174 {
1175 public:
1176 /* Memory allocation and deallocation for the embedded vector.
1177 Needed because we cannot have proper ctors/dtors defined. */
1178 void create (unsigned nelems CXX_MEM_STAT_INFO);
1179 void release (void);
1180
1181 /* Vector operations. */
1182 bool exists (void) const
1183 { return m_vec != NULL; }
1184
1185 bool is_empty (void) const
1186 { return m_vec ? m_vec->is_empty () : true; }
1187
1188 unsigned length (void) const
1189 { return m_vec ? m_vec->length () : 0; }
1190
1191 T *address (void)
1192 { return m_vec ? m_vec->m_vecdata : NULL; }
1193
1194 const T *address (void) const
1195 { return m_vec ? m_vec->m_vecdata : NULL; }
1196
1197 T *begin () { return address (); }
1198 const T *begin () const { return address (); }
1199 T *end () { return begin () + length (); }
1200 const T *end () const { return begin () + length (); }
1201 const T &operator[] (unsigned ix) const
1202 { return (*m_vec)[ix]; }
1203
1204 bool operator!=(const vec &other) const
1205 { return !(*this == other); }
1206
1207 bool operator==(const vec &other) const
1208 { return address () == other.address (); }
1209
1210 T &operator[] (unsigned ix)
1211 { return (*m_vec)[ix]; }
1212
1213 T &last (void)
1214 { return m_vec->last (); }
1215
1216 bool space (int nelems) const
1217 { return m_vec ? m_vec->space (nelems) : nelems == 0; }
1218
1219 bool iterate (unsigned ix, T *p) const;
1220 bool iterate (unsigned ix, T **p) const;
1221 vec copy (ALONE_CXX_MEM_STAT_INFO) const;
1222 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
1223 bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
1224 void splice (const vec &);
1225 void safe_splice (const vec & CXX_MEM_STAT_INFO);
1226 T *quick_push (const T &);
1227 T *safe_push (const T &CXX_MEM_STAT_INFO);
1228 T &pop (void);
1229 void truncate (unsigned);
1230 void safe_grow (unsigned CXX_MEM_STAT_INFO);
1231 void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO);
1232 void quick_grow (unsigned);
1233 void quick_grow_cleared (unsigned);
1234 void quick_insert (unsigned, const T &);
1235 void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
1236 void ordered_remove (unsigned);
1237 void unordered_remove (unsigned);
1238 void block_remove (unsigned, unsigned);
1239 void qsort (int (*) (const void *, const void *));
1240 T *bsearch (const void *key, int (*compar)(const void *, const void *));
1241 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
1242 bool contains (const T &search) const;
1243
1244 bool using_auto_storage () const;
1245
1246 /* FIXME - This field should be private, but we need to cater to
1247 compilers that have stricter notions of PODness for types. */
1248 vec<T, va_heap, vl_embed> *m_vec;
1249 };
1250
1251
1252 /* auto_vec is a subclass of vec that automatically manages creating and
1253 releasing the internal vector. If N is non zero then it has N elements of
1254 internal storage. The default is no internal storage, and you probably only
1255 want to ask for internal storage for vectors on the stack because if the
1256 size of the vector is larger than the internal storage that space is wasted.
1257 */
1258 template<typename T, size_t N = 0>
1259 class auto_vec : public vec<T, va_heap>
1260 {
1261 public:
1262 auto_vec ()
1263 {
1264 m_auto.embedded_init (MAX (N, 2), 0, 1);
1265 this->m_vec = &m_auto;
1266 }
1267
1268 ~auto_vec ()
1269 {
1270 this->release ();
1271 }
1272
1273 private:
1274 vec<T, va_heap, vl_embed> m_auto;
1275 T m_data[MAX (N - 1, 1)];
1276 };
1277
1278 /* auto_vec is a sub class of vec whose storage is released when it is
1279 destroyed. */
1280 template<typename T>
1281 class auto_vec<T, 0> : public vec<T, va_heap>
1282 {
1283 public:
1284 auto_vec () { this->m_vec = NULL; }
1285 auto_vec (size_t n) { this->create (n); }
1286 ~auto_vec () { this->release (); }
1287 };
1288
1289
1290 /* Allocate heap memory for pointer V and create the internal vector
1291 with space for NELEMS elements. If NELEMS is 0, the internal
1292 vector is initialized to empty. */
1293
1294 template<typename T>
1295 inline void
1296 vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
1297 {
1298 v = new vec<T>;
1299 v->create (nelems PASS_MEM_STAT);
1300 }
1301
1302
1303 /* Conditionally allocate heap memory for VEC and its internal vector. */
1304
1305 template<typename T>
1306 inline void
1307 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
1308 {
1309 if (!vec)
1310 vec_alloc (vec, nelems PASS_MEM_STAT);
1311 }
1312
1313
1314 /* Free the heap memory allocated by vector V and set it to NULL. */
1315
1316 template<typename T>
1317 inline void
1318 vec_free (vec<T> *&v)
1319 {
1320 if (v == NULL)
1321 return;
1322
1323 v->release ();
1324 delete v;
1325 v = NULL;
1326 }
1327
1328
1329 /* Return iteration condition and update PTR to point to the IX'th
1330 element of this vector. Use this to iterate over the elements of a
1331 vector as follows,
1332
1333 for (ix = 0; v.iterate (ix, &ptr); ix++)
1334 continue; */
1335
1336 template<typename T>
1337 inline bool
1338 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
1339 {
1340 if (m_vec)
1341 return m_vec->iterate (ix, ptr);
1342 else
1343 {
1344 *ptr = 0;
1345 return false;
1346 }
1347 }
1348
1349
1350 /* Return iteration condition and update *PTR to point to the
1351 IX'th element of this vector. Use this to iterate over the
1352 elements of a vector as follows,
1353
1354 for (ix = 0; v->iterate (ix, &ptr); ix++)
1355 continue;
1356
1357 This variant is for vectors of objects. */
1358
1359 template<typename T>
1360 inline bool
1361 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
1362 {
1363 if (m_vec)
1364 return m_vec->iterate (ix, ptr);
1365 else
1366 {
1367 *ptr = 0;
1368 return false;
1369 }
1370 }
1371
1372
1373 /* Convenience macro for forward iteration. */
1374 #define FOR_EACH_VEC_ELT(V, I, P) \
1375 for (I = 0; (V).iterate ((I), &(P)); ++(I))
1376
1377 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
1378 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1379
1380 /* Likewise, but start from FROM rather than 0. */
1381 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
1382 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1383
1384 /* Convenience macro for reverse iteration. */
1385 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
1386 for (I = (V).length () - 1; \
1387 (V).iterate ((I), &(P)); \
1388 (I)--)
1389
1390 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1391 for (I = vec_safe_length (V) - 1; \
1392 vec_safe_iterate ((V), (I), &(P)); \
1393 (I)--)
1394
1395
1396 /* Return a copy of this vector. */
1397
1398 template<typename T>
1399 inline vec<T, va_heap, vl_ptr>
1400 vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
1401 {
1402 vec<T, va_heap, vl_ptr> new_vec = vNULL;
1403 if (length ())
1404 new_vec.m_vec = m_vec->copy ();
1405 return new_vec;
1406 }
1407
1408
1409 /* Ensure that the vector has at least RESERVE slots available (if
1410 EXACT is false), or exactly RESERVE slots available (if EXACT is
1411 true).
1412
1413 This may create additional headroom if EXACT is false.
1414
1415 Note that this can cause the embedded vector to be reallocated.
1416 Returns true iff reallocation actually occurred. */
1417
1418 template<typename T>
1419 inline bool
1420 vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
1421 {
1422 if (space (nelems))
1423 return false;
1424
1425 /* For now play a game with va_heap::reserve to hide our auto storage if any,
1426 this is necessary because it doesn't have enough information to know the
1427 embedded vector is in auto storage, and so should not be freed. */
1428 vec<T, va_heap, vl_embed> *oldvec = m_vec;
1429 unsigned int oldsize = 0;
1430 bool handle_auto_vec = m_vec && using_auto_storage ();
1431 if (handle_auto_vec)
1432 {
1433 m_vec = NULL;
1434 oldsize = oldvec->length ();
1435 nelems += oldsize;
1436 }
1437
1438 va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
1439 if (handle_auto_vec)
1440 {
1441 memcpy (m_vec->address (), oldvec->address (), sizeof (T) * oldsize);
1442 m_vec->m_vecpfx.m_num = oldsize;
1443 }
1444
1445 return true;
1446 }
1447
1448
1449 /* Ensure that this vector has exactly NELEMS slots available. This
1450 will not create additional headroom. Note this can cause the
1451 embedded vector to be reallocated. Returns true iff reallocation
1452 actually occurred. */
1453
1454 template<typename T>
1455 inline bool
1456 vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
1457 {
1458 return reserve (nelems, true PASS_MEM_STAT);
1459 }
1460
1461
1462 /* Create the internal vector and reserve NELEMS for it. This is
1463 exactly like vec::reserve, but the internal vector is
1464 unconditionally allocated from scratch. The old one, if it
1465 existed, is lost. */
1466
1467 template<typename T>
1468 inline void
1469 vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
1470 {
1471 m_vec = NULL;
1472 if (nelems > 0)
1473 reserve_exact (nelems PASS_MEM_STAT);
1474 }
1475
1476
1477 /* Free the memory occupied by the embedded vector. */
1478
1479 template<typename T>
1480 inline void
1481 vec<T, va_heap, vl_ptr>::release (void)
1482 {
1483 if (!m_vec)
1484 return;
1485
1486 if (using_auto_storage ())
1487 {
1488 m_vec->m_vecpfx.m_num = 0;
1489 return;
1490 }
1491
1492 va_heap::release (m_vec);
1493 }
1494
1495 /* Copy the elements from SRC to the end of this vector as if by memcpy.
1496 SRC and this vector must be allocated with the same memory
1497 allocation mechanism. This vector is assumed to have sufficient
1498 headroom available. */
1499
1500 template<typename T>
1501 inline void
1502 vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
1503 {
1504 if (src.m_vec)
1505 m_vec->splice (*(src.m_vec));
1506 }
1507
1508
1509 /* Copy the elements in SRC to the end of this vector as if by memcpy.
1510 SRC and this vector must be allocated with the same mechanism.
1511 If there is not enough headroom in this vector, it will be reallocated
1512 as needed. */
1513
1514 template<typename T>
1515 inline void
1516 vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
1517 MEM_STAT_DECL)
1518 {
1519 if (src.length ())
1520 {
1521 reserve_exact (src.length ());
1522 splice (src);
1523 }
1524 }
1525
1526
1527 /* Push OBJ (a new element) onto the end of the vector. There must be
1528 sufficient space in the vector. Return a pointer to the slot
1529 where OBJ was inserted. */
1530
1531 template<typename T>
1532 inline T *
1533 vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
1534 {
1535 return m_vec->quick_push (obj);
1536 }
1537
1538
1539 /* Push a new element OBJ onto the end of this vector. Reallocates
1540 the embedded vector, if needed. Return a pointer to the slot where
1541 OBJ was inserted. */
1542
1543 template<typename T>
1544 inline T *
1545 vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
1546 {
1547 reserve (1, false PASS_MEM_STAT);
1548 return quick_push (obj);
1549 }
1550
1551
1552 /* Pop and return the last element off the end of the vector. */
1553
1554 template<typename T>
1555 inline T &
1556 vec<T, va_heap, vl_ptr>::pop (void)
1557 {
1558 return m_vec->pop ();
1559 }
1560
1561
1562 /* Set the length of the vector to LEN. The new length must be less
1563 than or equal to the current length. This is an O(1) operation. */
1564
1565 template<typename T>
1566 inline void
1567 vec<T, va_heap, vl_ptr>::truncate (unsigned size)
1568 {
1569 if (m_vec)
1570 m_vec->truncate (size);
1571 else
1572 gcc_checking_assert (size == 0);
1573 }
1574
1575
1576 /* Grow the vector to a specific length. LEN must be as long or
1577 longer than the current length. The new elements are
1578 uninitialized. Reallocate the internal vector, if needed. */
1579
1580 template<typename T>
1581 inline void
1582 vec<T, va_heap, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL)
1583 {
1584 unsigned oldlen = length ();
1585 gcc_checking_assert (oldlen <= len);
1586 reserve_exact (len - oldlen PASS_MEM_STAT);
1587 if (m_vec)
1588 m_vec->quick_grow (len);
1589 else
1590 gcc_checking_assert (len == 0);
1591 }
1592
1593
1594 /* Grow the embedded vector to a specific length. LEN must be as
1595 long or longer than the current length. The new elements are
1596 initialized to zero. Reallocate the internal vector, if needed. */
1597
1598 template<typename T>
1599 inline void
1600 vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL)
1601 {
1602 unsigned oldlen = length ();
1603 safe_grow (len PASS_MEM_STAT);
1604 memset (&(address ()[oldlen]), 0, sizeof (T) * (len - oldlen));
1605 }
1606
1607
1608 /* Same as vec::safe_grow but without reallocation of the internal vector.
1609 If the vector cannot be extended, a runtime assertion will be triggered. */
1610
1611 template<typename T>
1612 inline void
1613 vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
1614 {
1615 gcc_checking_assert (m_vec);
1616 m_vec->quick_grow (len);
1617 }
1618
1619
1620 /* Same as vec::quick_grow_cleared but without reallocation of the
1621 internal vector. If the vector cannot be extended, a runtime
1622 assertion will be triggered. */
1623
1624 template<typename T>
1625 inline void
1626 vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
1627 {
1628 gcc_checking_assert (m_vec);
1629 m_vec->quick_grow_cleared (len);
1630 }
1631
1632
1633 /* Insert an element, OBJ, at the IXth position of this vector. There
1634 must be sufficient space. */
1635
1636 template<typename T>
1637 inline void
1638 vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
1639 {
1640 m_vec->quick_insert (ix, obj);
1641 }
1642
1643
1644 /* Insert an element, OBJ, at the IXth position of the vector.
1645 Reallocate the embedded vector, if necessary. */
1646
1647 template<typename T>
1648 inline void
1649 vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
1650 {
1651 reserve (1, false PASS_MEM_STAT);
1652 quick_insert (ix, obj);
1653 }
1654
1655
1656 /* Remove an element from the IXth position of this vector. Ordering of
1657 remaining elements is preserved. This is an O(N) operation due to
1658 a memmove. */
1659
1660 template<typename T>
1661 inline void
1662 vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
1663 {
1664 m_vec->ordered_remove (ix);
1665 }
1666
1667
1668 /* Remove an element from the IXth position of this vector. Ordering
1669 of remaining elements is destroyed. This is an O(1) operation. */
1670
1671 template<typename T>
1672 inline void
1673 vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
1674 {
1675 m_vec->unordered_remove (ix);
1676 }
1677
1678
1679 /* Remove LEN elements starting at the IXth. Ordering is retained.
1680 This is an O(N) operation due to memmove. */
1681
1682 template<typename T>
1683 inline void
1684 vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
1685 {
1686 m_vec->block_remove (ix, len);
1687 }
1688
1689
1690 /* Sort the contents of this vector with qsort. CMP is the comparison
1691 function to pass to qsort. */
1692
1693 template<typename T>
1694 inline void
1695 vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
1696 {
1697 if (m_vec)
1698 m_vec->qsort (cmp);
1699 }
1700
1701
1702 /* Search the contents of the sorted vector with a binary search.
1703 CMP is the comparison function to pass to bsearch. */
1704
1705 template<typename T>
1706 inline T *
1707 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
1708 int (*cmp) (const void *, const void *))
1709 {
1710 if (m_vec)
1711 return m_vec->bsearch (key, cmp);
1712 return NULL;
1713 }
1714
1715
1716 /* Find and return the first position in which OBJ could be inserted
1717 without changing the ordering of this vector. LESSTHAN is a
1718 function that returns true if the first argument is strictly less
1719 than the second. */
1720
1721 template<typename T>
1722 inline unsigned
1723 vec<T, va_heap, vl_ptr>::lower_bound (T obj,
1724 bool (*lessthan)(const T &, const T &))
1725 const
1726 {
1727 return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
1728 }
1729
1730 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
1731 size of the vector and so should be used with care. */
1732
1733 template<typename T>
1734 inline bool
1735 vec<T, va_heap, vl_ptr>::contains (const T &search) const
1736 {
1737 return m_vec ? m_vec->contains (search) : false;
1738 }
1739
1740 template<typename T>
1741 inline bool
1742 vec<T, va_heap, vl_ptr>::using_auto_storage () const
1743 {
1744 return m_vec->m_vecpfx.m_using_auto_storage;
1745 }
1746
1747 /* Release VEC and call release of all element vectors. */
1748
1749 template<typename T>
1750 inline void
1751 release_vec_vec (vec<vec<T> > &vec)
1752 {
1753 for (unsigned i = 0; i < vec.length (); i++)
1754 vec[i].release ();
1755
1756 vec.release ();
1757 }
1758
1759 #if (GCC_VERSION >= 3000)
1760 # pragma GCC poison m_vec m_vecpfx m_vecdata
1761 #endif
1762
1763 #endif // GCC_VEC_H