Fix artificial overflow during GENERIC folding
[official-gcc.git] / gcc / vec.h
blob36918915701f0299c814224f78d63c57e0be1313
1 /* Vector API for GNU compiler.
2 Copyright (C) 2004-2023 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.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. */
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 /* 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). */
443 template<typename T>
444 void
445 debug_helper (vec<T> &ref)
447 unsigned i;
448 for (i = 0; i < ref.length (); ++i)
450 fprintf (stderr, "[%d] = ", i);
451 debug_slim (ref[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. */
461 template<typename T>
462 void
463 debug_helper (vec<T, va_gc> &ref)
465 unsigned i;
466 for (i = 0; i < ref.length (); ++i)
468 fprintf (stderr, "[%d] = ", i);
469 debug_slim (ref[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) \
489 if (ptr) \
490 debug (*ptr); \
491 else \
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) \
503 if (ptr) \
504 debug (*ptr); \
505 else \
506 fprintf (stderr, "<nil>\n"); \
509 /* Default-construct N elements in DST. */
511 template <typename T>
512 inline void
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);
529 #endif
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>
537 inline void
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. */
552 struct vnull { };
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).
571 Properties:
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>
585 public:
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 reinterpret_cast <T *> (this + 1); }
590 const T *address (void) const
591 { return reinterpret_cast <const T *> (this + 1); }
592 T *begin () { return address (); }
593 const T *begin () const { return address (); }
594 T *end () { return address () + length (); }
595 const T *end () const { return address () + length (); }
596 const T &operator[] (unsigned) const;
597 T &operator[] (unsigned);
598 T &last (void);
599 bool space (unsigned) const;
600 bool iterate (unsigned, T *) const;
601 bool iterate (unsigned, T **) const;
602 vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
603 void splice (const vec &);
604 void splice (const vec *src);
605 T *quick_push (const T &);
606 T &pop (void);
607 void truncate (unsigned);
608 void quick_insert (unsigned, const T &);
609 void ordered_remove (unsigned);
610 void unordered_remove (unsigned);
611 void block_remove (unsigned, unsigned);
612 void qsort (int (*) (const void *, const void *));
613 void sort (int (*) (const void *, const void *, void *), void *);
614 void stablesort (int (*) (const void *, const void *, void *), void *);
615 T *bsearch (const void *key, int (*compar) (const void *, const void *));
616 T *bsearch (const void *key,
617 int (*compar)(const void *, const void *, void *), void *);
618 unsigned lower_bound (const T &, bool (*) (const T &, const T &)) const;
619 bool contains (const T &search) const;
620 static size_t embedded_size (unsigned);
621 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
622 void quick_grow (unsigned len);
623 void quick_grow_cleared (unsigned len);
625 /* vec class can access our internal data and functions. */
626 template <typename, typename, typename> friend struct vec;
628 /* The allocator types also need access to our internals. */
629 friend struct va_gc;
630 friend struct va_gc_atomic;
631 friend struct va_heap;
633 /* FIXME - This field should be private, but we need to cater to
634 compilers that have stricter notions of PODness for types. */
635 /* Align m_vecpfx to simplify address (). */
636 alignas (T) alignas (vec_prefix) vec_prefix m_vecpfx;
640 /* Convenience wrapper functions to use when dealing with pointers to
641 embedded vectors. Some functionality for these vectors must be
642 provided via free functions for these reasons:
644 1- The pointer may be NULL (e.g., before initial allocation).
646 2- When the vector needs to grow, it must be reallocated, so
647 the pointer will change its value.
649 Because of limitations with the current GC machinery, all vectors
650 in GC memory *must* be pointers. */
653 /* If V contains no room for NELEMS elements, return false. Otherwise,
654 return true. */
655 template<typename T, typename A>
656 inline bool
657 vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
659 return v ? v->space (nelems) : nelems == 0;
663 /* If V is NULL, return 0. Otherwise, return V->length(). */
664 template<typename T, typename A>
665 inline unsigned
666 vec_safe_length (const vec<T, A, vl_embed> *v)
668 return v ? v->length () : 0;
672 /* If V is NULL, return NULL. Otherwise, return V->address(). */
673 template<typename T, typename A>
674 inline T *
675 vec_safe_address (vec<T, A, vl_embed> *v)
677 return v ? v->address () : NULL;
681 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
682 template<typename T, typename A>
683 inline bool
684 vec_safe_is_empty (vec<T, A, vl_embed> *v)
686 return v ? v->is_empty () : true;
689 /* If V does not have space for NELEMS elements, call
690 V->reserve(NELEMS, EXACT). */
691 template<typename T, typename A>
692 inline bool
693 vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
694 CXX_MEM_STAT_INFO)
696 bool extend = nelems ? !vec_safe_space (v, nelems) : false;
697 if (extend)
698 A::reserve (v, nelems, exact PASS_MEM_STAT);
699 return extend;
702 template<typename T, typename A>
703 inline bool
704 vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
705 CXX_MEM_STAT_INFO)
707 return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
711 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
712 is 0, V is initialized to NULL. */
714 template<typename T, typename A>
715 inline void
716 vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
718 v = NULL;
719 vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
723 /* Free the GC memory allocated by vector V and set it to NULL. */
725 template<typename T, typename A>
726 inline void
727 vec_free (vec<T, A, vl_embed> *&v)
729 A::release (v);
733 /* Grow V to length LEN. Allocate it, if necessary. */
734 template<typename T, typename A>
735 inline void
736 vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len,
737 bool exact = false CXX_MEM_STAT_INFO)
739 unsigned oldlen = vec_safe_length (v);
740 gcc_checking_assert (len >= oldlen);
741 vec_safe_reserve (v, len - oldlen, exact PASS_MEM_STAT);
742 v->quick_grow (len);
746 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
747 template<typename T, typename A>
748 inline void
749 vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len,
750 bool exact = false CXX_MEM_STAT_INFO)
752 unsigned oldlen = vec_safe_length (v);
753 vec_safe_grow (v, len, exact PASS_MEM_STAT);
754 vec_default_construct (v->address () + oldlen, len - oldlen);
758 /* Assume V is not NULL. */
760 template<typename T>
761 inline void
762 vec_safe_grow_cleared (vec<T, va_heap, vl_ptr> *&v,
763 unsigned len, bool exact = false CXX_MEM_STAT_INFO)
765 v->safe_grow_cleared (len, exact PASS_MEM_STAT);
768 /* If V does not have space for NELEMS elements, call
769 V->reserve(NELEMS, EXACT). */
771 template<typename T>
772 inline bool
773 vec_safe_reserve (vec<T, va_heap, vl_ptr> *&v, unsigned nelems, bool exact = false
774 CXX_MEM_STAT_INFO)
776 return v->reserve (nelems, exact);
780 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
781 template<typename T, typename A>
782 inline bool
783 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
785 if (v)
786 return v->iterate (ix, ptr);
787 else
789 *ptr = 0;
790 return false;
794 template<typename T, typename A>
795 inline bool
796 vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
798 if (v)
799 return v->iterate (ix, ptr);
800 else
802 *ptr = 0;
803 return false;
808 /* If V has no room for one more element, reallocate it. Then call
809 V->quick_push(OBJ). */
810 template<typename T, typename A>
811 inline T *
812 vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
814 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
815 return v->quick_push (obj);
819 /* if V has no room for one more element, reallocate it. Then call
820 V->quick_insert(IX, OBJ). */
821 template<typename T, typename A>
822 inline void
823 vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
824 CXX_MEM_STAT_INFO)
826 vec_safe_reserve (v, 1, false PASS_MEM_STAT);
827 v->quick_insert (ix, obj);
831 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
832 template<typename T, typename A>
833 inline void
834 vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
836 if (v)
837 v->truncate (size);
841 /* If SRC is not NULL, return a pointer to a copy of it. */
842 template<typename T, typename A>
843 inline vec<T, A, vl_embed> *
844 vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
846 return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
849 /* Copy the elements from SRC to the end of DST as if by memcpy.
850 Reallocate DST, if necessary. */
851 template<typename T, typename A>
852 inline void
853 vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
854 CXX_MEM_STAT_INFO)
856 unsigned src_len = vec_safe_length (src);
857 if (src_len)
859 vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
860 PASS_MEM_STAT);
861 dst->splice (*src);
865 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
866 size of the vector and so should be used with care. */
868 template<typename T, typename A>
869 inline bool
870 vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
872 return v ? v->contains (search) : false;
875 /* Index into vector. Return the IX'th element. IX must be in the
876 domain of the vector. */
878 template<typename T, typename A>
879 inline const T &
880 vec<T, A, vl_embed>::operator[] (unsigned ix) const
882 gcc_checking_assert (ix < m_vecpfx.m_num);
883 return address ()[ix];
886 template<typename T, typename A>
887 inline T &
888 vec<T, A, vl_embed>::operator[] (unsigned ix)
890 gcc_checking_assert (ix < m_vecpfx.m_num);
891 return address ()[ix];
895 /* Get the final element of the vector, which must not be empty. */
897 template<typename T, typename A>
898 inline T &
899 vec<T, A, vl_embed>::last (void)
901 gcc_checking_assert (m_vecpfx.m_num > 0);
902 return (*this)[m_vecpfx.m_num - 1];
906 /* If this vector has space for NELEMS additional entries, return
907 true. You usually only need to use this if you are doing your
908 own vector reallocation, for instance on an embedded vector. This
909 returns true in exactly the same circumstances that vec::reserve
910 will. */
912 template<typename T, typename A>
913 inline bool
914 vec<T, A, vl_embed>::space (unsigned nelems) const
916 return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
920 /* Return iteration condition and update *PTR to (a copy of) the IX'th
921 element of this vector. Use this to iterate over the elements of a
922 vector as follows,
924 for (ix = 0; v->iterate (ix, &val); ix++)
925 continue; */
927 template<typename T, typename A>
928 inline bool
929 vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
931 if (ix < m_vecpfx.m_num)
933 *ptr = address ()[ix];
934 return true;
936 else
938 *ptr = 0;
939 return false;
944 /* Return iteration condition and update *PTR to point to the
945 IX'th element of this vector. Use this to iterate over the
946 elements of a vector as follows,
948 for (ix = 0; v->iterate (ix, &ptr); ix++)
949 continue;
951 This variant is for vectors of objects. */
953 template<typename T, typename A>
954 inline bool
955 vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
957 if (ix < m_vecpfx.m_num)
959 *ptr = CONST_CAST (T *, &address ()[ix]);
960 return true;
962 else
964 *ptr = 0;
965 return false;
970 /* Return a pointer to a copy of this vector. */
972 template<typename T, typename A>
973 inline vec<T, A, vl_embed> *
974 vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
976 vec<T, A, vl_embed> *new_vec = NULL;
977 unsigned len = length ();
978 if (len)
980 vec_alloc (new_vec, len PASS_MEM_STAT);
981 new_vec->embedded_init (len, len);
982 vec_copy_construct (new_vec->address (), address (), len);
984 return new_vec;
988 /* Copy the elements from SRC to the end of this vector as if by memcpy.
989 The vector must have sufficient headroom available. */
991 template<typename T, typename A>
992 inline void
993 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
995 unsigned len = src.length ();
996 if (len)
998 gcc_checking_assert (space (len));
999 vec_copy_construct (end (), src.address (), len);
1000 m_vecpfx.m_num += len;
1004 template<typename T, typename A>
1005 inline void
1006 vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
1008 if (src)
1009 splice (*src);
1013 /* Push OBJ (a new element) onto the end of the vector. There must be
1014 sufficient space in the vector. Return a pointer to the slot
1015 where OBJ was inserted. */
1017 template<typename T, typename A>
1018 inline T *
1019 vec<T, A, vl_embed>::quick_push (const T &obj)
1021 gcc_checking_assert (space (1));
1022 T *slot = &address ()[m_vecpfx.m_num++];
1023 *slot = obj;
1024 return slot;
1028 /* Pop and return the last element off the end of the vector. */
1030 template<typename T, typename A>
1031 inline T &
1032 vec<T, A, vl_embed>::pop (void)
1034 gcc_checking_assert (length () > 0);
1035 return address ()[--m_vecpfx.m_num];
1039 /* Set the length of the vector to SIZE. The new length must be less
1040 than or equal to the current length. This is an O(1) operation. */
1042 template<typename T, typename A>
1043 inline void
1044 vec<T, A, vl_embed>::truncate (unsigned size)
1046 gcc_checking_assert (length () >= size);
1047 m_vecpfx.m_num = size;
1051 /* Insert an element, OBJ, at the IXth position of this vector. There
1052 must be sufficient space. */
1054 template<typename T, typename A>
1055 inline void
1056 vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
1058 gcc_checking_assert (length () < allocated ());
1059 gcc_checking_assert (ix <= length ());
1060 T *slot = &address ()[ix];
1061 memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
1062 *slot = obj;
1066 /* Remove an element from the IXth position of this vector. Ordering of
1067 remaining elements is preserved. This is an O(N) operation due to
1068 memmove. */
1070 template<typename T, typename A>
1071 inline void
1072 vec<T, A, vl_embed>::ordered_remove (unsigned ix)
1074 gcc_checking_assert (ix < length ());
1075 T *slot = &address ()[ix];
1076 memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
1080 /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
1081 remaining elements is preserved. This is an O(N) operation. */
1083 #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
1084 elem_ptr, start, end, cond) \
1086 gcc_assert ((end) <= (vec).length ()); \
1087 for (read_index = write_index = (start); read_index < (end); \
1088 ++read_index) \
1090 elem_ptr = &(vec)[read_index]; \
1091 bool remove_p = (cond); \
1092 if (remove_p) \
1093 continue; \
1095 if (read_index != write_index) \
1096 (vec)[write_index] = (vec)[read_index]; \
1098 write_index++; \
1101 if (read_index - write_index > 0) \
1102 (vec).block_remove (write_index, read_index - write_index); \
1106 /* Remove elements from VEC for which COND holds. Ordering of remaining
1107 elements is preserved. This is an O(N) operation. */
1109 #define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \
1110 cond) \
1111 VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
1112 elem_ptr, 0, (vec).length (), (cond))
1114 /* Remove an element from the IXth position of this vector. Ordering of
1115 remaining elements is destroyed. This is an O(1) operation. */
1117 template<typename T, typename A>
1118 inline void
1119 vec<T, A, vl_embed>::unordered_remove (unsigned ix)
1121 gcc_checking_assert (ix < length ());
1122 T *p = address ();
1123 p[ix] = p[--m_vecpfx.m_num];
1127 /* Remove LEN elements starting at the IXth. Ordering is retained.
1128 This is an O(N) operation due to memmove. */
1130 template<typename T, typename A>
1131 inline void
1132 vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
1134 gcc_checking_assert (ix + len <= length ());
1135 T *slot = &address ()[ix];
1136 m_vecpfx.m_num -= len;
1137 memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
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>::qsort (int (*cmp) (const void *, const void *))
1148 if (length () > 1)
1149 gcc_qsort (address (), length (), sizeof (T), cmp);
1152 /* Sort the contents of this vector with qsort. CMP is the comparison
1153 function to pass to qsort. */
1155 template<typename T, typename A>
1156 inline void
1157 vec<T, A, vl_embed>::sort (int (*cmp) (const void *, const void *, void *),
1158 void *data)
1160 if (length () > 1)
1161 gcc_sort_r (address (), length (), sizeof (T), cmp, data);
1164 /* Sort the contents of this vector with gcc_stablesort_r. CMP is the
1165 comparison function to pass to qsort. */
1167 template<typename T, typename A>
1168 inline void
1169 vec<T, A, vl_embed>::stablesort (int (*cmp) (const void *, const void *,
1170 void *), void *data)
1172 if (length () > 1)
1173 gcc_stablesort_r (address (), length (), sizeof (T), cmp, data);
1176 /* Search the contents of the sorted vector with a binary search.
1177 CMP is the comparison function to pass to bsearch. */
1179 template<typename T, typename A>
1180 inline T *
1181 vec<T, A, vl_embed>::bsearch (const void *key,
1182 int (*compar) (const void *, const void *))
1184 const void *base = this->address ();
1185 size_t nmemb = this->length ();
1186 size_t size = sizeof (T);
1187 /* The following is a copy of glibc stdlib-bsearch.h. */
1188 size_t l, u, idx;
1189 const void *p;
1190 int comparison;
1192 l = 0;
1193 u = nmemb;
1194 while (l < u)
1196 idx = (l + u) / 2;
1197 p = (const void *) (((const char *) base) + (idx * size));
1198 comparison = (*compar) (key, p);
1199 if (comparison < 0)
1200 u = idx;
1201 else if (comparison > 0)
1202 l = idx + 1;
1203 else
1204 return (T *)const_cast<void *>(p);
1207 return NULL;
1210 /* Search the contents of the sorted vector with a binary search.
1211 CMP is the comparison function to pass to bsearch. */
1213 template<typename T, typename A>
1214 inline T *
1215 vec<T, A, vl_embed>::bsearch (const void *key,
1216 int (*compar) (const void *, const void *,
1217 void *), void *data)
1219 const void *base = this->address ();
1220 size_t nmemb = this->length ();
1221 size_t size = sizeof (T);
1222 /* The following is a copy of glibc stdlib-bsearch.h. */
1223 size_t l, u, idx;
1224 const void *p;
1225 int comparison;
1227 l = 0;
1228 u = nmemb;
1229 while (l < u)
1231 idx = (l + u) / 2;
1232 p = (const void *) (((const char *) base) + (idx * size));
1233 comparison = (*compar) (key, p, data);
1234 if (comparison < 0)
1235 u = idx;
1236 else if (comparison > 0)
1237 l = idx + 1;
1238 else
1239 return (T *)const_cast<void *>(p);
1242 return NULL;
1245 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
1246 size of the vector and so should be used with care. */
1248 template<typename T, typename A>
1249 inline bool
1250 vec<T, A, vl_embed>::contains (const T &search) const
1252 unsigned int len = length ();
1253 const T *p = address ();
1254 for (unsigned int i = 0; i < len; i++)
1256 const T *slot = &p[i];
1257 if (*slot == search)
1258 return true;
1261 return false;
1264 /* Find and return the first position in which OBJ could be inserted
1265 without changing the ordering of this vector. LESSTHAN is a
1266 function that returns true if the first argument is strictly less
1267 than the second. */
1269 template<typename T, typename A>
1270 unsigned
1271 vec<T, A, vl_embed>::lower_bound (const T &obj,
1272 bool (*lessthan)(const T &, const T &))
1273 const
1275 unsigned int len = length ();
1276 unsigned int half, middle;
1277 unsigned int first = 0;
1278 while (len > 0)
1280 half = len / 2;
1281 middle = first;
1282 middle += half;
1283 const T &middle_elem = address ()[middle];
1284 if (lessthan (middle_elem, obj))
1286 first = middle;
1287 ++first;
1288 len = len - half - 1;
1290 else
1291 len = half;
1293 return first;
1297 /* Return the number of bytes needed to embed an instance of an
1298 embeddable vec inside another data structure.
1300 Use these methods to determine the required size and initialization
1301 of a vector V of type T embedded within another structure (as the
1302 final member):
1304 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1305 void v->embedded_init (unsigned alloc, unsigned num);
1307 These allow the caller to perform the memory allocation. */
1309 template<typename T, typename A>
1310 inline size_t
1311 vec<T, A, vl_embed>::embedded_size (unsigned alloc)
1313 struct alignas (T) U { char data[sizeof (T)]; };
1314 typedef vec<U, A, vl_embed> vec_embedded;
1315 typedef typename std::conditional<std::is_standard_layout<T>::value,
1316 vec, vec_embedded>::type vec_stdlayout;
1317 static_assert (sizeof (vec_stdlayout) == sizeof (vec), "");
1318 static_assert (alignof (vec_stdlayout) == alignof (vec), "");
1319 return sizeof (vec_stdlayout) + alloc * sizeof (T);
1323 /* Initialize the vector to contain room for ALLOC elements and
1324 NUM active elements. */
1326 template<typename T, typename A>
1327 inline void
1328 vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
1330 m_vecpfx.m_alloc = alloc;
1331 m_vecpfx.m_using_auto_storage = aut;
1332 m_vecpfx.m_num = num;
1336 /* Grow the vector to a specific length. LEN must be as long or longer than
1337 the current length. The new elements are uninitialized. */
1339 template<typename T, typename A>
1340 inline void
1341 vec<T, A, vl_embed>::quick_grow (unsigned len)
1343 gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
1344 m_vecpfx.m_num = len;
1348 /* Grow the vector to a specific length. LEN must be as long or longer than
1349 the current length. The new elements are initialized to zero. */
1351 template<typename T, typename A>
1352 inline void
1353 vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
1355 unsigned oldlen = length ();
1356 size_t growby = len - oldlen;
1357 quick_grow (len);
1358 if (growby != 0)
1359 vec_default_construct (address () + oldlen, growby);
1362 /* Garbage collection support for vec<T, A, vl_embed>. */
1364 template<typename T>
1365 void
1366 gt_ggc_mx (vec<T, va_gc> *v)
1368 extern void gt_ggc_mx (T &);
1369 for (unsigned i = 0; i < v->length (); i++)
1370 gt_ggc_mx ((*v)[i]);
1373 template<typename T>
1374 void
1375 gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
1377 /* Nothing to do. Vectors of atomic types wrt GC do not need to
1378 be traversed. */
1382 /* PCH support for vec<T, A, vl_embed>. */
1384 template<typename T, typename A>
1385 void
1386 gt_pch_nx (vec<T, A, vl_embed> *v)
1388 extern void gt_pch_nx (T &);
1389 for (unsigned i = 0; i < v->length (); i++)
1390 gt_pch_nx ((*v)[i]);
1393 template<typename T, typename A>
1394 void
1395 gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1397 for (unsigned i = 0; i < v->length (); i++)
1398 op (&((*v)[i]), NULL, cookie);
1401 template<typename T, typename A>
1402 void
1403 gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1405 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
1406 for (unsigned i = 0; i < v->length (); i++)
1407 gt_pch_nx (&((*v)[i]), op, cookie);
1411 /* Space efficient vector. These vectors can grow dynamically and are
1412 allocated together with their control data. They are suited to be
1413 included in data structures. Prior to initial allocation, they
1414 only take a single word of storage.
1416 These vectors are implemented as a pointer to an embeddable vector.
1417 The semantics allow for this pointer to be NULL to represent empty
1418 vectors. This way, empty vectors occupy minimal space in the
1419 structure containing them.
1421 Properties:
1423 - The whole vector and control data are allocated in a single
1424 contiguous block.
1425 - The whole vector may be re-allocated.
1426 - Vector data may grow and shrink.
1427 - Access and manipulation requires a pointer test and
1428 indirection.
1429 - It requires 1 word of storage (prior to vector allocation).
1432 Limitations:
1434 These vectors must be PODs because they are stored in unions.
1435 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1436 As long as we use C++03, we cannot have constructors nor
1437 destructors in classes that are stored in unions. */
1439 template<typename T, size_t N = 0>
1440 class auto_vec;
1442 template<typename T>
1443 struct vec<T, va_heap, vl_ptr>
1445 public:
1446 /* Default ctors to ensure triviality. Use value-initialization
1447 (e.g., vec() or vec v{ };) or vNULL to create a zero-initialized
1448 instance. */
1449 vec () = default;
1450 vec (const vec &) = default;
1451 /* Initialization from the generic vNULL. */
1452 vec (vnull): m_vec () { }
1453 /* Same as default ctor: vec storage must be released manually. */
1454 ~vec () = default;
1456 /* Defaulted same as copy ctor. */
1457 vec& operator= (const vec &) = default;
1459 /* Prevent implicit conversion from auto_vec. Use auto_vec::to_vec()
1460 instead. */
1461 template <size_t N>
1462 vec (auto_vec<T, N> &) = delete;
1464 template <size_t N>
1465 void operator= (auto_vec<T, N> &) = delete;
1467 /* Memory allocation and deallocation for the embedded vector.
1468 Needed because we cannot have proper ctors/dtors defined. */
1469 void create (unsigned nelems CXX_MEM_STAT_INFO);
1470 void release (void);
1472 /* Vector operations. */
1473 bool exists (void) const
1474 { return m_vec != NULL; }
1476 bool is_empty (void) const
1477 { return m_vec ? m_vec->is_empty () : true; }
1479 unsigned allocated (void) const
1480 { return m_vec ? m_vec->allocated () : 0; }
1482 unsigned length (void) const
1483 { return m_vec ? m_vec->length () : 0; }
1485 T *address (void)
1486 { return m_vec ? m_vec->address () : NULL; }
1488 const T *address (void) const
1489 { return m_vec ? m_vec->address () : NULL; }
1491 T *begin () { return address (); }
1492 const T *begin () const { return address (); }
1493 T *end () { return begin () + length (); }
1494 const T *end () const { return begin () + length (); }
1495 const T &operator[] (unsigned ix) const
1496 { return (*m_vec)[ix]; }
1498 bool operator!=(const vec &other) const
1499 { return !(*this == other); }
1501 bool operator==(const vec &other) const
1502 { return address () == other.address (); }
1504 T &operator[] (unsigned ix)
1505 { return (*m_vec)[ix]; }
1507 T &last (void)
1508 { return m_vec->last (); }
1510 bool space (int nelems) const
1511 { return m_vec ? m_vec->space (nelems) : nelems == 0; }
1513 bool iterate (unsigned ix, T *p) const;
1514 bool iterate (unsigned ix, T **p) const;
1515 vec copy (ALONE_CXX_MEM_STAT_INFO) const;
1516 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
1517 bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
1518 void splice (const vec &);
1519 void safe_splice (const vec & CXX_MEM_STAT_INFO);
1520 T *quick_push (const T &);
1521 T *safe_push (const T &CXX_MEM_STAT_INFO);
1522 T &pop (void);
1523 void truncate (unsigned);
1524 void safe_grow (unsigned, bool = false CXX_MEM_STAT_INFO);
1525 void safe_grow_cleared (unsigned, bool = false CXX_MEM_STAT_INFO);
1526 void quick_grow (unsigned);
1527 void quick_grow_cleared (unsigned);
1528 void quick_insert (unsigned, const T &);
1529 void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
1530 void ordered_remove (unsigned);
1531 void unordered_remove (unsigned);
1532 void block_remove (unsigned, unsigned);
1533 void qsort (int (*) (const void *, const void *));
1534 void sort (int (*) (const void *, const void *, void *), void *);
1535 void stablesort (int (*) (const void *, const void *, void *), void *);
1536 T *bsearch (const void *key, int (*compar)(const void *, const void *));
1537 T *bsearch (const void *key,
1538 int (*compar)(const void *, const void *, void *), void *);
1539 unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
1540 bool contains (const T &search) const;
1541 void reverse (void);
1543 bool using_auto_storage () const;
1545 /* FIXME - This field should be private, but we need to cater to
1546 compilers that have stricter notions of PODness for types. */
1547 vec<T, va_heap, vl_embed> *m_vec;
1551 /* auto_vec is a subclass of vec that automatically manages creating and
1552 releasing the internal vector. If N is non zero then it has N elements of
1553 internal storage. The default is no internal storage, and you probably only
1554 want to ask for internal storage for vectors on the stack because if the
1555 size of the vector is larger than the internal storage that space is wasted.
1557 template<typename T, size_t N /* = 0 */>
1558 class auto_vec : public vec<T, va_heap>
1560 public:
1561 auto_vec ()
1563 m_auto.embedded_init (N, 0, 1);
1564 /* ??? Instead of initializing m_vec from &m_auto directly use an
1565 expression that avoids refering to a specific member of 'this'
1566 to derail the -Wstringop-overflow diagnostic code, avoiding
1567 the impression that data accesses are supposed to be to the
1568 m_auto member storage. */
1569 size_t off = (char *) &m_auto - (char *) this;
1570 this->m_vec = (vec<T, va_heap, vl_embed> *) ((char *) this + off);
1573 auto_vec (size_t s CXX_MEM_STAT_INFO)
1575 if (s > N)
1577 this->create (s PASS_MEM_STAT);
1578 return;
1581 m_auto.embedded_init (N, 0, 1);
1582 /* ??? See above. */
1583 size_t off = (char *) &m_auto - (char *) this;
1584 this->m_vec = (vec<T, va_heap, vl_embed> *) ((char *) this + off);
1587 ~auto_vec ()
1589 this->release ();
1592 /* Explicitly convert to the base class. There is no conversion
1593 from a const auto_vec because a copy of the returned vec can
1594 be used to modify *THIS.
1595 This is a legacy function not to be used in new code. */
1596 vec<T, va_heap> to_vec_legacy () {
1597 return *static_cast<vec<T, va_heap> *>(this);
1600 private:
1601 vec<T, va_heap, vl_embed> m_auto;
1602 unsigned char m_data[sizeof (T) * N];
1605 /* auto_vec is a sub class of vec whose storage is released when it is
1606 destroyed. */
1607 template<typename T>
1608 class auto_vec<T, 0> : public vec<T, va_heap>
1610 public:
1611 auto_vec () { this->m_vec = NULL; }
1612 auto_vec (size_t n CXX_MEM_STAT_INFO) { this->create (n PASS_MEM_STAT); }
1613 ~auto_vec () { this->release (); }
1615 auto_vec (vec<T, va_heap>&& r)
1617 gcc_assert (!r.using_auto_storage ());
1618 this->m_vec = r.m_vec;
1619 r.m_vec = NULL;
1622 auto_vec (auto_vec<T> &&r)
1624 gcc_assert (!r.using_auto_storage ());
1625 this->m_vec = r.m_vec;
1626 r.m_vec = NULL;
1629 auto_vec& operator= (vec<T, va_heap>&& r)
1631 if (this == &r)
1632 return *this;
1634 gcc_assert (!r.using_auto_storage ());
1635 this->release ();
1636 this->m_vec = r.m_vec;
1637 r.m_vec = NULL;
1638 return *this;
1641 auto_vec& operator= (auto_vec<T> &&r)
1643 if (this == &r)
1644 return *this;
1646 gcc_assert (!r.using_auto_storage ());
1647 this->release ();
1648 this->m_vec = r.m_vec;
1649 r.m_vec = NULL;
1650 return *this;
1653 /* Explicitly convert to the base class. There is no conversion
1654 from a const auto_vec because a copy of the returned vec can
1655 be used to modify *THIS.
1656 This is a legacy function not to be used in new code. */
1657 vec<T, va_heap> to_vec_legacy () {
1658 return *static_cast<vec<T, va_heap> *>(this);
1661 // You probably don't want to copy a vector, so these are deleted to prevent
1662 // unintentional use. If you really need a copy of the vectors contents you
1663 // can use copy ().
1664 auto_vec(const auto_vec &) = delete;
1665 auto_vec &operator= (const auto_vec &) = delete;
1669 /* Allocate heap memory for pointer V and create the internal vector
1670 with space for NELEMS elements. If NELEMS is 0, the internal
1671 vector is initialized to empty. */
1673 template<typename T>
1674 inline void
1675 vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
1677 v = new vec<T>;
1678 v->create (nelems PASS_MEM_STAT);
1682 /* A subclass of auto_vec <char *> that frees all of its elements on
1683 deletion. */
1685 class auto_string_vec : public auto_vec <char *>
1687 public:
1688 ~auto_string_vec ();
1691 /* A subclass of auto_vec <T *> that deletes all of its elements on
1692 destruction.
1694 This is a crude way for a vec to "own" the objects it points to
1695 and clean up automatically.
1697 For example, no attempt is made to delete elements when an item
1698 within the vec is overwritten.
1700 We can't rely on gnu::unique_ptr within a container,
1701 since we can't rely on move semantics in C++98. */
1703 template <typename T>
1704 class auto_delete_vec : public auto_vec <T *>
1706 public:
1707 auto_delete_vec () {}
1708 auto_delete_vec (size_t s) : auto_vec <T *> (s) {}
1710 ~auto_delete_vec ();
1712 private:
1713 DISABLE_COPY_AND_ASSIGN(auto_delete_vec);
1716 /* Conditionally allocate heap memory for VEC and its internal vector. */
1718 template<typename T>
1719 inline void
1720 vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
1722 if (!vec)
1723 vec_alloc (vec, nelems PASS_MEM_STAT);
1727 /* Free the heap memory allocated by vector V and set it to NULL. */
1729 template<typename T>
1730 inline void
1731 vec_free (vec<T> *&v)
1733 if (v == NULL)
1734 return;
1736 v->release ();
1737 delete v;
1738 v = NULL;
1742 /* Return iteration condition and update PTR to point to the IX'th
1743 element of this vector. Use this to iterate over the elements of a
1744 vector as follows,
1746 for (ix = 0; v.iterate (ix, &ptr); ix++)
1747 continue; */
1749 template<typename T>
1750 inline bool
1751 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
1753 if (m_vec)
1754 return m_vec->iterate (ix, ptr);
1755 else
1757 *ptr = 0;
1758 return false;
1763 /* Return iteration condition and update *PTR to point to the
1764 IX'th element of this vector. Use this to iterate over the
1765 elements of a vector as follows,
1767 for (ix = 0; v->iterate (ix, &ptr); ix++)
1768 continue;
1770 This variant is for vectors of objects. */
1772 template<typename T>
1773 inline bool
1774 vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
1776 if (m_vec)
1777 return m_vec->iterate (ix, ptr);
1778 else
1780 *ptr = 0;
1781 return false;
1786 /* Convenience macro for forward iteration. */
1787 #define FOR_EACH_VEC_ELT(V, I, P) \
1788 for (I = 0; (V).iterate ((I), &(P)); ++(I))
1790 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
1791 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1793 /* Likewise, but start from FROM rather than 0. */
1794 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
1795 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1797 /* Convenience macro for reverse iteration. */
1798 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
1799 for (I = (V).length () - 1; \
1800 (V).iterate ((I), &(P)); \
1801 (I)--)
1803 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1804 for (I = vec_safe_length (V) - 1; \
1805 vec_safe_iterate ((V), (I), &(P)); \
1806 (I)--)
1808 /* auto_string_vec's dtor, freeing all contained strings, automatically
1809 chaining up to ~auto_vec <char *>, which frees the internal buffer. */
1811 inline
1812 auto_string_vec::~auto_string_vec ()
1814 int i;
1815 char *str;
1816 FOR_EACH_VEC_ELT (*this, i, str)
1817 free (str);
1820 /* auto_delete_vec's dtor, deleting all contained items, automatically
1821 chaining up to ~auto_vec <T*>, which frees the internal buffer. */
1823 template <typename T>
1824 inline
1825 auto_delete_vec<T>::~auto_delete_vec ()
1827 int i;
1828 T *item;
1829 FOR_EACH_VEC_ELT (*this, i, item)
1830 delete item;
1834 /* Return a copy of this vector. */
1836 template<typename T>
1837 inline vec<T, va_heap, vl_ptr>
1838 vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
1840 vec<T, va_heap, vl_ptr> new_vec{ };
1841 if (length ())
1842 new_vec.m_vec = m_vec->copy (ALONE_PASS_MEM_STAT);
1843 return new_vec;
1847 /* Ensure that the vector has at least RESERVE slots available (if
1848 EXACT is false), or exactly RESERVE slots available (if EXACT is
1849 true).
1851 This may create additional headroom if EXACT is false.
1853 Note that this can cause the embedded vector to be reallocated.
1854 Returns true iff reallocation actually occurred. */
1856 template<typename T>
1857 inline bool
1858 vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
1860 if (space (nelems))
1861 return false;
1863 /* For now play a game with va_heap::reserve to hide our auto storage if any,
1864 this is necessary because it doesn't have enough information to know the
1865 embedded vector is in auto storage, and so should not be freed. */
1866 vec<T, va_heap, vl_embed> *oldvec = m_vec;
1867 unsigned int oldsize = 0;
1868 bool handle_auto_vec = m_vec && using_auto_storage ();
1869 if (handle_auto_vec)
1871 m_vec = NULL;
1872 oldsize = oldvec->length ();
1873 nelems += oldsize;
1876 va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
1877 if (handle_auto_vec)
1879 vec_copy_construct (m_vec->address (), oldvec->address (), oldsize);
1880 m_vec->m_vecpfx.m_num = oldsize;
1883 return true;
1887 /* Ensure that this vector has exactly NELEMS slots available. This
1888 will not create additional headroom. Note this can cause the
1889 embedded vector to be reallocated. Returns true iff reallocation
1890 actually occurred. */
1892 template<typename T>
1893 inline bool
1894 vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
1896 return reserve (nelems, true PASS_MEM_STAT);
1900 /* Create the internal vector and reserve NELEMS for it. This is
1901 exactly like vec::reserve, but the internal vector is
1902 unconditionally allocated from scratch. The old one, if it
1903 existed, is lost. */
1905 template<typename T>
1906 inline void
1907 vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
1909 m_vec = NULL;
1910 if (nelems > 0)
1911 reserve_exact (nelems PASS_MEM_STAT);
1915 /* Free the memory occupied by the embedded vector. */
1917 template<typename T>
1918 inline void
1919 vec<T, va_heap, vl_ptr>::release (void)
1921 if (!m_vec)
1922 return;
1924 if (using_auto_storage ())
1926 m_vec->m_vecpfx.m_num = 0;
1927 return;
1930 va_heap::release (m_vec);
1933 /* Copy the elements from SRC to the end of this vector as if by memcpy.
1934 SRC and this vector must be allocated with the same memory
1935 allocation mechanism. This vector is assumed to have sufficient
1936 headroom available. */
1938 template<typename T>
1939 inline void
1940 vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
1942 if (src.length ())
1943 m_vec->splice (*(src.m_vec));
1947 /* Copy the elements in SRC to the end of this vector as if by memcpy.
1948 SRC and this vector must be allocated with the same mechanism.
1949 If there is not enough headroom in this vector, it will be reallocated
1950 as needed. */
1952 template<typename T>
1953 inline void
1954 vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
1955 MEM_STAT_DECL)
1957 if (src.length ())
1959 reserve_exact (src.length ());
1960 splice (src);
1965 /* Push OBJ (a new element) onto the end of the vector. There must be
1966 sufficient space in the vector. Return a pointer to the slot
1967 where OBJ was inserted. */
1969 template<typename T>
1970 inline T *
1971 vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
1973 return m_vec->quick_push (obj);
1977 /* Push a new element OBJ onto the end of this vector. Reallocates
1978 the embedded vector, if needed. Return a pointer to the slot where
1979 OBJ was inserted. */
1981 template<typename T>
1982 inline T *
1983 vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
1985 reserve (1, false PASS_MEM_STAT);
1986 return quick_push (obj);
1990 /* Pop and return the last element off the end of the vector. */
1992 template<typename T>
1993 inline T &
1994 vec<T, va_heap, vl_ptr>::pop (void)
1996 return m_vec->pop ();
2000 /* Set the length of the vector to LEN. The new length must be less
2001 than or equal to the current length. This is an O(1) operation. */
2003 template<typename T>
2004 inline void
2005 vec<T, va_heap, vl_ptr>::truncate (unsigned size)
2007 if (m_vec)
2008 m_vec->truncate (size);
2009 else
2010 gcc_checking_assert (size == 0);
2014 /* Grow the vector to a specific length. LEN must be as long or
2015 longer than the current length. The new elements are
2016 uninitialized. Reallocate the internal vector, if needed. */
2018 template<typename T>
2019 inline void
2020 vec<T, va_heap, vl_ptr>::safe_grow (unsigned len, bool exact MEM_STAT_DECL)
2022 unsigned oldlen = length ();
2023 gcc_checking_assert (oldlen <= len);
2024 reserve (len - oldlen, exact PASS_MEM_STAT);
2025 if (m_vec)
2026 m_vec->quick_grow (len);
2027 else
2028 gcc_checking_assert (len == 0);
2032 /* Grow the embedded vector to a specific length. LEN must be as
2033 long or longer than the current length. The new elements are
2034 initialized to zero. Reallocate the internal vector, if needed. */
2036 template<typename T>
2037 inline void
2038 vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len, bool exact
2039 MEM_STAT_DECL)
2041 unsigned oldlen = length ();
2042 size_t growby = len - oldlen;
2043 safe_grow (len, exact PASS_MEM_STAT);
2044 if (growby != 0)
2045 vec_default_construct (address () + oldlen, growby);
2049 /* Same as vec::safe_grow but without reallocation of the internal vector.
2050 If the vector cannot be extended, a runtime assertion will be triggered. */
2052 template<typename T>
2053 inline void
2054 vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
2056 gcc_checking_assert (m_vec);
2057 m_vec->quick_grow (len);
2061 /* Same as vec::quick_grow_cleared but without reallocation of the
2062 internal vector. If the vector cannot be extended, a runtime
2063 assertion will be triggered. */
2065 template<typename T>
2066 inline void
2067 vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
2069 gcc_checking_assert (m_vec);
2070 m_vec->quick_grow_cleared (len);
2074 /* Insert an element, OBJ, at the IXth position of this vector. There
2075 must be sufficient space. */
2077 template<typename T>
2078 inline void
2079 vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
2081 m_vec->quick_insert (ix, obj);
2085 /* Insert an element, OBJ, at the IXth position of the vector.
2086 Reallocate the embedded vector, if necessary. */
2088 template<typename T>
2089 inline void
2090 vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
2092 reserve (1, false PASS_MEM_STAT);
2093 quick_insert (ix, obj);
2097 /* Remove an element from the IXth position of this vector. Ordering of
2098 remaining elements is preserved. This is an O(N) operation due to
2099 a memmove. */
2101 template<typename T>
2102 inline void
2103 vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
2105 m_vec->ordered_remove (ix);
2109 /* Remove an element from the IXth position of this vector. Ordering
2110 of remaining elements is destroyed. This is an O(1) operation. */
2112 template<typename T>
2113 inline void
2114 vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
2116 m_vec->unordered_remove (ix);
2120 /* Remove LEN elements starting at the IXth. Ordering is retained.
2121 This is an O(N) operation due to memmove. */
2123 template<typename T>
2124 inline void
2125 vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
2127 m_vec->block_remove (ix, len);
2131 /* Sort the contents of this vector with qsort. CMP is the comparison
2132 function to pass to qsort. */
2134 template<typename T>
2135 inline void
2136 vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
2138 if (m_vec)
2139 m_vec->qsort (cmp);
2142 /* Sort the contents of this vector with qsort. CMP is the comparison
2143 function to pass to qsort. */
2145 template<typename T>
2146 inline void
2147 vec<T, va_heap, vl_ptr>::sort (int (*cmp) (const void *, const void *,
2148 void *), void *data)
2150 if (m_vec)
2151 m_vec->sort (cmp, data);
2154 /* Sort the contents of this vector with gcc_stablesort_r. CMP is the
2155 comparison function to pass to qsort. */
2157 template<typename T>
2158 inline void
2159 vec<T, va_heap, vl_ptr>::stablesort (int (*cmp) (const void *, const void *,
2160 void *), void *data)
2162 if (m_vec)
2163 m_vec->stablesort (cmp, data);
2166 /* Search the contents of the sorted vector with a binary search.
2167 CMP is the comparison function to pass to bsearch. */
2169 template<typename T>
2170 inline T *
2171 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
2172 int (*cmp) (const void *, const void *))
2174 if (m_vec)
2175 return m_vec->bsearch (key, cmp);
2176 return NULL;
2179 /* Search the contents of the sorted vector with a binary search.
2180 CMP is the comparison function to pass to bsearch. */
2182 template<typename T>
2183 inline T *
2184 vec<T, va_heap, vl_ptr>::bsearch (const void *key,
2185 int (*cmp) (const void *, const void *,
2186 void *), void *data)
2188 if (m_vec)
2189 return m_vec->bsearch (key, cmp, data);
2190 return NULL;
2194 /* Find and return the first position in which OBJ could be inserted
2195 without changing the ordering of this vector. LESSTHAN is a
2196 function that returns true if the first argument is strictly less
2197 than the second. */
2199 template<typename T>
2200 inline unsigned
2201 vec<T, va_heap, vl_ptr>::lower_bound (T obj,
2202 bool (*lessthan)(const T &, const T &))
2203 const
2205 return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
2208 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
2209 size of the vector and so should be used with care. */
2211 template<typename T>
2212 inline bool
2213 vec<T, va_heap, vl_ptr>::contains (const T &search) const
2215 return m_vec ? m_vec->contains (search) : false;
2218 /* Reverse content of the vector. */
2220 template<typename T>
2221 inline void
2222 vec<T, va_heap, vl_ptr>::reverse (void)
2224 unsigned l = length ();
2225 T *ptr = address ();
2227 for (unsigned i = 0; i < l / 2; i++)
2228 std::swap (ptr[i], ptr[l - i - 1]);
2231 template<typename T>
2232 inline bool
2233 vec<T, va_heap, vl_ptr>::using_auto_storage () const
2235 return m_vec ? m_vec->m_vecpfx.m_using_auto_storage : false;
2238 /* Release VEC and call release of all element vectors. */
2240 template<typename T>
2241 inline void
2242 release_vec_vec (vec<vec<T> > &vec)
2244 for (unsigned i = 0; i < vec.length (); i++)
2245 vec[i].release ();
2247 vec.release ();
2250 // Provide a subset of the std::span functionality. (We can't use std::span
2251 // itself because it's a C++20 feature.)
2253 // In addition, provide an invalid value that is distinct from all valid
2254 // sequences (including the empty sequence). This can be used to return
2255 // failure without having to use std::optional.
2257 // There is no operator bool because it would be ambiguous whether it is
2258 // testing for a valid value or an empty sequence.
2259 template<typename T>
2260 class array_slice
2262 template<typename OtherT> friend class array_slice;
2264 public:
2265 using value_type = T;
2266 using iterator = T *;
2267 using const_iterator = const T *;
2269 array_slice () : m_base (nullptr), m_size (0) {}
2271 template<typename OtherT>
2272 array_slice (array_slice<OtherT> other)
2273 : m_base (other.m_base), m_size (other.m_size) {}
2275 array_slice (iterator base, unsigned int size)
2276 : m_base (base), m_size (size) {}
2278 template<size_t N>
2279 array_slice (T (&array)[N]) : m_base (array), m_size (N) {}
2281 template<typename OtherT>
2282 array_slice (const vec<OtherT> &v)
2283 : m_base (v.address ()), m_size (v.length ()) {}
2285 template<typename OtherT>
2286 array_slice (vec<OtherT> &v)
2287 : m_base (v.address ()), m_size (v.length ()) {}
2289 template<typename OtherT>
2290 array_slice (const vec<OtherT, va_gc> *v)
2291 : m_base (v ? v->address () : nullptr), m_size (v ? v->length () : 0) {}
2293 template<typename OtherT>
2294 array_slice (vec<OtherT, va_gc> *v)
2295 : m_base (v ? v->address () : nullptr), m_size (v ? v->length () : 0) {}
2297 iterator begin () { return m_base; }
2298 iterator end () { return m_base + m_size; }
2300 const_iterator begin () const { return m_base; }
2301 const_iterator end () const { return m_base + m_size; }
2303 value_type &front ();
2304 value_type &back ();
2305 value_type &operator[] (unsigned int i);
2307 const value_type &front () const;
2308 const value_type &back () const;
2309 const value_type &operator[] (unsigned int i) const;
2311 size_t size () const { return m_size; }
2312 size_t size_bytes () const { return m_size * sizeof (T); }
2313 bool empty () const { return m_size == 0; }
2315 // An invalid array_slice that represents a failed operation. This is
2316 // distinct from an empty slice, which is a valid result in some contexts.
2317 static array_slice invalid () { return { nullptr, ~0U }; }
2319 // True if the array is valid, false if it is an array like INVALID.
2320 bool is_valid () const { return m_base || m_size == 0; }
2322 private:
2323 iterator m_base;
2324 unsigned int m_size;
2327 template<typename T>
2328 inline typename array_slice<T>::value_type &
2329 array_slice<T>::front ()
2331 gcc_checking_assert (m_size);
2332 return m_base[0];
2335 template<typename T>
2336 inline const typename array_slice<T>::value_type &
2337 array_slice<T>::front () const
2339 gcc_checking_assert (m_size);
2340 return m_base[0];
2343 template<typename T>
2344 inline typename array_slice<T>::value_type &
2345 array_slice<T>::back ()
2347 gcc_checking_assert (m_size);
2348 return m_base[m_size - 1];
2351 template<typename T>
2352 inline const typename array_slice<T>::value_type &
2353 array_slice<T>::back () const
2355 gcc_checking_assert (m_size);
2356 return m_base[m_size - 1];
2359 template<typename T>
2360 inline typename array_slice<T>::value_type &
2361 array_slice<T>::operator[] (unsigned int i)
2363 gcc_checking_assert (i < m_size);
2364 return m_base[i];
2367 template<typename T>
2368 inline const typename array_slice<T>::value_type &
2369 array_slice<T>::operator[] (unsigned int i) const
2371 gcc_checking_assert (i < m_size);
2372 return m_base[i];
2375 template<typename T>
2376 array_slice<T>
2377 make_array_slice (T *base, unsigned int size)
2379 return array_slice<T> (base, size);
2382 #if (GCC_VERSION >= 3000)
2383 # pragma GCC poison m_vec m_vecpfx m_vecdata
2384 #endif
2386 #endif // GCC_VEC_H