1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2022 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
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
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/>. */
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
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
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
106 The type, allocation and layout are specified when the vector is
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
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
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.
159 - The whole vector and control data are allocated in a single
161 - The whole vector may be re-allocated.
162 - Vector data may grow and shrink.
163 - Access and manipulation requires a pointer test and
165 - It requires 1 word of storage (prior to vector allocation).
167 An example of their use would be,
170 // A space-efficient vector of tree pointers in GC memory.
171 vec<tree, va_gc, vl_ptr> v;
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. */
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.cc. */
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. */
212 friend struct va_gc_atomic
;
213 friend struct va_heap
;
215 unsigned m_alloc
: 31;
216 unsigned m_using_auto_storage
: 1;
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. */
225 vec_prefix::calculate_allocation (vec_prefix
*pfx
, unsigned reserve
,
229 return (pfx
? pfx
->m_num
: 0) + reserve
;
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. */
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. */
256 /* Heap vectors are frequently regular instances, so use the vl_ptr
258 typedef vl_ptr default_layout
;
261 static void reserve (vec
<T
, va_heap
, vl_embed
> *&, unsigned, bool
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. */
276 va_heap::reserve (vec
<T
, va_heap
, vl_embed
> *&v
, unsigned reserve
, bool exact
279 size_t elt_size
= sizeof (T
);
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"
303 /* Free the heap space allocated for vector V. */
307 va_heap::release (vec
<T
, va_heap
, vl_embed
> *&v
)
309 size_t elt_size
= sizeof (T
);
313 if (GATHER_STATISTICS
)
314 v
->m_vecpfx
.release_overhead (v
, elt_size
* v
->allocated (),
315 v
->allocated (), true);
320 #if GCC_VERSION >= 4007
321 #pragma GCC diagnostic pop
324 /* Allocator type for GC vectors. Notice that we need the structure
325 declaration even if GC is not enabled. */
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
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
>
348 va_gc::release (vec
<T
, A
, vl_embed
> *&v
)
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
>
363 va_gc::reserve (vec
<T
, A
, vl_embed
> *&v
, unsigned reserve
, bool exact
367 = vec_prefix::calculate_allocation (v
? &v
->m_vecpfx
: 0, reserve
, exact
);
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
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
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). */
416 typename A
= va_heap
,
417 typename L
= typename
A::default_layout
>
418 struct GTY((user
)) vec
422 /* Allow C++11 range-based 'for' to work directly on vec<T>*. */
423 template<typename T
, typename A
, typename L
>
424 T
* begin (vec
<T
,A
,L
> *v
) { return v
? v
->begin () : nullptr; }
425 template<typename T
, typename A
, typename L
>
426 T
* end (vec
<T
,A
,L
> *v
) { return v
? v
->end () : nullptr; }
427 template<typename T
, typename A
, typename L
>
428 const T
* begin (const vec
<T
,A
,L
> *v
) { return v
? v
->begin () : nullptr; }
429 template<typename T
, typename A
, typename L
>
430 const T
* end (const vec
<T
,A
,L
> *v
) { return v
? v
->end () : nullptr; }
432 /* Generic vec<> debug helpers.
434 These need to be instantiated for each vec<TYPE> used throughout
435 the compiler like this:
437 DEFINE_DEBUG_VEC (TYPE)
439 The reason we have a debug_helper() is because GDB can't
440 disambiguate a plain call to debug(some_vec), and it must be called
441 like debug<TYPE>(some_vec). */
445 debug_helper (vec
<T
> &ref
)
448 for (i
= 0; i
< ref
.length (); ++i
)
450 fprintf (stderr
, "[%d] = ", i
);
452 fputc ('\n', stderr
);
456 /* We need a separate va_gc variant here because default template
457 argument for functions cannot be used in c++-98. Once this
458 restriction is removed, those variant should be folded with the
459 above debug_helper. */
463 debug_helper (vec
<T
, va_gc
> &ref
)
466 for (i
= 0; i
< ref
.length (); ++i
)
468 fprintf (stderr
, "[%d] = ", i
);
470 fputc ('\n', stderr
);
474 /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
475 functions for a type T. */
477 #define DEFINE_DEBUG_VEC(T) \
478 template void debug_helper (vec<T> &); \
479 template void debug_helper (vec<T, va_gc> &); \
480 /* Define the vec<T> debug functions. */ \
481 DEBUG_FUNCTION void \
482 debug (vec<T> &ref) \
484 debug_helper <T> (ref); \
486 DEBUG_FUNCTION void \
487 debug (vec<T> *ptr) \
492 fprintf (stderr, "<nil>\n"); \
494 /* Define the vec<T, va_gc> debug functions. */ \
495 DEBUG_FUNCTION void \
496 debug (vec<T, va_gc> &ref) \
498 debug_helper <T> (ref); \
500 DEBUG_FUNCTION void \
501 debug (vec<T, va_gc> *ptr) \
506 fprintf (stderr, "<nil>\n"); \
509 /* Default-construct N elements in DST. */
511 template <typename T
>
513 vec_default_construct (T
*dst
, unsigned n
)
515 #ifdef BROKEN_VALUE_INITIALIZATION
516 /* Versions of GCC before 4.4 sometimes leave certain objects
517 uninitialized when value initialized, though if the type has
518 user defined default ctor, that ctor is invoked. As a workaround
519 perform clearing first and then the value initialization, which
520 fixes the case when value initialization doesn't initialize due to
521 the bugs and should initialize to all zeros, but still allows
522 vectors for types with user defined default ctor that initializes
523 some or all elements to non-zero. If T has no user defined
524 default ctor and some non-static data members have user defined
525 default ctors that initialize to non-zero the workaround will
526 still not work properly; in that case we just need to provide
527 user defined default ctor. */
528 memset (dst
, '\0', sizeof (T
) * n
);
530 for ( ; n
; ++dst
, --n
)
531 ::new (static_cast<void*>(dst
)) T ();
534 /* Copy-construct N elements in DST from *SRC. */
536 template <typename T
>
538 vec_copy_construct (T
*dst
, const T
*src
, unsigned n
)
540 for ( ; n
; ++dst
, ++src
, --n
)
541 ::new (static_cast<void*>(dst
)) T (*src
);
544 /* Type to provide zero-initialized values for vec<T, A, L>. This is
545 used to provide nil initializers for vec instances. Since vec must
546 be a trivially copyable type that can be copied by memcpy and zeroed
547 out by memset, it must have defaulted default and copy ctor and copy
548 assignment. To initialize a vec either use value initialization
549 (e.g., vec() or vec v{ };) or assign it the value vNULL. This isn't
550 needed for file-scope and function-local static vectors, which are
551 zero-initialized by default. */
553 constexpr vnull vNULL
{ };
556 /* Embeddable vector. These vectors are suitable to be embedded
557 in other data structures so that they can be pre-allocated in a
558 contiguous memory block.
560 Embeddable vectors are implemented using the trailing array idiom,
561 thus they are not resizeable without changing the address of the
562 vector object itself. This means you cannot have variables or
563 fields of embeddable vector type -- always use a pointer to a
564 vector. The one exception is the final field of a structure, which
565 could be a vector type.
567 You will have to use the embedded_size & embedded_init calls to
568 create such objects, and they will not be resizeable (so the 'safe'
569 allocation variants are not available).
573 - The whole vector and control data are allocated in a single
574 contiguous block. It uses the trailing-vector idiom, so
575 allocation must reserve enough space for all the elements
576 in the vector plus its control data.
577 - The vector cannot be re-allocated.
578 - The vector cannot grow nor shrink.
579 - No indirections needed for access/manipulation.
580 - It requires 2 words of storage (prior to vector allocation). */
582 template<typename T
, typename A
>
583 struct GTY((user
)) vec
<T
, A
, vl_embed
>
586 unsigned allocated (void) const { return m_vecpfx
.m_alloc
; }
587 unsigned length (void) const { return m_vecpfx
.m_num
; }
588 bool is_empty (void) const { return m_vecpfx
.m_num
== 0; }
589 T
*address (void) { return m_vecdata
; }
590 const T
*address (void) const { return m_vecdata
; }
591 T
*begin () { return address (); }
592 const T
*begin () const { return address (); }
593 T
*end () { return address () + length (); }
594 const T
*end () const { return address () + length (); }
595 const T
&operator[] (unsigned) const;
596 T
&operator[] (unsigned);
598 bool space (unsigned) const;
599 bool iterate (unsigned, T
*) const;
600 bool iterate (unsigned, T
**) const;
601 vec
*copy (ALONE_CXX_MEM_STAT_INFO
) const;
602 void splice (const vec
&);
603 void splice (const vec
*src
);
604 T
*quick_push (const T
&);
606 void truncate (unsigned);
607 void quick_insert (unsigned, const T
&);
608 void ordered_remove (unsigned);
609 void unordered_remove (unsigned);
610 void block_remove (unsigned, unsigned);
611 void qsort (int (*) (const void *, const void *));
612 void sort (int (*) (const void *, const void *, void *), void *);
613 void stablesort (int (*) (const void *, const void *, void *), void *);
614 T
*bsearch (const void *key
, int (*compar
)(const void *, const void *));
615 T
*bsearch (const void *key
,
616 int (*compar
)(const void *, const void *, void *), void *);
617 unsigned lower_bound (T
, bool (*)(const T
&, const T
&)) const;
618 bool contains (const T
&search
) const;
619 static size_t embedded_size (unsigned);
620 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
621 void quick_grow (unsigned len
);
622 void quick_grow_cleared (unsigned len
);
624 /* vec class can access our internal data and functions. */
625 template <typename
, typename
, typename
> friend struct vec
;
627 /* The allocator types also need access to our internals. */
629 friend struct va_gc_atomic
;
630 friend struct va_heap
;
632 /* FIXME - These fields should be private, but we need to cater to
633 compilers that have stricter notions of PODness for types. */
639 /* Convenience wrapper functions to use when dealing with pointers to
640 embedded vectors. Some functionality for these vectors must be
641 provided via free functions for these reasons:
643 1- The pointer may be NULL (e.g., before initial allocation).
645 2- When the vector needs to grow, it must be reallocated, so
646 the pointer will change its value.
648 Because of limitations with the current GC machinery, all vectors
649 in GC memory *must* be pointers. */
652 /* If V contains no room for NELEMS elements, return false. Otherwise,
654 template<typename T
, typename A
>
656 vec_safe_space (const vec
<T
, A
, vl_embed
> *v
, unsigned nelems
)
658 return v
? v
->space (nelems
) : nelems
== 0;
662 /* If V is NULL, return 0. Otherwise, return V->length(). */
663 template<typename T
, typename A
>
665 vec_safe_length (const vec
<T
, A
, vl_embed
> *v
)
667 return v
? v
->length () : 0;
671 /* If V is NULL, return NULL. Otherwise, return V->address(). */
672 template<typename T
, typename A
>
674 vec_safe_address (vec
<T
, A
, vl_embed
> *v
)
676 return v
? v
->address () : NULL
;
680 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
681 template<typename T
, typename A
>
683 vec_safe_is_empty (vec
<T
, A
, vl_embed
> *v
)
685 return v
? v
->is_empty () : true;
688 /* If V does not have space for NELEMS elements, call
689 V->reserve(NELEMS, EXACT). */
690 template<typename T
, typename A
>
692 vec_safe_reserve (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems
, bool exact
= false
695 bool extend
= nelems
? !vec_safe_space (v
, nelems
) : false;
697 A::reserve (v
, nelems
, exact PASS_MEM_STAT
);
701 template<typename T
, typename A
>
703 vec_safe_reserve_exact (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems
706 return vec_safe_reserve (v
, nelems
, true PASS_MEM_STAT
);
710 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
711 is 0, V is initialized to NULL. */
713 template<typename T
, typename A
>
715 vec_alloc (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems CXX_MEM_STAT_INFO
)
718 vec_safe_reserve (v
, nelems
, false PASS_MEM_STAT
);
722 /* Free the GC memory allocated by vector V and set it to NULL. */
724 template<typename T
, typename A
>
726 vec_free (vec
<T
, A
, vl_embed
> *&v
)
732 /* Grow V to length LEN. Allocate it, if necessary. */
733 template<typename T
, typename A
>
735 vec_safe_grow (vec
<T
, A
, vl_embed
> *&v
, unsigned len
,
736 bool exact
= false CXX_MEM_STAT_INFO
)
738 unsigned oldlen
= vec_safe_length (v
);
739 gcc_checking_assert (len
>= oldlen
);
740 vec_safe_reserve (v
, len
- oldlen
, exact PASS_MEM_STAT
);
745 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
746 template<typename T
, typename A
>
748 vec_safe_grow_cleared (vec
<T
, A
, vl_embed
> *&v
, unsigned len
,
749 bool exact
= false CXX_MEM_STAT_INFO
)
751 unsigned oldlen
= vec_safe_length (v
);
752 vec_safe_grow (v
, len
, exact PASS_MEM_STAT
);
753 vec_default_construct (v
->address () + oldlen
, len
- oldlen
);
757 /* Assume V is not NULL. */
761 vec_safe_grow_cleared (vec
<T
, va_heap
, vl_ptr
> *&v
,
762 unsigned len
, bool exact
= false CXX_MEM_STAT_INFO
)
764 v
->safe_grow_cleared (len
, exact PASS_MEM_STAT
);
767 /* If V does not have space for NELEMS elements, call
768 V->reserve(NELEMS, EXACT). */
772 vec_safe_reserve (vec
<T
, va_heap
, vl_ptr
> *&v
, unsigned nelems
, bool exact
= false
775 return v
->reserve (nelems
, exact
);
779 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
780 template<typename T
, typename A
>
782 vec_safe_iterate (const vec
<T
, A
, vl_embed
> *v
, unsigned ix
, T
**ptr
)
785 return v
->iterate (ix
, ptr
);
793 template<typename T
, typename A
>
795 vec_safe_iterate (const vec
<T
, A
, vl_embed
> *v
, unsigned ix
, T
*ptr
)
798 return v
->iterate (ix
, ptr
);
807 /* If V has no room for one more element, reallocate it. Then call
808 V->quick_push(OBJ). */
809 template<typename T
, typename A
>
811 vec_safe_push (vec
<T
, A
, vl_embed
> *&v
, const T
&obj CXX_MEM_STAT_INFO
)
813 vec_safe_reserve (v
, 1, false PASS_MEM_STAT
);
814 return v
->quick_push (obj
);
818 /* if V has no room for one more element, reallocate it. Then call
819 V->quick_insert(IX, OBJ). */
820 template<typename T
, typename A
>
822 vec_safe_insert (vec
<T
, A
, vl_embed
> *&v
, unsigned ix
, const T
&obj
825 vec_safe_reserve (v
, 1, false PASS_MEM_STAT
);
826 v
->quick_insert (ix
, obj
);
830 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
831 template<typename T
, typename A
>
833 vec_safe_truncate (vec
<T
, A
, vl_embed
> *v
, unsigned size
)
840 /* If SRC is not NULL, return a pointer to a copy of it. */
841 template<typename T
, typename A
>
842 inline vec
<T
, A
, vl_embed
> *
843 vec_safe_copy (vec
<T
, A
, vl_embed
> *src CXX_MEM_STAT_INFO
)
845 return src
? src
->copy (ALONE_PASS_MEM_STAT
) : NULL
;
848 /* Copy the elements from SRC to the end of DST as if by memcpy.
849 Reallocate DST, if necessary. */
850 template<typename T
, typename A
>
852 vec_safe_splice (vec
<T
, A
, vl_embed
> *&dst
, const vec
<T
, A
, vl_embed
> *src
855 unsigned src_len
= vec_safe_length (src
);
858 vec_safe_reserve_exact (dst
, vec_safe_length (dst
) + src_len
864 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
865 size of the vector and so should be used with care. */
867 template<typename T
, typename A
>
869 vec_safe_contains (vec
<T
, A
, vl_embed
> *v
, const T
&search
)
871 return v
? v
->contains (search
) : false;
874 /* Index into vector. Return the IX'th element. IX must be in the
875 domain of the vector. */
877 template<typename T
, typename A
>
879 vec
<T
, A
, vl_embed
>::operator[] (unsigned ix
) const
881 gcc_checking_assert (ix
< m_vecpfx
.m_num
);
882 return m_vecdata
[ix
];
885 template<typename T
, typename A
>
887 vec
<T
, A
, vl_embed
>::operator[] (unsigned ix
)
889 gcc_checking_assert (ix
< m_vecpfx
.m_num
);
890 return m_vecdata
[ix
];
894 /* Get the final element of the vector, which must not be empty. */
896 template<typename T
, typename A
>
898 vec
<T
, A
, vl_embed
>::last (void)
900 gcc_checking_assert (m_vecpfx
.m_num
> 0);
901 return (*this)[m_vecpfx
.m_num
- 1];
905 /* If this vector has space for NELEMS additional entries, return
906 true. You usually only need to use this if you are doing your
907 own vector reallocation, for instance on an embedded vector. This
908 returns true in exactly the same circumstances that vec::reserve
911 template<typename T
, typename A
>
913 vec
<T
, A
, vl_embed
>::space (unsigned nelems
) const
915 return m_vecpfx
.m_alloc
- m_vecpfx
.m_num
>= nelems
;
919 /* Return iteration condition and update *PTR to (a copy of) the IX'th
920 element of this vector. Use this to iterate over the elements of a
923 for (ix = 0; v->iterate (ix, &val); ix++)
926 template<typename T
, typename A
>
928 vec
<T
, A
, vl_embed
>::iterate (unsigned ix
, T
*ptr
) const
930 if (ix
< m_vecpfx
.m_num
)
932 *ptr
= m_vecdata
[ix
];
943 /* Return iteration condition and update *PTR to point to the
944 IX'th element of this vector. Use this to iterate over the
945 elements of a vector as follows,
947 for (ix = 0; v->iterate (ix, &ptr); ix++)
950 This variant is for vectors of objects. */
952 template<typename T
, typename A
>
954 vec
<T
, A
, vl_embed
>::iterate (unsigned ix
, T
**ptr
) const
956 if (ix
< m_vecpfx
.m_num
)
958 *ptr
= CONST_CAST (T
*, &m_vecdata
[ix
]);
969 /* Return a pointer to a copy of this vector. */
971 template<typename T
, typename A
>
972 inline vec
<T
, A
, vl_embed
> *
973 vec
<T
, A
, vl_embed
>::copy (ALONE_MEM_STAT_DECL
) const
975 vec
<T
, A
, vl_embed
> *new_vec
= NULL
;
976 unsigned len
= length ();
979 vec_alloc (new_vec
, len PASS_MEM_STAT
);
980 new_vec
->embedded_init (len
, len
);
981 vec_copy_construct (new_vec
->address (), m_vecdata
, len
);
987 /* Copy the elements from SRC to the end of this vector as if by memcpy.
988 The vector must have sufficient headroom available. */
990 template<typename T
, typename A
>
992 vec
<T
, A
, vl_embed
>::splice (const vec
<T
, A
, vl_embed
> &src
)
994 unsigned len
= src
.length ();
997 gcc_checking_assert (space (len
));
998 vec_copy_construct (end (), src
.address (), len
);
999 m_vecpfx
.m_num
+= len
;
1003 template<typename T
, typename A
>
1005 vec
<T
, A
, vl_embed
>::splice (const vec
<T
, A
, vl_embed
> *src
)
1012 /* Push OBJ (a new element) onto the end of the vector. There must be
1013 sufficient space in the vector. Return a pointer to the slot
1014 where OBJ was inserted. */
1016 template<typename T
, typename A
>
1018 vec
<T
, A
, vl_embed
>::quick_push (const T
&obj
)
1020 gcc_checking_assert (space (1));
1021 T
*slot
= &m_vecdata
[m_vecpfx
.m_num
++];
1027 /* Pop and return the last element off the end of the vector. */
1029 template<typename T
, typename A
>
1031 vec
<T
, A
, vl_embed
>::pop (void)
1033 gcc_checking_assert (length () > 0);
1034 return m_vecdata
[--m_vecpfx
.m_num
];
1038 /* Set the length of the vector to SIZE. The new length must be less
1039 than or equal to the current length. This is an O(1) operation. */
1041 template<typename T
, typename A
>
1043 vec
<T
, A
, vl_embed
>::truncate (unsigned size
)
1045 gcc_checking_assert (length () >= size
);
1046 m_vecpfx
.m_num
= size
;
1050 /* Insert an element, OBJ, at the IXth position of this vector. There
1051 must be sufficient space. */
1053 template<typename T
, typename A
>
1055 vec
<T
, A
, vl_embed
>::quick_insert (unsigned ix
, const T
&obj
)
1057 gcc_checking_assert (length () < allocated ());
1058 gcc_checking_assert (ix
<= length ());
1059 T
*slot
= &m_vecdata
[ix
];
1060 memmove (slot
+ 1, slot
, (m_vecpfx
.m_num
++ - ix
) * sizeof (T
));
1065 /* Remove an element from the IXth position of this vector. Ordering of
1066 remaining elements is preserved. This is an O(N) operation due to
1069 template<typename T
, typename A
>
1071 vec
<T
, A
, vl_embed
>::ordered_remove (unsigned ix
)
1073 gcc_checking_assert (ix
< length ());
1074 T
*slot
= &m_vecdata
[ix
];
1075 memmove (slot
, slot
+ 1, (--m_vecpfx
.m_num
- ix
) * sizeof (T
));
1079 /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
1080 remaining elements is preserved. This is an O(N) operation. */
1082 #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
1083 elem_ptr, start, end, cond) \
1085 gcc_assert ((end) <= (vec).length ()); \
1086 for (read_index = write_index = (start); read_index < (end); \
1089 elem_ptr = &(vec)[read_index]; \
1090 bool remove_p = (cond); \
1094 if (read_index != write_index) \
1095 (vec)[write_index] = (vec)[read_index]; \
1100 if (read_index - write_index > 0) \
1101 (vec).block_remove (write_index, read_index - write_index); \
1105 /* Remove elements from VEC for which COND holds. Ordering of remaining
1106 elements is preserved. This is an O(N) operation. */
1108 #define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \
1110 VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
1111 elem_ptr, 0, (vec).length (), (cond))
1113 /* Remove an element from the IXth position of this vector. Ordering of
1114 remaining elements is destroyed. This is an O(1) operation. */
1116 template<typename T
, typename A
>
1118 vec
<T
, A
, vl_embed
>::unordered_remove (unsigned ix
)
1120 gcc_checking_assert (ix
< length ());
1121 m_vecdata
[ix
] = m_vecdata
[--m_vecpfx
.m_num
];
1125 /* Remove LEN elements starting at the IXth. Ordering is retained.
1126 This is an O(N) operation due to memmove. */
1128 template<typename T
, typename A
>
1130 vec
<T
, A
, vl_embed
>::block_remove (unsigned ix
, unsigned len
)
1132 gcc_checking_assert (ix
+ len
<= length ());
1133 T
*slot
= &m_vecdata
[ix
];
1134 m_vecpfx
.m_num
-= len
;
1135 memmove (slot
, slot
+ len
, (m_vecpfx
.m_num
- ix
) * sizeof (T
));
1139 /* Sort the contents of this vector with qsort. CMP is the comparison
1140 function to pass to qsort. */
1142 template<typename T
, typename A
>
1144 vec
<T
, A
, vl_embed
>::qsort (int (*cmp
) (const void *, const void *))
1147 gcc_qsort (address (), length (), sizeof (T
), cmp
);
1150 /* Sort the contents of this vector with qsort. CMP is the comparison
1151 function to pass to qsort. */
1153 template<typename T
, typename A
>
1155 vec
<T
, A
, vl_embed
>::sort (int (*cmp
) (const void *, const void *, void *),
1159 gcc_sort_r (address (), length (), sizeof (T
), cmp
, data
);
1162 /* Sort the contents of this vector with gcc_stablesort_r. CMP is the
1163 comparison function to pass to qsort. */
1165 template<typename T
, typename A
>
1167 vec
<T
, A
, vl_embed
>::stablesort (int (*cmp
) (const void *, const void *,
1168 void *), void *data
)
1171 gcc_stablesort_r (address (), length (), sizeof (T
), cmp
, data
);
1174 /* Search the contents of the sorted vector with a binary search.
1175 CMP is the comparison function to pass to bsearch. */
1177 template<typename T
, typename A
>
1179 vec
<T
, A
, vl_embed
>::bsearch (const void *key
,
1180 int (*compar
) (const void *, const void *))
1182 const void *base
= this->address ();
1183 size_t nmemb
= this->length ();
1184 size_t size
= sizeof (T
);
1185 /* The following is a copy of glibc stdlib-bsearch.h. */
1195 p
= (const void *) (((const char *) base
) + (idx
* size
));
1196 comparison
= (*compar
) (key
, p
);
1199 else if (comparison
> 0)
1202 return (T
*)const_cast<void *>(p
);
1208 /* Search the contents of the sorted vector with a binary search.
1209 CMP is the comparison function to pass to bsearch. */
1211 template<typename T
, typename A
>
1213 vec
<T
, A
, vl_embed
>::bsearch (const void *key
,
1214 int (*compar
) (const void *, const void *,
1215 void *), void *data
)
1217 const void *base
= this->address ();
1218 size_t nmemb
= this->length ();
1219 size_t size
= sizeof (T
);
1220 /* The following is a copy of glibc stdlib-bsearch.h. */
1230 p
= (const void *) (((const char *) base
) + (idx
* size
));
1231 comparison
= (*compar
) (key
, p
, data
);
1234 else if (comparison
> 0)
1237 return (T
*)const_cast<void *>(p
);
1243 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
1244 size of the vector and so should be used with care. */
1246 template<typename T
, typename A
>
1248 vec
<T
, A
, vl_embed
>::contains (const T
&search
) const
1250 unsigned int len
= length ();
1251 for (unsigned int i
= 0; i
< len
; i
++)
1252 if ((*this)[i
] == search
)
1258 /* Find and return the first position in which OBJ could be inserted
1259 without changing the ordering of this vector. LESSTHAN is a
1260 function that returns true if the first argument is strictly less
1263 template<typename T
, typename A
>
1265 vec
<T
, A
, vl_embed
>::lower_bound (T obj
, bool (*lessthan
)(const T
&, const T
&))
1268 unsigned int len
= length ();
1269 unsigned int half
, middle
;
1270 unsigned int first
= 0;
1276 T middle_elem
= (*this)[middle
];
1277 if (lessthan (middle_elem
, obj
))
1281 len
= len
- half
- 1;
1290 /* Return the number of bytes needed to embed an instance of an
1291 embeddable vec inside another data structure.
1293 Use these methods to determine the required size and initialization
1294 of a vector V of type T embedded within another structure (as the
1297 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1298 void v->embedded_init (unsigned alloc, unsigned num);
1300 These allow the caller to perform the memory allocation. */
1302 template<typename T
, typename A
>
1304 vec
<T
, A
, vl_embed
>::embedded_size (unsigned alloc
)
1306 struct alignas (T
) U
{ char data
[sizeof (T
)]; };
1307 typedef vec
<U
, A
, vl_embed
> vec_embedded
;
1308 typedef typename
std::conditional
<std::is_standard_layout
<T
>::value
,
1309 vec
, vec_embedded
>::type vec_stdlayout
;
1310 static_assert (sizeof (vec_stdlayout
) == sizeof (vec
), "");
1311 static_assert (alignof (vec_stdlayout
) == alignof (vec
), "");
1312 return offsetof (vec_stdlayout
, m_vecdata
) + alloc
* sizeof (T
);
1316 /* Initialize the vector to contain room for ALLOC elements and
1317 NUM active elements. */
1319 template<typename T
, typename A
>
1321 vec
<T
, A
, vl_embed
>::embedded_init (unsigned alloc
, unsigned num
, unsigned aut
)
1323 m_vecpfx
.m_alloc
= alloc
;
1324 m_vecpfx
.m_using_auto_storage
= aut
;
1325 m_vecpfx
.m_num
= num
;
1329 /* Grow the vector to a specific length. LEN must be as long or longer than
1330 the current length. The new elements are uninitialized. */
1332 template<typename T
, typename A
>
1334 vec
<T
, A
, vl_embed
>::quick_grow (unsigned len
)
1336 gcc_checking_assert (length () <= len
&& len
<= m_vecpfx
.m_alloc
);
1337 m_vecpfx
.m_num
= len
;
1341 /* Grow the vector to a specific length. LEN must be as long or longer than
1342 the current length. The new elements are initialized to zero. */
1344 template<typename T
, typename A
>
1346 vec
<T
, A
, vl_embed
>::quick_grow_cleared (unsigned len
)
1348 unsigned oldlen
= length ();
1349 size_t growby
= len
- oldlen
;
1352 vec_default_construct (address () + oldlen
, growby
);
1355 /* Garbage collection support for vec<T, A, vl_embed>. */
1357 template<typename T
>
1359 gt_ggc_mx (vec
<T
, va_gc
> *v
)
1361 extern void gt_ggc_mx (T
&);
1362 for (unsigned i
= 0; i
< v
->length (); i
++)
1363 gt_ggc_mx ((*v
)[i
]);
1366 template<typename T
>
1368 gt_ggc_mx (vec
<T
, va_gc_atomic
, vl_embed
> *v ATTRIBUTE_UNUSED
)
1370 /* Nothing to do. Vectors of atomic types wrt GC do not need to
1375 /* PCH support for vec<T, A, vl_embed>. */
1377 template<typename T
, typename A
>
1379 gt_pch_nx (vec
<T
, A
, vl_embed
> *v
)
1381 extern void gt_pch_nx (T
&);
1382 for (unsigned i
= 0; i
< v
->length (); i
++)
1383 gt_pch_nx ((*v
)[i
]);
1386 template<typename T
, typename A
>
1388 gt_pch_nx (vec
<T
*, A
, vl_embed
> *v
, gt_pointer_operator op
, void *cookie
)
1390 for (unsigned i
= 0; i
< v
->length (); i
++)
1391 op (&((*v
)[i
]), NULL
, cookie
);
1394 template<typename T
, typename A
>
1396 gt_pch_nx (vec
<T
, A
, vl_embed
> *v
, gt_pointer_operator op
, void *cookie
)
1398 extern void gt_pch_nx (T
*, gt_pointer_operator
, void *);
1399 for (unsigned i
= 0; i
< v
->length (); i
++)
1400 gt_pch_nx (&((*v
)[i
]), op
, cookie
);
1404 /* Space efficient vector. These vectors can grow dynamically and are
1405 allocated together with their control data. They are suited to be
1406 included in data structures. Prior to initial allocation, they
1407 only take a single word of storage.
1409 These vectors are implemented as a pointer to an embeddable vector.
1410 The semantics allow for this pointer to be NULL to represent empty
1411 vectors. This way, empty vectors occupy minimal space in the
1412 structure containing them.
1416 - The whole vector and control data are allocated in a single
1418 - The whole vector may be re-allocated.
1419 - Vector data may grow and shrink.
1420 - Access and manipulation requires a pointer test and
1422 - It requires 1 word of storage (prior to vector allocation).
1427 These vectors must be PODs because they are stored in unions.
1428 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1429 As long as we use C++03, we cannot have constructors nor
1430 destructors in classes that are stored in unions. */
1432 template<typename T
, size_t N
= 0>
1435 template<typename T
>
1436 struct vec
<T
, va_heap
, vl_ptr
>
1439 /* Default ctors to ensure triviality. Use value-initialization
1440 (e.g., vec() or vec v{ };) or vNULL to create a zero-initialized
1443 vec (const vec
&) = default;
1444 /* Initialization from the generic vNULL. */
1445 vec (vnull
): m_vec () { }
1446 /* Same as default ctor: vec storage must be released manually. */
1449 /* Defaulted same as copy ctor. */
1450 vec
& operator= (const vec
&) = default;
1452 /* Prevent implicit conversion from auto_vec. Use auto_vec::to_vec()
1455 vec (auto_vec
<T
, N
> &) = delete;
1458 void operator= (auto_vec
<T
, N
> &) = delete;
1460 /* Memory allocation and deallocation for the embedded vector.
1461 Needed because we cannot have proper ctors/dtors defined. */
1462 void create (unsigned nelems CXX_MEM_STAT_INFO
);
1463 void release (void);
1465 /* Vector operations. */
1466 bool exists (void) const
1467 { return m_vec
!= NULL
; }
1469 bool is_empty (void) const
1470 { return m_vec
? m_vec
->is_empty () : true; }
1472 unsigned allocated (void) const
1473 { return m_vec
? m_vec
->allocated () : 0; }
1475 unsigned length (void) const
1476 { return m_vec
? m_vec
->length () : 0; }
1479 { return m_vec
? m_vec
->m_vecdata
: NULL
; }
1481 const T
*address (void) const
1482 { return m_vec
? m_vec
->m_vecdata
: NULL
; }
1484 T
*begin () { return address (); }
1485 const T
*begin () const { return address (); }
1486 T
*end () { return begin () + length (); }
1487 const T
*end () const { return begin () + length (); }
1488 const T
&operator[] (unsigned ix
) const
1489 { return (*m_vec
)[ix
]; }
1491 bool operator!=(const vec
&other
) const
1492 { return !(*this == other
); }
1494 bool operator==(const vec
&other
) const
1495 { return address () == other
.address (); }
1497 T
&operator[] (unsigned ix
)
1498 { return (*m_vec
)[ix
]; }
1501 { return m_vec
->last (); }
1503 bool space (int nelems
) const
1504 { return m_vec
? m_vec
->space (nelems
) : nelems
== 0; }
1506 bool iterate (unsigned ix
, T
*p
) const;
1507 bool iterate (unsigned ix
, T
**p
) const;
1508 vec
copy (ALONE_CXX_MEM_STAT_INFO
) const;
1509 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO
);
1510 bool reserve_exact (unsigned CXX_MEM_STAT_INFO
);
1511 void splice (const vec
&);
1512 void safe_splice (const vec
& CXX_MEM_STAT_INFO
);
1513 T
*quick_push (const T
&);
1514 T
*safe_push (const T
&CXX_MEM_STAT_INFO
);
1516 void truncate (unsigned);
1517 void safe_grow (unsigned, bool = false CXX_MEM_STAT_INFO
);
1518 void safe_grow_cleared (unsigned, bool = false CXX_MEM_STAT_INFO
);
1519 void quick_grow (unsigned);
1520 void quick_grow_cleared (unsigned);
1521 void quick_insert (unsigned, const T
&);
1522 void safe_insert (unsigned, const T
& CXX_MEM_STAT_INFO
);
1523 void ordered_remove (unsigned);
1524 void unordered_remove (unsigned);
1525 void block_remove (unsigned, unsigned);
1526 void qsort (int (*) (const void *, const void *));
1527 void sort (int (*) (const void *, const void *, void *), void *);
1528 void stablesort (int (*) (const void *, const void *, void *), void *);
1529 T
*bsearch (const void *key
, int (*compar
)(const void *, const void *));
1530 T
*bsearch (const void *key
,
1531 int (*compar
)(const void *, const void *, void *), void *);
1532 unsigned lower_bound (T
, bool (*)(const T
&, const T
&)) const;
1533 bool contains (const T
&search
) const;
1534 void reverse (void);
1536 bool using_auto_storage () const;
1538 /* FIXME - This field should be private, but we need to cater to
1539 compilers that have stricter notions of PODness for types. */
1540 vec
<T
, va_heap
, vl_embed
> *m_vec
;
1544 /* auto_vec is a subclass of vec that automatically manages creating and
1545 releasing the internal vector. If N is non zero then it has N elements of
1546 internal storage. The default is no internal storage, and you probably only
1547 want to ask for internal storage for vectors on the stack because if the
1548 size of the vector is larger than the internal storage that space is wasted.
1550 template<typename T
, size_t N
/* = 0 */>
1551 class auto_vec
: public vec
<T
, va_heap
>
1556 m_auto
.embedded_init (MAX (N
, 2), 0, 1);
1557 this->m_vec
= &m_auto
;
1560 auto_vec (size_t s CXX_MEM_STAT_INFO
)
1564 this->create (s PASS_MEM_STAT
);
1568 m_auto
.embedded_init (MAX (N
, 2), 0, 1);
1569 this->m_vec
= &m_auto
;
1577 /* Explicitly convert to the base class. There is no conversion
1578 from a const auto_vec because a copy of the returned vec can
1579 be used to modify *THIS.
1580 This is a legacy function not to be used in new code. */
1581 vec
<T
, va_heap
> to_vec_legacy () {
1582 return *static_cast<vec
<T
, va_heap
> *>(this);
1586 vec
<T
, va_heap
, vl_embed
> m_auto
;
1587 T m_data
[MAX (N
- 1, 1)];
1590 /* auto_vec is a sub class of vec whose storage is released when it is
1592 template<typename T
>
1593 class auto_vec
<T
, 0> : public vec
<T
, va_heap
>
1596 auto_vec () { this->m_vec
= NULL
; }
1597 auto_vec (size_t n CXX_MEM_STAT_INFO
) { this->create (n PASS_MEM_STAT
); }
1598 ~auto_vec () { this->release (); }
1600 auto_vec (vec
<T
, va_heap
>&& r
)
1602 gcc_assert (!r
.using_auto_storage ());
1603 this->m_vec
= r
.m_vec
;
1607 auto_vec (auto_vec
<T
> &&r
)
1609 gcc_assert (!r
.using_auto_storage ());
1610 this->m_vec
= r
.m_vec
;
1614 auto_vec
& operator= (vec
<T
, va_heap
>&& r
)
1619 gcc_assert (!r
.using_auto_storage ());
1621 this->m_vec
= r
.m_vec
;
1626 auto_vec
& operator= (auto_vec
<T
> &&r
)
1631 gcc_assert (!r
.using_auto_storage ());
1633 this->m_vec
= r
.m_vec
;
1638 /* Explicitly convert to the base class. There is no conversion
1639 from a const auto_vec because a copy of the returned vec can
1640 be used to modify *THIS.
1641 This is a legacy function not to be used in new code. */
1642 vec
<T
, va_heap
> to_vec_legacy () {
1643 return *static_cast<vec
<T
, va_heap
> *>(this);
1646 // You probably don't want to copy a vector, so these are deleted to prevent
1647 // unintentional use. If you really need a copy of the vectors contents you
1649 auto_vec(const auto_vec
&) = delete;
1650 auto_vec
&operator= (const auto_vec
&) = delete;
1654 /* Allocate heap memory for pointer V and create the internal vector
1655 with space for NELEMS elements. If NELEMS is 0, the internal
1656 vector is initialized to empty. */
1658 template<typename T
>
1660 vec_alloc (vec
<T
> *&v
, unsigned nelems CXX_MEM_STAT_INFO
)
1663 v
->create (nelems PASS_MEM_STAT
);
1667 /* A subclass of auto_vec <char *> that frees all of its elements on
1670 class auto_string_vec
: public auto_vec
<char *>
1673 ~auto_string_vec ();
1676 /* A subclass of auto_vec <T *> that deletes all of its elements on
1679 This is a crude way for a vec to "own" the objects it points to
1680 and clean up automatically.
1682 For example, no attempt is made to delete elements when an item
1683 within the vec is overwritten.
1685 We can't rely on gnu::unique_ptr within a container,
1686 since we can't rely on move semantics in C++98. */
1688 template <typename T
>
1689 class auto_delete_vec
: public auto_vec
<T
*>
1692 auto_delete_vec () {}
1693 auto_delete_vec (size_t s
) : auto_vec
<T
*> (s
) {}
1695 ~auto_delete_vec ();
1698 DISABLE_COPY_AND_ASSIGN(auto_delete_vec
);
1701 /* Conditionally allocate heap memory for VEC and its internal vector. */
1703 template<typename T
>
1705 vec_check_alloc (vec
<T
, va_heap
> *&vec
, unsigned nelems CXX_MEM_STAT_INFO
)
1708 vec_alloc (vec
, nelems PASS_MEM_STAT
);
1712 /* Free the heap memory allocated by vector V and set it to NULL. */
1714 template<typename T
>
1716 vec_free (vec
<T
> *&v
)
1727 /* Return iteration condition and update PTR to point to the IX'th
1728 element of this vector. Use this to iterate over the elements of a
1731 for (ix = 0; v.iterate (ix, &ptr); ix++)
1734 template<typename T
>
1736 vec
<T
, va_heap
, vl_ptr
>::iterate (unsigned ix
, T
*ptr
) const
1739 return m_vec
->iterate (ix
, ptr
);
1748 /* Return iteration condition and update *PTR to point to the
1749 IX'th element of this vector. Use this to iterate over the
1750 elements of a vector as follows,
1752 for (ix = 0; v->iterate (ix, &ptr); ix++)
1755 This variant is for vectors of objects. */
1757 template<typename T
>
1759 vec
<T
, va_heap
, vl_ptr
>::iterate (unsigned ix
, T
**ptr
) const
1762 return m_vec
->iterate (ix
, ptr
);
1771 /* Convenience macro for forward iteration. */
1772 #define FOR_EACH_VEC_ELT(V, I, P) \
1773 for (I = 0; (V).iterate ((I), &(P)); ++(I))
1775 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
1776 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1778 /* Likewise, but start from FROM rather than 0. */
1779 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
1780 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1782 /* Convenience macro for reverse iteration. */
1783 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
1784 for (I = (V).length () - 1; \
1785 (V).iterate ((I), &(P)); \
1788 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1789 for (I = vec_safe_length (V) - 1; \
1790 vec_safe_iterate ((V), (I), &(P)); \
1793 /* auto_string_vec's dtor, freeing all contained strings, automatically
1794 chaining up to ~auto_vec <char *>, which frees the internal buffer. */
1797 auto_string_vec::~auto_string_vec ()
1801 FOR_EACH_VEC_ELT (*this, i
, str
)
1805 /* auto_delete_vec's dtor, deleting all contained items, automatically
1806 chaining up to ~auto_vec <T*>, which frees the internal buffer. */
1808 template <typename T
>
1810 auto_delete_vec
<T
>::~auto_delete_vec ()
1814 FOR_EACH_VEC_ELT (*this, i
, item
)
1819 /* Return a copy of this vector. */
1821 template<typename T
>
1822 inline vec
<T
, va_heap
, vl_ptr
>
1823 vec
<T
, va_heap
, vl_ptr
>::copy (ALONE_MEM_STAT_DECL
) const
1825 vec
<T
, va_heap
, vl_ptr
> new_vec
{ };
1827 new_vec
.m_vec
= m_vec
->copy (ALONE_PASS_MEM_STAT
);
1832 /* Ensure that the vector has at least RESERVE slots available (if
1833 EXACT is false), or exactly RESERVE slots available (if EXACT is
1836 This may create additional headroom if EXACT is false.
1838 Note that this can cause the embedded vector to be reallocated.
1839 Returns true iff reallocation actually occurred. */
1841 template<typename T
>
1843 vec
<T
, va_heap
, vl_ptr
>::reserve (unsigned nelems
, bool exact MEM_STAT_DECL
)
1848 /* For now play a game with va_heap::reserve to hide our auto storage if any,
1849 this is necessary because it doesn't have enough information to know the
1850 embedded vector is in auto storage, and so should not be freed. */
1851 vec
<T
, va_heap
, vl_embed
> *oldvec
= m_vec
;
1852 unsigned int oldsize
= 0;
1853 bool handle_auto_vec
= m_vec
&& using_auto_storage ();
1854 if (handle_auto_vec
)
1857 oldsize
= oldvec
->length ();
1861 va_heap::reserve (m_vec
, nelems
, exact PASS_MEM_STAT
);
1862 if (handle_auto_vec
)
1864 vec_copy_construct (m_vec
->address (), oldvec
->address (), oldsize
);
1865 m_vec
->m_vecpfx
.m_num
= oldsize
;
1872 /* Ensure that this vector has exactly NELEMS slots available. This
1873 will not create additional headroom. Note this can cause the
1874 embedded vector to be reallocated. Returns true iff reallocation
1875 actually occurred. */
1877 template<typename T
>
1879 vec
<T
, va_heap
, vl_ptr
>::reserve_exact (unsigned nelems MEM_STAT_DECL
)
1881 return reserve (nelems
, true PASS_MEM_STAT
);
1885 /* Create the internal vector and reserve NELEMS for it. This is
1886 exactly like vec::reserve, but the internal vector is
1887 unconditionally allocated from scratch. The old one, if it
1888 existed, is lost. */
1890 template<typename T
>
1892 vec
<T
, va_heap
, vl_ptr
>::create (unsigned nelems MEM_STAT_DECL
)
1896 reserve_exact (nelems PASS_MEM_STAT
);
1900 /* Free the memory occupied by the embedded vector. */
1902 template<typename T
>
1904 vec
<T
, va_heap
, vl_ptr
>::release (void)
1909 if (using_auto_storage ())
1911 m_vec
->m_vecpfx
.m_num
= 0;
1915 va_heap::release (m_vec
);
1918 /* Copy the elements from SRC to the end of this vector as if by memcpy.
1919 SRC and this vector must be allocated with the same memory
1920 allocation mechanism. This vector is assumed to have sufficient
1921 headroom available. */
1923 template<typename T
>
1925 vec
<T
, va_heap
, vl_ptr
>::splice (const vec
<T
, va_heap
, vl_ptr
> &src
)
1928 m_vec
->splice (*(src
.m_vec
));
1932 /* Copy the elements in SRC to the end of this vector as if by memcpy.
1933 SRC and this vector must be allocated with the same mechanism.
1934 If there is not enough headroom in this vector, it will be reallocated
1937 template<typename T
>
1939 vec
<T
, va_heap
, vl_ptr
>::safe_splice (const vec
<T
, va_heap
, vl_ptr
> &src
1944 reserve_exact (src
.length ());
1950 /* Push OBJ (a new element) onto the end of the vector. There must be
1951 sufficient space in the vector. Return a pointer to the slot
1952 where OBJ was inserted. */
1954 template<typename T
>
1956 vec
<T
, va_heap
, vl_ptr
>::quick_push (const T
&obj
)
1958 return m_vec
->quick_push (obj
);
1962 /* Push a new element OBJ onto the end of this vector. Reallocates
1963 the embedded vector, if needed. Return a pointer to the slot where
1964 OBJ was inserted. */
1966 template<typename T
>
1968 vec
<T
, va_heap
, vl_ptr
>::safe_push (const T
&obj MEM_STAT_DECL
)
1970 reserve (1, false PASS_MEM_STAT
);
1971 return quick_push (obj
);
1975 /* Pop and return the last element off the end of the vector. */
1977 template<typename T
>
1979 vec
<T
, va_heap
, vl_ptr
>::pop (void)
1981 return m_vec
->pop ();
1985 /* Set the length of the vector to LEN. The new length must be less
1986 than or equal to the current length. This is an O(1) operation. */
1988 template<typename T
>
1990 vec
<T
, va_heap
, vl_ptr
>::truncate (unsigned size
)
1993 m_vec
->truncate (size
);
1995 gcc_checking_assert (size
== 0);
1999 /* Grow the vector to a specific length. LEN must be as long or
2000 longer than the current length. The new elements are
2001 uninitialized. Reallocate the internal vector, if needed. */
2003 template<typename T
>
2005 vec
<T
, va_heap
, vl_ptr
>::safe_grow (unsigned len
, bool exact MEM_STAT_DECL
)
2007 unsigned oldlen
= length ();
2008 gcc_checking_assert (oldlen
<= len
);
2009 reserve (len
- oldlen
, exact PASS_MEM_STAT
);
2011 m_vec
->quick_grow (len
);
2013 gcc_checking_assert (len
== 0);
2017 /* Grow the embedded vector to a specific length. LEN must be as
2018 long or longer than the current length. The new elements are
2019 initialized to zero. Reallocate the internal vector, if needed. */
2021 template<typename T
>
2023 vec
<T
, va_heap
, vl_ptr
>::safe_grow_cleared (unsigned len
, bool exact
2026 unsigned oldlen
= length ();
2027 size_t growby
= len
- oldlen
;
2028 safe_grow (len
, exact PASS_MEM_STAT
);
2030 vec_default_construct (address () + oldlen
, growby
);
2034 /* Same as vec::safe_grow but without reallocation of the internal vector.
2035 If the vector cannot be extended, a runtime assertion will be triggered. */
2037 template<typename T
>
2039 vec
<T
, va_heap
, vl_ptr
>::quick_grow (unsigned len
)
2041 gcc_checking_assert (m_vec
);
2042 m_vec
->quick_grow (len
);
2046 /* Same as vec::quick_grow_cleared but without reallocation of the
2047 internal vector. If the vector cannot be extended, a runtime
2048 assertion will be triggered. */
2050 template<typename T
>
2052 vec
<T
, va_heap
, vl_ptr
>::quick_grow_cleared (unsigned len
)
2054 gcc_checking_assert (m_vec
);
2055 m_vec
->quick_grow_cleared (len
);
2059 /* Insert an element, OBJ, at the IXth position of this vector. There
2060 must be sufficient space. */
2062 template<typename T
>
2064 vec
<T
, va_heap
, vl_ptr
>::quick_insert (unsigned ix
, const T
&obj
)
2066 m_vec
->quick_insert (ix
, obj
);
2070 /* Insert an element, OBJ, at the IXth position of the vector.
2071 Reallocate the embedded vector, if necessary. */
2073 template<typename T
>
2075 vec
<T
, va_heap
, vl_ptr
>::safe_insert (unsigned ix
, const T
&obj MEM_STAT_DECL
)
2077 reserve (1, false PASS_MEM_STAT
);
2078 quick_insert (ix
, obj
);
2082 /* Remove an element from the IXth position of this vector. Ordering of
2083 remaining elements is preserved. This is an O(N) operation due to
2086 template<typename T
>
2088 vec
<T
, va_heap
, vl_ptr
>::ordered_remove (unsigned ix
)
2090 m_vec
->ordered_remove (ix
);
2094 /* Remove an element from the IXth position of this vector. Ordering
2095 of remaining elements is destroyed. This is an O(1) operation. */
2097 template<typename T
>
2099 vec
<T
, va_heap
, vl_ptr
>::unordered_remove (unsigned ix
)
2101 m_vec
->unordered_remove (ix
);
2105 /* Remove LEN elements starting at the IXth. Ordering is retained.
2106 This is an O(N) operation due to memmove. */
2108 template<typename T
>
2110 vec
<T
, va_heap
, vl_ptr
>::block_remove (unsigned ix
, unsigned len
)
2112 m_vec
->block_remove (ix
, len
);
2116 /* Sort the contents of this vector with qsort. CMP is the comparison
2117 function to pass to qsort. */
2119 template<typename T
>
2121 vec
<T
, va_heap
, vl_ptr
>::qsort (int (*cmp
) (const void *, const void *))
2127 /* Sort the contents of this vector with qsort. CMP is the comparison
2128 function to pass to qsort. */
2130 template<typename T
>
2132 vec
<T
, va_heap
, vl_ptr
>::sort (int (*cmp
) (const void *, const void *,
2133 void *), void *data
)
2136 m_vec
->sort (cmp
, data
);
2139 /* Sort the contents of this vector with gcc_stablesort_r. CMP is the
2140 comparison function to pass to qsort. */
2142 template<typename T
>
2144 vec
<T
, va_heap
, vl_ptr
>::stablesort (int (*cmp
) (const void *, const void *,
2145 void *), void *data
)
2148 m_vec
->stablesort (cmp
, data
);
2151 /* Search the contents of the sorted vector with a binary search.
2152 CMP is the comparison function to pass to bsearch. */
2154 template<typename T
>
2156 vec
<T
, va_heap
, vl_ptr
>::bsearch (const void *key
,
2157 int (*cmp
) (const void *, const void *))
2160 return m_vec
->bsearch (key
, cmp
);
2164 /* Search the contents of the sorted vector with a binary search.
2165 CMP is the comparison function to pass to bsearch. */
2167 template<typename T
>
2169 vec
<T
, va_heap
, vl_ptr
>::bsearch (const void *key
,
2170 int (*cmp
) (const void *, const void *,
2171 void *), void *data
)
2174 return m_vec
->bsearch (key
, cmp
, data
);
2179 /* Find and return the first position in which OBJ could be inserted
2180 without changing the ordering of this vector. LESSTHAN is a
2181 function that returns true if the first argument is strictly less
2184 template<typename T
>
2186 vec
<T
, va_heap
, vl_ptr
>::lower_bound (T obj
,
2187 bool (*lessthan
)(const T
&, const T
&))
2190 return m_vec
? m_vec
->lower_bound (obj
, lessthan
) : 0;
2193 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
2194 size of the vector and so should be used with care. */
2196 template<typename T
>
2198 vec
<T
, va_heap
, vl_ptr
>::contains (const T
&search
) const
2200 return m_vec
? m_vec
->contains (search
) : false;
2203 /* Reverse content of the vector. */
2205 template<typename T
>
2207 vec
<T
, va_heap
, vl_ptr
>::reverse (void)
2209 unsigned l
= length ();
2210 T
*ptr
= address ();
2212 for (unsigned i
= 0; i
< l
/ 2; i
++)
2213 std::swap (ptr
[i
], ptr
[l
- i
- 1]);
2216 template<typename T
>
2218 vec
<T
, va_heap
, vl_ptr
>::using_auto_storage () const
2220 return m_vec
? m_vec
->m_vecpfx
.m_using_auto_storage
: false;
2223 /* Release VEC and call release of all element vectors. */
2225 template<typename T
>
2227 release_vec_vec (vec
<vec
<T
> > &vec
)
2229 for (unsigned i
= 0; i
< vec
.length (); i
++)
2235 // Provide a subset of the std::span functionality. (We can't use std::span
2236 // itself because it's a C++20 feature.)
2238 // In addition, provide an invalid value that is distinct from all valid
2239 // sequences (including the empty sequence). This can be used to return
2240 // failure without having to use std::optional.
2242 // There is no operator bool because it would be ambiguous whether it is
2243 // testing for a valid value or an empty sequence.
2244 template<typename T
>
2247 template<typename OtherT
> friend class array_slice
;
2250 using value_type
= T
;
2251 using iterator
= T
*;
2252 using const_iterator
= const T
*;
2254 array_slice () : m_base (nullptr), m_size (0) {}
2256 template<typename OtherT
>
2257 array_slice (array_slice
<OtherT
> other
)
2258 : m_base (other
.m_base
), m_size (other
.m_size
) {}
2260 array_slice (iterator base
, unsigned int size
)
2261 : m_base (base
), m_size (size
) {}
2264 array_slice (T (&array
)[N
]) : m_base (array
), m_size (N
) {}
2266 template<typename OtherT
>
2267 array_slice (const vec
<OtherT
> &v
)
2268 : m_base (v
.address ()), m_size (v
.length ()) {}
2270 template<typename OtherT
>
2271 array_slice (vec
<OtherT
> &v
)
2272 : m_base (v
.address ()), m_size (v
.length ()) {}
2274 template<typename OtherT
>
2275 array_slice (const vec
<OtherT
, va_gc
> *v
)
2276 : m_base (v
? v
->address () : nullptr), m_size (v
? v
->length () : 0) {}
2278 template<typename OtherT
>
2279 array_slice (vec
<OtherT
, va_gc
> *v
)
2280 : m_base (v
? v
->address () : nullptr), m_size (v
? v
->length () : 0) {}
2282 iterator
begin () { return m_base
; }
2283 iterator
end () { return m_base
+ m_size
; }
2285 const_iterator
begin () const { return m_base
; }
2286 const_iterator
end () const { return m_base
+ m_size
; }
2288 value_type
&front ();
2289 value_type
&back ();
2290 value_type
&operator[] (unsigned int i
);
2292 const value_type
&front () const;
2293 const value_type
&back () const;
2294 const value_type
&operator[] (unsigned int i
) const;
2296 size_t size () const { return m_size
; }
2297 size_t size_bytes () const { return m_size
* sizeof (T
); }
2298 bool empty () const { return m_size
== 0; }
2300 // An invalid array_slice that represents a failed operation. This is
2301 // distinct from an empty slice, which is a valid result in some contexts.
2302 static array_slice
invalid () { return { nullptr, ~0U }; }
2304 // True if the array is valid, false if it is an array like INVALID.
2305 bool is_valid () const { return m_base
|| m_size
== 0; }
2309 unsigned int m_size
;
2312 template<typename T
>
2313 inline typename array_slice
<T
>::value_type
&
2314 array_slice
<T
>::front ()
2316 gcc_checking_assert (m_size
);
2320 template<typename T
>
2321 inline const typename array_slice
<T
>::value_type
&
2322 array_slice
<T
>::front () const
2324 gcc_checking_assert (m_size
);
2328 template<typename T
>
2329 inline typename array_slice
<T
>::value_type
&
2330 array_slice
<T
>::back ()
2332 gcc_checking_assert (m_size
);
2333 return m_base
[m_size
- 1];
2336 template<typename T
>
2337 inline const typename array_slice
<T
>::value_type
&
2338 array_slice
<T
>::back () const
2340 gcc_checking_assert (m_size
);
2341 return m_base
[m_size
- 1];
2344 template<typename T
>
2345 inline typename array_slice
<T
>::value_type
&
2346 array_slice
<T
>::operator[] (unsigned int i
)
2348 gcc_checking_assert (i
< m_size
);
2352 template<typename T
>
2353 inline const typename array_slice
<T
>::value_type
&
2354 array_slice
<T
>::operator[] (unsigned int i
) const
2356 gcc_checking_assert (i
< m_size
);
2360 template<typename T
>
2362 make_array_slice (T
*base
, unsigned int size
)
2364 return array_slice
<T
> (base
, size
);
2367 #if (GCC_VERSION >= 3000)
2368 # pragma GCC poison m_vec m_vecpfx m_vecdata