testsuite: Update scanning symbol sections to support AIX.
[official-gcc.git] / gcc / vec.h
blob14d77e873427a06a55885bd98eae3529eb3796f9
1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2020 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 /* 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.*/
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);
34 /* Templated vector type and associated interfaces.
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.
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.
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.
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.
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
76 Four allocation strategies are supported:
78 - Heap: allocation is done using malloc/free. This is the
79 default allocation strategy.
81 - GC: allocation is done using ggc_alloc/ggc_free.
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.
90 Two physical layouts are supported:
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).
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).
106 The type, allocation and layout are specified when the vector is
107 declared.
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.
114 Notes on the different layout strategies
116 * Embeddable vectors (vec<T, A, vl_embed>)
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.
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.
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).
133 Properties of embeddable vectors:
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).
145 * Space efficient vector (vec<T, A, vl_ptr>)
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.
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.
157 Properties:
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).
167 An example of their use would be,
169 struct my_struct {
170 // A space-efficient vector of tree pointers in GC memory.
171 vec<tree, va_gc, vl_ptr> v;
174 struct my_struct *s;
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 }
182 /* Support function for statistics. */
183 extern void dump_vec_loc_statistics (void);
185 /* Hashtable mapping vec addresses to descriptors. */
186 extern htab_t vec_mem_usage_hash;
188 /* Control data for vectors. This contains the number of allocated
189 and used slots inside a vector. */
191 struct vec_prefix
193 /* FIXME - These fields should be private, but we need to cater to
194 compilers that have stricter notions of PODness for types. */
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, size_t, bool CXX_MEM_STAT_INFO);
199 static unsigned calculate_allocation (vec_prefix *, unsigned, bool);
200 static unsigned calculate_allocation_1 (unsigned, unsigned);
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.
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;
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;
215 unsigned m_alloc : 31;
216 unsigned m_using_auto_storage : 1;
217 unsigned m_num;
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. */
224 inline unsigned
225 vec_prefix::calculate_allocation (vec_prefix *pfx, unsigned reserve,
226 bool exact)
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);
235 template<typename, typename, typename> struct vec;
237 /* Valid vector layouts
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 { };
246 /* Types of supported allocations
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. */
253 /* Allocator type for heap vectors. */
254 struct va_heap
256 /* Heap vectors are frequently regular instances, so use the vl_ptr
257 layout for them. */
258 typedef vl_ptr default_layout;
260 template<typename T>
261 static void reserve (vec<T, va_heap, vl_embed> *&, unsigned, bool
262 CXX_MEM_STAT_INFO);
264 template<typename T>
265 static void release (vec<T, va_heap, vl_embed> *&);
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. */
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)
279 size_t elt_size = sizeof (T);
280 unsigned alloc
281 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
282 gcc_checking_assert (alloc);
284 if (GATHER_STATISTICS && v)
285 v->m_vecpfx.release_overhead (v, elt_size * v->allocated (),
286 v->allocated (), false);
288 size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc);
289 unsigned nelem = v ? v->length () : 0;
290 v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size));
291 v->embedded_init (alloc, nelem);
293 if (GATHER_STATISTICS)
294 v->m_vecpfx.register_overhead (v, alloc, elt_size PASS_MEM_STAT);
298 #if GCC_VERSION >= 4007
299 #pragma GCC diagnostic push
300 #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
301 #endif
303 /* Free the heap space allocated for vector V. */
305 template<typename T>
306 void
307 va_heap::release (vec<T, va_heap, vl_embed> *&v)
309 size_t elt_size = sizeof (T);
310 if (v == NULL)
311 return;
313 if (GATHER_STATISTICS)
314 v->m_vecpfx.release_overhead (v, elt_size * v->allocated (),
315 v->allocated (), true);
316 ::free (v);
317 v = NULL;
320 #if GCC_VERSION >= 4007
321 #pragma GCC diagnostic pop
322 #endif
324 /* Allocator type for GC vectors. Notice that we need the structure
325 declaration even if GC is not enabled. */
327 struct va_gc
329 /* Use vl_embed as the default layout for GC vectors. Due to GTY
330 limitations, GC vectors must always be pointers, so it is more
331 efficient to use a pointer to the vl_embed layout, rather than
332 using a pointer to a pointer as would be the case with vl_ptr. */
333 typedef vl_embed default_layout;
335 template<typename T, typename A>
336 static void reserve (vec<T, A, vl_embed> *&, unsigned, bool
337 CXX_MEM_STAT_INFO);
339 template<typename T, typename A>
340 static void release (vec<T, A, vl_embed> *&v);
344 /* Free GC memory used by V and reset V to NULL. */
346 template<typename T, typename A>
347 inline void
348 va_gc::release (vec<T, A, vl_embed> *&v)
350 if (v)
351 ::ggc_free (v);
352 v = NULL;
356 /* Allocator for GC memory. Ensure there are at least RESERVE free
357 slots in V. If EXACT is true, grow exactly, else grow
358 exponentially. As a special case, if the vector had not been
359 allocated and RESERVE is 0, no vector will be created. */
361 template<typename T, typename A>
362 void
363 va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact
364 MEM_STAT_DECL)
366 unsigned alloc
367 = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
368 if (!alloc)
370 ::ggc_free (v);
371 v = NULL;
372 return;
375 /* Calculate the amount of space we want. */
376 size_t size = vec<T, A, vl_embed>::embedded_size (alloc);
378 /* Ask the allocator how much space it will really give us. */
379 size = ::ggc_round_alloc_size (size);
381 /* Adjust the number of slots accordingly. */
382 size_t vec_offset = sizeof (vec_prefix);
383 size_t elt_size = sizeof (T);
384 alloc = (size - vec_offset) / elt_size;
386 /* And finally, recalculate the amount of space we ask for. */
387 size = vec_offset + alloc * elt_size;
389 unsigned nelem = v ? v->length () : 0;
390 v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc (v, size
391 PASS_MEM_STAT));
392 v->embedded_init (alloc, nelem);
396 /* Allocator type for GC vectors. This is for vectors of types
397 atomics w.r.t. collection, so allocation and deallocation is
398 completely inherited from va_gc. */
399 struct va_gc_atomic : va_gc
404 /* Generic vector template. Default values for A and L indicate the
405 most commonly used strategies.
407 FIXME - Ideally, they would all be vl_ptr to encourage using regular
408 instances for vectors, but the existing GTY machinery is limited
409 in that it can only deal with GC objects that are pointers
410 themselves.
412 This means that vector operations that need to deal with
413 potentially NULL pointers, must be provided as free
414 functions (see the vec_safe_* functions above). */
415 template<typename T,
416 typename A = va_heap,
417 typename L = typename A::default_layout>
418 struct GTY((user)) vec
422 /* Generic vec<> debug helpers.
424 These need to be instantiated for each vec<TYPE> used throughout
425 the compiler like this:
427 DEFINE_DEBUG_VEC (TYPE)
429 The reason we have a debug_helper() is because GDB can't
430 disambiguate a plain call to debug(some_vec), and it must be called
431 like debug<TYPE>(some_vec). */
433 template<typename T>
434 void
435 debug_helper (vec<T> &ref)
437 unsigned i;
438 for (i = 0; i < ref.length (); ++i)
440 fprintf (stderr, "[%d] = ", i);
441 debug_slim (ref[i]);
442 fputc ('\n', stderr);
446 /* We need a separate va_gc variant here because default template
447 argument for functions cannot be used in c++-98. Once this
448 restriction is removed, those variant should be folded with the
449 above debug_helper. */
451 template<typename T>
452 void
453 debug_helper (vec<T, va_gc> &ref)
455 unsigned i;
456 for (i = 0; i < ref.length (); ++i)
458 fprintf (stderr, "[%d] = ", i);
459 debug_slim (ref[i]);
460 fputc ('\n', stderr);
464 /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
465 functions for a type T. */
467 #define DEFINE_DEBUG_VEC(T) \
468 template void debug_helper (vec<T> &); \
469 template void debug_helper (vec<T, va_gc> &); \
470 /* Define the vec<T> debug functions. */ \
471 DEBUG_FUNCTION void \
472 debug (vec<T> &ref) \
474 debug_helper <T> (ref); \
476 DEBUG_FUNCTION void \
477 debug (vec<T> *ptr) \
479 if (ptr) \
480 debug (*ptr); \
481 else \
482 fprintf (stderr, "<nil>\n"); \
484 /* Define the vec<T, va_gc> debug functions. */ \
485 DEBUG_FUNCTION void \
486 debug (vec<T, va_gc> &ref) \
488 debug_helper <T> (ref); \
490 DEBUG_FUNCTION void \
491 debug (vec<T, va_gc> *ptr) \
493 if (ptr) \
494 debug (*ptr); \
495 else \
496 fprintf (stderr, "<nil>\n"); \
499 /* Default-construct N elements in DST. */
501 template <typename T>
502 inline void
503 vec_default_construct (T *dst, unsigned n)
505 #ifdef BROKEN_VALUE_INITIALIZATION
506 /* Versions of GCC before 4.4 sometimes leave certain objects
507 uninitialized when value initialized, though if the type has
508 user defined default ctor, that ctor is invoked. As a workaround
509 perform clearing first and then the value initialization, which
510 fixes the case when value initialization doesn't initialize due to
511 the bugs and should initialize to all zeros, but still allows
512 vectors for types with user defined default ctor that initializes
513 some or all elements to non-zero. If T has no user defined
514 default ctor and some non-static data members have user defined
515 default ctors that initialize to non-zero the workaround will
516 still not work properly; in that case we just need to provide
517 user defined default ctor. */
518 memset (dst, '\0', sizeof (T) * n);
519 #endif
520 for ( ; n; ++dst, --n)
521 ::new (static_cast<void*>(dst)) T ();
524 /* Copy-construct N elements in DST from *SRC. */
526 template <typename T>
527 inline void
528 vec_copy_construct (T *dst, const T *src, unsigned n)
530 for ( ; n; ++dst, ++src, --n)
531 ::new (static_cast<void*>(dst)) T (*src);
534 /* Type to provide NULL values for vec<T, A, L>. This is used to
535 provide nil initializers for vec instances. Since vec must be
536 a POD, we cannot have proper ctor/dtor for it. To initialize
537 a vec instance, you can assign it the value vNULL. This isn't
538 needed for file-scope and function-local static vectors, which
539 are zero-initialized by default. */
540 struct vnull
542 template <typename T, typename A, typename L>
543 CONSTEXPR operator vec<T, A, L> () { return vec<T, A, L>(); }
545 extern vnull vNULL;
548 /* Embeddable vector. These vectors are suitable to be embedded
549 in other data structures so that they can be pre-allocated in a
550 contiguous memory block.
552 Embeddable vectors are implemented using the trailing array idiom,
553 thus they are not resizeable without changing the address of the
554 vector object itself. This means you cannot have variables or
555 fields of embeddable vector type -- always use a pointer to a
556 vector. The one exception is the final field of a structure, which
557 could be a vector type.
559 You will have to use the embedded_size & embedded_init calls to
560 create such objects, and they will not be resizeable (so the 'safe'
561 allocation variants are not available).
563 Properties:
565 - The whole vector and control data are allocated in a single
566 contiguous block. It uses the trailing-vector idiom, so
567 allocation must reserve enough space for all the elements
568 in the vector plus its control data.
569 - The vector cannot be re-allocated.
570 - The vector cannot grow nor shrink.
571 - No indirections needed for access/manipulation.
572 - It requires 2 words of storage (prior to vector allocation). */
574 template<typename T, typename A>
575 struct GTY((user)) vec<T, A, vl_embed>
577 public:
578 unsigned allocated (void) const { return m_vecpfx.m_alloc; }
579 unsigned length (void) const { return m_vecpfx.m_num; }
580 bool is_empty (void) const { return m_vecpfx.m_num == 0; }
581 T *address (void) { return m_vecdata; }
582 const T *address (void) const { return m_vecdata; }
583 T *begin () { return address (); }
584 const T *begin () const { return address (); }
585 T *end () { return address () + length (); }
586 const T *end () const { return address () + length (); }
587 const T &operator[] (unsigned) const;
588 T &operator[] (unsigned);
589 T &last (void);
590 bool space (unsigned) const;
591 bool iterate (unsigned, T *) const;
592 bool iterate (unsigned, T **) const;
593 vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
594 void splice (const vec &);
595 void splice (const vec *src);
596 T *quick_push (const T &);
597 T &pop (void);
598 void truncate (unsigned);
599 void quick_insert (unsigned, const T &);
600 void ordered_remove (unsigned);
601 void unordered_remove (unsigned);
602 void block_remove (unsigned, unsigned);
603 void qsort (int (*) (const void *, const void *));
604 void sort (int (*) (const void *, const void *, void *), void *);
605 T *bsearch (const void *key, int (*compar)(const void *, const void *));
606 T *bsearch (const void *key,
607 int (*compar)(const void *, const void *, void *), void *);
608 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
609 bool contains (const T &search) const;
610 static size_t embedded_size (unsigned);
611 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
612 void quick_grow (unsigned len);
613 void quick_grow_cleared (unsigned len);
615 /* vec class can access our internal data and functions. */
616 template <typename, typename, typename> friend struct vec;
618 /* The allocator types also need access to our internals. */
619 friend struct va_gc;
620 friend struct va_gc_atomic;
621 friend struct va_heap;
623 /* FIXME - These fields should be private, but we need to cater to
624 compilers that have stricter notions of PODness for types. */
625 vec_prefix m_vecpfx;
626 T m_vecdata[1];
630 /* Convenience wrapper functions to use when dealing with pointers to
631 embedded vectors. Some functionality for these vectors must be
632 provided via free functions for these reasons:
634 1- The pointer may be NULL (e.g., before initial allocation).
636 2- When the vector needs to grow, it must be reallocated, so
637 the pointer will change its value.
639 Because of limitations with the current GC machinery, all vectors
640 in GC memory *must* be pointers. */
643 /* If V contains no room for NELEMS elements, return false. Otherwise,
644 return true. */
645 template<typename T, typename A>
646 inline bool
647 vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
649 return v ? v->space (nelems) : nelems == 0;
653 /* If V is NULL, return 0. Otherwise, return V->length(). */
654 template<typename T, typename A>
655 inline unsigned
656 vec_safe_length (const vec<T, A, vl_embed> *v)
658 return v ? v->length () : 0;
662 /* If V is NULL, return NULL. Otherwise, return V->address(). */
663 template<typename T, typename A>
664 inline T *
665 vec_safe_address (vec<T, A, vl_embed> *v)
667 return v ? v->address () : NULL;
671 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
672 template<typename T, typename A>
673 inline bool
674 vec_safe_is_empty (vec<T, A, vl_embed> *v)
676 return v ? v->is_empty () : true;
679 /* If V does not have space for NELEMS elements, call
680 V->reserve(NELEMS, EXACT). */
681 template<typename T, typename A>
682 inline bool
683 vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
684 CXX_MEM_STAT_INFO)
686 bool extend = nelems ? !vec_safe_space (v, nelems) : false;
687 if (extend)
688 A::reserve (v, nelems, exact PASS_MEM_STAT);
689 return extend;
692 template<typename T, typename A>
693 inline bool
694 vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
695 CXX_MEM_STAT_INFO)
697 return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
701 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
702 is 0, V is initialized to NULL. */
704 template<typename T, typename A>
705 inline void
706 vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
708 v = NULL;
709 vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
713 /* Free the GC memory allocated by vector V and set it to NULL. */
715 template<typename T, typename A>
716 inline void
717 vec_free (vec<T, A, vl_embed> *&v)
719 A::release (v);
723 /* Grow V to length LEN. Allocate it, if necessary. */
724 template<typename T, typename A>
725 inline void
726 vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len,
727 bool exact = false CXX_MEM_STAT_INFO)
729 unsigned oldlen = vec_safe_length (v);
730 gcc_checking_assert (len >= oldlen);
731 vec_safe_reserve (v, len - oldlen, exact PASS_MEM_STAT);
732 v->quick_grow (len);
736 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
737 template<typename T, typename A>
738 inline void
739 vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len,
740 bool exact = false CXX_MEM_STAT_INFO)
742 unsigned oldlen = vec_safe_length (v);
743 vec_safe_grow (v, len, exact PASS_MEM_STAT);
744 vec_default_construct (v->address () + oldlen, len - oldlen);
748 /* Assume V is not NULL. */
750 template<typename T>
751 inline void
752 vec_safe_grow_cleared (vec<T, va_heap, vl_ptr> *&v,
753 unsigned len, bool exact = false CXX_MEM_STAT_INFO)
755 v->safe_grow_cleared (len, exact PASS_MEM_STAT);
758 /* If V does not have space for NELEMS elements, call
759 V->reserve(NELEMS, EXACT). */
761 template<typename T>
762 inline bool
763 vec_safe_reserve (vec<T, va_heap, vl_ptr> *&v, unsigned nelems, bool exact = false
764 CXX_MEM_STAT_INFO)
766 return v->reserve (nelems, exact);
770 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
771 template<typename T, typename A>
772 inline bool
773 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
775 if (v)
776 return v->iterate (ix, ptr);
777 else
779 *ptr = 0;
780 return false;
784 template<typename T, typename A>
785 inline bool
786 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
788 if (v)
789 return v->iterate (ix, ptr);
790 else
792 *ptr = 0;
793 return false;
798 /* If V has no room for one more element, reallocate it. Then call
799 V->quick_push(OBJ). */
800 template<typename T, typename A>
801 inline T *
802 vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
804 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
805 return v->quick_push (obj);
809 /* if V has no room for one more element, reallocate it. Then call
810 V->quick_insert(IX, OBJ). */
811 template<typename T, typename A>
812 inline void
813 vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
814 CXX_MEM_STAT_INFO)
816 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
817 v->quick_insert (ix, obj);
821 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
822 template<typename T, typename A>
823 inline void
824 vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
826 if (v)
827 v->truncate (size);
831 /* If SRC is not NULL, return a pointer to a copy of it. */
832 template<typename T, typename A>
833 inline vec<T, A, vl_embed> *
834 vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
836 return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
839 /* Copy the elements from SRC to the end of DST as if by memcpy.
840 Reallocate DST, if necessary. */
841 template<typename T, typename A>
842 inline void
843 vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
844 CXX_MEM_STAT_INFO)
846 unsigned src_len = vec_safe_length (src);
847 if (src_len)
849 vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
850 PASS_MEM_STAT);
851 dst->splice (*src);
855 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
856 size of the vector and so should be used with care. */
858 template<typename T, typename A>
859 inline bool
860 vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
862 return v ? v->contains (search) : false;
865 /* Index into vector. Return the IX'th element. IX must be in the
866 domain of the vector. */
868 template<typename T, typename A>
869 inline const T &
870 vec<T, A, vl_embed>::operator[] (unsigned ix) const
872 gcc_checking_assert (ix < m_vecpfx.m_num);
873 return m_vecdata[ix];
876 template<typename T, typename A>
877 inline T &
878 vec<T, A, vl_embed>::operator[] (unsigned ix)
880 gcc_checking_assert (ix < m_vecpfx.m_num);
881 return m_vecdata[ix];
885 /* Get the final element of the vector, which must not be empty. */
887 template<typename T, typename A>
888 inline T &
889 vec<T, A, vl_embed>::last (void)
891 gcc_checking_assert (m_vecpfx.m_num > 0);
892 return (*this)[m_vecpfx.m_num - 1];
896 /* If this vector has space for NELEMS additional entries, return
897 true. You usually only need to use this if you are doing your
898 own vector reallocation, for instance on an embedded vector. This
899 returns true in exactly the same circumstances that vec::reserve
900 will. */
902 template<typename T, typename A>
903 inline bool
904 vec<T, A, vl_embed>::space (unsigned nelems) const
906 return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
910 /* Return iteration condition and update PTR to point to the IX'th
911 element of this vector. Use this to iterate over the elements of a
912 vector as follows,
914 for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
915 continue; */
917 template<typename T, typename A>
918 inline bool
919 vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
921 if (ix < m_vecpfx.m_num)
923 *ptr = m_vecdata[ix];
924 return true;
926 else
928 *ptr = 0;
929 return false;
934 /* Return iteration condition and update *PTR to point to the
935 IX'th element of this vector. Use this to iterate over the
936 elements of a vector as follows,
938 for (ix = 0; v->iterate (ix, &ptr); ix++)
939 continue;
941 This variant is for vectors of objects. */
943 template<typename T, typename A>
944 inline bool
945 vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
947 if (ix < m_vecpfx.m_num)
949 *ptr = CONST_CAST (T *, &m_vecdata[ix]);
950 return true;
952 else
954 *ptr = 0;
955 return false;
960 /* Return a pointer to a copy of this vector. */
962 template<typename T, typename A>
963 inline vec<T, A, vl_embed> *
964 vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
966 vec<T, A, vl_embed> *new_vec = NULL;
967 unsigned len = length ();
968 if (len)
970 vec_alloc (new_vec, len PASS_MEM_STAT);
971 new_vec->embedded_init (len, len);
972 vec_copy_construct (new_vec->address (), m_vecdata, len);
974 return new_vec;
978 /* Copy the elements from SRC to the end of this vector as if by memcpy.
979 The vector must have sufficient headroom available. */
981 template<typename T, typename A>
982 inline void
983 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
985 unsigned len = src.length ();
986 if (len)
988 gcc_checking_assert (space (len));
989 vec_copy_construct (end (), src.address (), len);
990 m_vecpfx.m_num += len;
994 template<typename T, typename A>
995 inline void
996 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
998 if (src)
999 splice (*src);
1003 /* Push OBJ (a new element) onto the end of the vector. There must be
1004 sufficient space in the vector. Return a pointer to the slot
1005 where OBJ was inserted. */
1007 template<typename T, typename A>
1008 inline T *
1009 vec<T, A, vl_embed>::quick_push (const T &obj)
1011 gcc_checking_assert (space (1));
1012 T *slot = &m_vecdata[m_vecpfx.m_num++];
1013 *slot = obj;
1014 return slot;
1018 /* Pop and return the last element off the end of the vector. */
1020 template<typename T, typename A>
1021 inline T &
1022 vec<T, A, vl_embed>::pop (void)
1024 gcc_checking_assert (length () > 0);
1025 return m_vecdata[--m_vecpfx.m_num];
1029 /* Set the length of the vector to SIZE. The new length must be less
1030 than or equal to the current length. This is an O(1) operation. */
1032 template<typename T, typename A>
1033 inline void
1034 vec<T, A, vl_embed>::truncate (unsigned size)
1036 gcc_checking_assert (length () >= size);
1037 m_vecpfx.m_num = size;
1041 /* Insert an element, OBJ, at the IXth position of this vector. There
1042 must be sufficient space. */
1044 template<typename T, typename A>
1045 inline void
1046 vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
1048 gcc_checking_assert (length () < allocated ());
1049 gcc_checking_assert (ix <= length ());
1050 T *slot = &m_vecdata[ix];
1051 memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
1052 *slot = obj;
1056 /* Remove an element from the IXth position of this vector. Ordering of
1057 remaining elements is preserved. This is an O(N) operation due to
1058 memmove. */
1060 template<typename T, typename A>
1061 inline void
1062 vec<T, A, vl_embed>::ordered_remove (unsigned ix)
1064 gcc_checking_assert (ix < length ());
1065 T *slot = &m_vecdata[ix];
1066 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
1070 /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
1071 remaining elements is preserved. This is an O(N) operation. */
1073 #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
1074 elem_ptr, start, end, cond) \
1076 gcc_assert ((end) <= (vec).length ()); \
1077 for (read_index = write_index = (start); read_index < (end); \
1078 ++read_index) \
1080 elem_ptr = &(vec)[read_index]; \
1081 bool remove_p = (cond); \
1082 if (remove_p) \
1083 continue; \
1085 if (read_index != write_index) \
1086 (vec)[write_index] = (vec)[read_index]; \
1088 write_index++; \
1091 if (read_index - write_index > 0) \
1092 (vec).block_remove (write_index, read_index - write_index); \
1096 /* Remove elements from VEC for which COND holds. Ordering of remaining
1097 elements is preserved. This is an O(N) operation. */
1099 #define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \
1100 cond) \
1101 VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
1102 elem_ptr, 0, (vec).length (), (cond))
1104 /* Remove an element from the IXth position of this vector. Ordering of
1105 remaining elements is destroyed. This is an O(1) operation. */
1107 template<typename T, typename A>
1108 inline void
1109 vec<T, A, vl_embed>::unordered_remove (unsigned ix)
1111 gcc_checking_assert (ix < length ());
1112 m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
1116 /* Remove LEN elements starting at the IXth. Ordering is retained.
1117 This is an O(N) operation due to memmove. */
1119 template<typename T, typename A>
1120 inline void
1121 vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
1123 gcc_checking_assert (ix + len <= length ());
1124 T *slot = &m_vecdata[ix];
1125 m_vecpfx.m_num -= len;
1126 memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
1130 /* Sort the contents of this vector with qsort. CMP is the comparison
1131 function to pass to qsort. */
1133 template<typename T, typename A>
1134 inline void
1135 vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *))
1137 if (length () > 1)
1138 gcc_qsort (address (), length (), sizeof (T), cmp);
1141 /* Sort the contents of this vector with qsort. CMP is the comparison
1142 function to pass to qsort. */
1144 template<typename T, typename A>
1145 inline void
1146 vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *),
1147 void *data)
1149 if (length () > 1)
1150 gcc_sort_r (address (), length (), sizeof (T), cmp, data);
1154 /* Search the contents of the sorted vector with a binary search.
1155 CMP is the comparison function to pass to bsearch. */
1157 template<typename T, typename A>
1158 inline T *
1159 vec<T, A, vl_embed>::bsearch (const void *key,
1160 int (*compar) (const void *, const void *))
1162 const void *base = this->address ();
1163 size_t nmemb = this->length ();
1164 size_t size = sizeof (T);
1165 /* The following is a copy of glibc stdlib-bsearch.h. */
1166 size_t l, u, idx;
1167 const void *p;
1168 int comparison;
1170 l = 0;
1171 u = nmemb;
1172 while (l < u)
1174 idx = (l + u) / 2;
1175 p = (const void *) (((const char *) base) + (idx * size));
1176 comparison = (*compar) (key, p);
1177 if (comparison < 0)
1178 u = idx;
1179 else if (comparison > 0)
1180 l = idx + 1;
1181 else
1182 return (T *)const_cast<void *>(p);
1185 return NULL;
1188 /* Search the contents of the sorted vector with a binary search.
1189 CMP is the comparison function to pass to bsearch. */
1191 template<typename T, typename A>
1192 inline T *
1193 vec<T, A, vl_embed>::bsearch (const void *key,
1194 int (*compar) (const void *, const void *,
1195 void *), void *data)
1197 const void *base = this->address ();
1198 size_t nmemb = this->length ();
1199 size_t size = sizeof (T);
1200 /* The following is a copy of glibc stdlib-bsearch.h. */
1201 size_t l, u, idx;
1202 const void *p;
1203 int comparison;
1205 l = 0;
1206 u = nmemb;
1207 while (l < u)
1209 idx = (l + u) / 2;
1210 p = (const void *) (((const char *) base) + (idx * size));
1211 comparison = (*compar) (key, p, data);
1212 if (comparison < 0)
1213 u = idx;
1214 else if (comparison > 0)
1215 l = idx + 1;
1216 else
1217 return (T *)const_cast<void *>(p);
1220 return NULL;
1223 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
1224 size of the vector and so should be used with care. */
1226 template<typename T, typename A>
1227 inline bool
1228 vec<T, A, vl_embed>::contains (const T &search) const
1230 unsigned int len = length ();
1231 for (unsigned int i = 0; i < len; i++)
1232 if ((*this)[i] == search)
1233 return true;
1235 return false;
1238 /* Find and return the first position in which OBJ could be inserted
1239 without changing the ordering of this vector. LESSTHAN is a
1240 function that returns true if the first argument is strictly less
1241 than the second. */
1243 template<typename T, typename A>
1244 unsigned
1245 vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
1246 const
1248 unsigned int len = length ();
1249 unsigned int half, middle;
1250 unsigned int first = 0;
1251 while (len > 0)
1253 half = len / 2;
1254 middle = first;
1255 middle += half;
1256 T middle_elem = (*this)[middle];
1257 if (lessthan (middle_elem, obj))
1259 first = middle;
1260 ++first;
1261 len = len - half - 1;
1263 else
1264 len = half;
1266 return first;
1270 /* Return the number of bytes needed to embed an instance of an
1271 embeddable vec inside another data structure.
1273 Use these methods to determine the required size and initialization
1274 of a vector V of type T embedded within another structure (as the
1275 final member):
1277 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1278 void v->embedded_init (unsigned alloc, unsigned num);
1280 These allow the caller to perform the memory allocation. */
1282 template<typename T, typename A>
1283 inline size_t
1284 vec<T, A, vl_embed>::embedded_size (unsigned alloc)
1286 struct alignas (T) U { char data[sizeof (T)]; };
1287 typedef vec<U, A, vl_embed> vec_embedded;
1288 typedef typename std::conditional<std::is_standard_layout<T>::value,
1289 vec, vec_embedded>::type vec_stdlayout;
1290 static_assert (sizeof (vec_stdlayout) == sizeof (vec), "");
1291 static_assert (alignof (vec_stdlayout) == alignof (vec), "");
1292 return offsetof (vec_stdlayout, m_vecdata) + alloc * sizeof (T);
1296 /* Initialize the vector to contain room for ALLOC elements and
1297 NUM active elements. */
1299 template<typename T, typename A>
1300 inline void
1301 vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
1303 m_vecpfx.m_alloc = alloc;
1304 m_vecpfx.m_using_auto_storage = aut;
1305 m_vecpfx.m_num = num;
1309 /* Grow the vector to a specific length. LEN must be as long or longer than
1310 the current length. The new elements are uninitialized. */
1312 template<typename T, typename A>
1313 inline void
1314 vec<T, A, vl_embed>::quick_grow (unsigned len)
1316 gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
1317 m_vecpfx.m_num = len;
1321 /* Grow the vector to a specific length. LEN must be as long or longer than
1322 the current length. The new elements are initialized to zero. */
1324 template<typename T, typename A>
1325 inline void
1326 vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
1328 unsigned oldlen = length ();
1329 size_t growby = len - oldlen;
1330 quick_grow (len);
1331 if (growby != 0)
1332 vec_default_construct (address () + oldlen, growby);
1335 /* Garbage collection support for vec<T, A, vl_embed>. */
1337 template<typename T>
1338 void
1339 gt_ggc_mx (vec<T, va_gc> *v)
1341 extern void gt_ggc_mx (T &);
1342 for (unsigned i = 0; i < v->length (); i++)
1343 gt_ggc_mx ((*v)[i]);
1346 template<typename T>
1347 void
1348 gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
1350 /* Nothing to do. Vectors of atomic types wrt GC do not need to
1351 be traversed. */
1355 /* PCH support for vec<T, A, vl_embed>. */
1357 template<typename T, typename A>
1358 void
1359 gt_pch_nx (vec<T, A, vl_embed> *v)
1361 extern void gt_pch_nx (T &);
1362 for (unsigned i = 0; i < v->length (); i++)
1363 gt_pch_nx ((*v)[i]);
1366 template<typename T, typename A>
1367 void
1368 gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1370 for (unsigned i = 0; i < v->length (); i++)
1371 op (&((*v)[i]), cookie);
1374 template<typename T, typename A>
1375 void
1376 gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1378 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
1379 for (unsigned i = 0; i < v->length (); i++)
1380 gt_pch_nx (&((*v)[i]), op, cookie);
1384 /* Space efficient vector. These vectors can grow dynamically and are
1385 allocated together with their control data. They are suited to be
1386 included in data structures. Prior to initial allocation, they
1387 only take a single word of storage.
1389 These vectors are implemented as a pointer to an embeddable vector.
1390 The semantics allow for this pointer to be NULL to represent empty
1391 vectors. This way, empty vectors occupy minimal space in the
1392 structure containing them.
1394 Properties:
1396 - The whole vector and control data are allocated in a single
1397 contiguous block.
1398 - The whole vector may be re-allocated.
1399 - Vector data may grow and shrink.
1400 - Access and manipulation requires a pointer test and
1401 indirection.
1402 - It requires 1 word of storage (prior to vector allocation).
1405 Limitations:
1407 These vectors must be PODs because they are stored in unions.
1408 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1409 As long as we use C++03, we cannot have constructors nor
1410 destructors in classes that are stored in unions. */
1412 template<typename T>
1413 struct vec<T, va_heap, vl_ptr>
1415 public:
1416 /* Memory allocation and deallocation for the embedded vector.
1417 Needed because we cannot have proper ctors/dtors defined. */
1418 void create (unsigned nelems CXX_MEM_STAT_INFO);
1419 void release (void);
1421 /* Vector operations. */
1422 bool exists (void) const
1423 { return m_vec != NULL; }
1425 bool is_empty (void) const
1426 { return m_vec ? m_vec->is_empty () : true; }
1428 unsigned length (void) const
1429 { return m_vec ? m_vec->length () : 0; }
1431 T *address (void)
1432 { return m_vec ? m_vec->m_vecdata : NULL; }
1434 const T *address (void) const
1435 { return m_vec ? m_vec->m_vecdata : NULL; }
1437 T *begin () { return address (); }
1438 const T *begin () const { return address (); }
1439 T *end () { return begin () + length (); }
1440 const T *end () const { return begin () + length (); }
1441 const T &operator[] (unsigned ix) const
1442 { return (*m_vec)[ix]; }
1444 bool operator!=(const vec &other) const
1445 { return !(*this == other); }
1447 bool operator==(const vec &other) const
1448 { return address () == other.address (); }
1450 T &operator[] (unsigned ix)
1451 { return (*m_vec)[ix]; }
1453 T &last (void)
1454 { return m_vec->last (); }
1456 bool space (int nelems) const
1457 { return m_vec ? m_vec->space (nelems) : nelems == 0; }
1459 bool iterate (unsigned ix, T *p) const;
1460 bool iterate (unsigned ix, T **p) const;
1461 vec copy (ALONE_CXX_MEM_STAT_INFO) const;
1462 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
1463 bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
1464 void splice (const vec &);
1465 void safe_splice (const vec & CXX_MEM_STAT_INFO);
1466 T *quick_push (const T &);
1467 T *safe_push (const T &CXX_MEM_STAT_INFO);
1468 T &pop (void);
1469 void truncate (unsigned);
1470 void safe_grow (unsigned, bool = false CXX_MEM_STAT_INFO);
1471 void safe_grow_cleared (unsigned, bool = false CXX_MEM_STAT_INFO);
1472 void quick_grow (unsigned);
1473 void quick_grow_cleared (unsigned);
1474 void quick_insert (unsigned, const T &);
1475 void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
1476 void ordered_remove (unsigned);
1477 void unordered_remove (unsigned);
1478 void block_remove (unsigned, unsigned);
1479 void qsort (int (*) (const void *, const void *));
1480 void sort (int (*) (const void *, const void *, void *), void *);
1481 T *bsearch (const void *key, int (*compar)(const void *, const void *));
1482 T *bsearch (const void *key,
1483 int (*compar)(const void *, const void *, void *), void *);
1484 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
1485 bool contains (const T &search) const;
1486 void reverse (void);
1488 bool using_auto_storage () const;
1490 /* FIXME - This field should be private, but we need to cater to
1491 compilers that have stricter notions of PODness for types. */
1492 vec<T, va_heap, vl_embed> *m_vec;
1496 /* auto_vec is a subclass of vec that automatically manages creating and
1497 releasing the internal vector. If N is non zero then it has N elements of
1498 internal storage. The default is no internal storage, and you probably only
1499 want to ask for internal storage for vectors on the stack because if the
1500 size of the vector is larger than the internal storage that space is wasted.
1502 template<typename T, size_t N = 0>
1503 class auto_vec : public vec<T, va_heap>
1505 public:
1506 auto_vec ()
1508 m_auto.embedded_init (MAX (N, 2), 0, 1);
1509 this->m_vec = &m_auto;
1512 auto_vec (size_t s)
1514 if (s > N)
1516 this->create (s);
1517 return;
1520 m_auto.embedded_init (MAX (N, 2), 0, 1);
1521 this->m_vec = &m_auto;
1524 ~auto_vec ()
1526 this->release ();
1529 private:
1530 vec<T, va_heap, vl_embed> m_auto;
1531 T m_data[MAX (N - 1, 1)];
1534 /* auto_vec is a sub class of vec whose storage is released when it is
1535 destroyed. */
1536 template<typename T>
1537 class auto_vec<T, 0> : public vec<T, va_heap>
1539 public:
1540 auto_vec () { this->m_vec = NULL; }
1541 auto_vec (size_t n) { this->create (n); }
1542 ~auto_vec () { this->release (); }
1544 auto_vec (vec<T, va_heap>&& r)
1546 gcc_assert (!r.using_auto_storage ());
1547 this->m_vec = r.m_vec;
1548 r.m_vec = NULL;
1550 auto_vec& operator= (vec<T, va_heap>&& r)
1552 gcc_assert (!r.using_auto_storage ());
1553 this->release ();
1554 this->m_vec = r.m_vec;
1555 r.m_vec = NULL;
1556 return *this;
1561 /* Allocate heap memory for pointer V and create the internal vector
1562 with space for NELEMS elements. If NELEMS is 0, the internal
1563 vector is initialized to empty. */
1565 template<typename T>
1566 inline void
1567 vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
1569 v = new vec<T>;
1570 v->create (nelems PASS_MEM_STAT);
1574 /* A subclass of auto_vec <char *> that frees all of its elements on
1575 deletion. */
1577 class auto_string_vec : public auto_vec <char *>
1579 public:
1580 ~auto_string_vec ();
1583 /* A subclass of auto_vec <T *> that deletes all of its elements on
1584 destruction.
1586 This is a crude way for a vec to "own" the objects it points to
1587 and clean up automatically.
1589 For example, no attempt is made to delete elements when an item
1590 within the vec is overwritten.
1592 We can't rely on gnu::unique_ptr within a container,
1593 since we can't rely on move semantics in C++98. */
1595 template <typename T>
1596 class auto_delete_vec : public auto_vec <T *>
1598 public:
1599 auto_delete_vec () {}
1600 auto_delete_vec (size_t s) : auto_vec <T *> (s) {}
1602 ~auto_delete_vec ();
1604 private:
1605 DISABLE_COPY_AND_ASSIGN(auto_delete_vec<T>);
1608 /* Conditionally allocate heap memory for VEC and its internal vector. */
1610 template<typename T>
1611 inline void
1612 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
1614 if (!vec)
1615 vec_alloc (vec, nelems PASS_MEM_STAT);
1619 /* Free the heap memory allocated by vector V and set it to NULL. */
1621 template<typename T>
1622 inline void
1623 vec_free (vec<T> *&v)
1625 if (v == NULL)
1626 return;
1628 v->release ();
1629 delete v;
1630 v = NULL;
1634 /* Return iteration condition and update PTR to point to the IX'th
1635 element of this vector. Use this to iterate over the elements of a
1636 vector as follows,
1638 for (ix = 0; v.iterate (ix, &ptr); ix++)
1639 continue; */
1641 template<typename T>
1642 inline bool
1643 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
1645 if (m_vec)
1646 return m_vec->iterate (ix, ptr);
1647 else
1649 *ptr = 0;
1650 return false;
1655 /* Return iteration condition and update *PTR to point to the
1656 IX'th element of this vector. Use this to iterate over the
1657 elements of a vector as follows,
1659 for (ix = 0; v->iterate (ix, &ptr); ix++)
1660 continue;
1662 This variant is for vectors of objects. */
1664 template<typename T>
1665 inline bool
1666 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
1668 if (m_vec)
1669 return m_vec->iterate (ix, ptr);
1670 else
1672 *ptr = 0;
1673 return false;
1678 /* Convenience macro for forward iteration. */
1679 #define FOR_EACH_VEC_ELT(V, I, P) \
1680 for (I = 0; (V).iterate ((I), &(P)); ++(I))
1682 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
1683 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1685 /* Likewise, but start from FROM rather than 0. */
1686 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
1687 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1689 /* Convenience macro for reverse iteration. */
1690 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
1691 for (I = (V).length () - 1; \
1692 (V).iterate ((I), &(P)); \
1693 (I)--)
1695 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1696 for (I = vec_safe_length (V) - 1; \
1697 vec_safe_iterate ((V), (I), &(P)); \
1698 (I)--)
1700 /* auto_string_vec's dtor, freeing all contained strings, automatically
1701 chaining up to ~auto_vec <char *>, which frees the internal buffer. */
1703 inline
1704 auto_string_vec::~auto_string_vec ()
1706 int i;
1707 char *str;
1708 FOR_EACH_VEC_ELT (*this, i, str)
1709 free (str);
1712 /* auto_delete_vec's dtor, deleting all contained items, automatically
1713 chaining up to ~auto_vec <T*>, which frees the internal buffer. */
1715 template <typename T>
1716 inline
1717 auto_delete_vec<T>::~auto_delete_vec ()
1719 int i;
1720 T *item;
1721 FOR_EACH_VEC_ELT (*this, i, item)
1722 delete item;
1726 /* Return a copy of this vector. */
1728 template<typename T>
1729 inline vec<T, va_heap, vl_ptr>
1730 vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
1732 vec<T, va_heap, vl_ptr> new_vec = vNULL;
1733 if (length ())
1734 new_vec.m_vec = m_vec->copy (ALONE_PASS_MEM_STAT);
1735 return new_vec;
1739 /* Ensure that the vector has at least RESERVE slots available (if
1740 EXACT is false), or exactly RESERVE slots available (if EXACT is
1741 true).
1743 This may create additional headroom if EXACT is false.
1745 Note that this can cause the embedded vector to be reallocated.
1746 Returns true iff reallocation actually occurred. */
1748 template<typename T>
1749 inline bool
1750 vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
1752 if (space (nelems))
1753 return false;
1755 /* For now play a game with va_heap::reserve to hide our auto storage if any,
1756 this is necessary because it doesn't have enough information to know the
1757 embedded vector is in auto storage, and so should not be freed. */
1758 vec<T, va_heap, vl_embed> *oldvec = m_vec;
1759 unsigned int oldsize = 0;
1760 bool handle_auto_vec = m_vec && using_auto_storage ();
1761 if (handle_auto_vec)
1763 m_vec = NULL;
1764 oldsize = oldvec->length ();
1765 nelems += oldsize;
1768 va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
1769 if (handle_auto_vec)
1771 vec_copy_construct (m_vec->address (), oldvec->address (), oldsize);
1772 m_vec->m_vecpfx.m_num = oldsize;
1775 return true;
1779 /* Ensure that this vector has exactly NELEMS slots available. This
1780 will not create additional headroom. Note this can cause the
1781 embedded vector to be reallocated. Returns true iff reallocation
1782 actually occurred. */
1784 template<typename T>
1785 inline bool
1786 vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
1788 return reserve (nelems, true PASS_MEM_STAT);
1792 /* Create the internal vector and reserve NELEMS for it. This is
1793 exactly like vec::reserve, but the internal vector is
1794 unconditionally allocated from scratch. The old one, if it
1795 existed, is lost. */
1797 template<typename T>
1798 inline void
1799 vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
1801 m_vec = NULL;
1802 if (nelems > 0)
1803 reserve_exact (nelems PASS_MEM_STAT);
1807 /* Free the memory occupied by the embedded vector. */
1809 template<typename T>
1810 inline void
1811 vec<T, va_heap, vl_ptr>::release (void)
1813 if (!m_vec)
1814 return;
1816 if (using_auto_storage ())
1818 m_vec->m_vecpfx.m_num = 0;
1819 return;
1822 va_heap::release (m_vec);
1825 /* Copy the elements from SRC to the end of this vector as if by memcpy.
1826 SRC and this vector must be allocated with the same memory
1827 allocation mechanism. This vector is assumed to have sufficient
1828 headroom available. */
1830 template<typename T>
1831 inline void
1832 vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
1834 if (src.length ())
1835 m_vec->splice (*(src.m_vec));
1839 /* Copy the elements in SRC to the end of this vector as if by memcpy.
1840 SRC and this vector must be allocated with the same mechanism.
1841 If there is not enough headroom in this vector, it will be reallocated
1842 as needed. */
1844 template<typename T>
1845 inline void
1846 vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
1847 MEM_STAT_DECL)
1849 if (src.length ())
1851 reserve_exact (src.length ());
1852 splice (src);
1857 /* Push OBJ (a new element) onto the end of the vector. There must be
1858 sufficient space in the vector. Return a pointer to the slot
1859 where OBJ was inserted. */
1861 template<typename T>
1862 inline T *
1863 vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
1865 return m_vec->quick_push (obj);
1869 /* Push a new element OBJ onto the end of this vector. Reallocates
1870 the embedded vector, if needed. Return a pointer to the slot where
1871 OBJ was inserted. */
1873 template<typename T>
1874 inline T *
1875 vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
1877 reserve (1, false PASS_MEM_STAT);
1878 return quick_push (obj);
1882 /* Pop and return the last element off the end of the vector. */
1884 template<typename T>
1885 inline T &
1886 vec<T, va_heap, vl_ptr>::pop (void)
1888 return m_vec->pop ();
1892 /* Set the length of the vector to LEN. The new length must be less
1893 than or equal to the current length. This is an O(1) operation. */
1895 template<typename T>
1896 inline void
1897 vec<T, va_heap, vl_ptr>::truncate (unsigned size)
1899 if (m_vec)
1900 m_vec->truncate (size);
1901 else
1902 gcc_checking_assert (size == 0);
1906 /* Grow the vector to a specific length. LEN must be as long or
1907 longer than the current length. The new elements are
1908 uninitialized. Reallocate the internal vector, if needed. */
1910 template<typename T>
1911 inline void
1912 vec<T, va_heap, vl_ptr>::safe_grow (unsigned len, bool exact MEM_STAT_DECL)
1914 unsigned oldlen = length ();
1915 gcc_checking_assert (oldlen <= len);
1916 reserve (len - oldlen, exact PASS_MEM_STAT);
1917 if (m_vec)
1918 m_vec->quick_grow (len);
1919 else
1920 gcc_checking_assert (len == 0);
1924 /* Grow the embedded vector to a specific length. LEN must be as
1925 long or longer than the current length. The new elements are
1926 initialized to zero. Reallocate the internal vector, if needed. */
1928 template<typename T>
1929 inline void
1930 vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len, bool exact
1931 MEM_STAT_DECL)
1933 unsigned oldlen = length ();
1934 size_t growby = len - oldlen;
1935 safe_grow (len, exact PASS_MEM_STAT);
1936 if (growby != 0)
1937 vec_default_construct (address () + oldlen, growby);
1941 /* Same as vec::safe_grow but without reallocation of the internal vector.
1942 If the vector cannot be extended, a runtime assertion will be triggered. */
1944 template<typename T>
1945 inline void
1946 vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
1948 gcc_checking_assert (m_vec);
1949 m_vec->quick_grow (len);
1953 /* Same as vec::quick_grow_cleared but without reallocation of the
1954 internal vector. If the vector cannot be extended, a runtime
1955 assertion will be triggered. */
1957 template<typename T>
1958 inline void
1959 vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
1961 gcc_checking_assert (m_vec);
1962 m_vec->quick_grow_cleared (len);
1966 /* Insert an element, OBJ, at the IXth position of this vector. There
1967 must be sufficient space. */
1969 template<typename T>
1970 inline void
1971 vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
1973 m_vec->quick_insert (ix, obj);
1977 /* Insert an element, OBJ, at the IXth position of the vector.
1978 Reallocate the embedded vector, if necessary. */
1980 template<typename T>
1981 inline void
1982 vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
1984 reserve (1, false PASS_MEM_STAT);
1985 quick_insert (ix, obj);
1989 /* Remove an element from the IXth position of this vector. Ordering of
1990 remaining elements is preserved. This is an O(N) operation due to
1991 a memmove. */
1993 template<typename T>
1994 inline void
1995 vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
1997 m_vec->ordered_remove (ix);
2001 /* Remove an element from the IXth position of this vector. Ordering
2002 of remaining elements is destroyed. This is an O(1) operation. */
2004 template<typename T>
2005 inline void
2006 vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
2008 m_vec->unordered_remove (ix);
2012 /* Remove LEN elements starting at the IXth. Ordering is retained.
2013 This is an O(N) operation due to memmove. */
2015 template<typename T>
2016 inline void
2017 vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
2019 m_vec->block_remove (ix, len);
2023 /* Sort the contents of this vector with qsort. CMP is the comparison
2024 function to pass to qsort. */
2026 template<typename T>
2027 inline void
2028 vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
2030 if (m_vec)
2031 m_vec->qsort (cmp);
2034 /* Sort the contents of this vector with qsort. CMP is the comparison
2035 function to pass to qsort. */
2037 template<typename T>
2038 inline void
2039 vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *,
2040 void *), void *data)
2042 if (m_vec)
2043 m_vec->sort (cmp, data);
2047 /* Search the contents of the sorted vector with a binary search.
2048 CMP is the comparison function to pass to bsearch. */
2050 template<typename T>
2051 inline T *
2052 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
2053 int (*cmp) (const void *, const void *))
2055 if (m_vec)
2056 return m_vec->bsearch (key, cmp);
2057 return NULL;
2060 /* Search the contents of the sorted vector with a binary search.
2061 CMP is the comparison function to pass to bsearch. */
2063 template<typename T>
2064 inline T *
2065 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
2066 int (*cmp) (const void *, const void *,
2067 void *), void *data)
2069 if (m_vec)
2070 return m_vec->bsearch (key, cmp, data);
2071 return NULL;
2075 /* Find and return the first position in which OBJ could be inserted
2076 without changing the ordering of this vector. LESSTHAN is a
2077 function that returns true if the first argument is strictly less
2078 than the second. */
2080 template<typename T>
2081 inline unsigned
2082 vec<T, va_heap, vl_ptr>::lower_bound (T obj,
2083 bool (*lessthan)(const T &, const T &))
2084 const
2086 return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
2089 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
2090 size of the vector and so should be used with care. */
2092 template<typename T>
2093 inline bool
2094 vec<T, va_heap, vl_ptr>::contains (const T &search) const
2096 return m_vec ? m_vec->contains (search) : false;
2099 /* Reverse content of the vector. */
2101 template<typename T>
2102 inline void
2103 vec<T, va_heap, vl_ptr>::reverse (void)
2105 unsigned l = length ();
2106 T *ptr = address ();
2108 for (unsigned i = 0; i < l / 2; i++)
2109 std::swap (ptr[i], ptr[l - i - 1]);
2112 template<typename T>
2113 inline bool
2114 vec<T, va_heap, vl_ptr>::using_auto_storage () const
2116 return m_vec->m_vecpfx.m_using_auto_storage;
2119 /* Release VEC and call release of all element vectors. */
2121 template<typename T>
2122 inline void
2123 release_vec_vec (vec<vec<T> > &vec)
2125 for (unsigned i = 0; i < vec.length (); i++)
2126 vec[i].release ();
2128 vec.release ();
2131 #if (GCC_VERSION >= 3000)
2132 # pragma GCC poison m_vec m_vecpfx m_vecdata
2133 #endif
2135 #endif // GCC_VEC_H