PR libstdc++/66354
[official-gcc.git] / gcc / vec.h
blob7b9697922ad6ebec924804c4c125e3b46677299e
1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
4 Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #ifndef GCC_VEC_H
23 #define GCC_VEC_H
25 /* FIXME - When compiling some of the gen* binaries, we cannot enable GC
26 support because the headers generated by gengtype are still not
27 present. In particular, the header file gtype-desc.h is missing,
28 so compilation may fail if we try to include ggc.h.
30 Since we use some of those declarations, we need to provide them
31 (even if the GC-based templates are not used). This is not a
32 problem because the code that runs before gengtype is built will
33 never need to use GC vectors. But it does force us to declare
34 these functions more than once. */
35 #ifdef GENERATOR_FILE
36 #define VEC_GC_ENABLED 0
37 #else
38 #define VEC_GC_ENABLED 1
39 #endif // GENERATOR_FILE
41 #include "statistics.h" // For CXX_MEM_STAT_INFO.
43 #if VEC_GC_ENABLED
44 #include "ggc.h"
45 #else
46 # ifndef GCC_GGC_H
47 /* Even if we think that GC is not enabled, the test that sets it is
48 weak. There are files compiled with -DGENERATOR_FILE that already
49 include ggc.h. We only need to provide these definitions if ggc.h
50 has not been included. Sigh. */
52 extern void ggc_free (void *);
53 extern size_t ggc_round_alloc_size (size_t requested_size);
54 extern void *ggc_realloc (void *, size_t MEM_STAT_DECL);
55 # endif // GCC_GGC_H
56 #endif // VEC_GC_ENABLED
58 /* Templated vector type and associated interfaces.
60 The interface functions are typesafe and use inline functions,
61 sometimes backed by out-of-line generic functions. The vectors are
62 designed to interoperate with the GTY machinery.
64 There are both 'index' and 'iterate' accessors. The index accessor
65 is implemented by operator[]. The iterator returns a boolean
66 iteration condition and updates the iteration variable passed by
67 reference. Because the iterator will be inlined, the address-of
68 can be optimized away.
70 Each operation that increases the number of active elements is
71 available in 'quick' and 'safe' variants. The former presumes that
72 there is sufficient allocated space for the operation to succeed
73 (it dies if there is not). The latter will reallocate the
74 vector, if needed. Reallocation causes an exponential increase in
75 vector size. If you know you will be adding N elements, it would
76 be more efficient to use the reserve operation before adding the
77 elements with the 'quick' operation. This will ensure there are at
78 least as many elements as you ask for, it will exponentially
79 increase if there are too few spare slots. If you want reserve a
80 specific number of slots, but do not want the exponential increase
81 (for instance, you know this is the last allocation), use the
82 reserve_exact operation. You can also create a vector of a
83 specific size from the get go.
85 You should prefer the push and pop operations, as they append and
86 remove from the end of the vector. If you need to remove several
87 items in one go, use the truncate operation. The insert and remove
88 operations allow you to change elements in the middle of the
89 vector. There are two remove operations, one which preserves the
90 element ordering 'ordered_remove', and one which does not
91 'unordered_remove'. The latter function copies the end element
92 into the removed slot, rather than invoke a memmove operation. The
93 'lower_bound' function will determine where to place an item in the
94 array using insert that will maintain sorted order.
96 Vectors are template types with three arguments: the type of the
97 elements in the vector, the allocation strategy, and the physical
98 layout to use
100 Four allocation strategies are supported:
102 - Heap: allocation is done using malloc/free. This is the
103 default allocation strategy.
105 - GC: allocation is done using ggc_alloc/ggc_free.
107 - GC atomic: same as GC with the exception that the elements
108 themselves are assumed to be of an atomic type that does
109 not need to be garbage collected. This means that marking
110 routines do not need to traverse the array marking the
111 individual elements. This increases the performance of
112 GC activities.
114 Two physical layouts are supported:
116 - Embedded: The vector is structured using the trailing array
117 idiom. The last member of the structure is an array of size
118 1. When the vector is initially allocated, a single memory
119 block is created to hold the vector's control data and the
120 array of elements. These vectors cannot grow without
121 reallocation (see discussion on embeddable vectors below).
123 - Space efficient: The vector is structured as a pointer to an
124 embedded vector. This is the default layout. It means that
125 vectors occupy a single word of storage before initial
126 allocation. Vectors are allowed to grow (the internal
127 pointer is reallocated but the main vector instance does not
128 need to relocate).
130 The type, allocation and layout are specified when the vector is
131 declared.
133 If you need to directly manipulate a vector, then the 'address'
134 accessor will return the address of the start of the vector. Also
135 the 'space' predicate will tell you whether there is spare capacity
136 in the vector. You will not normally need to use these two functions.
138 Notes on the different layout strategies
140 * Embeddable vectors (vec<T, A, vl_embed>)
142 These vectors are suitable to be embedded in other data
143 structures so that they can be pre-allocated in a contiguous
144 memory block.
146 Embeddable vectors are implemented using the trailing array
147 idiom, thus they are not resizeable without changing the address
148 of the vector object itself. This means you cannot have
149 variables or fields of embeddable vector type -- always use a
150 pointer to a vector. The one exception is the final field of a
151 structure, which could be a vector type.
153 You will have to use the embedded_size & embedded_init calls to
154 create such objects, and they will not be resizeable (so the
155 'safe' allocation variants are not available).
157 Properties of embeddable vectors:
159 - The whole vector and control data are allocated in a single
160 contiguous block. It uses the trailing-vector idiom, so
161 allocation must reserve enough space for all the elements
162 in the vector plus its control data.
163 - The vector cannot be re-allocated.
164 - The vector cannot grow nor shrink.
165 - No indirections needed for access/manipulation.
166 - It requires 2 words of storage (prior to vector allocation).
169 * Space efficient vector (vec<T, A, vl_ptr>)
171 These vectors can grow dynamically and are allocated together
172 with their control data. They are suited to be included in data
173 structures. Prior to initial allocation, they only take a single
174 word of storage.
176 These vectors are implemented as a pointer to embeddable vectors.
177 The semantics allow for this pointer to be NULL to represent
178 empty vectors. This way, empty vectors occupy minimal space in
179 the structure containing them.
181 Properties:
183 - The whole vector and control data are allocated in a single
184 contiguous block.
185 - The whole vector may be re-allocated.
186 - Vector data may grow and shrink.
187 - Access and manipulation requires a pointer test and
188 indirection.
189 - It requires 1 word of storage (prior to vector allocation).
191 An example of their use would be,
193 struct my_struct {
194 // A space-efficient vector of tree pointers in GC memory.
195 vec<tree, va_gc, vl_ptr> v;
198 struct my_struct *s;
200 if (s->v.length ()) { we have some contents }
201 s->v.safe_push (decl); // append some decl onto the end
202 for (ix = 0; s->v.iterate (ix, &elt); ix++)
203 { do something with elt }
206 /* Support function for statistics. */
207 extern void dump_vec_loc_statistics (void);
209 /* Hashtable mapping vec addresses to descriptors. */
210 extern htab_t vec_mem_usage_hash;
212 /* Control data for vectors. This contains the number of allocated
213 and used slots inside a vector. */
215 struct vec_prefix
217 /* FIXME - These fields should be private, but we need to cater to
218 compilers that have stricter notions of PODness for types. */
220 /* Memory allocation support routines in vec.c. */
221 void register_overhead (void *, size_t, size_t CXX_MEM_STAT_INFO);
222 void release_overhead (void *, size_t, bool CXX_MEM_STAT_INFO);
223 static unsigned calculate_allocation (vec_prefix *, unsigned, bool);
224 static unsigned calculate_allocation_1 (unsigned, unsigned);
226 /* Note that vec_prefix should be a base class for vec, but we use
227 offsetof() on vector fields of tree structures (e.g.,
228 tree_binfo::base_binfos), and offsetof only supports base types.
230 To compensate, we make vec_prefix a field inside vec and make
231 vec a friend class of vec_prefix so it can access its fields. */
232 template <typename, typename, typename> friend struct vec;
234 /* The allocator types also need access to our internals. */
235 friend struct va_gc;
236 friend struct va_gc_atomic;
237 friend struct va_heap;
239 unsigned m_alloc : 31;
240 unsigned m_using_auto_storage : 1;
241 unsigned m_num;
244 /* Calculate the number of slots to reserve a vector, making sure that
245 RESERVE slots are free. If EXACT grow exactly, otherwise grow
246 exponentially. PFX is the control data for the vector. */
248 inline unsigned
249 vec_prefix::calculate_allocation (vec_prefix *pfx, unsigned reserve,
250 bool exact)
252 if (exact)
253 return (pfx ? pfx->m_num : 0) + reserve;
254 else if (!pfx)
255 return MAX (4, reserve);
256 return calculate_allocation_1 (pfx->m_alloc, pfx->m_num + reserve);
259 template<typename, typename, typename> struct vec;
261 /* Valid vector layouts
263 vl_embed - Embeddable vector that uses the trailing array idiom.
264 vl_ptr - Space efficient vector that uses a pointer to an
265 embeddable vector. */
266 struct vl_embed { };
267 struct vl_ptr { };
270 /* Types of supported allocations
272 va_heap - Allocation uses malloc/free.
273 va_gc - Allocation uses ggc_alloc.
274 va_gc_atomic - Same as GC, but individual elements of the array
275 do not need to be marked during collection. */
277 /* Allocator type for heap vectors. */
278 struct va_heap
280 /* Heap vectors are frequently regular instances, so use the vl_ptr
281 layout for them. */
282 typedef vl_ptr default_layout;
284 template<typename T>
285 static void reserve (vec<T, va_heap, vl_embed> *&, unsigned, bool
286 CXX_MEM_STAT_INFO);
288 template<typename T>
289 static void release (vec<T, va_heap, vl_embed> *&);
293 /* Allocator for heap memory. Ensure there are at least RESERVE free
294 slots in V. If EXACT is true, grow exactly, else grow
295 exponentially. As a special case, if the vector had not been
296 allocated and and RESERVE is 0, no vector will be created. */
298 template<typename T>
299 inline void
300 va_heap::reserve (vec<T, va_heap, vl_embed> *&v, unsigned reserve, bool exact
301 MEM_STAT_DECL)
303 unsigned alloc
304 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
305 gcc_checking_assert (alloc);
307 if (GATHER_STATISTICS && v)
308 v->m_vecpfx.release_overhead (v, v->allocated (), false);
310 size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc);
311 unsigned nelem = v ? v->length () : 0;
312 v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size));
313 v->embedded_init (alloc, nelem);
315 if (GATHER_STATISTICS)
316 v->m_vecpfx.register_overhead (v, alloc, nelem PASS_MEM_STAT);
320 /* Free the heap space allocated for vector V. */
322 template<typename T>
323 void
324 va_heap::release (vec<T, va_heap, vl_embed> *&v)
326 if (v == NULL)
327 return;
329 if (GATHER_STATISTICS)
330 v->m_vecpfx.release_overhead (v, v->allocated (), true);
331 ::free (v);
332 v = NULL;
336 /* Allocator type for GC vectors. Notice that we need the structure
337 declaration even if GC is not enabled. */
339 struct va_gc
341 /* Use vl_embed as the default layout for GC vectors. Due to GTY
342 limitations, GC vectors must always be pointers, so it is more
343 efficient to use a pointer to the vl_embed layout, rather than
344 using a pointer to a pointer as would be the case with vl_ptr. */
345 typedef vl_embed default_layout;
347 template<typename T, typename A>
348 static void reserve (vec<T, A, vl_embed> *&, unsigned, bool
349 CXX_MEM_STAT_INFO);
351 template<typename T, typename A>
352 static void release (vec<T, A, vl_embed> *&v);
356 /* Free GC memory used by V and reset V to NULL. */
358 template<typename T, typename A>
359 inline void
360 va_gc::release (vec<T, A, vl_embed> *&v)
362 if (v)
363 ::ggc_free (v);
364 v = NULL;
368 /* Allocator for GC memory. Ensure there are at least RESERVE free
369 slots in V. If EXACT is true, grow exactly, else grow
370 exponentially. As a special case, if the vector had not been
371 allocated and and RESERVE is 0, no vector will be created. */
373 template<typename T, typename A>
374 void
375 va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact
376 MEM_STAT_DECL)
378 unsigned alloc
379 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
380 if (!alloc)
382 ::ggc_free (v);
383 v = NULL;
384 return;
387 /* Calculate the amount of space we want. */
388 size_t size = vec<T, A, vl_embed>::embedded_size (alloc);
390 /* Ask the allocator how much space it will really give us. */
391 size = ::ggc_round_alloc_size (size);
393 /* Adjust the number of slots accordingly. */
394 size_t vec_offset = sizeof (vec_prefix);
395 size_t elt_size = sizeof (T);
396 alloc = (size - vec_offset) / elt_size;
398 /* And finally, recalculate the amount of space we ask for. */
399 size = vec_offset + alloc * elt_size;
401 unsigned nelem = v ? v->length () : 0;
402 v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc (v, size
403 PASS_MEM_STAT));
404 v->embedded_init (alloc, nelem);
408 /* Allocator type for GC vectors. This is for vectors of types
409 atomics w.r.t. collection, so allocation and deallocation is
410 completely inherited from va_gc. */
411 struct va_gc_atomic : va_gc
416 /* Generic vector template. Default values for A and L indicate the
417 most commonly used strategies.
419 FIXME - Ideally, they would all be vl_ptr to encourage using regular
420 instances for vectors, but the existing GTY machinery is limited
421 in that it can only deal with GC objects that are pointers
422 themselves.
424 This means that vector operations that need to deal with
425 potentially NULL pointers, must be provided as free
426 functions (see the vec_safe_* functions above). */
427 template<typename T,
428 typename A = va_heap,
429 typename L = typename A::default_layout>
430 struct GTY((user)) vec
434 /* Type to provide NULL values for vec<T, A, L>. This is used to
435 provide nil initializers for vec instances. Since vec must be
436 a POD, we cannot have proper ctor/dtor for it. To initialize
437 a vec instance, you can assign it the value vNULL. */
438 struct vnull
440 template <typename T, typename A, typename L>
441 operator vec<T, A, L> () { return vec<T, A, L>(); }
443 extern vnull vNULL;
446 /* Embeddable vector. These vectors are suitable to be embedded
447 in other data structures so that they can be pre-allocated in a
448 contiguous memory block.
450 Embeddable vectors are implemented using the trailing array idiom,
451 thus they are not resizeable without changing the address of the
452 vector object itself. This means you cannot have variables or
453 fields of embeddable vector type -- always use a pointer to a
454 vector. The one exception is the final field of a structure, which
455 could be a vector type.
457 You will have to use the embedded_size & embedded_init calls to
458 create such objects, and they will not be resizeable (so the 'safe'
459 allocation variants are not available).
461 Properties:
463 - The whole vector and control data are allocated in a single
464 contiguous block. It uses the trailing-vector idiom, so
465 allocation must reserve enough space for all the elements
466 in the vector plus its control data.
467 - The vector cannot be re-allocated.
468 - The vector cannot grow nor shrink.
469 - No indirections needed for access/manipulation.
470 - It requires 2 words of storage (prior to vector allocation). */
472 template<typename T, typename A>
473 struct GTY((user)) vec<T, A, vl_embed>
475 public:
476 unsigned allocated (void) const { return m_vecpfx.m_alloc; }
477 unsigned length (void) const { return m_vecpfx.m_num; }
478 bool is_empty (void) const { return m_vecpfx.m_num == 0; }
479 T *address (void) { return m_vecdata; }
480 const T *address (void) const { return m_vecdata; }
481 const T &operator[] (unsigned) const;
482 T &operator[] (unsigned);
483 T &last (void);
484 bool space (unsigned) const;
485 bool iterate (unsigned, T *) const;
486 bool iterate (unsigned, T **) const;
487 vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
488 void splice (const vec &);
489 void splice (const vec *src);
490 T *quick_push (const T &);
491 T &pop (void);
492 void truncate (unsigned);
493 void quick_insert (unsigned, const T &);
494 void ordered_remove (unsigned);
495 void unordered_remove (unsigned);
496 void block_remove (unsigned, unsigned);
497 void qsort (int (*) (const void *, const void *));
498 T *bsearch (const void *key, int (*compar)(const void *, const void *));
499 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
500 static size_t embedded_size (unsigned);
501 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
502 void quick_grow (unsigned len);
503 void quick_grow_cleared (unsigned len);
505 /* vec class can access our internal data and functions. */
506 template <typename, typename, typename> friend struct vec;
508 /* The allocator types also need access to our internals. */
509 friend struct va_gc;
510 friend struct va_gc_atomic;
511 friend struct va_heap;
513 /* FIXME - These fields should be private, but we need to cater to
514 compilers that have stricter notions of PODness for types. */
515 vec_prefix m_vecpfx;
516 T m_vecdata[1];
520 /* Convenience wrapper functions to use when dealing with pointers to
521 embedded vectors. Some functionality for these vectors must be
522 provided via free functions for these reasons:
524 1- The pointer may be NULL (e.g., before initial allocation).
526 2- When the vector needs to grow, it must be reallocated, so
527 the pointer will change its value.
529 Because of limitations with the current GC machinery, all vectors
530 in GC memory *must* be pointers. */
533 /* If V contains no room for NELEMS elements, return false. Otherwise,
534 return true. */
535 template<typename T, typename A>
536 inline bool
537 vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
539 return v ? v->space (nelems) : nelems == 0;
543 /* If V is NULL, return 0. Otherwise, return V->length(). */
544 template<typename T, typename A>
545 inline unsigned
546 vec_safe_length (const vec<T, A, vl_embed> *v)
548 return v ? v->length () : 0;
552 /* If V is NULL, return NULL. Otherwise, return V->address(). */
553 template<typename T, typename A>
554 inline T *
555 vec_safe_address (vec<T, A, vl_embed> *v)
557 return v ? v->address () : NULL;
561 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
562 template<typename T, typename A>
563 inline bool
564 vec_safe_is_empty (vec<T, A, vl_embed> *v)
566 return v ? v->is_empty () : true;
570 /* If V does not have space for NELEMS elements, call
571 V->reserve(NELEMS, EXACT). */
572 template<typename T, typename A>
573 inline bool
574 vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
575 CXX_MEM_STAT_INFO)
577 bool extend = nelems ? !vec_safe_space (v, nelems) : false;
578 if (extend)
579 A::reserve (v, nelems, exact PASS_MEM_STAT);
580 return extend;
583 template<typename T, typename A>
584 inline bool
585 vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
586 CXX_MEM_STAT_INFO)
588 return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
592 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
593 is 0, V is initialized to NULL. */
595 template<typename T, typename A>
596 inline void
597 vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
599 v = NULL;
600 vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
604 /* Free the GC memory allocated by vector V and set it to NULL. */
606 template<typename T, typename A>
607 inline void
608 vec_free (vec<T, A, vl_embed> *&v)
610 A::release (v);
614 /* Grow V to length LEN. Allocate it, if necessary. */
615 template<typename T, typename A>
616 inline void
617 vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
619 unsigned oldlen = vec_safe_length (v);
620 gcc_checking_assert (len >= oldlen);
621 vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT);
622 v->quick_grow (len);
626 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
627 template<typename T, typename A>
628 inline void
629 vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
631 unsigned oldlen = vec_safe_length (v);
632 vec_safe_grow (v, len PASS_MEM_STAT);
633 memset (&(v->address ()[oldlen]), 0, sizeof (T) * (len - oldlen));
637 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
638 template<typename T, typename A>
639 inline bool
640 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
642 if (v)
643 return v->iterate (ix, ptr);
644 else
646 *ptr = 0;
647 return false;
651 template<typename T, typename A>
652 inline bool
653 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
655 if (v)
656 return v->iterate (ix, ptr);
657 else
659 *ptr = 0;
660 return false;
665 /* If V has no room for one more element, reallocate it. Then call
666 V->quick_push(OBJ). */
667 template<typename T, typename A>
668 inline T *
669 vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
671 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
672 return v->quick_push (obj);
676 /* if V has no room for one more element, reallocate it. Then call
677 V->quick_insert(IX, OBJ). */
678 template<typename T, typename A>
679 inline void
680 vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
681 CXX_MEM_STAT_INFO)
683 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
684 v->quick_insert (ix, obj);
688 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
689 template<typename T, typename A>
690 inline void
691 vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
693 if (v)
694 v->truncate (size);
698 /* If SRC is not NULL, return a pointer to a copy of it. */
699 template<typename T, typename A>
700 inline vec<T, A, vl_embed> *
701 vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
703 return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
706 /* Copy the elements from SRC to the end of DST as if by memcpy.
707 Reallocate DST, if necessary. */
708 template<typename T, typename A>
709 inline void
710 vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
711 CXX_MEM_STAT_INFO)
713 unsigned src_len = vec_safe_length (src);
714 if (src_len)
716 vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
717 PASS_MEM_STAT);
718 dst->splice (*src);
723 /* Index into vector. Return the IX'th element. IX must be in the
724 domain of the vector. */
726 template<typename T, typename A>
727 inline const T &
728 vec<T, A, vl_embed>::operator[] (unsigned ix) const
730 gcc_checking_assert (ix < m_vecpfx.m_num);
731 return m_vecdata[ix];
734 template<typename T, typename A>
735 inline T &
736 vec<T, A, vl_embed>::operator[] (unsigned ix)
738 gcc_checking_assert (ix < m_vecpfx.m_num);
739 return m_vecdata[ix];
743 /* Get the final element of the vector, which must not be empty. */
745 template<typename T, typename A>
746 inline T &
747 vec<T, A, vl_embed>::last (void)
749 gcc_checking_assert (m_vecpfx.m_num > 0);
750 return (*this)[m_vecpfx.m_num - 1];
754 /* If this vector has space for NELEMS additional entries, return
755 true. You usually only need to use this if you are doing your
756 own vector reallocation, for instance on an embedded vector. This
757 returns true in exactly the same circumstances that vec::reserve
758 will. */
760 template<typename T, typename A>
761 inline bool
762 vec<T, A, vl_embed>::space (unsigned nelems) const
764 return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
768 /* Return iteration condition and update PTR to point to the IX'th
769 element of this vector. Use this to iterate over the elements of a
770 vector as follows,
772 for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
773 continue; */
775 template<typename T, typename A>
776 inline bool
777 vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
779 if (ix < m_vecpfx.m_num)
781 *ptr = m_vecdata[ix];
782 return true;
784 else
786 *ptr = 0;
787 return false;
792 /* Return iteration condition and update *PTR to point to the
793 IX'th element of this vector. Use this to iterate over the
794 elements of a vector as follows,
796 for (ix = 0; v->iterate (ix, &ptr); ix++)
797 continue;
799 This variant is for vectors of objects. */
801 template<typename T, typename A>
802 inline bool
803 vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
805 if (ix < m_vecpfx.m_num)
807 *ptr = CONST_CAST (T *, &m_vecdata[ix]);
808 return true;
810 else
812 *ptr = 0;
813 return false;
818 /* Return a pointer to a copy of this vector. */
820 template<typename T, typename A>
821 inline vec<T, A, vl_embed> *
822 vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
824 vec<T, A, vl_embed> *new_vec = NULL;
825 unsigned len = length ();
826 if (len)
828 vec_alloc (new_vec, len PASS_MEM_STAT);
829 new_vec->embedded_init (len, len);
830 memcpy (new_vec->address (), m_vecdata, sizeof (T) * len);
832 return new_vec;
836 /* Copy the elements from SRC to the end of this vector as if by memcpy.
837 The vector must have sufficient headroom available. */
839 template<typename T, typename A>
840 inline void
841 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
843 unsigned len = src.length ();
844 if (len)
846 gcc_checking_assert (space (len));
847 memcpy (address () + length (), src.address (), len * sizeof (T));
848 m_vecpfx.m_num += len;
852 template<typename T, typename A>
853 inline void
854 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
856 if (src)
857 splice (*src);
861 /* Push OBJ (a new element) onto the end of the vector. There must be
862 sufficient space in the vector. Return a pointer to the slot
863 where OBJ was inserted. */
865 template<typename T, typename A>
866 inline T *
867 vec<T, A, vl_embed>::quick_push (const T &obj)
869 gcc_checking_assert (space (1));
870 T *slot = &m_vecdata[m_vecpfx.m_num++];
871 *slot = obj;
872 return slot;
876 /* Pop and return the last element off the end of the vector. */
878 template<typename T, typename A>
879 inline T &
880 vec<T, A, vl_embed>::pop (void)
882 gcc_checking_assert (length () > 0);
883 return m_vecdata[--m_vecpfx.m_num];
887 /* Set the length of the vector to SIZE. The new length must be less
888 than or equal to the current length. This is an O(1) operation. */
890 template<typename T, typename A>
891 inline void
892 vec<T, A, vl_embed>::truncate (unsigned size)
894 gcc_checking_assert (length () >= size);
895 m_vecpfx.m_num = size;
899 /* Insert an element, OBJ, at the IXth position of this vector. There
900 must be sufficient space. */
902 template<typename T, typename A>
903 inline void
904 vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
906 gcc_checking_assert (length () < allocated ());
907 gcc_checking_assert (ix <= length ());
908 T *slot = &m_vecdata[ix];
909 memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
910 *slot = obj;
914 /* Remove an element from the IXth position of this vector. Ordering of
915 remaining elements is preserved. This is an O(N) operation due to
916 memmove. */
918 template<typename T, typename A>
919 inline void
920 vec<T, A, vl_embed>::ordered_remove (unsigned ix)
922 gcc_checking_assert (ix < length ());
923 T *slot = &m_vecdata[ix];
924 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
928 /* Remove an element from the IXth position of this vector. Ordering of
929 remaining elements is destroyed. This is an O(1) operation. */
931 template<typename T, typename A>
932 inline void
933 vec<T, A, vl_embed>::unordered_remove (unsigned ix)
935 gcc_checking_assert (ix < length ());
936 m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
940 /* Remove LEN elements starting at the IXth. Ordering is retained.
941 This is an O(N) operation due to memmove. */
943 template<typename T, typename A>
944 inline void
945 vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
947 gcc_checking_assert (ix + len <= length ());
948 T *slot = &m_vecdata[ix];
949 m_vecpfx.m_num -= len;
950 memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
954 /* Sort the contents of this vector with qsort. CMP is the comparison
955 function to pass to qsort. */
957 template<typename T, typename A>
958 inline void
959 vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *))
961 if (length () > 1)
962 ::qsort (address (), length (), sizeof (T), cmp);
966 /* Search the contents of the sorted vector with a binary search.
967 CMP is the comparison function to pass to bsearch. */
969 template<typename T, typename A>
970 inline T *
971 vec<T, A, vl_embed>::bsearch (const void *key,
972 int (*compar) (const void *, const void *))
974 const void *base = this->address ();
975 size_t nmemb = this->length ();
976 size_t size = sizeof (T);
977 /* The following is a copy of glibc stdlib-bsearch.h. */
978 size_t l, u, idx;
979 const void *p;
980 int comparison;
982 l = 0;
983 u = nmemb;
984 while (l < u)
986 idx = (l + u) / 2;
987 p = (const void *) (((const char *) base) + (idx * size));
988 comparison = (*compar) (key, p);
989 if (comparison < 0)
990 u = idx;
991 else if (comparison > 0)
992 l = idx + 1;
993 else
994 return (T *)const_cast<void *>(p);
997 return NULL;
1001 /* Find and return the first position in which OBJ could be inserted
1002 without changing the ordering of this vector. LESSTHAN is a
1003 function that returns true if the first argument is strictly less
1004 than the second. */
1006 template<typename T, typename A>
1007 unsigned
1008 vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
1009 const
1011 unsigned int len = length ();
1012 unsigned int half, middle;
1013 unsigned int first = 0;
1014 while (len > 0)
1016 half = len / 2;
1017 middle = first;
1018 middle += half;
1019 T middle_elem = (*this)[middle];
1020 if (lessthan (middle_elem, obj))
1022 first = middle;
1023 ++first;
1024 len = len - half - 1;
1026 else
1027 len = half;
1029 return first;
1033 /* Return the number of bytes needed to embed an instance of an
1034 embeddable vec inside another data structure.
1036 Use these methods to determine the required size and initialization
1037 of a vector V of type T embedded within another structure (as the
1038 final member):
1040 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1041 void v->embedded_init (unsigned alloc, unsigned num);
1043 These allow the caller to perform the memory allocation. */
1045 template<typename T, typename A>
1046 inline size_t
1047 vec<T, A, vl_embed>::embedded_size (unsigned alloc)
1049 typedef vec<T, A, vl_embed> vec_embedded;
1050 return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
1054 /* Initialize the vector to contain room for ALLOC elements and
1055 NUM active elements. */
1057 template<typename T, typename A>
1058 inline void
1059 vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
1061 m_vecpfx.m_alloc = alloc;
1062 m_vecpfx.m_using_auto_storage = aut;
1063 m_vecpfx.m_num = num;
1067 /* Grow the vector to a specific length. LEN must be as long or longer than
1068 the current length. The new elements are uninitialized. */
1070 template<typename T, typename A>
1071 inline void
1072 vec<T, A, vl_embed>::quick_grow (unsigned len)
1074 gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
1075 m_vecpfx.m_num = len;
1079 /* Grow the vector to a specific length. LEN must be as long or longer than
1080 the current length. The new elements are initialized to zero. */
1082 template<typename T, typename A>
1083 inline void
1084 vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
1086 unsigned oldlen = length ();
1087 quick_grow (len);
1088 memset (&(address ()[oldlen]), 0, sizeof (T) * (len - oldlen));
1092 /* Garbage collection support for vec<T, A, vl_embed>. */
1094 template<typename T>
1095 void
1096 gt_ggc_mx (vec<T, va_gc> *v)
1098 extern void gt_ggc_mx (T &);
1099 for (unsigned i = 0; i < v->length (); i++)
1100 gt_ggc_mx ((*v)[i]);
1103 template<typename T>
1104 void
1105 gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
1107 /* Nothing to do. Vectors of atomic types wrt GC do not need to
1108 be traversed. */
1112 /* PCH support for vec<T, A, vl_embed>. */
1114 template<typename T, typename A>
1115 void
1116 gt_pch_nx (vec<T, A, vl_embed> *v)
1118 extern void gt_pch_nx (T &);
1119 for (unsigned i = 0; i < v->length (); i++)
1120 gt_pch_nx ((*v)[i]);
1123 template<typename T, typename A>
1124 void
1125 gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1127 for (unsigned i = 0; i < v->length (); i++)
1128 op (&((*v)[i]), cookie);
1131 template<typename T, typename A>
1132 void
1133 gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1135 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
1136 for (unsigned i = 0; i < v->length (); i++)
1137 gt_pch_nx (&((*v)[i]), op, cookie);
1141 /* Space efficient vector. These vectors can grow dynamically and are
1142 allocated together with their control data. They are suited to be
1143 included in data structures. Prior to initial allocation, they
1144 only take a single word of storage.
1146 These vectors are implemented as a pointer to an embeddable vector.
1147 The semantics allow for this pointer to be NULL to represent empty
1148 vectors. This way, empty vectors occupy minimal space in the
1149 structure containing them.
1151 Properties:
1153 - The whole vector and control data are allocated in a single
1154 contiguous block.
1155 - The whole vector may be re-allocated.
1156 - Vector data may grow and shrink.
1157 - Access and manipulation requires a pointer test and
1158 indirection.
1159 - It requires 1 word of storage (prior to vector allocation).
1162 Limitations:
1164 These vectors must be PODs because they are stored in unions.
1165 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1166 As long as we use C++03, we cannot have constructors nor
1167 destructors in classes that are stored in unions. */
1169 template<typename T>
1170 struct vec<T, va_heap, vl_ptr>
1172 public:
1173 /* Memory allocation and deallocation for the embedded vector.
1174 Needed because we cannot have proper ctors/dtors defined. */
1175 void create (unsigned nelems CXX_MEM_STAT_INFO);
1176 void release (void);
1178 /* Vector operations. */
1179 bool exists (void) const
1180 { return m_vec != NULL; }
1182 bool is_empty (void) const
1183 { return m_vec ? m_vec->is_empty () : true; }
1185 unsigned length (void) const
1186 { return m_vec ? m_vec->length () : 0; }
1188 T *address (void)
1189 { return m_vec ? m_vec->m_vecdata : NULL; }
1191 const T *address (void) const
1192 { return m_vec ? m_vec->m_vecdata : NULL; }
1194 const T &operator[] (unsigned ix) const
1195 { return (*m_vec)[ix]; }
1197 bool operator!=(const vec &other) const
1198 { return !(*this == other); }
1200 bool operator==(const vec &other) const
1201 { return address () == other.address (); }
1203 T &operator[] (unsigned ix)
1204 { return (*m_vec)[ix]; }
1206 T &last (void)
1207 { return m_vec->last (); }
1209 bool space (int nelems) const
1210 { return m_vec ? m_vec->space (nelems) : nelems == 0; }
1212 bool iterate (unsigned ix, T *p) const;
1213 bool iterate (unsigned ix, T **p) const;
1214 vec copy (ALONE_CXX_MEM_STAT_INFO) const;
1215 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
1216 bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
1217 void splice (const vec &);
1218 void safe_splice (const vec & CXX_MEM_STAT_INFO);
1219 T *quick_push (const T &);
1220 T *safe_push (const T &CXX_MEM_STAT_INFO);
1221 T &pop (void);
1222 void truncate (unsigned);
1223 void safe_grow (unsigned CXX_MEM_STAT_INFO);
1224 void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO);
1225 void quick_grow (unsigned);
1226 void quick_grow_cleared (unsigned);
1227 void quick_insert (unsigned, const T &);
1228 void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
1229 void ordered_remove (unsigned);
1230 void unordered_remove (unsigned);
1231 void block_remove (unsigned, unsigned);
1232 void qsort (int (*) (const void *, const void *));
1233 T *bsearch (const void *key, int (*compar)(const void *, const void *));
1234 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
1236 bool using_auto_storage () const;
1238 /* FIXME - This field should be private, but we need to cater to
1239 compilers that have stricter notions of PODness for types. */
1240 vec<T, va_heap, vl_embed> *m_vec;
1244 /* auto_vec is a subclass of vec that automatically manages creating and
1245 releasing the internal vector. If N is non zero then it has N elements of
1246 internal storage. The default is no internal storage, and you probably only
1247 want to ask for internal storage for vectors on the stack because if the
1248 size of the vector is larger than the internal storage that space is wasted.
1250 template<typename T, size_t N = 0>
1251 class auto_vec : public vec<T, va_heap>
1253 public:
1254 auto_vec ()
1256 m_auto.embedded_init (MAX (N, 2), 0, 1);
1257 this->m_vec = &m_auto;
1260 ~auto_vec ()
1262 this->release ();
1265 private:
1266 vec<T, va_heap, vl_embed> m_auto;
1267 T m_data[MAX (N - 1, 1)];
1270 /* auto_vec is a sub class of vec whose storage is released when it is
1271 destroyed. */
1272 template<typename T>
1273 class auto_vec<T, 0> : public vec<T, va_heap>
1275 public:
1276 auto_vec () { this->m_vec = NULL; }
1277 auto_vec (size_t n) { this->create (n); }
1278 ~auto_vec () { this->release (); }
1282 /* Allocate heap memory for pointer V and create the internal vector
1283 with space for NELEMS elements. If NELEMS is 0, the internal
1284 vector is initialized to empty. */
1286 template<typename T>
1287 inline void
1288 vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
1290 v = new vec<T>;
1291 v->create (nelems PASS_MEM_STAT);
1295 /* Conditionally allocate heap memory for VEC and its internal vector. */
1297 template<typename T>
1298 inline void
1299 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
1301 if (!vec)
1302 vec_alloc (vec, nelems PASS_MEM_STAT);
1306 /* Free the heap memory allocated by vector V and set it to NULL. */
1308 template<typename T>
1309 inline void
1310 vec_free (vec<T> *&v)
1312 if (v == NULL)
1313 return;
1315 v->release ();
1316 delete v;
1317 v = NULL;
1321 /* Return iteration condition and update PTR to point to the IX'th
1322 element of this vector. Use this to iterate over the elements of a
1323 vector as follows,
1325 for (ix = 0; v.iterate (ix, &ptr); ix++)
1326 continue; */
1328 template<typename T>
1329 inline bool
1330 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
1332 if (m_vec)
1333 return m_vec->iterate (ix, ptr);
1334 else
1336 *ptr = 0;
1337 return false;
1342 /* Return iteration condition and update *PTR to point to the
1343 IX'th element of this vector. Use this to iterate over the
1344 elements of a vector as follows,
1346 for (ix = 0; v->iterate (ix, &ptr); ix++)
1347 continue;
1349 This variant is for vectors of objects. */
1351 template<typename T>
1352 inline bool
1353 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
1355 if (m_vec)
1356 return m_vec->iterate (ix, ptr);
1357 else
1359 *ptr = 0;
1360 return false;
1365 /* Convenience macro for forward iteration. */
1366 #define FOR_EACH_VEC_ELT(V, I, P) \
1367 for (I = 0; (V).iterate ((I), &(P)); ++(I))
1369 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
1370 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1372 /* Likewise, but start from FROM rather than 0. */
1373 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
1374 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1376 /* Convenience macro for reverse iteration. */
1377 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
1378 for (I = (V).length () - 1; \
1379 (V).iterate ((I), &(P)); \
1380 (I)--)
1382 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1383 for (I = vec_safe_length (V) - 1; \
1384 vec_safe_iterate ((V), (I), &(P)); \
1385 (I)--)
1388 /* Return a copy of this vector. */
1390 template<typename T>
1391 inline vec<T, va_heap, vl_ptr>
1392 vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
1394 vec<T, va_heap, vl_ptr> new_vec = vNULL;
1395 if (length ())
1396 new_vec.m_vec = m_vec->copy ();
1397 return new_vec;
1401 /* Ensure that the vector has at least RESERVE slots available (if
1402 EXACT is false), or exactly RESERVE slots available (if EXACT is
1403 true).
1405 This may create additional headroom if EXACT is false.
1407 Note that this can cause the embedded vector to be reallocated.
1408 Returns true iff reallocation actually occurred. */
1410 template<typename T>
1411 inline bool
1412 vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
1414 if (space (nelems))
1415 return false;
1417 /* For now play a game with va_heap::reserve to hide our auto storage if any,
1418 this is necessary because it doesn't have enough information to know the
1419 embedded vector is in auto storage, and so should not be freed. */
1420 vec<T, va_heap, vl_embed> *oldvec = m_vec;
1421 unsigned int oldsize = 0;
1422 bool handle_auto_vec = m_vec && using_auto_storage ();
1423 if (handle_auto_vec)
1425 m_vec = NULL;
1426 oldsize = oldvec->length ();
1427 nelems += oldsize;
1430 va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
1431 if (handle_auto_vec)
1433 memcpy (m_vec->address (), oldvec->address (), sizeof (T) * oldsize);
1434 m_vec->m_vecpfx.m_num = oldsize;
1437 return true;
1441 /* Ensure that this vector has exactly NELEMS slots available. This
1442 will not create additional headroom. Note this can cause the
1443 embedded vector to be reallocated. Returns true iff reallocation
1444 actually occurred. */
1446 template<typename T>
1447 inline bool
1448 vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
1450 return reserve (nelems, true PASS_MEM_STAT);
1454 /* Create the internal vector and reserve NELEMS for it. This is
1455 exactly like vec::reserve, but the internal vector is
1456 unconditionally allocated from scratch. The old one, if it
1457 existed, is lost. */
1459 template<typename T>
1460 inline void
1461 vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
1463 m_vec = NULL;
1464 if (nelems > 0)
1465 reserve_exact (nelems PASS_MEM_STAT);
1469 /* Free the memory occupied by the embedded vector. */
1471 template<typename T>
1472 inline void
1473 vec<T, va_heap, vl_ptr>::release (void)
1475 if (!m_vec)
1476 return;
1478 if (using_auto_storage ())
1480 m_vec->m_vecpfx.m_num = 0;
1481 return;
1484 va_heap::release (m_vec);
1487 /* Copy the elements from SRC to the end of this vector as if by memcpy.
1488 SRC and this vector must be allocated with the same memory
1489 allocation mechanism. This vector is assumed to have sufficient
1490 headroom available. */
1492 template<typename T>
1493 inline void
1494 vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
1496 if (src.m_vec)
1497 m_vec->splice (*(src.m_vec));
1501 /* Copy the elements in SRC to the end of this vector as if by memcpy.
1502 SRC and this vector must be allocated with the same mechanism.
1503 If there is not enough headroom in this vector, it will be reallocated
1504 as needed. */
1506 template<typename T>
1507 inline void
1508 vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
1509 MEM_STAT_DECL)
1511 if (src.length ())
1513 reserve_exact (src.length ());
1514 splice (src);
1519 /* Push OBJ (a new element) onto the end of the vector. There must be
1520 sufficient space in the vector. Return a pointer to the slot
1521 where OBJ was inserted. */
1523 template<typename T>
1524 inline T *
1525 vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
1527 return m_vec->quick_push (obj);
1531 /* Push a new element OBJ onto the end of this vector. Reallocates
1532 the embedded vector, if needed. Return a pointer to the slot where
1533 OBJ was inserted. */
1535 template<typename T>
1536 inline T *
1537 vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
1539 reserve (1, false PASS_MEM_STAT);
1540 return quick_push (obj);
1544 /* Pop and return the last element off the end of the vector. */
1546 template<typename T>
1547 inline T &
1548 vec<T, va_heap, vl_ptr>::pop (void)
1550 return m_vec->pop ();
1554 /* Set the length of the vector to LEN. The new length must be less
1555 than or equal to the current length. This is an O(1) operation. */
1557 template<typename T>
1558 inline void
1559 vec<T, va_heap, vl_ptr>::truncate (unsigned size)
1561 if (m_vec)
1562 m_vec->truncate (size);
1563 else
1564 gcc_checking_assert (size == 0);
1568 /* Grow the vector to a specific length. LEN must be as long or
1569 longer than the current length. The new elements are
1570 uninitialized. Reallocate the internal vector, if needed. */
1572 template<typename T>
1573 inline void
1574 vec<T, va_heap, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL)
1576 unsigned oldlen = length ();
1577 gcc_checking_assert (oldlen <= len);
1578 reserve_exact (len - oldlen PASS_MEM_STAT);
1579 if (m_vec)
1580 m_vec->quick_grow (len);
1581 else
1582 gcc_checking_assert (len == 0);
1586 /* Grow the embedded vector to a specific length. LEN must be as
1587 long or longer than the current length. The new elements are
1588 initialized to zero. Reallocate the internal vector, if needed. */
1590 template<typename T>
1591 inline void
1592 vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL)
1594 unsigned oldlen = length ();
1595 safe_grow (len PASS_MEM_STAT);
1596 memset (&(address ()[oldlen]), 0, sizeof (T) * (len - oldlen));
1600 /* Same as vec::safe_grow but without reallocation of the internal vector.
1601 If the vector cannot be extended, a runtime assertion will be triggered. */
1603 template<typename T>
1604 inline void
1605 vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
1607 gcc_checking_assert (m_vec);
1608 m_vec->quick_grow (len);
1612 /* Same as vec::quick_grow_cleared but without reallocation of the
1613 internal vector. If the vector cannot be extended, a runtime
1614 assertion will be triggered. */
1616 template<typename T>
1617 inline void
1618 vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
1620 gcc_checking_assert (m_vec);
1621 m_vec->quick_grow_cleared (len);
1625 /* Insert an element, OBJ, at the IXth position of this vector. There
1626 must be sufficient space. */
1628 template<typename T>
1629 inline void
1630 vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
1632 m_vec->quick_insert (ix, obj);
1636 /* Insert an element, OBJ, at the IXth position of the vector.
1637 Reallocate the embedded vector, if necessary. */
1639 template<typename T>
1640 inline void
1641 vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
1643 reserve (1, false PASS_MEM_STAT);
1644 quick_insert (ix, obj);
1648 /* Remove an element from the IXth position of this vector. Ordering of
1649 remaining elements is preserved. This is an O(N) operation due to
1650 a memmove. */
1652 template<typename T>
1653 inline void
1654 vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
1656 m_vec->ordered_remove (ix);
1660 /* Remove an element from the IXth position of this vector. Ordering
1661 of remaining elements is destroyed. This is an O(1) operation. */
1663 template<typename T>
1664 inline void
1665 vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
1667 m_vec->unordered_remove (ix);
1671 /* Remove LEN elements starting at the IXth. Ordering is retained.
1672 This is an O(N) operation due to memmove. */
1674 template<typename T>
1675 inline void
1676 vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
1678 m_vec->block_remove (ix, len);
1682 /* Sort the contents of this vector with qsort. CMP is the comparison
1683 function to pass to qsort. */
1685 template<typename T>
1686 inline void
1687 vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
1689 if (m_vec)
1690 m_vec->qsort (cmp);
1694 /* Search the contents of the sorted vector with a binary search.
1695 CMP is the comparison function to pass to bsearch. */
1697 template<typename T>
1698 inline T *
1699 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
1700 int (*cmp) (const void *, const void *))
1702 if (m_vec)
1703 return m_vec->bsearch (key, cmp);
1704 return NULL;
1708 /* Find and return the first position in which OBJ could be inserted
1709 without changing the ordering of this vector. LESSTHAN is a
1710 function that returns true if the first argument is strictly less
1711 than the second. */
1713 template<typename T>
1714 inline unsigned
1715 vec<T, va_heap, vl_ptr>::lower_bound (T obj,
1716 bool (*lessthan)(const T &, const T &))
1717 const
1719 return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
1722 template<typename T>
1723 inline bool
1724 vec<T, va_heap, vl_ptr>::using_auto_storage () const
1726 return m_vec->m_vecpfx.m_using_auto_storage;
1729 #if (GCC_VERSION >= 3000)
1730 # pragma GCC poison m_vec m_vecpfx m_vecdata
1731 #endif
1733 #endif // GCC_VEC_H