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
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 Not all vector operations support non-POD types and such restrictions
115 are enforced through static assertions. Some operations which often use
116 memmove to move elements around like quick_insert, safe_insert,
117 ordered_remove, unordered_remove, block_remove etc. require trivially
118 copyable types. Sorting operations, qsort, sort and stablesort, require
119 those too but as an extension allow also std::pair of 2 trivially copyable
120 types which happens to work even when std::pair itself isn't trivially
121 copyable. The quick_grow and safe_grow operations require trivially
122 default constructible types. One can use quick_grow_cleared or
123 safe_grow_cleared for non-trivially default constructible types if needed
124 (but of course such operation is more expensive then). The pop operation
125 returns reference to the last element only for trivially destructible
126 types, for non-trivially destructible types one should use last operation
127 followed by pop which in that case returns void.
128 And finally, the GC and GC atomic vectors should always be used with
129 trivially destructible types, as nothing will invoke destructors when they
132 Notes on the different layout strategies
134 * Embeddable vectors (vec<T, A, vl_embed>)
136 These vectors are suitable to be embedded in other data
137 structures so that they can be pre-allocated in a contiguous
140 Embeddable vectors are implemented using the trailing array
141 idiom, thus they are not resizeable without changing the address
142 of the vector object itself. This means you cannot have
143 variables or fields of embeddable vector type -- always use a
144 pointer to a vector. The one exception is the final field of a
145 structure, which could be a vector type.
147 You will have to use the embedded_size & embedded_init calls to
148 create such objects, and they will not be resizeable (so the
149 'safe' allocation variants are not available).
151 Properties of embeddable vectors:
153 - The whole vector and control data are allocated in a single
154 contiguous block. It uses the trailing-vector idiom, so
155 allocation must reserve enough space for all the elements
156 in the vector plus its control data.
157 - The vector cannot be re-allocated.
158 - The vector cannot grow nor shrink.
159 - No indirections needed for access/manipulation.
160 - It requires 2 words of storage (prior to vector allocation).
163 * Space efficient vector (vec<T, A, vl_ptr>)
165 These vectors can grow dynamically and are allocated together
166 with their control data. They are suited to be included in data
167 structures. Prior to initial allocation, they only take a single
170 These vectors are implemented as a pointer to embeddable vectors.
171 The semantics allow for this pointer to be NULL to represent
172 empty vectors. This way, empty vectors occupy minimal space in
173 the structure containing them.
177 - The whole vector and control data are allocated in a single
179 - The whole vector may be re-allocated.
180 - Vector data may grow and shrink.
181 - Access and manipulation requires a pointer test and
183 - It requires 1 word of storage (prior to vector allocation).
185 An example of their use would be,
188 // A space-efficient vector of tree pointers in GC memory.
189 vec<tree, va_gc, vl_ptr> v;
194 if (s->v.length ()) { we have some contents }
195 s->v.safe_push (decl); // append some decl onto the end
196 for (ix = 0; s->v.iterate (ix, &elt); ix++)
197 { do something with elt }
200 /* Support function for statistics. */
201 extern void dump_vec_loc_statistics (void);
203 /* Hashtable mapping vec addresses to descriptors. */
204 extern htab_t vec_mem_usage_hash
;
206 /* Destruct N elements in DST. */
208 template <typename T
>
210 vec_destruct (T
*dst
, unsigned n
)
212 for ( ; n
; ++dst
, --n
)
216 /* Control data for vectors. This contains the number of allocated
217 and used slots inside a vector. */
221 /* FIXME - These fields should be private, but we need to cater to
222 compilers that have stricter notions of PODness for types. */
224 /* Memory allocation support routines in vec.cc. */
225 void register_overhead (void *, size_t, size_t CXX_MEM_STAT_INFO
);
226 void release_overhead (void *, size_t, size_t, bool CXX_MEM_STAT_INFO
);
227 static unsigned calculate_allocation (vec_prefix
*, unsigned, bool);
228 static unsigned calculate_allocation_1 (unsigned, unsigned);
230 /* Note that vec_prefix should be a base class for vec, but we use
231 offsetof() on vector fields of tree structures (e.g.,
232 tree_binfo::base_binfos), and offsetof only supports base types.
234 To compensate, we make vec_prefix a field inside vec and make
235 vec a friend class of vec_prefix so it can access its fields. */
236 template <typename
, typename
, typename
> friend struct vec
;
238 /* The allocator types also need access to our internals. */
240 friend struct va_gc_atomic
;
241 friend struct va_heap
;
243 unsigned m_alloc
: 31;
244 unsigned m_using_auto_storage
: 1;
248 /* Calculate the number of slots to reserve a vector, making sure that
249 RESERVE slots are free. If EXACT grow exactly, otherwise grow
250 exponentially. PFX is the control data for the vector. */
253 vec_prefix::calculate_allocation (vec_prefix
*pfx
, unsigned reserve
,
257 return (pfx
? pfx
->m_num
: 0) + reserve
;
259 return MAX (4, reserve
);
260 return calculate_allocation_1 (pfx
->m_alloc
, pfx
->m_num
+ reserve
);
263 template<typename
, typename
, typename
> struct vec
;
265 /* Valid vector layouts
267 vl_embed - Embeddable vector that uses the trailing array idiom.
268 vl_ptr - Space efficient vector that uses a pointer to an
269 embeddable vector. */
274 /* Types of supported allocations
276 va_heap - Allocation uses malloc/free.
277 va_gc - Allocation uses ggc_alloc.
278 va_gc_atomic - Same as GC, but individual elements of the array
279 do not need to be marked during collection. */
281 /* Allocator type for heap vectors. */
284 /* Heap vectors are frequently regular instances, so use the vl_ptr
286 typedef vl_ptr default_layout
;
289 static void reserve (vec
<T
, va_heap
, vl_embed
> *&, unsigned, bool
293 static void release (vec
<T
, va_heap
, vl_embed
> *&);
297 /* Allocator for heap memory. Ensure there are at least RESERVE free
298 slots in V. If EXACT is true, grow exactly, else grow
299 exponentially. As a special case, if the vector had not been
300 allocated and RESERVE is 0, no vector will be created. */
304 va_heap::reserve (vec
<T
, va_heap
, vl_embed
> *&v
, unsigned reserve
, bool exact
307 size_t elt_size
= sizeof (T
);
309 = vec_prefix::calculate_allocation (v
? &v
->m_vecpfx
: 0, reserve
, exact
);
310 gcc_checking_assert (alloc
);
312 if (GATHER_STATISTICS
&& v
)
313 v
->m_vecpfx
.release_overhead (v
, elt_size
* v
->allocated (),
314 v
->allocated (), false);
316 size_t size
= vec
<T
, va_heap
, vl_embed
>::embedded_size (alloc
);
317 unsigned nelem
= v
? v
->length () : 0;
318 v
= static_cast <vec
<T
, va_heap
, vl_embed
> *> (xrealloc (v
, size
));
319 v
->embedded_init (alloc
, nelem
);
321 if (GATHER_STATISTICS
)
322 v
->m_vecpfx
.register_overhead (v
, alloc
, elt_size PASS_MEM_STAT
);
326 #if GCC_VERSION >= 4007
327 #pragma GCC diagnostic push
328 #pragma GCC diagnostic ignored "-Wfree-nonheap-object"
331 /* Free the heap space allocated for vector V. */
335 va_heap::release (vec
<T
, va_heap
, vl_embed
> *&v
)
337 size_t elt_size
= sizeof (T
);
341 if (!std::is_trivially_destructible
<T
>::value
)
342 vec_destruct (v
->address (), v
->length ());
344 if (GATHER_STATISTICS
)
345 v
->m_vecpfx
.release_overhead (v
, elt_size
* v
->allocated (),
346 v
->allocated (), true);
351 #if GCC_VERSION >= 4007
352 #pragma GCC diagnostic pop
355 /* Allocator type for GC vectors. Notice that we need the structure
356 declaration even if GC is not enabled. */
360 /* Use vl_embed as the default layout for GC vectors. Due to GTY
361 limitations, GC vectors must always be pointers, so it is more
362 efficient to use a pointer to the vl_embed layout, rather than
363 using a pointer to a pointer as would be the case with vl_ptr. */
364 typedef vl_embed default_layout
;
366 template<typename T
, typename A
>
367 static void reserve (vec
<T
, A
, vl_embed
> *&, unsigned, bool
370 template<typename T
, typename A
>
371 static void release (vec
<T
, A
, vl_embed
> *&v
);
375 /* Free GC memory used by V and reset V to NULL. */
377 template<typename T
, typename A
>
379 va_gc::release (vec
<T
, A
, vl_embed
> *&v
)
387 /* Allocator for GC memory. Ensure there are at least RESERVE free
388 slots in V. If EXACT is true, grow exactly, else grow
389 exponentially. As a special case, if the vector had not been
390 allocated and RESERVE is 0, no vector will be created. */
392 template<typename T
, typename A
>
394 va_gc::reserve (vec
<T
, A
, vl_embed
> *&v
, unsigned reserve
, bool exact
398 = vec_prefix::calculate_allocation (v
? &v
->m_vecpfx
: 0, reserve
, exact
);
406 /* Calculate the amount of space we want. */
407 size_t size
= vec
<T
, A
, vl_embed
>::embedded_size (alloc
);
409 /* Ask the allocator how much space it will really give us. */
410 size
= ::ggc_round_alloc_size (size
);
412 /* Adjust the number of slots accordingly. */
413 size_t vec_offset
= sizeof (vec_prefix
);
414 size_t elt_size
= sizeof (T
);
415 alloc
= (size
- vec_offset
) / elt_size
;
417 /* And finally, recalculate the amount of space we ask for. */
418 size
= vec_offset
+ alloc
* elt_size
;
420 unsigned nelem
= v
? v
->length () : 0;
421 v
= static_cast <vec
<T
, A
, vl_embed
> *> (::ggc_realloc (v
, size
423 v
->embedded_init (alloc
, nelem
);
427 /* Allocator type for GC vectors. This is for vectors of types
428 atomics w.r.t. collection, so allocation and deallocation is
429 completely inherited from va_gc. */
430 struct va_gc_atomic
: va_gc
435 /* Generic vector template. Default values for A and L indicate the
436 most commonly used strategies.
438 FIXME - Ideally, they would all be vl_ptr to encourage using regular
439 instances for vectors, but the existing GTY machinery is limited
440 in that it can only deal with GC objects that are pointers
443 This means that vector operations that need to deal with
444 potentially NULL pointers, must be provided as free
445 functions (see the vec_safe_* functions above). */
447 typename A
= va_heap
,
448 typename L
= typename
A::default_layout
>
449 struct GTY((user
)) vec
453 /* Allow C++11 range-based 'for' to work directly on vec<T>*. */
454 template<typename T
, typename A
, typename L
>
455 T
* begin (vec
<T
,A
,L
> *v
) { return v
? v
->begin () : nullptr; }
456 template<typename T
, typename A
, typename L
>
457 T
* end (vec
<T
,A
,L
> *v
) { return v
? v
->end () : nullptr; }
458 template<typename T
, typename A
, typename L
>
459 const T
* begin (const vec
<T
,A
,L
> *v
) { return v
? v
->begin () : nullptr; }
460 template<typename T
, typename A
, typename L
>
461 const T
* end (const vec
<T
,A
,L
> *v
) { return v
? v
->end () : nullptr; }
463 /* Generic vec<> debug helpers.
465 These need to be instantiated for each vec<TYPE> used throughout
466 the compiler like this:
468 DEFINE_DEBUG_VEC (TYPE)
470 The reason we have a debug_helper() is because GDB can't
471 disambiguate a plain call to debug(some_vec), and it must be called
472 like debug<TYPE>(some_vec). */
476 debug_helper (vec
<T
> &ref
)
479 for (i
= 0; i
< ref
.length (); ++i
)
481 fprintf (stderr
, "[%d] = ", i
);
483 fputc ('\n', stderr
);
487 /* We need a separate va_gc variant here because default template
488 argument for functions cannot be used in c++-98. Once this
489 restriction is removed, those variant should be folded with the
490 above debug_helper. */
494 debug_helper (vec
<T
, va_gc
> &ref
)
497 for (i
= 0; i
< ref
.length (); ++i
)
499 fprintf (stderr
, "[%d] = ", i
);
501 fputc ('\n', stderr
);
505 /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
506 functions for a type T. */
508 #define DEFINE_DEBUG_VEC(T) \
509 template void debug_helper (vec<T> &); \
510 template void debug_helper (vec<T, va_gc> &); \
511 /* Define the vec<T> debug functions. */ \
512 DEBUG_FUNCTION void \
513 debug (vec<T> &ref) \
515 debug_helper <T> (ref); \
517 DEBUG_FUNCTION void \
518 debug (vec<T> *ptr) \
523 fprintf (stderr, "<nil>\n"); \
525 /* Define the vec<T, va_gc> debug functions. */ \
526 DEBUG_FUNCTION void \
527 debug (vec<T, va_gc> &ref) \
529 debug_helper <T> (ref); \
531 DEBUG_FUNCTION void \
532 debug (vec<T, va_gc> *ptr) \
537 fprintf (stderr, "<nil>\n"); \
540 /* Default-construct N elements in DST. */
542 template <typename T
>
544 vec_default_construct (T
*dst
, unsigned n
)
546 for ( ; n
; ++dst
, --n
)
547 ::new (static_cast<void*>(dst
)) T ();
550 /* Copy-construct N elements in DST from *SRC. */
552 template <typename T
>
554 vec_copy_construct (T
*dst
, const T
*src
, unsigned n
)
556 for ( ; n
; ++dst
, ++src
, --n
)
557 ::new (static_cast<void*>(dst
)) T (*src
);
560 /* Type to provide zero-initialized values for vec<T, A, L>. This is
561 used to provide nil initializers for vec instances. Since vec must
562 be a trivially copyable type that can be copied by memcpy and zeroed
563 out by memset, it must have defaulted default and copy ctor and copy
564 assignment. To initialize a vec either use value initialization
565 (e.g., vec() or vec v{ };) or assign it the value vNULL. This isn't
566 needed for file-scope and function-local static vectors, which are
567 zero-initialized by default. */
569 constexpr vnull vNULL
{ };
572 /* Embeddable vector. These vectors are suitable to be embedded
573 in other data structures so that they can be pre-allocated in a
574 contiguous memory block.
576 Embeddable vectors are implemented using the trailing array idiom,
577 thus they are not resizeable without changing the address of the
578 vector object itself. This means you cannot have variables or
579 fields of embeddable vector type -- always use a pointer to a
580 vector. The one exception is the final field of a structure, which
581 could be a vector type.
583 You will have to use the embedded_size & embedded_init calls to
584 create such objects, and they will not be resizeable (so the 'safe'
585 allocation variants are not available).
589 - The whole vector and control data are allocated in a single
590 contiguous block. It uses the trailing-vector idiom, so
591 allocation must reserve enough space for all the elements
592 in the vector plus its control data.
593 - The vector cannot be re-allocated.
594 - The vector cannot grow nor shrink.
595 - No indirections needed for access/manipulation.
596 - It requires 2 words of storage (prior to vector allocation). */
598 template<typename T
, typename A
>
599 struct GTY((user
)) vec
<T
, A
, vl_embed
>
602 unsigned allocated (void) const { return m_vecpfx
.m_alloc
; }
603 unsigned length (void) const { return m_vecpfx
.m_num
; }
604 bool is_empty (void) const { return m_vecpfx
.m_num
== 0; }
605 T
*address (void) { return reinterpret_cast <T
*> (this + 1); }
606 const T
*address (void) const
607 { return reinterpret_cast <const T
*> (this + 1); }
608 T
*begin () { return address (); }
609 const T
*begin () const { return address (); }
610 T
*end () { return address () + length (); }
611 const T
*end () const { return address () + length (); }
612 const T
&operator[] (unsigned) const;
613 T
&operator[] (unsigned);
615 bool space (unsigned) const;
616 bool iterate (unsigned, T
*) const;
617 bool iterate (unsigned, T
**) const;
618 vec
*copy (ALONE_CXX_MEM_STAT_INFO
) const;
619 void splice (const vec
&);
620 void splice (const vec
*src
);
621 T
*quick_push (const T
&);
623 = typename
std::conditional
<std::is_trivially_destructible
<T
>::value
,
625 pop_ret_type
pop (void);
626 void truncate (unsigned);
627 void quick_insert (unsigned, const T
&);
628 void ordered_remove (unsigned);
629 void unordered_remove (unsigned);
630 void block_remove (unsigned, unsigned);
631 void qsort (int (*) (const void *, const void *));
632 void sort (int (*) (const void *, const void *, void *), void *);
633 void stablesort (int (*) (const void *, const void *, void *), void *);
634 T
*bsearch (const void *key
, int (*compar
) (const void *, const void *));
635 T
*bsearch (const void *key
,
636 int (*compar
)(const void *, const void *, void *), void *);
637 unsigned lower_bound (const T
&, bool (*) (const T
&, const T
&)) const;
638 bool contains (const T
&search
) const;
639 static size_t embedded_size (unsigned);
640 void embedded_init (unsigned, unsigned = 0, unsigned = 0);
641 void quick_grow (unsigned len
);
642 void quick_grow_cleared (unsigned len
);
644 /* vec class can access our internal data and functions. */
645 template <typename
, typename
, typename
> friend struct vec
;
647 /* The allocator types also need access to our internals. */
649 friend struct va_gc_atomic
;
650 friend struct va_heap
;
652 /* FIXME - This field should be private, but we need to cater to
653 compilers that have stricter notions of PODness for types. */
654 /* Align m_vecpfx to simplify address (). */
655 alignas (T
) alignas (vec_prefix
) vec_prefix m_vecpfx
;
659 /* Convenience wrapper functions to use when dealing with pointers to
660 embedded vectors. Some functionality for these vectors must be
661 provided via free functions for these reasons:
663 1- The pointer may be NULL (e.g., before initial allocation).
665 2- When the vector needs to grow, it must be reallocated, so
666 the pointer will change its value.
668 Because of limitations with the current GC machinery, all vectors
669 in GC memory *must* be pointers. */
672 /* If V contains no room for NELEMS elements, return false. Otherwise,
674 template<typename T
, typename A
>
676 vec_safe_space (const vec
<T
, A
, vl_embed
> *v
, unsigned nelems
)
678 return v
? v
->space (nelems
) : nelems
== 0;
682 /* If V is NULL, return 0. Otherwise, return V->length(). */
683 template<typename T
, typename A
>
685 vec_safe_length (const vec
<T
, A
, vl_embed
> *v
)
687 return v
? v
->length () : 0;
691 /* If V is NULL, return NULL. Otherwise, return V->address(). */
692 template<typename T
, typename A
>
694 vec_safe_address (vec
<T
, A
, vl_embed
> *v
)
696 return v
? v
->address () : NULL
;
700 /* If V is NULL, return true. Otherwise, return V->is_empty(). */
701 template<typename T
, typename A
>
703 vec_safe_is_empty (vec
<T
, A
, vl_embed
> *v
)
705 return v
? v
->is_empty () : true;
708 /* If V does not have space for NELEMS elements, call
709 V->reserve(NELEMS, EXACT). */
710 template<typename T
, typename A
>
712 vec_safe_reserve (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems
, bool exact
= false
715 bool extend
= nelems
? !vec_safe_space (v
, nelems
) : false;
717 A::reserve (v
, nelems
, exact PASS_MEM_STAT
);
721 template<typename T
, typename A
>
723 vec_safe_reserve_exact (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems
726 return vec_safe_reserve (v
, nelems
, true PASS_MEM_STAT
);
730 /* Allocate GC memory for V with space for NELEMS slots. If NELEMS
731 is 0, V is initialized to NULL. */
733 template<typename T
, typename A
>
735 vec_alloc (vec
<T
, A
, vl_embed
> *&v
, unsigned nelems CXX_MEM_STAT_INFO
)
738 vec_safe_reserve (v
, nelems
, false PASS_MEM_STAT
);
742 /* Free the GC memory allocated by vector V and set it to NULL. */
744 template<typename T
, typename A
>
746 vec_free (vec
<T
, A
, vl_embed
> *&v
)
752 /* Grow V to length LEN. Allocate it, if necessary. */
753 template<typename T
, typename A
>
755 vec_safe_grow (vec
<T
, A
, vl_embed
> *&v
, unsigned len
,
756 bool exact
= false CXX_MEM_STAT_INFO
)
758 unsigned oldlen
= vec_safe_length (v
);
759 gcc_checking_assert (len
>= oldlen
);
760 vec_safe_reserve (v
, len
- oldlen
, exact PASS_MEM_STAT
);
765 /* If V is NULL, allocate it. Call V->safe_grow_cleared(LEN). */
766 template<typename T
, typename A
>
768 vec_safe_grow_cleared (vec
<T
, A
, vl_embed
> *&v
, unsigned len
,
769 bool exact
= false CXX_MEM_STAT_INFO
)
771 unsigned oldlen
= vec_safe_length (v
);
772 gcc_checking_assert (len
>= oldlen
);
773 vec_safe_reserve (v
, len
- oldlen
, exact PASS_MEM_STAT
);
774 v
->quick_grow_cleared (len
);
778 /* Assume V is not NULL. */
782 vec_safe_grow_cleared (vec
<T
, va_heap
, vl_ptr
> *&v
,
783 unsigned len
, bool exact
= false CXX_MEM_STAT_INFO
)
785 v
->safe_grow_cleared (len
, exact PASS_MEM_STAT
);
788 /* If V does not have space for NELEMS elements, call
789 V->reserve(NELEMS, EXACT). */
793 vec_safe_reserve (vec
<T
, va_heap
, vl_ptr
> *&v
, unsigned nelems
, bool exact
= false
796 return v
->reserve (nelems
, exact
);
800 /* If V is NULL return false, otherwise return V->iterate(IX, PTR). */
801 template<typename T
, typename A
>
803 vec_safe_iterate (const vec
<T
, A
, vl_embed
> *v
, unsigned ix
, T
**ptr
)
806 return v
->iterate (ix
, ptr
);
814 template<typename T
, typename A
>
816 vec_safe_iterate (const vec
<T
, A
, vl_embed
> *v
, unsigned ix
, T
*ptr
)
819 return v
->iterate (ix
, ptr
);
828 /* If V has no room for one more element, reallocate it. Then call
829 V->quick_push(OBJ). */
830 template<typename T
, typename A
>
832 vec_safe_push (vec
<T
, A
, vl_embed
> *&v
, const T
&obj CXX_MEM_STAT_INFO
)
834 vec_safe_reserve (v
, 1, false PASS_MEM_STAT
);
835 return v
->quick_push (obj
);
839 /* if V has no room for one more element, reallocate it. Then call
840 V->quick_insert(IX, OBJ). */
841 template<typename T
, typename A
>
843 vec_safe_insert (vec
<T
, A
, vl_embed
> *&v
, unsigned ix
, const T
&obj
846 vec_safe_reserve (v
, 1, false PASS_MEM_STAT
);
847 v
->quick_insert (ix
, obj
);
851 /* If V is NULL, do nothing. Otherwise, call V->truncate(SIZE). */
852 template<typename T
, typename A
>
854 vec_safe_truncate (vec
<T
, A
, vl_embed
> *v
, unsigned size
)
861 /* If SRC is not NULL, return a pointer to a copy of it. */
862 template<typename T
, typename A
>
863 inline vec
<T
, A
, vl_embed
> *
864 vec_safe_copy (vec
<T
, A
, vl_embed
> *src CXX_MEM_STAT_INFO
)
866 return src
? src
->copy (ALONE_PASS_MEM_STAT
) : NULL
;
869 /* Copy the elements from SRC to the end of DST as if by memcpy.
870 Reallocate DST, if necessary. */
871 template<typename T
, typename A
>
873 vec_safe_splice (vec
<T
, A
, vl_embed
> *&dst
, const vec
<T
, A
, vl_embed
> *src
876 unsigned src_len
= vec_safe_length (src
);
879 vec_safe_reserve_exact (dst
, vec_safe_length (dst
) + src_len
885 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
886 size of the vector and so should be used with care. */
888 template<typename T
, typename A
>
890 vec_safe_contains (vec
<T
, A
, vl_embed
> *v
, const T
&search
)
892 return v
? v
->contains (search
) : false;
895 /* Index into vector. Return the IX'th element. IX must be in the
896 domain of the vector. */
898 template<typename T
, typename A
>
900 vec
<T
, A
, vl_embed
>::operator[] (unsigned ix
) const
902 gcc_checking_assert (ix
< m_vecpfx
.m_num
);
903 return address ()[ix
];
906 template<typename T
, typename A
>
908 vec
<T
, A
, vl_embed
>::operator[] (unsigned ix
)
910 gcc_checking_assert (ix
< m_vecpfx
.m_num
);
911 return address ()[ix
];
915 /* Get the final element of the vector, which must not be empty. */
917 template<typename T
, typename A
>
919 vec
<T
, A
, vl_embed
>::last (void)
921 gcc_checking_assert (m_vecpfx
.m_num
> 0);
922 return (*this)[m_vecpfx
.m_num
- 1];
926 /* If this vector has space for NELEMS additional entries, return
927 true. You usually only need to use this if you are doing your
928 own vector reallocation, for instance on an embedded vector. This
929 returns true in exactly the same circumstances that vec::reserve
932 template<typename T
, typename A
>
934 vec
<T
, A
, vl_embed
>::space (unsigned nelems
) const
936 return m_vecpfx
.m_alloc
- m_vecpfx
.m_num
>= nelems
;
940 /* Return iteration condition and update *PTR to (a copy of) the IX'th
941 element of this vector. Use this to iterate over the elements of a
944 for (ix = 0; v->iterate (ix, &val); ix++)
947 template<typename T
, typename A
>
949 vec
<T
, A
, vl_embed
>::iterate (unsigned ix
, T
*ptr
) const
951 if (ix
< m_vecpfx
.m_num
)
953 *ptr
= address ()[ix
];
964 /* Return iteration condition and update *PTR to point to the
965 IX'th element of this vector. Use this to iterate over the
966 elements of a vector as follows,
968 for (ix = 0; v->iterate (ix, &ptr); ix++)
971 This variant is for vectors of objects. */
973 template<typename T
, typename A
>
975 vec
<T
, A
, vl_embed
>::iterate (unsigned ix
, T
**ptr
) const
977 if (ix
< m_vecpfx
.m_num
)
979 *ptr
= CONST_CAST (T
*, &address ()[ix
]);
990 /* Return a pointer to a copy of this vector. */
992 template<typename T
, typename A
>
993 inline vec
<T
, A
, vl_embed
> *
994 vec
<T
, A
, vl_embed
>::copy (ALONE_MEM_STAT_DECL
) const
996 vec
<T
, A
, vl_embed
> *new_vec
= NULL
;
997 unsigned len
= length ();
1000 vec_alloc (new_vec
, len PASS_MEM_STAT
);
1001 new_vec
->embedded_init (len
, len
);
1002 vec_copy_construct (new_vec
->address (), address (), len
);
1008 /* Copy the elements from SRC to the end of this vector as if by memcpy.
1009 The vector must have sufficient headroom available. */
1011 template<typename T
, typename A
>
1013 vec
<T
, A
, vl_embed
>::splice (const vec
<T
, A
, vl_embed
> &src
)
1015 unsigned len
= src
.length ();
1018 gcc_checking_assert (space (len
));
1019 vec_copy_construct (end (), src
.address (), len
);
1020 m_vecpfx
.m_num
+= len
;
1024 template<typename T
, typename A
>
1026 vec
<T
, A
, vl_embed
>::splice (const vec
<T
, A
, vl_embed
> *src
)
1033 /* Push OBJ (a new element) onto the end of the vector. There must be
1034 sufficient space in the vector. Return a pointer to the slot
1035 where OBJ was inserted. */
1037 template<typename T
, typename A
>
1039 vec
<T
, A
, vl_embed
>::quick_push (const T
&obj
)
1041 gcc_checking_assert (space (1));
1042 T
*slot
= &address ()[m_vecpfx
.m_num
++];
1043 ::new (static_cast<void*>(slot
)) T (obj
);
1048 /* Pop and return a reference to the last element off the end of the
1049 vector. If T has non-trivial destructor, this method just pops
1050 the element and returns void type. */
1052 template<typename T
, typename A
>
1053 inline typename vec
<T
, A
, vl_embed
>::pop_ret_type
1054 vec
<T
, A
, vl_embed
>::pop (void)
1056 gcc_checking_assert (length () > 0);
1057 T
&last
= address ()[--m_vecpfx
.m_num
];
1058 if (!std::is_trivially_destructible
<T
>::value
)
1060 return static_cast <pop_ret_type
> (last
);
1064 /* Set the length of the vector to SIZE. The new length must be less
1065 than or equal to the current length. This is an O(1) operation. */
1067 template<typename T
, typename A
>
1069 vec
<T
, A
, vl_embed
>::truncate (unsigned size
)
1071 unsigned l
= length ();
1072 gcc_checking_assert (l
>= size
);
1073 if (!std::is_trivially_destructible
<T
>::value
)
1074 vec_destruct (address () + size
, l
- size
);
1075 m_vecpfx
.m_num
= size
;
1079 /* Insert an element, OBJ, at the IXth position of this vector. There
1080 must be sufficient space. This operation is not suitable for non-trivially
1083 template<typename T
, typename A
>
1085 vec
<T
, A
, vl_embed
>::quick_insert (unsigned ix
, const T
&obj
)
1087 gcc_checking_assert (length () < allocated ());
1088 gcc_checking_assert (ix
<= length ());
1089 #if GCC_VERSION >= 5000
1090 /* GCC 4.8 and 4.9 only implement std::is_trivially_destructible,
1091 but not std::is_trivially_copyable nor
1092 std::is_trivially_default_constructible. */
1093 static_assert (std::is_trivially_copyable
<T
>::value
, "");
1095 T
*slot
= &address ()[ix
];
1096 memmove (slot
+ 1, slot
, (m_vecpfx
.m_num
++ - ix
) * sizeof (T
));
1101 /* Remove an element from the IXth position of this vector. Ordering of
1102 remaining elements is preserved. This is an O(N) operation due to
1103 memmove. Not suitable for non-trivially copyable types. */
1105 template<typename T
, typename A
>
1107 vec
<T
, A
, vl_embed
>::ordered_remove (unsigned ix
)
1109 gcc_checking_assert (ix
< length ());
1110 #if GCC_VERSION >= 5000
1111 static_assert (std::is_trivially_copyable
<T
>::value
, "");
1113 T
*slot
= &address ()[ix
];
1114 memmove (slot
, slot
+ 1, (--m_vecpfx
.m_num
- ix
) * sizeof (T
));
1118 /* Remove elements in [START, END) from VEC for which COND holds. Ordering of
1119 remaining elements is preserved. This is an O(N) operation. */
1121 #define VEC_ORDERED_REMOVE_IF_FROM_TO(vec, read_index, write_index, \
1122 elem_ptr, start, end, cond) \
1124 gcc_assert ((end) <= (vec).length ()); \
1125 for (read_index = write_index = (start); read_index < (end); \
1128 elem_ptr = &(vec)[read_index]; \
1129 bool remove_p = (cond); \
1133 if (read_index != write_index) \
1134 (vec)[write_index] = (vec)[read_index]; \
1139 if (read_index - write_index > 0) \
1140 (vec).block_remove (write_index, read_index - write_index); \
1144 /* Remove elements from VEC for which COND holds. Ordering of remaining
1145 elements is preserved. This is an O(N) operation. */
1147 #define VEC_ORDERED_REMOVE_IF(vec, read_index, write_index, elem_ptr, \
1149 VEC_ORDERED_REMOVE_IF_FROM_TO ((vec), read_index, write_index, \
1150 elem_ptr, 0, (vec).length (), (cond))
1152 /* Remove an element from the IXth position of this vector. Ordering of
1153 remaining elements is destroyed. This is an O(1) operation. */
1155 template<typename T
, typename A
>
1157 vec
<T
, A
, vl_embed
>::unordered_remove (unsigned ix
)
1159 gcc_checking_assert (ix
< length ());
1160 #if GCC_VERSION >= 5000
1161 static_assert (std::is_trivially_copyable
<T
>::value
, "");
1164 p
[ix
] = p
[--m_vecpfx
.m_num
];
1168 /* Remove LEN elements starting at the IXth. Ordering is retained.
1169 This is an O(N) operation due to memmove. */
1171 template<typename T
, typename A
>
1173 vec
<T
, A
, vl_embed
>::block_remove (unsigned ix
, unsigned len
)
1175 gcc_checking_assert (ix
+ len
<= length ());
1176 #if GCC_VERSION >= 5000
1177 static_assert (std::is_trivially_copyable
<T
>::value
, "");
1179 T
*slot
= &address ()[ix
];
1180 m_vecpfx
.m_num
-= len
;
1181 memmove (slot
, slot
+ len
, (m_vecpfx
.m_num
- ix
) * sizeof (T
));
1185 #if GCC_VERSION >= 5000
1186 namespace vec_detail
1188 /* gcc_{qsort,qsort_r,stablesort_r} implementation under the hood
1189 uses memcpy/memmove to reorder the array elements.
1190 We want to assert these methods aren't used on types for which
1191 that isn't appropriate, but unfortunately std::pair of 2 trivially
1192 copyable types isn't trivially copyable and we use qsort on many
1193 such std::pair instantiations. Let's allow both trivially
1194 copyable types and std::pair of 2 trivially copyable types as
1195 exception for qsort/sort/stablesort. */
1196 template<typename T
>
1197 struct is_trivially_copyable_or_pair
: std::is_trivially_copyable
<T
> { };
1199 template<typename T
, typename U
>
1200 struct is_trivially_copyable_or_pair
<std::pair
<T
, U
> >
1201 : std::integral_constant
<bool, std::is_trivially_copyable
<T
>::value
1202 && std::is_trivially_copyable
<U
>::value
> { };
1206 /* Sort the contents of this vector with qsort. CMP is the comparison
1207 function to pass to qsort. */
1209 template<typename T
, typename A
>
1211 vec
<T
, A
, vl_embed
>::qsort (int (*cmp
) (const void *, const void *))
1213 #if GCC_VERSION >= 5000
1214 static_assert (vec_detail::is_trivially_copyable_or_pair
<T
>::value
, "");
1217 gcc_qsort (address (), length (), sizeof (T
), cmp
);
1220 /* Sort the contents of this vector with qsort. CMP is the comparison
1221 function to pass to qsort. */
1223 template<typename T
, typename A
>
1225 vec
<T
, A
, vl_embed
>::sort (int (*cmp
) (const void *, const void *, void *),
1228 #if GCC_VERSION >= 5000
1229 static_assert (vec_detail::is_trivially_copyable_or_pair
<T
>::value
, "");
1232 gcc_sort_r (address (), length (), sizeof (T
), cmp
, data
);
1235 /* Sort the contents of this vector with gcc_stablesort_r. CMP is the
1236 comparison function to pass to qsort. */
1238 template<typename T
, typename A
>
1240 vec
<T
, A
, vl_embed
>::stablesort (int (*cmp
) (const void *, const void *,
1241 void *), void *data
)
1243 #if GCC_VERSION >= 5000
1244 static_assert (vec_detail::is_trivially_copyable_or_pair
<T
>::value
, "");
1247 gcc_stablesort_r (address (), length (), sizeof (T
), cmp
, data
);
1250 /* Search the contents of the sorted vector with a binary search.
1251 CMP is the comparison function to pass to bsearch. */
1253 template<typename T
, typename A
>
1255 vec
<T
, A
, vl_embed
>::bsearch (const void *key
,
1256 int (*compar
) (const void *, const void *))
1258 const void *base
= this->address ();
1259 size_t nmemb
= this->length ();
1260 size_t size
= sizeof (T
);
1261 /* The following is a copy of glibc stdlib-bsearch.h. */
1271 p
= (const void *) (((const char *) base
) + (idx
* size
));
1272 comparison
= (*compar
) (key
, p
);
1275 else if (comparison
> 0)
1278 return (T
*)const_cast<void *>(p
);
1284 /* Search the contents of the sorted vector with a binary search.
1285 CMP is the comparison function to pass to bsearch. */
1287 template<typename T
, typename A
>
1289 vec
<T
, A
, vl_embed
>::bsearch (const void *key
,
1290 int (*compar
) (const void *, const void *,
1291 void *), void *data
)
1293 const void *base
= this->address ();
1294 size_t nmemb
= this->length ();
1295 size_t size
= sizeof (T
);
1296 /* The following is a copy of glibc stdlib-bsearch.h. */
1306 p
= (const void *) (((const char *) base
) + (idx
* size
));
1307 comparison
= (*compar
) (key
, p
, data
);
1310 else if (comparison
> 0)
1313 return (T
*)const_cast<void *>(p
);
1319 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
1320 size of the vector and so should be used with care. */
1322 template<typename T
, typename A
>
1324 vec
<T
, A
, vl_embed
>::contains (const T
&search
) const
1326 unsigned int len
= length ();
1327 const T
*p
= address ();
1328 for (unsigned int i
= 0; i
< len
; i
++)
1330 const T
*slot
= &p
[i
];
1331 if (*slot
== search
)
1338 /* Find and return the first position in which OBJ could be inserted
1339 without changing the ordering of this vector. LESSTHAN is a
1340 function that returns true if the first argument is strictly less
1343 template<typename T
, typename A
>
1345 vec
<T
, A
, vl_embed
>::lower_bound (const T
&obj
,
1346 bool (*lessthan
)(const T
&, const T
&))
1349 unsigned int len
= length ();
1350 unsigned int half
, middle
;
1351 unsigned int first
= 0;
1357 const T
&middle_elem
= address ()[middle
];
1358 if (lessthan (middle_elem
, obj
))
1362 len
= len
- half
- 1;
1371 /* Return the number of bytes needed to embed an instance of an
1372 embeddable vec inside another data structure.
1374 Use these methods to determine the required size and initialization
1375 of a vector V of type T embedded within another structure (as the
1378 size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1379 void v->embedded_init (unsigned alloc, unsigned num);
1381 These allow the caller to perform the memory allocation. */
1383 template<typename T
, typename A
>
1385 vec
<T
, A
, vl_embed
>::embedded_size (unsigned alloc
)
1387 struct alignas (T
) U
{ char data
[sizeof (T
)]; };
1388 typedef vec
<U
, A
, vl_embed
> vec_embedded
;
1389 typedef typename
std::conditional
<std::is_standard_layout
<T
>::value
,
1390 vec
, vec_embedded
>::type vec_stdlayout
;
1391 static_assert (sizeof (vec_stdlayout
) == sizeof (vec
), "");
1392 static_assert (alignof (vec_stdlayout
) == alignof (vec
), "");
1393 return sizeof (vec_stdlayout
) + alloc
* sizeof (T
);
1397 /* Initialize the vector to contain room for ALLOC elements and
1398 NUM active elements. */
1400 template<typename T
, typename A
>
1402 vec
<T
, A
, vl_embed
>::embedded_init (unsigned alloc
, unsigned num
, unsigned aut
)
1404 m_vecpfx
.m_alloc
= alloc
;
1405 m_vecpfx
.m_using_auto_storage
= aut
;
1406 m_vecpfx
.m_num
= num
;
1410 /* Grow the vector to a specific length. LEN must be as long or longer than
1411 the current length. The new elements are uninitialized. */
1413 template<typename T
, typename A
>
1415 vec
<T
, A
, vl_embed
>::quick_grow (unsigned len
)
1417 gcc_checking_assert (length () <= len
&& len
<= m_vecpfx
.m_alloc
);
1418 #if GCC_VERSION >= 5000
1419 static_assert (std::is_trivially_default_constructible
<T
>::value
, "");
1421 m_vecpfx
.m_num
= len
;
1425 /* Grow the vector to a specific length. LEN must be as long or longer than
1426 the current length. The new elements are initialized to zero. */
1428 template<typename T
, typename A
>
1430 vec
<T
, A
, vl_embed
>::quick_grow_cleared (unsigned len
)
1432 unsigned oldlen
= length ();
1433 size_t growby
= len
- oldlen
;
1434 gcc_checking_assert (length () <= len
&& len
<= m_vecpfx
.m_alloc
);
1435 m_vecpfx
.m_num
= len
;
1437 vec_default_construct (address () + oldlen
, growby
);
1440 /* Garbage collection support for vec<T, A, vl_embed>. */
1442 template<typename T
>
1444 gt_ggc_mx (vec
<T
, va_gc
> *v
)
1446 static_assert (std::is_trivially_destructible
<T
>::value
, "");
1447 extern void gt_ggc_mx (T
&);
1448 for (unsigned i
= 0; i
< v
->length (); i
++)
1449 gt_ggc_mx ((*v
)[i
]);
1452 template<typename T
>
1454 gt_ggc_mx (vec
<T
, va_gc_atomic
, vl_embed
> *v ATTRIBUTE_UNUSED
)
1456 static_assert (std::is_trivially_destructible
<T
>::value
, "");
1457 /* Nothing to do. Vectors of atomic types wrt GC do not need to
1462 /* PCH support for vec<T, A, vl_embed>. */
1464 template<typename T
, typename A
>
1466 gt_pch_nx (vec
<T
, A
, vl_embed
> *v
)
1468 extern void gt_pch_nx (T
&);
1469 for (unsigned i
= 0; i
< v
->length (); i
++)
1470 gt_pch_nx ((*v
)[i
]);
1473 template<typename T
>
1475 gt_pch_nx (vec
<T
, va_gc_atomic
, vl_embed
> *)
1477 /* No pointers to note. */
1480 template<typename T
, typename A
>
1482 gt_pch_nx (vec
<T
*, A
, vl_embed
> *v
, gt_pointer_operator op
, void *cookie
)
1484 for (unsigned i
= 0; i
< v
->length (); i
++)
1485 op (&((*v
)[i
]), NULL
, cookie
);
1488 template<typename T
, typename A
>
1490 gt_pch_nx (vec
<T
, A
, vl_embed
> *v
, gt_pointer_operator op
, void *cookie
)
1492 extern void gt_pch_nx (T
*, gt_pointer_operator
, void *);
1493 for (unsigned i
= 0; i
< v
->length (); i
++)
1494 gt_pch_nx (&((*v
)[i
]), op
, cookie
);
1497 template<typename T
>
1499 gt_pch_nx (vec
<T
, va_gc_atomic
, vl_embed
> *, gt_pointer_operator
, void *)
1501 /* No pointers to note. */
1505 /* Space efficient vector. These vectors can grow dynamically and are
1506 allocated together with their control data. They are suited to be
1507 included in data structures. Prior to initial allocation, they
1508 only take a single word of storage.
1510 These vectors are implemented as a pointer to an embeddable vector.
1511 The semantics allow for this pointer to be NULL to represent empty
1512 vectors. This way, empty vectors occupy minimal space in the
1513 structure containing them.
1517 - The whole vector and control data are allocated in a single
1519 - The whole vector may be re-allocated.
1520 - Vector data may grow and shrink.
1521 - Access and manipulation requires a pointer test and
1523 - It requires 1 word of storage (prior to vector allocation).
1528 These vectors must be PODs because they are stored in unions.
1529 (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1530 As long as we use C++03, we cannot have constructors nor
1531 destructors in classes that are stored in unions. */
1533 template<typename T
, size_t N
= 0>
1536 template<typename T
>
1537 struct vec
<T
, va_heap
, vl_ptr
>
1540 /* Default ctors to ensure triviality. Use value-initialization
1541 (e.g., vec() or vec v{ };) or vNULL to create a zero-initialized
1544 vec (const vec
&) = default;
1545 /* Initialization from the generic vNULL. */
1546 vec (vnull
): m_vec () { }
1547 /* Same as default ctor: vec storage must be released manually. */
1550 /* Defaulted same as copy ctor. */
1551 vec
& operator= (const vec
&) = default;
1553 /* Prevent implicit conversion from auto_vec. Use auto_vec::to_vec()
1556 vec (auto_vec
<T
, N
> &) = delete;
1559 void operator= (auto_vec
<T
, N
> &) = delete;
1561 /* Memory allocation and deallocation for the embedded vector.
1562 Needed because we cannot have proper ctors/dtors defined. */
1563 void create (unsigned nelems CXX_MEM_STAT_INFO
);
1564 void release (void);
1566 /* Vector operations. */
1567 bool exists (void) const
1568 { return m_vec
!= NULL
; }
1570 bool is_empty (void) const
1571 { return m_vec
? m_vec
->is_empty () : true; }
1573 unsigned allocated (void) const
1574 { return m_vec
? m_vec
->allocated () : 0; }
1576 unsigned length (void) const
1577 { return m_vec
? m_vec
->length () : 0; }
1580 { return m_vec
? m_vec
->address () : NULL
; }
1582 const T
*address (void) const
1583 { return m_vec
? m_vec
->address () : NULL
; }
1585 T
*begin () { return address (); }
1586 const T
*begin () const { return address (); }
1587 T
*end () { return begin () + length (); }
1588 const T
*end () const { return begin () + length (); }
1589 const T
&operator[] (unsigned ix
) const
1590 { return (*m_vec
)[ix
]; }
1592 bool operator!=(const vec
&other
) const
1593 { return !(*this == other
); }
1595 bool operator==(const vec
&other
) const
1596 { return address () == other
.address (); }
1598 T
&operator[] (unsigned ix
)
1599 { return (*m_vec
)[ix
]; }
1602 { return m_vec
->last (); }
1604 bool space (int nelems
) const
1605 { return m_vec
? m_vec
->space (nelems
) : nelems
== 0; }
1607 bool iterate (unsigned ix
, T
*p
) const;
1608 bool iterate (unsigned ix
, T
**p
) const;
1609 vec
copy (ALONE_CXX_MEM_STAT_INFO
) const;
1610 bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO
);
1611 bool reserve_exact (unsigned CXX_MEM_STAT_INFO
);
1612 void splice (const vec
&);
1613 void safe_splice (const vec
& CXX_MEM_STAT_INFO
);
1614 T
*quick_push (const T
&);
1615 T
*safe_push (const T
&CXX_MEM_STAT_INFO
);
1617 = typename
std::conditional
<std::is_trivially_destructible
<T
>::value
,
1619 pop_ret_type
pop (void);
1620 void truncate (unsigned);
1621 void safe_grow (unsigned, bool = false CXX_MEM_STAT_INFO
);
1622 void safe_grow_cleared (unsigned, bool = false CXX_MEM_STAT_INFO
);
1623 void quick_grow (unsigned);
1624 void quick_grow_cleared (unsigned);
1625 void quick_insert (unsigned, const T
&);
1626 void safe_insert (unsigned, const T
& CXX_MEM_STAT_INFO
);
1627 void ordered_remove (unsigned);
1628 void unordered_remove (unsigned);
1629 void block_remove (unsigned, unsigned);
1630 void qsort (int (*) (const void *, const void *));
1631 void sort (int (*) (const void *, const void *, void *), void *);
1632 void stablesort (int (*) (const void *, const void *, void *), void *);
1633 T
*bsearch (const void *key
, int (*compar
)(const void *, const void *));
1634 T
*bsearch (const void *key
,
1635 int (*compar
)(const void *, const void *, void *), void *);
1636 unsigned lower_bound (T
, bool (*)(const T
&, const T
&)) const;
1637 bool contains (const T
&search
) const;
1638 void reverse (void);
1640 bool using_auto_storage () const;
1642 /* FIXME - This field should be private, but we need to cater to
1643 compilers that have stricter notions of PODness for types. */
1644 vec
<T
, va_heap
, vl_embed
> *m_vec
;
1648 /* auto_vec is a subclass of vec that automatically manages creating and
1649 releasing the internal vector. If N is non zero then it has N elements of
1650 internal storage. The default is no internal storage, and you probably only
1651 want to ask for internal storage for vectors on the stack because if the
1652 size of the vector is larger than the internal storage that space is wasted.
1654 template<typename T
, size_t N
/* = 0 */>
1655 class auto_vec
: public vec
<T
, va_heap
>
1660 m_auto
.embedded_init (N
, 0, 1);
1661 /* ??? Instead of initializing m_vec from &m_auto directly use an
1662 expression that avoids refering to a specific member of 'this'
1663 to derail the -Wstringop-overflow diagnostic code, avoiding
1664 the impression that data accesses are supposed to be to the
1665 m_auto member storage. */
1666 size_t off
= (char *) &m_auto
- (char *) this;
1667 this->m_vec
= (vec
<T
, va_heap
, vl_embed
> *) ((char *) this + off
);
1670 auto_vec (size_t s CXX_MEM_STAT_INFO
)
1674 this->create (s PASS_MEM_STAT
);
1678 m_auto
.embedded_init (N
, 0, 1);
1679 /* ??? See above. */
1680 size_t off
= (char *) &m_auto
- (char *) this;
1681 this->m_vec
= (vec
<T
, va_heap
, vl_embed
> *) ((char *) this + off
);
1689 /* Explicitly convert to the base class. There is no conversion
1690 from a const auto_vec because a copy of the returned vec can
1691 be used to modify *THIS.
1692 This is a legacy function not to be used in new code. */
1693 vec
<T
, va_heap
> to_vec_legacy () {
1694 return *static_cast<vec
<T
, va_heap
> *>(this);
1698 vec
<T
, va_heap
, vl_embed
> m_auto
;
1699 unsigned char m_data
[sizeof (T
) * N
];
1702 /* auto_vec is a sub class of vec whose storage is released when it is
1704 template<typename T
>
1705 class auto_vec
<T
, 0> : public vec
<T
, va_heap
>
1708 auto_vec () { this->m_vec
= NULL
; }
1709 auto_vec (size_t n CXX_MEM_STAT_INFO
) { this->create (n PASS_MEM_STAT
); }
1710 ~auto_vec () { this->release (); }
1712 auto_vec (vec
<T
, va_heap
>&& r
)
1714 gcc_assert (!r
.using_auto_storage ());
1715 this->m_vec
= r
.m_vec
;
1719 auto_vec (auto_vec
<T
> &&r
)
1721 gcc_assert (!r
.using_auto_storage ());
1722 this->m_vec
= r
.m_vec
;
1726 auto_vec
& operator= (vec
<T
, va_heap
>&& r
)
1731 gcc_assert (!r
.using_auto_storage ());
1733 this->m_vec
= r
.m_vec
;
1738 auto_vec
& operator= (auto_vec
<T
> &&r
)
1743 gcc_assert (!r
.using_auto_storage ());
1745 this->m_vec
= r
.m_vec
;
1750 /* Explicitly convert to the base class. There is no conversion
1751 from a const auto_vec because a copy of the returned vec can
1752 be used to modify *THIS.
1753 This is a legacy function not to be used in new code. */
1754 vec
<T
, va_heap
> to_vec_legacy () {
1755 return *static_cast<vec
<T
, va_heap
> *>(this);
1758 // You probably don't want to copy a vector, so these are deleted to prevent
1759 // unintentional use. If you really need a copy of the vectors contents you
1761 auto_vec (const auto_vec
&) = delete;
1762 auto_vec
&operator= (const auto_vec
&) = delete;
1766 /* Allocate heap memory for pointer V and create the internal vector
1767 with space for NELEMS elements. If NELEMS is 0, the internal
1768 vector is initialized to empty. */
1770 template<typename T
>
1772 vec_alloc (vec
<T
> *&v
, unsigned nelems CXX_MEM_STAT_INFO
)
1775 v
->create (nelems PASS_MEM_STAT
);
1779 /* A subclass of auto_vec <char *> that frees all of its elements on
1782 class auto_string_vec
: public auto_vec
<char *>
1785 ~auto_string_vec ();
1788 /* A subclass of auto_vec <T *> that deletes all of its elements on
1791 This is a crude way for a vec to "own" the objects it points to
1792 and clean up automatically.
1794 For example, no attempt is made to delete elements when an item
1795 within the vec is overwritten.
1797 We can't rely on gnu::unique_ptr within a container,
1798 since we can't rely on move semantics in C++98. */
1800 template <typename T
>
1801 class auto_delete_vec
: public auto_vec
<T
*>
1804 auto_delete_vec () {}
1805 auto_delete_vec (size_t s
) : auto_vec
<T
*> (s
) {}
1807 ~auto_delete_vec ();
1810 DISABLE_COPY_AND_ASSIGN(auto_delete_vec
);
1813 /* Conditionally allocate heap memory for VEC and its internal vector. */
1815 template<typename T
>
1817 vec_check_alloc (vec
<T
, va_heap
> *&vec
, unsigned nelems CXX_MEM_STAT_INFO
)
1820 vec_alloc (vec
, nelems PASS_MEM_STAT
);
1824 /* Free the heap memory allocated by vector V and set it to NULL. */
1826 template<typename T
>
1828 vec_free (vec
<T
> *&v
)
1839 /* Return iteration condition and update PTR to point to the IX'th
1840 element of this vector. Use this to iterate over the elements of a
1843 for (ix = 0; v.iterate (ix, &ptr); ix++)
1846 template<typename T
>
1848 vec
<T
, va_heap
, vl_ptr
>::iterate (unsigned ix
, T
*ptr
) const
1851 return m_vec
->iterate (ix
, ptr
);
1860 /* Return iteration condition and update *PTR to point to the
1861 IX'th element of this vector. Use this to iterate over the
1862 elements of a vector as follows,
1864 for (ix = 0; v->iterate (ix, &ptr); ix++)
1867 This variant is for vectors of objects. */
1869 template<typename T
>
1871 vec
<T
, va_heap
, vl_ptr
>::iterate (unsigned ix
, T
**ptr
) const
1874 return m_vec
->iterate (ix
, ptr
);
1883 /* Convenience macro for forward iteration. */
1884 #define FOR_EACH_VEC_ELT(V, I, P) \
1885 for (I = 0; (V).iterate ((I), &(P)); ++(I))
1887 #define FOR_EACH_VEC_SAFE_ELT(V, I, P) \
1888 for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1890 /* Likewise, but start from FROM rather than 0. */
1891 #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM) \
1892 for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1894 /* Convenience macro for reverse iteration. */
1895 #define FOR_EACH_VEC_ELT_REVERSE(V, I, P) \
1896 for (I = (V).length () - 1; \
1897 (V).iterate ((I), &(P)); \
1900 #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P) \
1901 for (I = vec_safe_length (V) - 1; \
1902 vec_safe_iterate ((V), (I), &(P)); \
1905 /* auto_string_vec's dtor, freeing all contained strings, automatically
1906 chaining up to ~auto_vec <char *>, which frees the internal buffer. */
1909 auto_string_vec::~auto_string_vec ()
1913 FOR_EACH_VEC_ELT (*this, i
, str
)
1917 /* auto_delete_vec's dtor, deleting all contained items, automatically
1918 chaining up to ~auto_vec <T*>, which frees the internal buffer. */
1920 template <typename T
>
1922 auto_delete_vec
<T
>::~auto_delete_vec ()
1926 FOR_EACH_VEC_ELT (*this, i
, item
)
1931 /* Return a copy of this vector. */
1933 template<typename T
>
1934 inline vec
<T
, va_heap
, vl_ptr
>
1935 vec
<T
, va_heap
, vl_ptr
>::copy (ALONE_MEM_STAT_DECL
) const
1937 vec
<T
, va_heap
, vl_ptr
> new_vec
{ };
1939 new_vec
.m_vec
= m_vec
->copy (ALONE_PASS_MEM_STAT
);
1944 /* Ensure that the vector has at least RESERVE slots available (if
1945 EXACT is false), or exactly RESERVE slots available (if EXACT is
1948 This may create additional headroom if EXACT is false.
1950 Note that this can cause the embedded vector to be reallocated.
1951 Returns true iff reallocation actually occurred. */
1953 template<typename T
>
1955 vec
<T
, va_heap
, vl_ptr
>::reserve (unsigned nelems
, bool exact MEM_STAT_DECL
)
1960 /* For now play a game with va_heap::reserve to hide our auto storage if any,
1961 this is necessary because it doesn't have enough information to know the
1962 embedded vector is in auto storage, and so should not be freed. */
1963 vec
<T
, va_heap
, vl_embed
> *oldvec
= m_vec
;
1964 unsigned int oldsize
= 0;
1965 bool handle_auto_vec
= m_vec
&& using_auto_storage ();
1966 if (handle_auto_vec
)
1969 oldsize
= oldvec
->length ();
1973 va_heap::reserve (m_vec
, nelems
, exact PASS_MEM_STAT
);
1974 if (handle_auto_vec
)
1976 vec_copy_construct (m_vec
->address (), oldvec
->address (), oldsize
);
1977 m_vec
->m_vecpfx
.m_num
= oldsize
;
1984 /* Ensure that this vector has exactly NELEMS slots available. This
1985 will not create additional headroom. Note this can cause the
1986 embedded vector to be reallocated. Returns true iff reallocation
1987 actually occurred. */
1989 template<typename T
>
1991 vec
<T
, va_heap
, vl_ptr
>::reserve_exact (unsigned nelems MEM_STAT_DECL
)
1993 return reserve (nelems
, true PASS_MEM_STAT
);
1997 /* Create the internal vector and reserve NELEMS for it. This is
1998 exactly like vec::reserve, but the internal vector is
1999 unconditionally allocated from scratch. The old one, if it
2000 existed, is lost. */
2002 template<typename T
>
2004 vec
<T
, va_heap
, vl_ptr
>::create (unsigned nelems MEM_STAT_DECL
)
2008 reserve_exact (nelems PASS_MEM_STAT
);
2012 /* Free the memory occupied by the embedded vector. */
2014 template<typename T
>
2016 vec
<T
, va_heap
, vl_ptr
>::release (void)
2021 if (using_auto_storage ())
2023 m_vec
->m_vecpfx
.m_num
= 0;
2027 va_heap::release (m_vec
);
2030 /* Copy the elements from SRC to the end of this vector as if by memcpy.
2031 SRC and this vector must be allocated with the same memory
2032 allocation mechanism. This vector is assumed to have sufficient
2033 headroom available. */
2035 template<typename T
>
2037 vec
<T
, va_heap
, vl_ptr
>::splice (const vec
<T
, va_heap
, vl_ptr
> &src
)
2040 m_vec
->splice (*(src
.m_vec
));
2044 /* Copy the elements in SRC to the end of this vector as if by memcpy.
2045 SRC and this vector must be allocated with the same mechanism.
2046 If there is not enough headroom in this vector, it will be reallocated
2049 template<typename T
>
2051 vec
<T
, va_heap
, vl_ptr
>::safe_splice (const vec
<T
, va_heap
, vl_ptr
> &src
2056 reserve_exact (src
.length ());
2062 /* Push OBJ (a new element) onto the end of the vector. There must be
2063 sufficient space in the vector. Return a pointer to the slot
2064 where OBJ was inserted. */
2066 template<typename T
>
2068 vec
<T
, va_heap
, vl_ptr
>::quick_push (const T
&obj
)
2070 return m_vec
->quick_push (obj
);
2074 /* Push a new element OBJ onto the end of this vector. Reallocates
2075 the embedded vector, if needed. Return a pointer to the slot where
2076 OBJ was inserted. */
2078 template<typename T
>
2080 vec
<T
, va_heap
, vl_ptr
>::safe_push (const T
&obj MEM_STAT_DECL
)
2082 reserve (1, false PASS_MEM_STAT
);
2083 return quick_push (obj
);
2087 /* Pop and return a reference to the last element off the end of the
2088 vector. If T has non-trivial destructor, this method just pops
2089 last element and returns void. */
2091 template<typename T
>
2092 inline typename vec
<T
, va_heap
, vl_ptr
>::pop_ret_type
2093 vec
<T
, va_heap
, vl_ptr
>::pop (void)
2095 return m_vec
->pop ();
2099 /* Set the length of the vector to LEN. The new length must be less
2100 than or equal to the current length. This is an O(1) operation. */
2102 template<typename T
>
2104 vec
<T
, va_heap
, vl_ptr
>::truncate (unsigned size
)
2107 m_vec
->truncate (size
);
2109 gcc_checking_assert (size
== 0);
2113 /* Grow the vector to a specific length. LEN must be as long or
2114 longer than the current length. The new elements are
2115 uninitialized. Reallocate the internal vector, if needed. */
2117 template<typename T
>
2119 vec
<T
, va_heap
, vl_ptr
>::safe_grow (unsigned len
, bool exact MEM_STAT_DECL
)
2121 unsigned oldlen
= length ();
2122 gcc_checking_assert (oldlen
<= len
);
2123 reserve (len
- oldlen
, exact PASS_MEM_STAT
);
2125 m_vec
->quick_grow (len
);
2127 gcc_checking_assert (len
== 0);
2131 /* Grow the embedded vector to a specific length. LEN must be as
2132 long or longer than the current length. The new elements are
2133 initialized to zero. Reallocate the internal vector, if needed. */
2135 template<typename T
>
2137 vec
<T
, va_heap
, vl_ptr
>::safe_grow_cleared (unsigned len
, bool exact
2140 unsigned oldlen
= length ();
2141 gcc_checking_assert (oldlen
<= len
);
2142 reserve (len
- oldlen
, exact PASS_MEM_STAT
);
2144 m_vec
->quick_grow_cleared (len
);
2146 gcc_checking_assert (len
== 0);
2150 /* Same as vec::safe_grow but without reallocation of the internal vector.
2151 If the vector cannot be extended, a runtime assertion will be triggered. */
2153 template<typename T
>
2155 vec
<T
, va_heap
, vl_ptr
>::quick_grow (unsigned len
)
2157 gcc_checking_assert (m_vec
);
2158 m_vec
->quick_grow (len
);
2162 /* Same as vec::quick_grow_cleared but without reallocation of the
2163 internal vector. If the vector cannot be extended, a runtime
2164 assertion will be triggered. */
2166 template<typename T
>
2168 vec
<T
, va_heap
, vl_ptr
>::quick_grow_cleared (unsigned len
)
2170 gcc_checking_assert (m_vec
);
2171 m_vec
->quick_grow_cleared (len
);
2175 /* Insert an element, OBJ, at the IXth position of this vector. There
2176 must be sufficient space. */
2178 template<typename T
>
2180 vec
<T
, va_heap
, vl_ptr
>::quick_insert (unsigned ix
, const T
&obj
)
2182 m_vec
->quick_insert (ix
, obj
);
2186 /* Insert an element, OBJ, at the IXth position of the vector.
2187 Reallocate the embedded vector, if necessary. */
2189 template<typename T
>
2191 vec
<T
, va_heap
, vl_ptr
>::safe_insert (unsigned ix
, const T
&obj MEM_STAT_DECL
)
2193 reserve (1, false PASS_MEM_STAT
);
2194 quick_insert (ix
, obj
);
2198 /* Remove an element from the IXth position of this vector. Ordering of
2199 remaining elements is preserved. This is an O(N) operation due to
2202 template<typename T
>
2204 vec
<T
, va_heap
, vl_ptr
>::ordered_remove (unsigned ix
)
2206 m_vec
->ordered_remove (ix
);
2210 /* Remove an element from the IXth position of this vector. Ordering
2211 of remaining elements is destroyed. This is an O(1) operation. */
2213 template<typename T
>
2215 vec
<T
, va_heap
, vl_ptr
>::unordered_remove (unsigned ix
)
2217 m_vec
->unordered_remove (ix
);
2221 /* Remove LEN elements starting at the IXth. Ordering is retained.
2222 This is an O(N) operation due to memmove. */
2224 template<typename T
>
2226 vec
<T
, va_heap
, vl_ptr
>::block_remove (unsigned ix
, unsigned len
)
2228 m_vec
->block_remove (ix
, len
);
2232 /* Sort the contents of this vector with qsort. CMP is the comparison
2233 function to pass to qsort. */
2235 template<typename T
>
2237 vec
<T
, va_heap
, vl_ptr
>::qsort (int (*cmp
) (const void *, const void *))
2243 /* Sort the contents of this vector with qsort. CMP is the comparison
2244 function to pass to qsort. */
2246 template<typename T
>
2248 vec
<T
, va_heap
, vl_ptr
>::sort (int (*cmp
) (const void *, const void *,
2249 void *), void *data
)
2252 m_vec
->sort (cmp
, data
);
2255 /* Sort the contents of this vector with gcc_stablesort_r. CMP is the
2256 comparison function to pass to qsort. */
2258 template<typename T
>
2260 vec
<T
, va_heap
, vl_ptr
>::stablesort (int (*cmp
) (const void *, const void *,
2261 void *), void *data
)
2264 m_vec
->stablesort (cmp
, data
);
2267 /* Search the contents of the sorted vector with a binary search.
2268 CMP is the comparison function to pass to bsearch. */
2270 template<typename T
>
2272 vec
<T
, va_heap
, vl_ptr
>::bsearch (const void *key
,
2273 int (*cmp
) (const void *, const void *))
2276 return m_vec
->bsearch (key
, cmp
);
2280 /* Search the contents of the sorted vector with a binary search.
2281 CMP is the comparison function to pass to bsearch. */
2283 template<typename T
>
2285 vec
<T
, va_heap
, vl_ptr
>::bsearch (const void *key
,
2286 int (*cmp
) (const void *, const void *,
2287 void *), void *data
)
2290 return m_vec
->bsearch (key
, cmp
, data
);
2295 /* Find and return the first position in which OBJ could be inserted
2296 without changing the ordering of this vector. LESSTHAN is a
2297 function that returns true if the first argument is strictly less
2300 template<typename T
>
2302 vec
<T
, va_heap
, vl_ptr
>::lower_bound (T obj
,
2303 bool (*lessthan
)(const T
&, const T
&))
2306 return m_vec
? m_vec
->lower_bound (obj
, lessthan
) : 0;
2309 /* Return true if SEARCH is an element of V. Note that this is O(N) in the
2310 size of the vector and so should be used with care. */
2312 template<typename T
>
2314 vec
<T
, va_heap
, vl_ptr
>::contains (const T
&search
) const
2316 return m_vec
? m_vec
->contains (search
) : false;
2319 /* Reverse content of the vector. */
2321 template<typename T
>
2323 vec
<T
, va_heap
, vl_ptr
>::reverse (void)
2325 unsigned l
= length ();
2326 T
*ptr
= address ();
2328 for (unsigned i
= 0; i
< l
/ 2; i
++)
2329 std::swap (ptr
[i
], ptr
[l
- i
- 1]);
2332 template<typename T
>
2334 vec
<T
, va_heap
, vl_ptr
>::using_auto_storage () const
2336 return m_vec
? m_vec
->m_vecpfx
.m_using_auto_storage
: false;
2339 /* Release VEC and call release of all element vectors. */
2341 template<typename T
>
2343 release_vec_vec (vec
<vec
<T
> > &vec
)
2345 for (unsigned i
= 0; i
< vec
.length (); i
++)
2351 // Provide a subset of the std::span functionality. (We can't use std::span
2352 // itself because it's a C++20 feature.)
2354 // In addition, provide an invalid value that is distinct from all valid
2355 // sequences (including the empty sequence). This can be used to return
2356 // failure without having to use std::optional.
2358 // There is no operator bool because it would be ambiguous whether it is
2359 // testing for a valid value or an empty sequence.
2360 template<typename T
>
2363 template<typename OtherT
> friend class array_slice
;
2366 using value_type
= T
;
2367 using iterator
= T
*;
2368 using const_iterator
= const T
*;
2370 array_slice () : m_base (nullptr), m_size (0) {}
2372 template<typename OtherT
>
2373 array_slice (array_slice
<OtherT
> other
)
2374 : m_base (other
.m_base
), m_size (other
.m_size
) {}
2376 array_slice (iterator base
, unsigned int size
)
2377 : m_base (base
), m_size (size
) {}
2380 array_slice (T (&array
)[N
]) : m_base (array
), m_size (N
) {}
2382 template<typename OtherT
>
2383 array_slice (const vec
<OtherT
> &v
)
2384 : m_base (v
.address ()), m_size (v
.length ()) {}
2386 template<typename OtherT
>
2387 array_slice (vec
<OtherT
> &v
)
2388 : m_base (v
.address ()), m_size (v
.length ()) {}
2390 template<typename OtherT
, typename A
>
2391 array_slice (const vec
<OtherT
, A
, vl_embed
> *v
)
2392 : m_base (v
? v
->address () : nullptr), m_size (v
? v
->length () : 0) {}
2394 template<typename OtherT
, typename A
>
2395 array_slice (vec
<OtherT
, A
, vl_embed
> *v
)
2396 : m_base (v
? v
->address () : nullptr), m_size (v
? v
->length () : 0) {}
2398 iterator
begin () { return m_base
; }
2399 iterator
end () { return m_base
+ m_size
; }
2401 const_iterator
begin () const { return m_base
; }
2402 const_iterator
end () const { return m_base
+ m_size
; }
2404 value_type
&front ();
2405 value_type
&back ();
2406 value_type
&operator[] (unsigned int i
);
2408 const value_type
&front () const;
2409 const value_type
&back () const;
2410 const value_type
&operator[] (unsigned int i
) const;
2412 size_t size () const { return m_size
; }
2413 size_t size_bytes () const { return m_size
* sizeof (T
); }
2414 bool empty () const { return m_size
== 0; }
2416 // An invalid array_slice that represents a failed operation. This is
2417 // distinct from an empty slice, which is a valid result in some contexts.
2418 static array_slice
invalid () { return { nullptr, ~0U }; }
2420 // True if the array is valid, false if it is an array like INVALID.
2421 bool is_valid () const { return m_base
|| m_size
== 0; }
2425 unsigned int m_size
;
2428 template<typename T
>
2429 inline typename array_slice
<T
>::value_type
&
2430 array_slice
<T
>::front ()
2432 gcc_checking_assert (m_size
);
2436 template<typename T
>
2437 inline const typename array_slice
<T
>::value_type
&
2438 array_slice
<T
>::front () const
2440 gcc_checking_assert (m_size
);
2444 template<typename T
>
2445 inline typename array_slice
<T
>::value_type
&
2446 array_slice
<T
>::back ()
2448 gcc_checking_assert (m_size
);
2449 return m_base
[m_size
- 1];
2452 template<typename T
>
2453 inline const typename array_slice
<T
>::value_type
&
2454 array_slice
<T
>::back () const
2456 gcc_checking_assert (m_size
);
2457 return m_base
[m_size
- 1];
2460 template<typename T
>
2461 inline typename array_slice
<T
>::value_type
&
2462 array_slice
<T
>::operator[] (unsigned int i
)
2464 gcc_checking_assert (i
< m_size
);
2468 template<typename T
>
2469 inline const typename array_slice
<T
>::value_type
&
2470 array_slice
<T
>::operator[] (unsigned int i
) const
2472 gcc_checking_assert (i
< m_size
);
2476 template<typename T
>
2478 make_array_slice (T
*base
, unsigned int size
)
2480 return array_slice
<T
> (base
, size
);
2483 #if (GCC_VERSION >= 3000)
2484 # pragma GCC poison m_vec m_vecpfx m_vecdata