xref: /dflybsd-src/contrib/gcc-8.0/gcc/vec.h (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* Vector API for GNU compiler.
2*38fd1498Szrj    Copyright (C) 2004-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Nathan Sidwell <nathan@codesourcery.com>
4*38fd1498Szrj    Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
5*38fd1498Szrj 
6*38fd1498Szrj This file is part of GCC.
7*38fd1498Szrj 
8*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
9*38fd1498Szrj the terms of the GNU General Public License as published by the Free
10*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
11*38fd1498Szrj version.
12*38fd1498Szrj 
13*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
15*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16*38fd1498Szrj for more details.
17*38fd1498Szrj 
18*38fd1498Szrj You should have received a copy of the GNU General Public License
19*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
20*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
21*38fd1498Szrj 
22*38fd1498Szrj #ifndef GCC_VEC_H
23*38fd1498Szrj #define GCC_VEC_H
24*38fd1498Szrj 
25*38fd1498Szrj /* Some gen* file have no ggc support as the header file gtype-desc.h is
26*38fd1498Szrj    missing.  Provide these definitions in case ggc.h has not been included.
27*38fd1498Szrj    This is not a problem because any code that runs before gengtype is built
28*38fd1498Szrj    will never need to use GC vectors.*/
29*38fd1498Szrj 
30*38fd1498Szrj extern void ggc_free (void *);
31*38fd1498Szrj extern size_t ggc_round_alloc_size (size_t requested_size);
32*38fd1498Szrj extern void *ggc_realloc (void *, size_t MEM_STAT_DECL);
33*38fd1498Szrj 
34*38fd1498Szrj /* Templated vector type and associated interfaces.
35*38fd1498Szrj 
36*38fd1498Szrj    The interface functions are typesafe and use inline functions,
37*38fd1498Szrj    sometimes backed by out-of-line generic functions.  The vectors are
38*38fd1498Szrj    designed to interoperate with the GTY machinery.
39*38fd1498Szrj 
40*38fd1498Szrj    There are both 'index' and 'iterate' accessors.  The index accessor
41*38fd1498Szrj    is implemented by operator[].  The iterator returns a boolean
42*38fd1498Szrj    iteration condition and updates the iteration variable passed by
43*38fd1498Szrj    reference.  Because the iterator will be inlined, the address-of
44*38fd1498Szrj    can be optimized away.
45*38fd1498Szrj 
46*38fd1498Szrj    Each operation that increases the number of active elements is
47*38fd1498Szrj    available in 'quick' and 'safe' variants.  The former presumes that
48*38fd1498Szrj    there is sufficient allocated space for the operation to succeed
49*38fd1498Szrj    (it dies if there is not).  The latter will reallocate the
50*38fd1498Szrj    vector, if needed.  Reallocation causes an exponential increase in
51*38fd1498Szrj    vector size.  If you know you will be adding N elements, it would
52*38fd1498Szrj    be more efficient to use the reserve operation before adding the
53*38fd1498Szrj    elements with the 'quick' operation.  This will ensure there are at
54*38fd1498Szrj    least as many elements as you ask for, it will exponentially
55*38fd1498Szrj    increase if there are too few spare slots.  If you want reserve a
56*38fd1498Szrj    specific number of slots, but do not want the exponential increase
57*38fd1498Szrj    (for instance, you know this is the last allocation), use the
58*38fd1498Szrj    reserve_exact operation.  You can also create a vector of a
59*38fd1498Szrj    specific size from the get go.
60*38fd1498Szrj 
61*38fd1498Szrj    You should prefer the push and pop operations, as they append and
62*38fd1498Szrj    remove from the end of the vector. If you need to remove several
63*38fd1498Szrj    items in one go, use the truncate operation.  The insert and remove
64*38fd1498Szrj    operations allow you to change elements in the middle of the
65*38fd1498Szrj    vector.  There are two remove operations, one which preserves the
66*38fd1498Szrj    element ordering 'ordered_remove', and one which does not
67*38fd1498Szrj    'unordered_remove'.  The latter function copies the end element
68*38fd1498Szrj    into the removed slot, rather than invoke a memmove operation.  The
69*38fd1498Szrj    'lower_bound' function will determine where to place an item in the
70*38fd1498Szrj    array using insert that will maintain sorted order.
71*38fd1498Szrj 
72*38fd1498Szrj    Vectors are template types with three arguments: the type of the
73*38fd1498Szrj    elements in the vector, the allocation strategy, and the physical
74*38fd1498Szrj    layout to use
75*38fd1498Szrj 
76*38fd1498Szrj    Four allocation strategies are supported:
77*38fd1498Szrj 
78*38fd1498Szrj 	- Heap: allocation is done using malloc/free.  This is the
79*38fd1498Szrj 	  default allocation strategy.
80*38fd1498Szrj 
81*38fd1498Szrj   	- GC: allocation is done using ggc_alloc/ggc_free.
82*38fd1498Szrj 
83*38fd1498Szrj   	- GC atomic: same as GC with the exception that the elements
84*38fd1498Szrj 	  themselves are assumed to be of an atomic type that does
85*38fd1498Szrj 	  not need to be garbage collected.  This means that marking
86*38fd1498Szrj 	  routines do not need to traverse the array marking the
87*38fd1498Szrj 	  individual elements.  This increases the performance of
88*38fd1498Szrj 	  GC activities.
89*38fd1498Szrj 
90*38fd1498Szrj    Two physical layouts are supported:
91*38fd1498Szrj 
92*38fd1498Szrj 	- Embedded: The vector is structured using the trailing array
93*38fd1498Szrj 	  idiom.  The last member of the structure is an array of size
94*38fd1498Szrj 	  1.  When the vector is initially allocated, a single memory
95*38fd1498Szrj 	  block is created to hold the vector's control data and the
96*38fd1498Szrj 	  array of elements.  These vectors cannot grow without
97*38fd1498Szrj 	  reallocation (see discussion on embeddable vectors below).
98*38fd1498Szrj 
99*38fd1498Szrj 	- Space efficient: The vector is structured as a pointer to an
100*38fd1498Szrj 	  embedded vector.  This is the default layout.  It means that
101*38fd1498Szrj 	  vectors occupy a single word of storage before initial
102*38fd1498Szrj 	  allocation.  Vectors are allowed to grow (the internal
103*38fd1498Szrj 	  pointer is reallocated but the main vector instance does not
104*38fd1498Szrj 	  need to relocate).
105*38fd1498Szrj 
106*38fd1498Szrj    The type, allocation and layout are specified when the vector is
107*38fd1498Szrj    declared.
108*38fd1498Szrj 
109*38fd1498Szrj    If you need to directly manipulate a vector, then the 'address'
110*38fd1498Szrj    accessor will return the address of the start of the vector.  Also
111*38fd1498Szrj    the 'space' predicate will tell you whether there is spare capacity
112*38fd1498Szrj    in the vector.  You will not normally need to use these two functions.
113*38fd1498Szrj 
114*38fd1498Szrj    Notes on the different layout strategies
115*38fd1498Szrj 
116*38fd1498Szrj    * Embeddable vectors (vec<T, A, vl_embed>)
117*38fd1498Szrj 
118*38fd1498Szrj      These vectors are suitable to be embedded in other data
119*38fd1498Szrj      structures so that they can be pre-allocated in a contiguous
120*38fd1498Szrj      memory block.
121*38fd1498Szrj 
122*38fd1498Szrj      Embeddable vectors are implemented using the trailing array
123*38fd1498Szrj      idiom, thus they are not resizeable without changing the address
124*38fd1498Szrj      of the vector object itself.  This means you cannot have
125*38fd1498Szrj      variables or fields of embeddable vector type -- always use a
126*38fd1498Szrj      pointer to a vector.  The one exception is the final field of a
127*38fd1498Szrj      structure, which could be a vector type.
128*38fd1498Szrj 
129*38fd1498Szrj      You will have to use the embedded_size & embedded_init calls to
130*38fd1498Szrj      create such objects, and they will not be resizeable (so the
131*38fd1498Szrj      'safe' allocation variants are not available).
132*38fd1498Szrj 
133*38fd1498Szrj      Properties of embeddable vectors:
134*38fd1498Szrj 
135*38fd1498Szrj 	  - The whole vector and control data are allocated in a single
136*38fd1498Szrj 	    contiguous block.  It uses the trailing-vector idiom, so
137*38fd1498Szrj 	    allocation must reserve enough space for all the elements
138*38fd1498Szrj 	    in the vector plus its control data.
139*38fd1498Szrj 	  - The vector cannot be re-allocated.
140*38fd1498Szrj 	  - The vector cannot grow nor shrink.
141*38fd1498Szrj 	  - No indirections needed for access/manipulation.
142*38fd1498Szrj 	  - It requires 2 words of storage (prior to vector allocation).
143*38fd1498Szrj 
144*38fd1498Szrj 
145*38fd1498Szrj    * Space efficient vector (vec<T, A, vl_ptr>)
146*38fd1498Szrj 
147*38fd1498Szrj      These vectors can grow dynamically and are allocated together
148*38fd1498Szrj      with their control data.  They are suited to be included in data
149*38fd1498Szrj      structures.  Prior to initial allocation, they only take a single
150*38fd1498Szrj      word of storage.
151*38fd1498Szrj 
152*38fd1498Szrj      These vectors are implemented as a pointer to embeddable vectors.
153*38fd1498Szrj      The semantics allow for this pointer to be NULL to represent
154*38fd1498Szrj      empty vectors.  This way, empty vectors occupy minimal space in
155*38fd1498Szrj      the structure containing them.
156*38fd1498Szrj 
157*38fd1498Szrj      Properties:
158*38fd1498Szrj 
159*38fd1498Szrj 	- The whole vector and control data are allocated in a single
160*38fd1498Szrj 	  contiguous block.
161*38fd1498Szrj   	- The whole vector may be re-allocated.
162*38fd1498Szrj   	- Vector data may grow and shrink.
163*38fd1498Szrj   	- Access and manipulation requires a pointer test and
164*38fd1498Szrj 	  indirection.
165*38fd1498Szrj   	- It requires 1 word of storage (prior to vector allocation).
166*38fd1498Szrj 
167*38fd1498Szrj    An example of their use would be,
168*38fd1498Szrj 
169*38fd1498Szrj    struct my_struct {
170*38fd1498Szrj      // A space-efficient vector of tree pointers in GC memory.
171*38fd1498Szrj      vec<tree, va_gc, vl_ptr> v;
172*38fd1498Szrj    };
173*38fd1498Szrj 
174*38fd1498Szrj    struct my_struct *s;
175*38fd1498Szrj 
176*38fd1498Szrj    if (s->v.length ()) { we have some contents }
177*38fd1498Szrj    s->v.safe_push (decl); // append some decl onto the end
178*38fd1498Szrj    for (ix = 0; s->v.iterate (ix, &elt); ix++)
179*38fd1498Szrj      { do something with elt }
180*38fd1498Szrj */
181*38fd1498Szrj 
182*38fd1498Szrj /* Support function for statistics.  */
183*38fd1498Szrj extern void dump_vec_loc_statistics (void);
184*38fd1498Szrj 
185*38fd1498Szrj /* Hashtable mapping vec addresses to descriptors.  */
186*38fd1498Szrj extern htab_t vec_mem_usage_hash;
187*38fd1498Szrj 
188*38fd1498Szrj /* Control data for vectors.  This contains the number of allocated
189*38fd1498Szrj    and used slots inside a vector.  */
190*38fd1498Szrj 
191*38fd1498Szrj struct vec_prefix
192*38fd1498Szrj {
193*38fd1498Szrj   /* FIXME - These fields should be private, but we need to cater to
194*38fd1498Szrj 	     compilers that have stricter notions of PODness for types.  */
195*38fd1498Szrj 
196*38fd1498Szrj   /* Memory allocation support routines in vec.c.  */
197*38fd1498Szrj   void register_overhead (void *, size_t, size_t CXX_MEM_STAT_INFO);
198*38fd1498Szrj   void release_overhead (void *, size_t, bool CXX_MEM_STAT_INFO);
199*38fd1498Szrj   static unsigned calculate_allocation (vec_prefix *, unsigned, bool);
200*38fd1498Szrj   static unsigned calculate_allocation_1 (unsigned, unsigned);
201*38fd1498Szrj 
202*38fd1498Szrj   /* Note that vec_prefix should be a base class for vec, but we use
203*38fd1498Szrj      offsetof() on vector fields of tree structures (e.g.,
204*38fd1498Szrj      tree_binfo::base_binfos), and offsetof only supports base types.
205*38fd1498Szrj 
206*38fd1498Szrj      To compensate, we make vec_prefix a field inside vec and make
207*38fd1498Szrj      vec a friend class of vec_prefix so it can access its fields.  */
208*38fd1498Szrj   template <typename, typename, typename> friend struct vec;
209*38fd1498Szrj 
210*38fd1498Szrj   /* The allocator types also need access to our internals.  */
211*38fd1498Szrj   friend struct va_gc;
212*38fd1498Szrj   friend struct va_gc_atomic;
213*38fd1498Szrj   friend struct va_heap;
214*38fd1498Szrj 
215*38fd1498Szrj   unsigned m_alloc : 31;
216*38fd1498Szrj   unsigned m_using_auto_storage : 1;
217*38fd1498Szrj   unsigned m_num;
218*38fd1498Szrj };
219*38fd1498Szrj 
220*38fd1498Szrj /* Calculate the number of slots to reserve a vector, making sure that
221*38fd1498Szrj    RESERVE slots are free.  If EXACT grow exactly, otherwise grow
222*38fd1498Szrj    exponentially.  PFX is the control data for the vector.  */
223*38fd1498Szrj 
224*38fd1498Szrj inline unsigned
calculate_allocation(vec_prefix * pfx,unsigned reserve,bool exact)225*38fd1498Szrj vec_prefix::calculate_allocation (vec_prefix *pfx, unsigned reserve,
226*38fd1498Szrj 				  bool exact)
227*38fd1498Szrj {
228*38fd1498Szrj   if (exact)
229*38fd1498Szrj     return (pfx ? pfx->m_num : 0) + reserve;
230*38fd1498Szrj   else if (!pfx)
231*38fd1498Szrj     return MAX (4, reserve);
232*38fd1498Szrj   return calculate_allocation_1 (pfx->m_alloc, pfx->m_num + reserve);
233*38fd1498Szrj }
234*38fd1498Szrj 
235*38fd1498Szrj template<typename, typename, typename> struct vec;
236*38fd1498Szrj 
237*38fd1498Szrj /* Valid vector layouts
238*38fd1498Szrj 
239*38fd1498Szrj    vl_embed	- Embeddable vector that uses the trailing array idiom.
240*38fd1498Szrj    vl_ptr	- Space efficient vector that uses a pointer to an
241*38fd1498Szrj 		  embeddable vector.  */
242*38fd1498Szrj struct vl_embed { };
243*38fd1498Szrj struct vl_ptr { };
244*38fd1498Szrj 
245*38fd1498Szrj 
246*38fd1498Szrj /* Types of supported allocations
247*38fd1498Szrj 
248*38fd1498Szrj    va_heap	- Allocation uses malloc/free.
249*38fd1498Szrj    va_gc	- Allocation uses ggc_alloc.
250*38fd1498Szrj    va_gc_atomic	- Same as GC, but individual elements of the array
251*38fd1498Szrj 		  do not need to be marked during collection.  */
252*38fd1498Szrj 
253*38fd1498Szrj /* Allocator type for heap vectors.  */
254*38fd1498Szrj struct va_heap
255*38fd1498Szrj {
256*38fd1498Szrj   /* Heap vectors are frequently regular instances, so use the vl_ptr
257*38fd1498Szrj      layout for them.  */
258*38fd1498Szrj   typedef vl_ptr default_layout;
259*38fd1498Szrj 
260*38fd1498Szrj   template<typename T>
261*38fd1498Szrj   static void reserve (vec<T, va_heap, vl_embed> *&, unsigned, bool
262*38fd1498Szrj 		       CXX_MEM_STAT_INFO);
263*38fd1498Szrj 
264*38fd1498Szrj   template<typename T>
265*38fd1498Szrj   static void release (vec<T, va_heap, vl_embed> *&);
266*38fd1498Szrj };
267*38fd1498Szrj 
268*38fd1498Szrj 
269*38fd1498Szrj /* Allocator for heap memory.  Ensure there are at least RESERVE free
270*38fd1498Szrj    slots in V.  If EXACT is true, grow exactly, else grow
271*38fd1498Szrj    exponentially.  As a special case, if the vector had not been
272*38fd1498Szrj    allocated and RESERVE is 0, no vector will be created.  */
273*38fd1498Szrj 
274*38fd1498Szrj template<typename T>
275*38fd1498Szrj inline void
reserve(vec<T,va_heap,vl_embed> * & v,unsigned reserve,bool exact MEM_STAT_DECL)276*38fd1498Szrj va_heap::reserve (vec<T, va_heap, vl_embed> *&v, unsigned reserve, bool exact
277*38fd1498Szrj 		  MEM_STAT_DECL)
278*38fd1498Szrj {
279*38fd1498Szrj   unsigned alloc
280*38fd1498Szrj     = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
281*38fd1498Szrj   gcc_checking_assert (alloc);
282*38fd1498Szrj 
283*38fd1498Szrj   if (GATHER_STATISTICS && v)
284*38fd1498Szrj     v->m_vecpfx.release_overhead (v, v->allocated (), false);
285*38fd1498Szrj 
286*38fd1498Szrj   size_t size = vec<T, va_heap, vl_embed>::embedded_size (alloc);
287*38fd1498Szrj   unsigned nelem = v ? v->length () : 0;
288*38fd1498Szrj   v = static_cast <vec<T, va_heap, vl_embed> *> (xrealloc (v, size));
289*38fd1498Szrj   v->embedded_init (alloc, nelem);
290*38fd1498Szrj 
291*38fd1498Szrj   if (GATHER_STATISTICS)
292*38fd1498Szrj     v->m_vecpfx.register_overhead (v, alloc, nelem PASS_MEM_STAT);
293*38fd1498Szrj }
294*38fd1498Szrj 
295*38fd1498Szrj 
296*38fd1498Szrj /* Free the heap space allocated for vector V.  */
297*38fd1498Szrj 
298*38fd1498Szrj template<typename T>
299*38fd1498Szrj void
release(vec<T,va_heap,vl_embed> * & v)300*38fd1498Szrj va_heap::release (vec<T, va_heap, vl_embed> *&v)
301*38fd1498Szrj {
302*38fd1498Szrj   if (v == NULL)
303*38fd1498Szrj     return;
304*38fd1498Szrj 
305*38fd1498Szrj   if (GATHER_STATISTICS)
306*38fd1498Szrj     v->m_vecpfx.release_overhead (v, v->allocated (), true);
307*38fd1498Szrj   ::free (v);
308*38fd1498Szrj   v = NULL;
309*38fd1498Szrj }
310*38fd1498Szrj 
311*38fd1498Szrj 
312*38fd1498Szrj /* Allocator type for GC vectors.  Notice that we need the structure
313*38fd1498Szrj    declaration even if GC is not enabled.  */
314*38fd1498Szrj 
315*38fd1498Szrj struct va_gc
316*38fd1498Szrj {
317*38fd1498Szrj   /* Use vl_embed as the default layout for GC vectors.  Due to GTY
318*38fd1498Szrj      limitations, GC vectors must always be pointers, so it is more
319*38fd1498Szrj      efficient to use a pointer to the vl_embed layout, rather than
320*38fd1498Szrj      using a pointer to a pointer as would be the case with vl_ptr.  */
321*38fd1498Szrj   typedef vl_embed default_layout;
322*38fd1498Szrj 
323*38fd1498Szrj   template<typename T, typename A>
324*38fd1498Szrj   static void reserve (vec<T, A, vl_embed> *&, unsigned, bool
325*38fd1498Szrj 		       CXX_MEM_STAT_INFO);
326*38fd1498Szrj 
327*38fd1498Szrj   template<typename T, typename A>
328*38fd1498Szrj   static void release (vec<T, A, vl_embed> *&v);
329*38fd1498Szrj };
330*38fd1498Szrj 
331*38fd1498Szrj 
332*38fd1498Szrj /* Free GC memory used by V and reset V to NULL.  */
333*38fd1498Szrj 
334*38fd1498Szrj template<typename T, typename A>
335*38fd1498Szrj inline void
release(vec<T,A,vl_embed> * & v)336*38fd1498Szrj va_gc::release (vec<T, A, vl_embed> *&v)
337*38fd1498Szrj {
338*38fd1498Szrj   if (v)
339*38fd1498Szrj     ::ggc_free (v);
340*38fd1498Szrj   v = NULL;
341*38fd1498Szrj }
342*38fd1498Szrj 
343*38fd1498Szrj 
344*38fd1498Szrj /* Allocator for GC memory.  Ensure there are at least RESERVE free
345*38fd1498Szrj    slots in V.  If EXACT is true, grow exactly, else grow
346*38fd1498Szrj    exponentially.  As a special case, if the vector had not been
347*38fd1498Szrj    allocated and RESERVE is 0, no vector will be created.  */
348*38fd1498Szrj 
349*38fd1498Szrj template<typename T, typename A>
350*38fd1498Szrj void
reserve(vec<T,A,vl_embed> * & v,unsigned reserve,bool exact MEM_STAT_DECL)351*38fd1498Szrj va_gc::reserve (vec<T, A, vl_embed> *&v, unsigned reserve, bool exact
352*38fd1498Szrj 		MEM_STAT_DECL)
353*38fd1498Szrj {
354*38fd1498Szrj   unsigned alloc
355*38fd1498Szrj     = vec_prefix::calculate_allocation (v ? &v->m_vecpfx : 0, reserve, exact);
356*38fd1498Szrj   if (!alloc)
357*38fd1498Szrj     {
358*38fd1498Szrj       ::ggc_free (v);
359*38fd1498Szrj       v = NULL;
360*38fd1498Szrj       return;
361*38fd1498Szrj     }
362*38fd1498Szrj 
363*38fd1498Szrj   /* Calculate the amount of space we want.  */
364*38fd1498Szrj   size_t size = vec<T, A, vl_embed>::embedded_size (alloc);
365*38fd1498Szrj 
366*38fd1498Szrj   /* Ask the allocator how much space it will really give us.  */
367*38fd1498Szrj   size = ::ggc_round_alloc_size (size);
368*38fd1498Szrj 
369*38fd1498Szrj   /* Adjust the number of slots accordingly.  */
370*38fd1498Szrj   size_t vec_offset = sizeof (vec_prefix);
371*38fd1498Szrj   size_t elt_size = sizeof (T);
372*38fd1498Szrj   alloc = (size - vec_offset) / elt_size;
373*38fd1498Szrj 
374*38fd1498Szrj   /* And finally, recalculate the amount of space we ask for.  */
375*38fd1498Szrj   size = vec_offset + alloc * elt_size;
376*38fd1498Szrj 
377*38fd1498Szrj   unsigned nelem = v ? v->length () : 0;
378*38fd1498Szrj   v = static_cast <vec<T, A, vl_embed> *> (::ggc_realloc (v, size
379*38fd1498Szrj 							       PASS_MEM_STAT));
380*38fd1498Szrj   v->embedded_init (alloc, nelem);
381*38fd1498Szrj }
382*38fd1498Szrj 
383*38fd1498Szrj 
384*38fd1498Szrj /* Allocator type for GC vectors.  This is for vectors of types
385*38fd1498Szrj    atomics w.r.t. collection, so allocation and deallocation is
386*38fd1498Szrj    completely inherited from va_gc.  */
387*38fd1498Szrj struct va_gc_atomic : va_gc
388*38fd1498Szrj {
389*38fd1498Szrj };
390*38fd1498Szrj 
391*38fd1498Szrj 
392*38fd1498Szrj /* Generic vector template.  Default values for A and L indicate the
393*38fd1498Szrj    most commonly used strategies.
394*38fd1498Szrj 
395*38fd1498Szrj    FIXME - Ideally, they would all be vl_ptr to encourage using regular
396*38fd1498Szrj            instances for vectors, but the existing GTY machinery is limited
397*38fd1498Szrj 	   in that it can only deal with GC objects that are pointers
398*38fd1498Szrj 	   themselves.
399*38fd1498Szrj 
400*38fd1498Szrj 	   This means that vector operations that need to deal with
401*38fd1498Szrj 	   potentially NULL pointers, must be provided as free
402*38fd1498Szrj 	   functions (see the vec_safe_* functions above).  */
403*38fd1498Szrj template<typename T,
404*38fd1498Szrj          typename A = va_heap,
405*38fd1498Szrj          typename L = typename A::default_layout>
406*38fd1498Szrj struct GTY((user)) vec
407*38fd1498Szrj {
408*38fd1498Szrj };
409*38fd1498Szrj 
410*38fd1498Szrj /* Generic vec<> debug helpers.
411*38fd1498Szrj 
412*38fd1498Szrj    These need to be instantiated for each vec<TYPE> used throughout
413*38fd1498Szrj    the compiler like this:
414*38fd1498Szrj 
415*38fd1498Szrj     DEFINE_DEBUG_VEC (TYPE)
416*38fd1498Szrj 
417*38fd1498Szrj    The reason we have a debug_helper() is because GDB can't
418*38fd1498Szrj    disambiguate a plain call to debug(some_vec), and it must be called
419*38fd1498Szrj    like debug<TYPE>(some_vec).  */
420*38fd1498Szrj 
421*38fd1498Szrj template<typename T>
422*38fd1498Szrj void
debug_helper(vec<T> & ref)423*38fd1498Szrj debug_helper (vec<T> &ref)
424*38fd1498Szrj {
425*38fd1498Szrj   unsigned i;
426*38fd1498Szrj   for (i = 0; i < ref.length (); ++i)
427*38fd1498Szrj     {
428*38fd1498Szrj       fprintf (stderr, "[%d] = ", i);
429*38fd1498Szrj       debug_slim (ref[i]);
430*38fd1498Szrj       fputc ('\n', stderr);
431*38fd1498Szrj     }
432*38fd1498Szrj }
433*38fd1498Szrj 
434*38fd1498Szrj /* We need a separate va_gc variant here because default template
435*38fd1498Szrj    argument for functions cannot be used in c++-98.  Once this
436*38fd1498Szrj    restriction is removed, those variant should be folded with the
437*38fd1498Szrj    above debug_helper.  */
438*38fd1498Szrj 
439*38fd1498Szrj template<typename T>
440*38fd1498Szrj void
debug_helper(vec<T,va_gc> & ref)441*38fd1498Szrj debug_helper (vec<T, va_gc> &ref)
442*38fd1498Szrj {
443*38fd1498Szrj   unsigned i;
444*38fd1498Szrj   for (i = 0; i < ref.length (); ++i)
445*38fd1498Szrj     {
446*38fd1498Szrj       fprintf (stderr, "[%d] = ", i);
447*38fd1498Szrj       debug_slim (ref[i]);
448*38fd1498Szrj       fputc ('\n', stderr);
449*38fd1498Szrj     }
450*38fd1498Szrj }
451*38fd1498Szrj 
452*38fd1498Szrj /* Macro to define debug(vec<T>) and debug(vec<T, va_gc>) helper
453*38fd1498Szrj    functions for a type T.  */
454*38fd1498Szrj 
455*38fd1498Szrj #define DEFINE_DEBUG_VEC(T) \
456*38fd1498Szrj   template void debug_helper (vec<T> &);		\
457*38fd1498Szrj   template void debug_helper (vec<T, va_gc> &);		\
458*38fd1498Szrj   /* Define the vec<T> debug functions.  */		\
459*38fd1498Szrj   DEBUG_FUNCTION void					\
460*38fd1498Szrj   debug (vec<T> &ref)					\
461*38fd1498Szrj   {							\
462*38fd1498Szrj     debug_helper <T> (ref);				\
463*38fd1498Szrj   }							\
464*38fd1498Szrj   DEBUG_FUNCTION void					\
465*38fd1498Szrj   debug (vec<T> *ptr)					\
466*38fd1498Szrj   {							\
467*38fd1498Szrj     if (ptr)						\
468*38fd1498Szrj       debug (*ptr);					\
469*38fd1498Szrj     else						\
470*38fd1498Szrj       fprintf (stderr, "<nil>\n");			\
471*38fd1498Szrj   }							\
472*38fd1498Szrj   /* Define the vec<T, va_gc> debug functions.  */	\
473*38fd1498Szrj   DEBUG_FUNCTION void					\
474*38fd1498Szrj   debug (vec<T, va_gc> &ref)				\
475*38fd1498Szrj   {							\
476*38fd1498Szrj     debug_helper <T> (ref);				\
477*38fd1498Szrj   }							\
478*38fd1498Szrj   DEBUG_FUNCTION void					\
479*38fd1498Szrj   debug (vec<T, va_gc> *ptr)				\
480*38fd1498Szrj   {							\
481*38fd1498Szrj     if (ptr)						\
482*38fd1498Szrj       debug (*ptr);					\
483*38fd1498Szrj     else						\
484*38fd1498Szrj       fprintf (stderr, "<nil>\n");			\
485*38fd1498Szrj   }
486*38fd1498Szrj 
487*38fd1498Szrj /* Default-construct N elements in DST.  */
488*38fd1498Szrj 
489*38fd1498Szrj template <typename T>
490*38fd1498Szrj inline void
vec_default_construct(T * dst,unsigned n)491*38fd1498Szrj vec_default_construct (T *dst, unsigned n)
492*38fd1498Szrj {
493*38fd1498Szrj #ifdef BROKEN_VALUE_INITIALIZATION
494*38fd1498Szrj   /* Versions of GCC before 4.4 sometimes leave certain objects
495*38fd1498Szrj      uninitialized when value initialized, though if the type has
496*38fd1498Szrj      user defined default ctor, that ctor is invoked.  As a workaround
497*38fd1498Szrj      perform clearing first and then the value initialization, which
498*38fd1498Szrj      fixes the case when value initialization doesn't initialize due to
499*38fd1498Szrj      the bugs and should initialize to all zeros, but still allows
500*38fd1498Szrj      vectors for types with user defined default ctor that initializes
501*38fd1498Szrj      some or all elements to non-zero.  If T has no user defined
502*38fd1498Szrj      default ctor and some non-static data members have user defined
503*38fd1498Szrj      default ctors that initialize to non-zero the workaround will
504*38fd1498Szrj      still not work properly; in that case we just need to provide
505*38fd1498Szrj      user defined default ctor.  */
506*38fd1498Szrj   memset (dst, '\0', sizeof (T) * n);
507*38fd1498Szrj #endif
508*38fd1498Szrj   for ( ; n; ++dst, --n)
509*38fd1498Szrj     ::new (static_cast<void*>(dst)) T ();
510*38fd1498Szrj }
511*38fd1498Szrj 
512*38fd1498Szrj /* Copy-construct N elements in DST from *SRC.  */
513*38fd1498Szrj 
514*38fd1498Szrj template <typename T>
515*38fd1498Szrj inline void
vec_copy_construct(T * dst,const T * src,unsigned n)516*38fd1498Szrj vec_copy_construct (T *dst, const T *src, unsigned n)
517*38fd1498Szrj {
518*38fd1498Szrj   for ( ; n; ++dst, ++src, --n)
519*38fd1498Szrj     ::new (static_cast<void*>(dst)) T (*src);
520*38fd1498Szrj }
521*38fd1498Szrj 
522*38fd1498Szrj /* Type to provide NULL values for vec<T, A, L>.  This is used to
523*38fd1498Szrj    provide nil initializers for vec instances.  Since vec must be
524*38fd1498Szrj    a POD, we cannot have proper ctor/dtor for it.  To initialize
525*38fd1498Szrj    a vec instance, you can assign it the value vNULL.  This isn't
526*38fd1498Szrj    needed for file-scope and function-local static vectors, which
527*38fd1498Szrj    are zero-initialized by default.  */
528*38fd1498Szrj struct vnull
529*38fd1498Szrj {
530*38fd1498Szrj   template <typename T, typename A, typename L>
531*38fd1498Szrj   CONSTEXPR operator vec<T, A, L> () { return vec<T, A, L>(); }
532*38fd1498Szrj };
533*38fd1498Szrj extern vnull vNULL;
534*38fd1498Szrj 
535*38fd1498Szrj 
536*38fd1498Szrj /* Embeddable vector.  These vectors are suitable to be embedded
537*38fd1498Szrj    in other data structures so that they can be pre-allocated in a
538*38fd1498Szrj    contiguous memory block.
539*38fd1498Szrj 
540*38fd1498Szrj    Embeddable vectors are implemented using the trailing array idiom,
541*38fd1498Szrj    thus they are not resizeable without changing the address of the
542*38fd1498Szrj    vector object itself.  This means you cannot have variables or
543*38fd1498Szrj    fields of embeddable vector type -- always use a pointer to a
544*38fd1498Szrj    vector.  The one exception is the final field of a structure, which
545*38fd1498Szrj    could be a vector type.
546*38fd1498Szrj 
547*38fd1498Szrj    You will have to use the embedded_size & embedded_init calls to
548*38fd1498Szrj    create such objects, and they will not be resizeable (so the 'safe'
549*38fd1498Szrj    allocation variants are not available).
550*38fd1498Szrj 
551*38fd1498Szrj    Properties:
552*38fd1498Szrj 
553*38fd1498Szrj 	- The whole vector and control data are allocated in a single
554*38fd1498Szrj 	  contiguous block.  It uses the trailing-vector idiom, so
555*38fd1498Szrj 	  allocation must reserve enough space for all the elements
556*38fd1498Szrj   	  in the vector plus its control data.
557*38fd1498Szrj   	- The vector cannot be re-allocated.
558*38fd1498Szrj   	- The vector cannot grow nor shrink.
559*38fd1498Szrj   	- No indirections needed for access/manipulation.
560*38fd1498Szrj   	- It requires 2 words of storage (prior to vector allocation).  */
561*38fd1498Szrj 
562*38fd1498Szrj template<typename T, typename A>
563*38fd1498Szrj struct GTY((user)) vec<T, A, vl_embed>
564*38fd1498Szrj {
565*38fd1498Szrj public:
566*38fd1498Szrj   unsigned allocated (void) const { return m_vecpfx.m_alloc; }
567*38fd1498Szrj   unsigned length (void) const { return m_vecpfx.m_num; }
568*38fd1498Szrj   bool is_empty (void) const { return m_vecpfx.m_num == 0; }
569*38fd1498Szrj   T *address (void) { return m_vecdata; }
570*38fd1498Szrj   const T *address (void) const { return m_vecdata; }
571*38fd1498Szrj   T *begin () { return address (); }
572*38fd1498Szrj   const T *begin () const { return address (); }
573*38fd1498Szrj   T *end () { return address () + length (); }
574*38fd1498Szrj   const T *end () const { return address () + length (); }
575*38fd1498Szrj   const T &operator[] (unsigned) const;
576*38fd1498Szrj   T &operator[] (unsigned);
577*38fd1498Szrj   T &last (void);
578*38fd1498Szrj   bool space (unsigned) const;
579*38fd1498Szrj   bool iterate (unsigned, T *) const;
580*38fd1498Szrj   bool iterate (unsigned, T **) const;
581*38fd1498Szrj   vec *copy (ALONE_CXX_MEM_STAT_INFO) const;
582*38fd1498Szrj   void splice (const vec &);
583*38fd1498Szrj   void splice (const vec *src);
584*38fd1498Szrj   T *quick_push (const T &);
585*38fd1498Szrj   T &pop (void);
586*38fd1498Szrj   void truncate (unsigned);
587*38fd1498Szrj   void quick_insert (unsigned, const T &);
588*38fd1498Szrj   void ordered_remove (unsigned);
589*38fd1498Szrj   void unordered_remove (unsigned);
590*38fd1498Szrj   void block_remove (unsigned, unsigned);
591*38fd1498Szrj   void qsort (int (*) (const void *, const void *));
592*38fd1498Szrj   T *bsearch (const void *key, int (*compar)(const void *, const void *));
593*38fd1498Szrj   unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
594*38fd1498Szrj   bool contains (const T &search) const;
595*38fd1498Szrj   static size_t embedded_size (unsigned);
596*38fd1498Szrj   void embedded_init (unsigned, unsigned = 0, unsigned = 0);
597*38fd1498Szrj   void quick_grow (unsigned len);
598*38fd1498Szrj   void quick_grow_cleared (unsigned len);
599*38fd1498Szrj 
600*38fd1498Szrj   /* vec class can access our internal data and functions.  */
601*38fd1498Szrj   template <typename, typename, typename> friend struct vec;
602*38fd1498Szrj 
603*38fd1498Szrj   /* The allocator types also need access to our internals.  */
604*38fd1498Szrj   friend struct va_gc;
605*38fd1498Szrj   friend struct va_gc_atomic;
606*38fd1498Szrj   friend struct va_heap;
607*38fd1498Szrj 
608*38fd1498Szrj   /* FIXME - These fields should be private, but we need to cater to
609*38fd1498Szrj 	     compilers that have stricter notions of PODness for types.  */
610*38fd1498Szrj   vec_prefix m_vecpfx;
611*38fd1498Szrj   T m_vecdata[1];
612*38fd1498Szrj };
613*38fd1498Szrj 
614*38fd1498Szrj 
615*38fd1498Szrj /* Convenience wrapper functions to use when dealing with pointers to
616*38fd1498Szrj    embedded vectors.  Some functionality for these vectors must be
617*38fd1498Szrj    provided via free functions for these reasons:
618*38fd1498Szrj 
619*38fd1498Szrj 	1- The pointer may be NULL (e.g., before initial allocation).
620*38fd1498Szrj 
621*38fd1498Szrj   	2- When the vector needs to grow, it must be reallocated, so
622*38fd1498Szrj   	   the pointer will change its value.
623*38fd1498Szrj 
624*38fd1498Szrj    Because of limitations with the current GC machinery, all vectors
625*38fd1498Szrj    in GC memory *must* be pointers.  */
626*38fd1498Szrj 
627*38fd1498Szrj 
628*38fd1498Szrj /* If V contains no room for NELEMS elements, return false. Otherwise,
629*38fd1498Szrj    return true.  */
630*38fd1498Szrj template<typename T, typename A>
631*38fd1498Szrj inline bool
632*38fd1498Szrj vec_safe_space (const vec<T, A, vl_embed> *v, unsigned nelems)
633*38fd1498Szrj {
634*38fd1498Szrj   return v ? v->space (nelems) : nelems == 0;
635*38fd1498Szrj }
636*38fd1498Szrj 
637*38fd1498Szrj 
638*38fd1498Szrj /* If V is NULL, return 0.  Otherwise, return V->length().  */
639*38fd1498Szrj template<typename T, typename A>
640*38fd1498Szrj inline unsigned
641*38fd1498Szrj vec_safe_length (const vec<T, A, vl_embed> *v)
642*38fd1498Szrj {
643*38fd1498Szrj   return v ? v->length () : 0;
644*38fd1498Szrj }
645*38fd1498Szrj 
646*38fd1498Szrj 
647*38fd1498Szrj /* If V is NULL, return NULL.  Otherwise, return V->address().  */
648*38fd1498Szrj template<typename T, typename A>
649*38fd1498Szrj inline T *
650*38fd1498Szrj vec_safe_address (vec<T, A, vl_embed> *v)
651*38fd1498Szrj {
652*38fd1498Szrj   return v ? v->address () : NULL;
653*38fd1498Szrj }
654*38fd1498Szrj 
655*38fd1498Szrj 
656*38fd1498Szrj /* If V is NULL, return true.  Otherwise, return V->is_empty().  */
657*38fd1498Szrj template<typename T, typename A>
658*38fd1498Szrj inline bool
659*38fd1498Szrj vec_safe_is_empty (vec<T, A, vl_embed> *v)
660*38fd1498Szrj {
661*38fd1498Szrj   return v ? v->is_empty () : true;
662*38fd1498Szrj }
663*38fd1498Szrj 
664*38fd1498Szrj /* If V does not have space for NELEMS elements, call
665*38fd1498Szrj    V->reserve(NELEMS, EXACT).  */
666*38fd1498Szrj template<typename T, typename A>
667*38fd1498Szrj inline bool
668*38fd1498Szrj vec_safe_reserve (vec<T, A, vl_embed> *&v, unsigned nelems, bool exact = false
669*38fd1498Szrj 		  CXX_MEM_STAT_INFO)
670*38fd1498Szrj {
671*38fd1498Szrj   bool extend = nelems ? !vec_safe_space (v, nelems) : false;
672*38fd1498Szrj   if (extend)
673*38fd1498Szrj     A::reserve (v, nelems, exact PASS_MEM_STAT);
674*38fd1498Szrj   return extend;
675*38fd1498Szrj }
676*38fd1498Szrj 
677*38fd1498Szrj template<typename T, typename A>
678*38fd1498Szrj inline bool
679*38fd1498Szrj vec_safe_reserve_exact (vec<T, A, vl_embed> *&v, unsigned nelems
680*38fd1498Szrj 			CXX_MEM_STAT_INFO)
681*38fd1498Szrj {
682*38fd1498Szrj   return vec_safe_reserve (v, nelems, true PASS_MEM_STAT);
683*38fd1498Szrj }
684*38fd1498Szrj 
685*38fd1498Szrj 
686*38fd1498Szrj /* Allocate GC memory for V with space for NELEMS slots.  If NELEMS
687*38fd1498Szrj    is 0, V is initialized to NULL.  */
688*38fd1498Szrj 
689*38fd1498Szrj template<typename T, typename A>
690*38fd1498Szrj inline void
691*38fd1498Szrj vec_alloc (vec<T, A, vl_embed> *&v, unsigned nelems CXX_MEM_STAT_INFO)
692*38fd1498Szrj {
693*38fd1498Szrj   v = NULL;
694*38fd1498Szrj   vec_safe_reserve (v, nelems, false PASS_MEM_STAT);
695*38fd1498Szrj }
696*38fd1498Szrj 
697*38fd1498Szrj 
698*38fd1498Szrj /* Free the GC memory allocated by vector V and set it to NULL.  */
699*38fd1498Szrj 
700*38fd1498Szrj template<typename T, typename A>
701*38fd1498Szrj inline void
702*38fd1498Szrj vec_free (vec<T, A, vl_embed> *&v)
703*38fd1498Szrj {
704*38fd1498Szrj   A::release (v);
705*38fd1498Szrj }
706*38fd1498Szrj 
707*38fd1498Szrj 
708*38fd1498Szrj /* Grow V to length LEN.  Allocate it, if necessary.  */
709*38fd1498Szrj template<typename T, typename A>
710*38fd1498Szrj inline void
711*38fd1498Szrj vec_safe_grow (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
712*38fd1498Szrj {
713*38fd1498Szrj   unsigned oldlen = vec_safe_length (v);
714*38fd1498Szrj   gcc_checking_assert (len >= oldlen);
715*38fd1498Szrj   vec_safe_reserve_exact (v, len - oldlen PASS_MEM_STAT);
716*38fd1498Szrj   v->quick_grow (len);
717*38fd1498Szrj }
718*38fd1498Szrj 
719*38fd1498Szrj 
720*38fd1498Szrj /* If V is NULL, allocate it.  Call V->safe_grow_cleared(LEN).  */
721*38fd1498Szrj template<typename T, typename A>
722*38fd1498Szrj inline void
723*38fd1498Szrj vec_safe_grow_cleared (vec<T, A, vl_embed> *&v, unsigned len CXX_MEM_STAT_INFO)
724*38fd1498Szrj {
725*38fd1498Szrj   unsigned oldlen = vec_safe_length (v);
726*38fd1498Szrj   vec_safe_grow (v, len PASS_MEM_STAT);
727*38fd1498Szrj   vec_default_construct (v->address () + oldlen, len - oldlen);
728*38fd1498Szrj }
729*38fd1498Szrj 
730*38fd1498Szrj 
731*38fd1498Szrj /* If V is NULL return false, otherwise return V->iterate(IX, PTR).  */
732*38fd1498Szrj template<typename T, typename A>
733*38fd1498Szrj inline bool
734*38fd1498Szrj vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T **ptr)
735*38fd1498Szrj {
736*38fd1498Szrj   if (v)
737*38fd1498Szrj     return v->iterate (ix, ptr);
738*38fd1498Szrj   else
739*38fd1498Szrj     {
740*38fd1498Szrj       *ptr = 0;
741*38fd1498Szrj       return false;
742*38fd1498Szrj     }
743*38fd1498Szrj }
744*38fd1498Szrj 
745*38fd1498Szrj template<typename T, typename A>
746*38fd1498Szrj inline bool
747*38fd1498Szrj vec_safe_iterate (const vec<T, A, vl_embed> *v, unsigned ix, T *ptr)
748*38fd1498Szrj {
749*38fd1498Szrj   if (v)
750*38fd1498Szrj     return v->iterate (ix, ptr);
751*38fd1498Szrj   else
752*38fd1498Szrj     {
753*38fd1498Szrj       *ptr = 0;
754*38fd1498Szrj       return false;
755*38fd1498Szrj     }
756*38fd1498Szrj }
757*38fd1498Szrj 
758*38fd1498Szrj 
759*38fd1498Szrj /* If V has no room for one more element, reallocate it.  Then call
760*38fd1498Szrj    V->quick_push(OBJ).  */
761*38fd1498Szrj template<typename T, typename A>
762*38fd1498Szrj inline T *
763*38fd1498Szrj vec_safe_push (vec<T, A, vl_embed> *&v, const T &obj CXX_MEM_STAT_INFO)
764*38fd1498Szrj {
765*38fd1498Szrj   vec_safe_reserve (v, 1, false PASS_MEM_STAT);
766*38fd1498Szrj   return v->quick_push (obj);
767*38fd1498Szrj }
768*38fd1498Szrj 
769*38fd1498Szrj 
770*38fd1498Szrj /* if V has no room for one more element, reallocate it.  Then call
771*38fd1498Szrj    V->quick_insert(IX, OBJ).  */
772*38fd1498Szrj template<typename T, typename A>
773*38fd1498Szrj inline void
774*38fd1498Szrj vec_safe_insert (vec<T, A, vl_embed> *&v, unsigned ix, const T &obj
775*38fd1498Szrj 		 CXX_MEM_STAT_INFO)
776*38fd1498Szrj {
777*38fd1498Szrj   vec_safe_reserve (v, 1, false PASS_MEM_STAT);
778*38fd1498Szrj   v->quick_insert (ix, obj);
779*38fd1498Szrj }
780*38fd1498Szrj 
781*38fd1498Szrj 
782*38fd1498Szrj /* If V is NULL, do nothing.  Otherwise, call V->truncate(SIZE).  */
783*38fd1498Szrj template<typename T, typename A>
784*38fd1498Szrj inline void
785*38fd1498Szrj vec_safe_truncate (vec<T, A, vl_embed> *v, unsigned size)
786*38fd1498Szrj {
787*38fd1498Szrj   if (v)
788*38fd1498Szrj     v->truncate (size);
789*38fd1498Szrj }
790*38fd1498Szrj 
791*38fd1498Szrj 
792*38fd1498Szrj /* If SRC is not NULL, return a pointer to a copy of it.  */
793*38fd1498Szrj template<typename T, typename A>
794*38fd1498Szrj inline vec<T, A, vl_embed> *
795*38fd1498Szrj vec_safe_copy (vec<T, A, vl_embed> *src CXX_MEM_STAT_INFO)
796*38fd1498Szrj {
797*38fd1498Szrj   return src ? src->copy (ALONE_PASS_MEM_STAT) : NULL;
798*38fd1498Szrj }
799*38fd1498Szrj 
800*38fd1498Szrj /* Copy the elements from SRC to the end of DST as if by memcpy.
801*38fd1498Szrj    Reallocate DST, if necessary.  */
802*38fd1498Szrj template<typename T, typename A>
803*38fd1498Szrj inline void
804*38fd1498Szrj vec_safe_splice (vec<T, A, vl_embed> *&dst, const vec<T, A, vl_embed> *src
805*38fd1498Szrj 		 CXX_MEM_STAT_INFO)
806*38fd1498Szrj {
807*38fd1498Szrj   unsigned src_len = vec_safe_length (src);
808*38fd1498Szrj   if (src_len)
809*38fd1498Szrj     {
810*38fd1498Szrj       vec_safe_reserve_exact (dst, vec_safe_length (dst) + src_len
811*38fd1498Szrj 			      PASS_MEM_STAT);
812*38fd1498Szrj       dst->splice (*src);
813*38fd1498Szrj     }
814*38fd1498Szrj }
815*38fd1498Szrj 
816*38fd1498Szrj /* Return true if SEARCH is an element of V.  Note that this is O(N) in the
817*38fd1498Szrj    size of the vector and so should be used with care.  */
818*38fd1498Szrj 
819*38fd1498Szrj template<typename T, typename A>
820*38fd1498Szrj inline bool
821*38fd1498Szrj vec_safe_contains (vec<T, A, vl_embed> *v, const T &search)
822*38fd1498Szrj {
823*38fd1498Szrj   return v ? v->contains (search) : false;
824*38fd1498Szrj }
825*38fd1498Szrj 
826*38fd1498Szrj /* Index into vector.  Return the IX'th element.  IX must be in the
827*38fd1498Szrj    domain of the vector.  */
828*38fd1498Szrj 
829*38fd1498Szrj template<typename T, typename A>
830*38fd1498Szrj inline const T &
831*38fd1498Szrj vec<T, A, vl_embed>::operator[] (unsigned ix) const
832*38fd1498Szrj {
833*38fd1498Szrj   gcc_checking_assert (ix < m_vecpfx.m_num);
834*38fd1498Szrj   return m_vecdata[ix];
835*38fd1498Szrj }
836*38fd1498Szrj 
837*38fd1498Szrj template<typename T, typename A>
838*38fd1498Szrj inline T &
839*38fd1498Szrj vec<T, A, vl_embed>::operator[] (unsigned ix)
840*38fd1498Szrj {
841*38fd1498Szrj   gcc_checking_assert (ix < m_vecpfx.m_num);
842*38fd1498Szrj   return m_vecdata[ix];
843*38fd1498Szrj }
844*38fd1498Szrj 
845*38fd1498Szrj 
846*38fd1498Szrj /* Get the final element of the vector, which must not be empty.  */
847*38fd1498Szrj 
848*38fd1498Szrj template<typename T, typename A>
849*38fd1498Szrj inline T &
850*38fd1498Szrj vec<T, A, vl_embed>::last (void)
851*38fd1498Szrj {
852*38fd1498Szrj   gcc_checking_assert (m_vecpfx.m_num > 0);
853*38fd1498Szrj   return (*this)[m_vecpfx.m_num - 1];
854*38fd1498Szrj }
855*38fd1498Szrj 
856*38fd1498Szrj 
857*38fd1498Szrj /* If this vector has space for NELEMS additional entries, return
858*38fd1498Szrj    true.  You usually only need to use this if you are doing your
859*38fd1498Szrj    own vector reallocation, for instance on an embedded vector.  This
860*38fd1498Szrj    returns true in exactly the same circumstances that vec::reserve
861*38fd1498Szrj    will.  */
862*38fd1498Szrj 
863*38fd1498Szrj template<typename T, typename A>
864*38fd1498Szrj inline bool
865*38fd1498Szrj vec<T, A, vl_embed>::space (unsigned nelems) const
866*38fd1498Szrj {
867*38fd1498Szrj   return m_vecpfx.m_alloc - m_vecpfx.m_num >= nelems;
868*38fd1498Szrj }
869*38fd1498Szrj 
870*38fd1498Szrj 
871*38fd1498Szrj /* Return iteration condition and update PTR to point to the IX'th
872*38fd1498Szrj    element of this vector.  Use this to iterate over the elements of a
873*38fd1498Szrj    vector as follows,
874*38fd1498Szrj 
875*38fd1498Szrj      for (ix = 0; vec<T, A>::iterate (v, ix, &ptr); ix++)
876*38fd1498Szrj        continue;  */
877*38fd1498Szrj 
878*38fd1498Szrj template<typename T, typename A>
879*38fd1498Szrj inline bool
880*38fd1498Szrj vec<T, A, vl_embed>::iterate (unsigned ix, T *ptr) const
881*38fd1498Szrj {
882*38fd1498Szrj   if (ix < m_vecpfx.m_num)
883*38fd1498Szrj     {
884*38fd1498Szrj       *ptr = m_vecdata[ix];
885*38fd1498Szrj       return true;
886*38fd1498Szrj     }
887*38fd1498Szrj   else
888*38fd1498Szrj     {
889*38fd1498Szrj       *ptr = 0;
890*38fd1498Szrj       return false;
891*38fd1498Szrj     }
892*38fd1498Szrj }
893*38fd1498Szrj 
894*38fd1498Szrj 
895*38fd1498Szrj /* Return iteration condition and update *PTR to point to the
896*38fd1498Szrj    IX'th element of this vector.  Use this to iterate over the
897*38fd1498Szrj    elements of a vector as follows,
898*38fd1498Szrj 
899*38fd1498Szrj      for (ix = 0; v->iterate (ix, &ptr); ix++)
900*38fd1498Szrj        continue;
901*38fd1498Szrj 
902*38fd1498Szrj    This variant is for vectors of objects.  */
903*38fd1498Szrj 
904*38fd1498Szrj template<typename T, typename A>
905*38fd1498Szrj inline bool
906*38fd1498Szrj vec<T, A, vl_embed>::iterate (unsigned ix, T **ptr) const
907*38fd1498Szrj {
908*38fd1498Szrj   if (ix < m_vecpfx.m_num)
909*38fd1498Szrj     {
910*38fd1498Szrj       *ptr = CONST_CAST (T *, &m_vecdata[ix]);
911*38fd1498Szrj       return true;
912*38fd1498Szrj     }
913*38fd1498Szrj   else
914*38fd1498Szrj     {
915*38fd1498Szrj       *ptr = 0;
916*38fd1498Szrj       return false;
917*38fd1498Szrj     }
918*38fd1498Szrj }
919*38fd1498Szrj 
920*38fd1498Szrj 
921*38fd1498Szrj /* Return a pointer to a copy of this vector.  */
922*38fd1498Szrj 
923*38fd1498Szrj template<typename T, typename A>
924*38fd1498Szrj inline vec<T, A, vl_embed> *
925*38fd1498Szrj vec<T, A, vl_embed>::copy (ALONE_MEM_STAT_DECL) const
926*38fd1498Szrj {
927*38fd1498Szrj   vec<T, A, vl_embed> *new_vec = NULL;
928*38fd1498Szrj   unsigned len = length ();
929*38fd1498Szrj   if (len)
930*38fd1498Szrj     {
931*38fd1498Szrj       vec_alloc (new_vec, len PASS_MEM_STAT);
932*38fd1498Szrj       new_vec->embedded_init (len, len);
933*38fd1498Szrj       vec_copy_construct (new_vec->address (), m_vecdata, len);
934*38fd1498Szrj     }
935*38fd1498Szrj   return new_vec;
936*38fd1498Szrj }
937*38fd1498Szrj 
938*38fd1498Szrj 
939*38fd1498Szrj /* Copy the elements from SRC to the end of this vector as if by memcpy.
940*38fd1498Szrj    The vector must have sufficient headroom available.  */
941*38fd1498Szrj 
942*38fd1498Szrj template<typename T, typename A>
943*38fd1498Szrj inline void
944*38fd1498Szrj vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> &src)
945*38fd1498Szrj {
946*38fd1498Szrj   unsigned len = src.length ();
947*38fd1498Szrj   if (len)
948*38fd1498Szrj     {
949*38fd1498Szrj       gcc_checking_assert (space (len));
950*38fd1498Szrj       vec_copy_construct (end (), src.address (), len);
951*38fd1498Szrj       m_vecpfx.m_num += len;
952*38fd1498Szrj     }
953*38fd1498Szrj }
954*38fd1498Szrj 
955*38fd1498Szrj template<typename T, typename A>
956*38fd1498Szrj inline void
957*38fd1498Szrj vec<T, A, vl_embed>::splice (const vec<T, A, vl_embed> *src)
958*38fd1498Szrj {
959*38fd1498Szrj   if (src)
960*38fd1498Szrj     splice (*src);
961*38fd1498Szrj }
962*38fd1498Szrj 
963*38fd1498Szrj 
964*38fd1498Szrj /* Push OBJ (a new element) onto the end of the vector.  There must be
965*38fd1498Szrj    sufficient space in the vector.  Return a pointer to the slot
966*38fd1498Szrj    where OBJ was inserted.  */
967*38fd1498Szrj 
968*38fd1498Szrj template<typename T, typename A>
969*38fd1498Szrj inline T *
970*38fd1498Szrj vec<T, A, vl_embed>::quick_push (const T &obj)
971*38fd1498Szrj {
972*38fd1498Szrj   gcc_checking_assert (space (1));
973*38fd1498Szrj   T *slot = &m_vecdata[m_vecpfx.m_num++];
974*38fd1498Szrj   *slot = obj;
975*38fd1498Szrj   return slot;
976*38fd1498Szrj }
977*38fd1498Szrj 
978*38fd1498Szrj 
979*38fd1498Szrj /* Pop and return the last element off the end of the vector.  */
980*38fd1498Szrj 
981*38fd1498Szrj template<typename T, typename A>
982*38fd1498Szrj inline T &
983*38fd1498Szrj vec<T, A, vl_embed>::pop (void)
984*38fd1498Szrj {
985*38fd1498Szrj   gcc_checking_assert (length () > 0);
986*38fd1498Szrj   return m_vecdata[--m_vecpfx.m_num];
987*38fd1498Szrj }
988*38fd1498Szrj 
989*38fd1498Szrj 
990*38fd1498Szrj /* Set the length of the vector to SIZE.  The new length must be less
991*38fd1498Szrj    than or equal to the current length.  This is an O(1) operation.  */
992*38fd1498Szrj 
993*38fd1498Szrj template<typename T, typename A>
994*38fd1498Szrj inline void
995*38fd1498Szrj vec<T, A, vl_embed>::truncate (unsigned size)
996*38fd1498Szrj {
997*38fd1498Szrj   gcc_checking_assert (length () >= size);
998*38fd1498Szrj   m_vecpfx.m_num = size;
999*38fd1498Szrj }
1000*38fd1498Szrj 
1001*38fd1498Szrj 
1002*38fd1498Szrj /* Insert an element, OBJ, at the IXth position of this vector.  There
1003*38fd1498Szrj    must be sufficient space.  */
1004*38fd1498Szrj 
1005*38fd1498Szrj template<typename T, typename A>
1006*38fd1498Szrj inline void
1007*38fd1498Szrj vec<T, A, vl_embed>::quick_insert (unsigned ix, const T &obj)
1008*38fd1498Szrj {
1009*38fd1498Szrj   gcc_checking_assert (length () < allocated ());
1010*38fd1498Szrj   gcc_checking_assert (ix <= length ());
1011*38fd1498Szrj   T *slot = &m_vecdata[ix];
1012*38fd1498Szrj   memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T));
1013*38fd1498Szrj   *slot = obj;
1014*38fd1498Szrj }
1015*38fd1498Szrj 
1016*38fd1498Szrj 
1017*38fd1498Szrj /* Remove an element from the IXth position of this vector.  Ordering of
1018*38fd1498Szrj    remaining elements is preserved.  This is an O(N) operation due to
1019*38fd1498Szrj    memmove.  */
1020*38fd1498Szrj 
1021*38fd1498Szrj template<typename T, typename A>
1022*38fd1498Szrj inline void
1023*38fd1498Szrj vec<T, A, vl_embed>::ordered_remove (unsigned ix)
1024*38fd1498Szrj {
1025*38fd1498Szrj   gcc_checking_assert (ix < length ());
1026*38fd1498Szrj   T *slot = &m_vecdata[ix];
1027*38fd1498Szrj   memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T));
1028*38fd1498Szrj }
1029*38fd1498Szrj 
1030*38fd1498Szrj 
1031*38fd1498Szrj /* Remove an element from the IXth position of this vector.  Ordering of
1032*38fd1498Szrj    remaining elements is destroyed.  This is an O(1) operation.  */
1033*38fd1498Szrj 
1034*38fd1498Szrj template<typename T, typename A>
1035*38fd1498Szrj inline void
1036*38fd1498Szrj vec<T, A, vl_embed>::unordered_remove (unsigned ix)
1037*38fd1498Szrj {
1038*38fd1498Szrj   gcc_checking_assert (ix < length ());
1039*38fd1498Szrj   m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num];
1040*38fd1498Szrj }
1041*38fd1498Szrj 
1042*38fd1498Szrj 
1043*38fd1498Szrj /* Remove LEN elements starting at the IXth.  Ordering is retained.
1044*38fd1498Szrj    This is an O(N) operation due to memmove.  */
1045*38fd1498Szrj 
1046*38fd1498Szrj template<typename T, typename A>
1047*38fd1498Szrj inline void
1048*38fd1498Szrj vec<T, A, vl_embed>::block_remove (unsigned ix, unsigned len)
1049*38fd1498Szrj {
1050*38fd1498Szrj   gcc_checking_assert (ix + len <= length ());
1051*38fd1498Szrj   T *slot = &m_vecdata[ix];
1052*38fd1498Szrj   m_vecpfx.m_num -= len;
1053*38fd1498Szrj   memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T));
1054*38fd1498Szrj }
1055*38fd1498Szrj 
1056*38fd1498Szrj 
1057*38fd1498Szrj /* Sort the contents of this vector with qsort.  CMP is the comparison
1058*38fd1498Szrj    function to pass to qsort.  */
1059*38fd1498Szrj 
1060*38fd1498Szrj template<typename T, typename A>
1061*38fd1498Szrj inline void
1062*38fd1498Szrj vec<T, A, vl_embed>::qsort (int (*cmp) (const void *, const void *))
1063*38fd1498Szrj {
1064*38fd1498Szrj   if (length () > 1)
1065*38fd1498Szrj     ::qsort (address (), length (), sizeof (T), cmp);
1066*38fd1498Szrj }
1067*38fd1498Szrj 
1068*38fd1498Szrj 
1069*38fd1498Szrj /* Search the contents of the sorted vector with a binary search.
1070*38fd1498Szrj    CMP is the comparison function to pass to bsearch.  */
1071*38fd1498Szrj 
1072*38fd1498Szrj template<typename T, typename A>
1073*38fd1498Szrj inline T *
1074*38fd1498Szrj vec<T, A, vl_embed>::bsearch (const void *key,
1075*38fd1498Szrj 			      int (*compar) (const void *, const void *))
1076*38fd1498Szrj {
1077*38fd1498Szrj   const void *base = this->address ();
1078*38fd1498Szrj   size_t nmemb = this->length ();
1079*38fd1498Szrj   size_t size = sizeof (T);
1080*38fd1498Szrj   /* The following is a copy of glibc stdlib-bsearch.h.  */
1081*38fd1498Szrj   size_t l, u, idx;
1082*38fd1498Szrj   const void *p;
1083*38fd1498Szrj   int comparison;
1084*38fd1498Szrj 
1085*38fd1498Szrj   l = 0;
1086*38fd1498Szrj   u = nmemb;
1087*38fd1498Szrj   while (l < u)
1088*38fd1498Szrj     {
1089*38fd1498Szrj       idx = (l + u) / 2;
1090*38fd1498Szrj       p = (const void *) (((const char *) base) + (idx * size));
1091*38fd1498Szrj       comparison = (*compar) (key, p);
1092*38fd1498Szrj       if (comparison < 0)
1093*38fd1498Szrj 	u = idx;
1094*38fd1498Szrj       else if (comparison > 0)
1095*38fd1498Szrj 	l = idx + 1;
1096*38fd1498Szrj       else
1097*38fd1498Szrj 	return (T *)const_cast<void *>(p);
1098*38fd1498Szrj     }
1099*38fd1498Szrj 
1100*38fd1498Szrj   return NULL;
1101*38fd1498Szrj }
1102*38fd1498Szrj 
1103*38fd1498Szrj /* Return true if SEARCH is an element of V.  Note that this is O(N) in the
1104*38fd1498Szrj    size of the vector and so should be used with care.  */
1105*38fd1498Szrj 
1106*38fd1498Szrj template<typename T, typename A>
1107*38fd1498Szrj inline bool
1108*38fd1498Szrj vec<T, A, vl_embed>::contains (const T &search) const
1109*38fd1498Szrj {
1110*38fd1498Szrj   unsigned int len = length ();
1111*38fd1498Szrj   for (unsigned int i = 0; i < len; i++)
1112*38fd1498Szrj     if ((*this)[i] == search)
1113*38fd1498Szrj       return true;
1114*38fd1498Szrj 
1115*38fd1498Szrj   return false;
1116*38fd1498Szrj }
1117*38fd1498Szrj 
1118*38fd1498Szrj /* Find and return the first position in which OBJ could be inserted
1119*38fd1498Szrj    without changing the ordering of this vector.  LESSTHAN is a
1120*38fd1498Szrj    function that returns true if the first argument is strictly less
1121*38fd1498Szrj    than the second.  */
1122*38fd1498Szrj 
1123*38fd1498Szrj template<typename T, typename A>
1124*38fd1498Szrj unsigned
1125*38fd1498Szrj vec<T, A, vl_embed>::lower_bound (T obj, bool (*lessthan)(const T &, const T &))
1126*38fd1498Szrj   const
1127*38fd1498Szrj {
1128*38fd1498Szrj   unsigned int len = length ();
1129*38fd1498Szrj   unsigned int half, middle;
1130*38fd1498Szrj   unsigned int first = 0;
1131*38fd1498Szrj   while (len > 0)
1132*38fd1498Szrj     {
1133*38fd1498Szrj       half = len / 2;
1134*38fd1498Szrj       middle = first;
1135*38fd1498Szrj       middle += half;
1136*38fd1498Szrj       T middle_elem = (*this)[middle];
1137*38fd1498Szrj       if (lessthan (middle_elem, obj))
1138*38fd1498Szrj 	{
1139*38fd1498Szrj 	  first = middle;
1140*38fd1498Szrj 	  ++first;
1141*38fd1498Szrj 	  len = len - half - 1;
1142*38fd1498Szrj 	}
1143*38fd1498Szrj       else
1144*38fd1498Szrj 	len = half;
1145*38fd1498Szrj     }
1146*38fd1498Szrj   return first;
1147*38fd1498Szrj }
1148*38fd1498Szrj 
1149*38fd1498Szrj 
1150*38fd1498Szrj /* Return the number of bytes needed to embed an instance of an
1151*38fd1498Szrj    embeddable vec inside another data structure.
1152*38fd1498Szrj 
1153*38fd1498Szrj    Use these methods to determine the required size and initialization
1154*38fd1498Szrj    of a vector V of type T embedded within another structure (as the
1155*38fd1498Szrj    final member):
1156*38fd1498Szrj 
1157*38fd1498Szrj    size_t vec<T, A, vl_embed>::embedded_size (unsigned alloc);
1158*38fd1498Szrj    void v->embedded_init (unsigned alloc, unsigned num);
1159*38fd1498Szrj 
1160*38fd1498Szrj    These allow the caller to perform the memory allocation.  */
1161*38fd1498Szrj 
1162*38fd1498Szrj template<typename T, typename A>
1163*38fd1498Szrj inline size_t
1164*38fd1498Szrj vec<T, A, vl_embed>::embedded_size (unsigned alloc)
1165*38fd1498Szrj {
1166*38fd1498Szrj   typedef vec<T, A, vl_embed> vec_embedded;
1167*38fd1498Szrj   return offsetof (vec_embedded, m_vecdata) + alloc * sizeof (T);
1168*38fd1498Szrj }
1169*38fd1498Szrj 
1170*38fd1498Szrj 
1171*38fd1498Szrj /* Initialize the vector to contain room for ALLOC elements and
1172*38fd1498Szrj    NUM active elements.  */
1173*38fd1498Szrj 
1174*38fd1498Szrj template<typename T, typename A>
1175*38fd1498Szrj inline void
1176*38fd1498Szrj vec<T, A, vl_embed>::embedded_init (unsigned alloc, unsigned num, unsigned aut)
1177*38fd1498Szrj {
1178*38fd1498Szrj   m_vecpfx.m_alloc = alloc;
1179*38fd1498Szrj   m_vecpfx.m_using_auto_storage = aut;
1180*38fd1498Szrj   m_vecpfx.m_num = num;
1181*38fd1498Szrj }
1182*38fd1498Szrj 
1183*38fd1498Szrj 
1184*38fd1498Szrj /* Grow the vector to a specific length.  LEN must be as long or longer than
1185*38fd1498Szrj    the current length.  The new elements are uninitialized.  */
1186*38fd1498Szrj 
1187*38fd1498Szrj template<typename T, typename A>
1188*38fd1498Szrj inline void
1189*38fd1498Szrj vec<T, A, vl_embed>::quick_grow (unsigned len)
1190*38fd1498Szrj {
1191*38fd1498Szrj   gcc_checking_assert (length () <= len && len <= m_vecpfx.m_alloc);
1192*38fd1498Szrj   m_vecpfx.m_num = len;
1193*38fd1498Szrj }
1194*38fd1498Szrj 
1195*38fd1498Szrj 
1196*38fd1498Szrj /* Grow the vector to a specific length.  LEN must be as long or longer than
1197*38fd1498Szrj    the current length.  The new elements are initialized to zero.  */
1198*38fd1498Szrj 
1199*38fd1498Szrj template<typename T, typename A>
1200*38fd1498Szrj inline void
1201*38fd1498Szrj vec<T, A, vl_embed>::quick_grow_cleared (unsigned len)
1202*38fd1498Szrj {
1203*38fd1498Szrj   unsigned oldlen = length ();
1204*38fd1498Szrj   size_t growby = len - oldlen;
1205*38fd1498Szrj   quick_grow (len);
1206*38fd1498Szrj   if (growby != 0)
1207*38fd1498Szrj     vec_default_construct (address () + oldlen, growby);
1208*38fd1498Szrj }
1209*38fd1498Szrj 
1210*38fd1498Szrj /* Garbage collection support for vec<T, A, vl_embed>.  */
1211*38fd1498Szrj 
1212*38fd1498Szrj template<typename T>
1213*38fd1498Szrj void
1214*38fd1498Szrj gt_ggc_mx (vec<T, va_gc> *v)
1215*38fd1498Szrj {
1216*38fd1498Szrj   extern void gt_ggc_mx (T &);
1217*38fd1498Szrj   for (unsigned i = 0; i < v->length (); i++)
1218*38fd1498Szrj     gt_ggc_mx ((*v)[i]);
1219*38fd1498Szrj }
1220*38fd1498Szrj 
1221*38fd1498Szrj template<typename T>
1222*38fd1498Szrj void
1223*38fd1498Szrj gt_ggc_mx (vec<T, va_gc_atomic, vl_embed> *v ATTRIBUTE_UNUSED)
1224*38fd1498Szrj {
1225*38fd1498Szrj   /* Nothing to do.  Vectors of atomic types wrt GC do not need to
1226*38fd1498Szrj      be traversed.  */
1227*38fd1498Szrj }
1228*38fd1498Szrj 
1229*38fd1498Szrj 
1230*38fd1498Szrj /* PCH support for vec<T, A, vl_embed>.  */
1231*38fd1498Szrj 
1232*38fd1498Szrj template<typename T, typename A>
1233*38fd1498Szrj void
1234*38fd1498Szrj gt_pch_nx (vec<T, A, vl_embed> *v)
1235*38fd1498Szrj {
1236*38fd1498Szrj   extern void gt_pch_nx (T &);
1237*38fd1498Szrj   for (unsigned i = 0; i < v->length (); i++)
1238*38fd1498Szrj     gt_pch_nx ((*v)[i]);
1239*38fd1498Szrj }
1240*38fd1498Szrj 
1241*38fd1498Szrj template<typename T, typename A>
1242*38fd1498Szrj void
1243*38fd1498Szrj gt_pch_nx (vec<T *, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1244*38fd1498Szrj {
1245*38fd1498Szrj   for (unsigned i = 0; i < v->length (); i++)
1246*38fd1498Szrj     op (&((*v)[i]), cookie);
1247*38fd1498Szrj }
1248*38fd1498Szrj 
1249*38fd1498Szrj template<typename T, typename A>
1250*38fd1498Szrj void
1251*38fd1498Szrj gt_pch_nx (vec<T, A, vl_embed> *v, gt_pointer_operator op, void *cookie)
1252*38fd1498Szrj {
1253*38fd1498Szrj   extern void gt_pch_nx (T *, gt_pointer_operator, void *);
1254*38fd1498Szrj   for (unsigned i = 0; i < v->length (); i++)
1255*38fd1498Szrj     gt_pch_nx (&((*v)[i]), op, cookie);
1256*38fd1498Szrj }
1257*38fd1498Szrj 
1258*38fd1498Szrj 
1259*38fd1498Szrj /* Space efficient vector.  These vectors can grow dynamically and are
1260*38fd1498Szrj    allocated together with their control data.  They are suited to be
1261*38fd1498Szrj    included in data structures.  Prior to initial allocation, they
1262*38fd1498Szrj    only take a single word of storage.
1263*38fd1498Szrj 
1264*38fd1498Szrj    These vectors are implemented as a pointer to an embeddable vector.
1265*38fd1498Szrj    The semantics allow for this pointer to be NULL to represent empty
1266*38fd1498Szrj    vectors.  This way, empty vectors occupy minimal space in the
1267*38fd1498Szrj    structure containing them.
1268*38fd1498Szrj 
1269*38fd1498Szrj    Properties:
1270*38fd1498Szrj 
1271*38fd1498Szrj 	- The whole vector and control data are allocated in a single
1272*38fd1498Szrj 	  contiguous block.
1273*38fd1498Szrj   	- The whole vector may be re-allocated.
1274*38fd1498Szrj   	- Vector data may grow and shrink.
1275*38fd1498Szrj   	- Access and manipulation requires a pointer test and
1276*38fd1498Szrj 	  indirection.
1277*38fd1498Szrj 	- It requires 1 word of storage (prior to vector allocation).
1278*38fd1498Szrj 
1279*38fd1498Szrj 
1280*38fd1498Szrj    Limitations:
1281*38fd1498Szrj 
1282*38fd1498Szrj    These vectors must be PODs because they are stored in unions.
1283*38fd1498Szrj    (http://en.wikipedia.org/wiki/Plain_old_data_structures).
1284*38fd1498Szrj    As long as we use C++03, we cannot have constructors nor
1285*38fd1498Szrj    destructors in classes that are stored in unions.  */
1286*38fd1498Szrj 
1287*38fd1498Szrj template<typename T>
1288*38fd1498Szrj struct vec<T, va_heap, vl_ptr>
1289*38fd1498Szrj {
1290*38fd1498Szrj public:
1291*38fd1498Szrj   /* Memory allocation and deallocation for the embedded vector.
1292*38fd1498Szrj      Needed because we cannot have proper ctors/dtors defined.  */
1293*38fd1498Szrj   void create (unsigned nelems CXX_MEM_STAT_INFO);
1294*38fd1498Szrj   void release (void);
1295*38fd1498Szrj 
1296*38fd1498Szrj   /* Vector operations.  */
1297*38fd1498Szrj   bool exists (void) const
1298*38fd1498Szrj   { return m_vec != NULL; }
1299*38fd1498Szrj 
1300*38fd1498Szrj   bool is_empty (void) const
1301*38fd1498Szrj   { return m_vec ? m_vec->is_empty () : true; }
1302*38fd1498Szrj 
1303*38fd1498Szrj   unsigned length (void) const
1304*38fd1498Szrj   { return m_vec ? m_vec->length () : 0; }
1305*38fd1498Szrj 
1306*38fd1498Szrj   T *address (void)
1307*38fd1498Szrj   { return m_vec ? m_vec->m_vecdata : NULL; }
1308*38fd1498Szrj 
1309*38fd1498Szrj   const T *address (void) const
1310*38fd1498Szrj   { return m_vec ? m_vec->m_vecdata : NULL; }
1311*38fd1498Szrj 
1312*38fd1498Szrj   T *begin () { return address (); }
1313*38fd1498Szrj   const T *begin () const { return address (); }
1314*38fd1498Szrj   T *end () { return begin () + length (); }
1315*38fd1498Szrj   const T *end () const { return begin () + length (); }
1316*38fd1498Szrj   const T &operator[] (unsigned ix) const
1317*38fd1498Szrj   { return (*m_vec)[ix]; }
1318*38fd1498Szrj 
1319*38fd1498Szrj   bool operator!=(const vec &other) const
1320*38fd1498Szrj   { return !(*this == other); }
1321*38fd1498Szrj 
1322*38fd1498Szrj   bool operator==(const vec &other) const
1323*38fd1498Szrj   { return address () == other.address (); }
1324*38fd1498Szrj 
1325*38fd1498Szrj   T &operator[] (unsigned ix)
1326*38fd1498Szrj   { return (*m_vec)[ix]; }
1327*38fd1498Szrj 
1328*38fd1498Szrj   T &last (void)
1329*38fd1498Szrj   { return m_vec->last (); }
1330*38fd1498Szrj 
1331*38fd1498Szrj   bool space (int nelems) const
1332*38fd1498Szrj   { return m_vec ? m_vec->space (nelems) : nelems == 0; }
1333*38fd1498Szrj 
1334*38fd1498Szrj   bool iterate (unsigned ix, T *p) const;
1335*38fd1498Szrj   bool iterate (unsigned ix, T **p) const;
1336*38fd1498Szrj   vec copy (ALONE_CXX_MEM_STAT_INFO) const;
1337*38fd1498Szrj   bool reserve (unsigned, bool = false CXX_MEM_STAT_INFO);
1338*38fd1498Szrj   bool reserve_exact (unsigned CXX_MEM_STAT_INFO);
1339*38fd1498Szrj   void splice (const vec &);
1340*38fd1498Szrj   void safe_splice (const vec & CXX_MEM_STAT_INFO);
1341*38fd1498Szrj   T *quick_push (const T &);
1342*38fd1498Szrj   T *safe_push (const T &CXX_MEM_STAT_INFO);
1343*38fd1498Szrj   T &pop (void);
1344*38fd1498Szrj   void truncate (unsigned);
1345*38fd1498Szrj   void safe_grow (unsigned CXX_MEM_STAT_INFO);
1346*38fd1498Szrj   void safe_grow_cleared (unsigned CXX_MEM_STAT_INFO);
1347*38fd1498Szrj   void quick_grow (unsigned);
1348*38fd1498Szrj   void quick_grow_cleared (unsigned);
1349*38fd1498Szrj   void quick_insert (unsigned, const T &);
1350*38fd1498Szrj   void safe_insert (unsigned, const T & CXX_MEM_STAT_INFO);
1351*38fd1498Szrj   void ordered_remove (unsigned);
1352*38fd1498Szrj   void unordered_remove (unsigned);
1353*38fd1498Szrj   void block_remove (unsigned, unsigned);
1354*38fd1498Szrj   void qsort (int (*) (const void *, const void *));
1355*38fd1498Szrj   T *bsearch (const void *key, int (*compar)(const void *, const void *));
1356*38fd1498Szrj   unsigned lower_bound (T, bool (*)(const T &, const T &)) const;
1357*38fd1498Szrj   bool contains (const T &search) const;
1358*38fd1498Szrj 
1359*38fd1498Szrj   bool using_auto_storage () const;
1360*38fd1498Szrj 
1361*38fd1498Szrj   /* FIXME - This field should be private, but we need to cater to
1362*38fd1498Szrj 	     compilers that have stricter notions of PODness for types.  */
1363*38fd1498Szrj   vec<T, va_heap, vl_embed> *m_vec;
1364*38fd1498Szrj };
1365*38fd1498Szrj 
1366*38fd1498Szrj 
1367*38fd1498Szrj /* auto_vec is a subclass of vec that automatically manages creating and
1368*38fd1498Szrj    releasing the internal vector. If N is non zero then it has N elements of
1369*38fd1498Szrj    internal storage.  The default is no internal storage, and you probably only
1370*38fd1498Szrj    want to ask for internal storage for vectors on the stack because if the
1371*38fd1498Szrj    size of the vector is larger than the internal storage that space is wasted.
1372*38fd1498Szrj    */
1373*38fd1498Szrj template<typename T, size_t N = 0>
1374*38fd1498Szrj class auto_vec : public vec<T, va_heap>
1375*38fd1498Szrj {
1376*38fd1498Szrj public:
1377*38fd1498Szrj   auto_vec ()
1378*38fd1498Szrj   {
1379*38fd1498Szrj     m_auto.embedded_init (MAX (N, 2), 0, 1);
1380*38fd1498Szrj     this->m_vec = &m_auto;
1381*38fd1498Szrj   }
1382*38fd1498Szrj 
1383*38fd1498Szrj   auto_vec (size_t s)
1384*38fd1498Szrj   {
1385*38fd1498Szrj     if (s > N)
1386*38fd1498Szrj       {
1387*38fd1498Szrj 	this->create (s);
1388*38fd1498Szrj 	return;
1389*38fd1498Szrj       }
1390*38fd1498Szrj 
1391*38fd1498Szrj     m_auto.embedded_init (MAX (N, 2), 0, 1);
1392*38fd1498Szrj     this->m_vec = &m_auto;
1393*38fd1498Szrj   }
1394*38fd1498Szrj 
1395*38fd1498Szrj   ~auto_vec ()
1396*38fd1498Szrj   {
1397*38fd1498Szrj     this->release ();
1398*38fd1498Szrj   }
1399*38fd1498Szrj 
1400*38fd1498Szrj private:
1401*38fd1498Szrj   vec<T, va_heap, vl_embed> m_auto;
1402*38fd1498Szrj   T m_data[MAX (N - 1, 1)];
1403*38fd1498Szrj };
1404*38fd1498Szrj 
1405*38fd1498Szrj /* auto_vec is a sub class of vec whose storage is released when it is
1406*38fd1498Szrj   destroyed. */
1407*38fd1498Szrj template<typename T>
1408*38fd1498Szrj class auto_vec<T, 0> : public vec<T, va_heap>
1409*38fd1498Szrj {
1410*38fd1498Szrj public:
1411*38fd1498Szrj   auto_vec () { this->m_vec = NULL; }
1412*38fd1498Szrj   auto_vec (size_t n) { this->create (n); }
1413*38fd1498Szrj   ~auto_vec () { this->release (); }
1414*38fd1498Szrj };
1415*38fd1498Szrj 
1416*38fd1498Szrj 
1417*38fd1498Szrj /* Allocate heap memory for pointer V and create the internal vector
1418*38fd1498Szrj    with space for NELEMS elements.  If NELEMS is 0, the internal
1419*38fd1498Szrj    vector is initialized to empty.  */
1420*38fd1498Szrj 
1421*38fd1498Szrj template<typename T>
1422*38fd1498Szrj inline void
1423*38fd1498Szrj vec_alloc (vec<T> *&v, unsigned nelems CXX_MEM_STAT_INFO)
1424*38fd1498Szrj {
1425*38fd1498Szrj   v = new vec<T>;
1426*38fd1498Szrj   v->create (nelems PASS_MEM_STAT);
1427*38fd1498Szrj }
1428*38fd1498Szrj 
1429*38fd1498Szrj 
1430*38fd1498Szrj /* Conditionally allocate heap memory for VEC and its internal vector.  */
1431*38fd1498Szrj 
1432*38fd1498Szrj template<typename T>
1433*38fd1498Szrj inline void
1434*38fd1498Szrj vec_check_alloc (vec<T, va_heap> *&vec, unsigned nelems CXX_MEM_STAT_INFO)
1435*38fd1498Szrj {
1436*38fd1498Szrj   if (!vec)
1437*38fd1498Szrj     vec_alloc (vec, nelems PASS_MEM_STAT);
1438*38fd1498Szrj }
1439*38fd1498Szrj 
1440*38fd1498Szrj 
1441*38fd1498Szrj /* Free the heap memory allocated by vector V and set it to NULL.  */
1442*38fd1498Szrj 
1443*38fd1498Szrj template<typename T>
1444*38fd1498Szrj inline void
1445*38fd1498Szrj vec_free (vec<T> *&v)
1446*38fd1498Szrj {
1447*38fd1498Szrj   if (v == NULL)
1448*38fd1498Szrj     return;
1449*38fd1498Szrj 
1450*38fd1498Szrj   v->release ();
1451*38fd1498Szrj   delete v;
1452*38fd1498Szrj   v = NULL;
1453*38fd1498Szrj }
1454*38fd1498Szrj 
1455*38fd1498Szrj 
1456*38fd1498Szrj /* Return iteration condition and update PTR to point to the IX'th
1457*38fd1498Szrj    element of this vector.  Use this to iterate over the elements of a
1458*38fd1498Szrj    vector as follows,
1459*38fd1498Szrj 
1460*38fd1498Szrj      for (ix = 0; v.iterate (ix, &ptr); ix++)
1461*38fd1498Szrj        continue;  */
1462*38fd1498Szrj 
1463*38fd1498Szrj template<typename T>
1464*38fd1498Szrj inline bool
1465*38fd1498Szrj vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T *ptr) const
1466*38fd1498Szrj {
1467*38fd1498Szrj   if (m_vec)
1468*38fd1498Szrj     return m_vec->iterate (ix, ptr);
1469*38fd1498Szrj   else
1470*38fd1498Szrj     {
1471*38fd1498Szrj       *ptr = 0;
1472*38fd1498Szrj       return false;
1473*38fd1498Szrj     }
1474*38fd1498Szrj }
1475*38fd1498Szrj 
1476*38fd1498Szrj 
1477*38fd1498Szrj /* Return iteration condition and update *PTR to point to the
1478*38fd1498Szrj    IX'th element of this vector.  Use this to iterate over the
1479*38fd1498Szrj    elements of a vector as follows,
1480*38fd1498Szrj 
1481*38fd1498Szrj      for (ix = 0; v->iterate (ix, &ptr); ix++)
1482*38fd1498Szrj        continue;
1483*38fd1498Szrj 
1484*38fd1498Szrj    This variant is for vectors of objects.  */
1485*38fd1498Szrj 
1486*38fd1498Szrj template<typename T>
1487*38fd1498Szrj inline bool
1488*38fd1498Szrj vec<T, va_heap, vl_ptr>::iterate (unsigned ix, T **ptr) const
1489*38fd1498Szrj {
1490*38fd1498Szrj   if (m_vec)
1491*38fd1498Szrj     return m_vec->iterate (ix, ptr);
1492*38fd1498Szrj   else
1493*38fd1498Szrj     {
1494*38fd1498Szrj       *ptr = 0;
1495*38fd1498Szrj       return false;
1496*38fd1498Szrj     }
1497*38fd1498Szrj }
1498*38fd1498Szrj 
1499*38fd1498Szrj 
1500*38fd1498Szrj /* Convenience macro for forward iteration.  */
1501*38fd1498Szrj #define FOR_EACH_VEC_ELT(V, I, P)			\
1502*38fd1498Szrj   for (I = 0; (V).iterate ((I), &(P)); ++(I))
1503*38fd1498Szrj 
1504*38fd1498Szrj #define FOR_EACH_VEC_SAFE_ELT(V, I, P)			\
1505*38fd1498Szrj   for (I = 0; vec_safe_iterate ((V), (I), &(P)); ++(I))
1506*38fd1498Szrj 
1507*38fd1498Szrj /* Likewise, but start from FROM rather than 0.  */
1508*38fd1498Szrj #define FOR_EACH_VEC_ELT_FROM(V, I, P, FROM)		\
1509*38fd1498Szrj   for (I = (FROM); (V).iterate ((I), &(P)); ++(I))
1510*38fd1498Szrj 
1511*38fd1498Szrj /* Convenience macro for reverse iteration.  */
1512*38fd1498Szrj #define FOR_EACH_VEC_ELT_REVERSE(V, I, P)		\
1513*38fd1498Szrj   for (I = (V).length () - 1;				\
1514*38fd1498Szrj        (V).iterate ((I), &(P));				\
1515*38fd1498Szrj        (I)--)
1516*38fd1498Szrj 
1517*38fd1498Szrj #define FOR_EACH_VEC_SAFE_ELT_REVERSE(V, I, P)		\
1518*38fd1498Szrj   for (I = vec_safe_length (V) - 1;			\
1519*38fd1498Szrj        vec_safe_iterate ((V), (I), &(P));	\
1520*38fd1498Szrj        (I)--)
1521*38fd1498Szrj 
1522*38fd1498Szrj 
1523*38fd1498Szrj /* Return a copy of this vector.  */
1524*38fd1498Szrj 
1525*38fd1498Szrj template<typename T>
1526*38fd1498Szrj inline vec<T, va_heap, vl_ptr>
1527*38fd1498Szrj vec<T, va_heap, vl_ptr>::copy (ALONE_MEM_STAT_DECL) const
1528*38fd1498Szrj {
1529*38fd1498Szrj   vec<T, va_heap, vl_ptr> new_vec = vNULL;
1530*38fd1498Szrj   if (length ())
1531*38fd1498Szrj     new_vec.m_vec = m_vec->copy ();
1532*38fd1498Szrj   return new_vec;
1533*38fd1498Szrj }
1534*38fd1498Szrj 
1535*38fd1498Szrj 
1536*38fd1498Szrj /* Ensure that the vector has at least RESERVE slots available (if
1537*38fd1498Szrj    EXACT is false), or exactly RESERVE slots available (if EXACT is
1538*38fd1498Szrj    true).
1539*38fd1498Szrj 
1540*38fd1498Szrj    This may create additional headroom if EXACT is false.
1541*38fd1498Szrj 
1542*38fd1498Szrj    Note that this can cause the embedded vector to be reallocated.
1543*38fd1498Szrj    Returns true iff reallocation actually occurred.  */
1544*38fd1498Szrj 
1545*38fd1498Szrj template<typename T>
1546*38fd1498Szrj inline bool
1547*38fd1498Szrj vec<T, va_heap, vl_ptr>::reserve (unsigned nelems, bool exact MEM_STAT_DECL)
1548*38fd1498Szrj {
1549*38fd1498Szrj   if (space (nelems))
1550*38fd1498Szrj     return false;
1551*38fd1498Szrj 
1552*38fd1498Szrj   /* For now play a game with va_heap::reserve to hide our auto storage if any,
1553*38fd1498Szrj      this is necessary because it doesn't have enough information to know the
1554*38fd1498Szrj      embedded vector is in auto storage, and so should not be freed.  */
1555*38fd1498Szrj   vec<T, va_heap, vl_embed> *oldvec = m_vec;
1556*38fd1498Szrj   unsigned int oldsize = 0;
1557*38fd1498Szrj   bool handle_auto_vec = m_vec && using_auto_storage ();
1558*38fd1498Szrj   if (handle_auto_vec)
1559*38fd1498Szrj     {
1560*38fd1498Szrj       m_vec = NULL;
1561*38fd1498Szrj       oldsize = oldvec->length ();
1562*38fd1498Szrj       nelems += oldsize;
1563*38fd1498Szrj     }
1564*38fd1498Szrj 
1565*38fd1498Szrj   va_heap::reserve (m_vec, nelems, exact PASS_MEM_STAT);
1566*38fd1498Szrj   if (handle_auto_vec)
1567*38fd1498Szrj     {
1568*38fd1498Szrj       vec_copy_construct (m_vec->address (), oldvec->address (), oldsize);
1569*38fd1498Szrj       m_vec->m_vecpfx.m_num = oldsize;
1570*38fd1498Szrj     }
1571*38fd1498Szrj 
1572*38fd1498Szrj   return true;
1573*38fd1498Szrj }
1574*38fd1498Szrj 
1575*38fd1498Szrj 
1576*38fd1498Szrj /* Ensure that this vector has exactly NELEMS slots available.  This
1577*38fd1498Szrj    will not create additional headroom.  Note this can cause the
1578*38fd1498Szrj    embedded vector to be reallocated.  Returns true iff reallocation
1579*38fd1498Szrj    actually occurred.  */
1580*38fd1498Szrj 
1581*38fd1498Szrj template<typename T>
1582*38fd1498Szrj inline bool
1583*38fd1498Szrj vec<T, va_heap, vl_ptr>::reserve_exact (unsigned nelems MEM_STAT_DECL)
1584*38fd1498Szrj {
1585*38fd1498Szrj   return reserve (nelems, true PASS_MEM_STAT);
1586*38fd1498Szrj }
1587*38fd1498Szrj 
1588*38fd1498Szrj 
1589*38fd1498Szrj /* Create the internal vector and reserve NELEMS for it.  This is
1590*38fd1498Szrj    exactly like vec::reserve, but the internal vector is
1591*38fd1498Szrj    unconditionally allocated from scratch.  The old one, if it
1592*38fd1498Szrj    existed, is lost.  */
1593*38fd1498Szrj 
1594*38fd1498Szrj template<typename T>
1595*38fd1498Szrj inline void
1596*38fd1498Szrj vec<T, va_heap, vl_ptr>::create (unsigned nelems MEM_STAT_DECL)
1597*38fd1498Szrj {
1598*38fd1498Szrj   m_vec = NULL;
1599*38fd1498Szrj   if (nelems > 0)
1600*38fd1498Szrj     reserve_exact (nelems PASS_MEM_STAT);
1601*38fd1498Szrj }
1602*38fd1498Szrj 
1603*38fd1498Szrj 
1604*38fd1498Szrj /* Free the memory occupied by the embedded vector.  */
1605*38fd1498Szrj 
1606*38fd1498Szrj template<typename T>
1607*38fd1498Szrj inline void
1608*38fd1498Szrj vec<T, va_heap, vl_ptr>::release (void)
1609*38fd1498Szrj {
1610*38fd1498Szrj   if (!m_vec)
1611*38fd1498Szrj     return;
1612*38fd1498Szrj 
1613*38fd1498Szrj   if (using_auto_storage ())
1614*38fd1498Szrj     {
1615*38fd1498Szrj       m_vec->m_vecpfx.m_num = 0;
1616*38fd1498Szrj       return;
1617*38fd1498Szrj     }
1618*38fd1498Szrj 
1619*38fd1498Szrj   va_heap::release (m_vec);
1620*38fd1498Szrj }
1621*38fd1498Szrj 
1622*38fd1498Szrj /* Copy the elements from SRC to the end of this vector as if by memcpy.
1623*38fd1498Szrj    SRC and this vector must be allocated with the same memory
1624*38fd1498Szrj    allocation mechanism. This vector is assumed to have sufficient
1625*38fd1498Szrj    headroom available.  */
1626*38fd1498Szrj 
1627*38fd1498Szrj template<typename T>
1628*38fd1498Szrj inline void
1629*38fd1498Szrj vec<T, va_heap, vl_ptr>::splice (const vec<T, va_heap, vl_ptr> &src)
1630*38fd1498Szrj {
1631*38fd1498Szrj   if (src.m_vec)
1632*38fd1498Szrj     m_vec->splice (*(src.m_vec));
1633*38fd1498Szrj }
1634*38fd1498Szrj 
1635*38fd1498Szrj 
1636*38fd1498Szrj /* Copy the elements in SRC to the end of this vector as if by memcpy.
1637*38fd1498Szrj    SRC and this vector must be allocated with the same mechanism.
1638*38fd1498Szrj    If there is not enough headroom in this vector, it will be reallocated
1639*38fd1498Szrj    as needed.  */
1640*38fd1498Szrj 
1641*38fd1498Szrj template<typename T>
1642*38fd1498Szrj inline void
1643*38fd1498Szrj vec<T, va_heap, vl_ptr>::safe_splice (const vec<T, va_heap, vl_ptr> &src
1644*38fd1498Szrj 				      MEM_STAT_DECL)
1645*38fd1498Szrj {
1646*38fd1498Szrj   if (src.length ())
1647*38fd1498Szrj     {
1648*38fd1498Szrj       reserve_exact (src.length ());
1649*38fd1498Szrj       splice (src);
1650*38fd1498Szrj     }
1651*38fd1498Szrj }
1652*38fd1498Szrj 
1653*38fd1498Szrj 
1654*38fd1498Szrj /* Push OBJ (a new element) onto the end of the vector.  There must be
1655*38fd1498Szrj    sufficient space in the vector.  Return a pointer to the slot
1656*38fd1498Szrj    where OBJ was inserted.  */
1657*38fd1498Szrj 
1658*38fd1498Szrj template<typename T>
1659*38fd1498Szrj inline T *
1660*38fd1498Szrj vec<T, va_heap, vl_ptr>::quick_push (const T &obj)
1661*38fd1498Szrj {
1662*38fd1498Szrj   return m_vec->quick_push (obj);
1663*38fd1498Szrj }
1664*38fd1498Szrj 
1665*38fd1498Szrj 
1666*38fd1498Szrj /* Push a new element OBJ onto the end of this vector.  Reallocates
1667*38fd1498Szrj    the embedded vector, if needed.  Return a pointer to the slot where
1668*38fd1498Szrj    OBJ was inserted.  */
1669*38fd1498Szrj 
1670*38fd1498Szrj template<typename T>
1671*38fd1498Szrj inline T *
1672*38fd1498Szrj vec<T, va_heap, vl_ptr>::safe_push (const T &obj MEM_STAT_DECL)
1673*38fd1498Szrj {
1674*38fd1498Szrj   reserve (1, false PASS_MEM_STAT);
1675*38fd1498Szrj   return quick_push (obj);
1676*38fd1498Szrj }
1677*38fd1498Szrj 
1678*38fd1498Szrj 
1679*38fd1498Szrj /* Pop and return the last element off the end of the vector.  */
1680*38fd1498Szrj 
1681*38fd1498Szrj template<typename T>
1682*38fd1498Szrj inline T &
1683*38fd1498Szrj vec<T, va_heap, vl_ptr>::pop (void)
1684*38fd1498Szrj {
1685*38fd1498Szrj   return m_vec->pop ();
1686*38fd1498Szrj }
1687*38fd1498Szrj 
1688*38fd1498Szrj 
1689*38fd1498Szrj /* Set the length of the vector to LEN.  The new length must be less
1690*38fd1498Szrj    than or equal to the current length.  This is an O(1) operation.  */
1691*38fd1498Szrj 
1692*38fd1498Szrj template<typename T>
1693*38fd1498Szrj inline void
1694*38fd1498Szrj vec<T, va_heap, vl_ptr>::truncate (unsigned size)
1695*38fd1498Szrj {
1696*38fd1498Szrj   if (m_vec)
1697*38fd1498Szrj     m_vec->truncate (size);
1698*38fd1498Szrj   else
1699*38fd1498Szrj     gcc_checking_assert (size == 0);
1700*38fd1498Szrj }
1701*38fd1498Szrj 
1702*38fd1498Szrj 
1703*38fd1498Szrj /* Grow the vector to a specific length.  LEN must be as long or
1704*38fd1498Szrj    longer than the current length.  The new elements are
1705*38fd1498Szrj    uninitialized.  Reallocate the internal vector, if needed.  */
1706*38fd1498Szrj 
1707*38fd1498Szrj template<typename T>
1708*38fd1498Szrj inline void
1709*38fd1498Szrj vec<T, va_heap, vl_ptr>::safe_grow (unsigned len MEM_STAT_DECL)
1710*38fd1498Szrj {
1711*38fd1498Szrj   unsigned oldlen = length ();
1712*38fd1498Szrj   gcc_checking_assert (oldlen <= len);
1713*38fd1498Szrj   reserve_exact (len - oldlen PASS_MEM_STAT);
1714*38fd1498Szrj   if (m_vec)
1715*38fd1498Szrj     m_vec->quick_grow (len);
1716*38fd1498Szrj   else
1717*38fd1498Szrj     gcc_checking_assert (len == 0);
1718*38fd1498Szrj }
1719*38fd1498Szrj 
1720*38fd1498Szrj 
1721*38fd1498Szrj /* Grow the embedded vector to a specific length.  LEN must be as
1722*38fd1498Szrj    long or longer than the current length.  The new elements are
1723*38fd1498Szrj    initialized to zero.  Reallocate the internal vector, if needed.  */
1724*38fd1498Szrj 
1725*38fd1498Szrj template<typename T>
1726*38fd1498Szrj inline void
1727*38fd1498Szrj vec<T, va_heap, vl_ptr>::safe_grow_cleared (unsigned len MEM_STAT_DECL)
1728*38fd1498Szrj {
1729*38fd1498Szrj   unsigned oldlen = length ();
1730*38fd1498Szrj   size_t growby = len - oldlen;
1731*38fd1498Szrj   safe_grow (len PASS_MEM_STAT);
1732*38fd1498Szrj   if (growby != 0)
1733*38fd1498Szrj     vec_default_construct (address () + oldlen, growby);
1734*38fd1498Szrj }
1735*38fd1498Szrj 
1736*38fd1498Szrj 
1737*38fd1498Szrj /* Same as vec::safe_grow but without reallocation of the internal vector.
1738*38fd1498Szrj    If the vector cannot be extended, a runtime assertion will be triggered.  */
1739*38fd1498Szrj 
1740*38fd1498Szrj template<typename T>
1741*38fd1498Szrj inline void
1742*38fd1498Szrj vec<T, va_heap, vl_ptr>::quick_grow (unsigned len)
1743*38fd1498Szrj {
1744*38fd1498Szrj   gcc_checking_assert (m_vec);
1745*38fd1498Szrj   m_vec->quick_grow (len);
1746*38fd1498Szrj }
1747*38fd1498Szrj 
1748*38fd1498Szrj 
1749*38fd1498Szrj /* Same as vec::quick_grow_cleared but without reallocation of the
1750*38fd1498Szrj    internal vector. If the vector cannot be extended, a runtime
1751*38fd1498Szrj    assertion will be triggered.  */
1752*38fd1498Szrj 
1753*38fd1498Szrj template<typename T>
1754*38fd1498Szrj inline void
1755*38fd1498Szrj vec<T, va_heap, vl_ptr>::quick_grow_cleared (unsigned len)
1756*38fd1498Szrj {
1757*38fd1498Szrj   gcc_checking_assert (m_vec);
1758*38fd1498Szrj   m_vec->quick_grow_cleared (len);
1759*38fd1498Szrj }
1760*38fd1498Szrj 
1761*38fd1498Szrj 
1762*38fd1498Szrj /* Insert an element, OBJ, at the IXth position of this vector.  There
1763*38fd1498Szrj    must be sufficient space.  */
1764*38fd1498Szrj 
1765*38fd1498Szrj template<typename T>
1766*38fd1498Szrj inline void
1767*38fd1498Szrj vec<T, va_heap, vl_ptr>::quick_insert (unsigned ix, const T &obj)
1768*38fd1498Szrj {
1769*38fd1498Szrj   m_vec->quick_insert (ix, obj);
1770*38fd1498Szrj }
1771*38fd1498Szrj 
1772*38fd1498Szrj 
1773*38fd1498Szrj /* Insert an element, OBJ, at the IXth position of the vector.
1774*38fd1498Szrj    Reallocate the embedded vector, if necessary.  */
1775*38fd1498Szrj 
1776*38fd1498Szrj template<typename T>
1777*38fd1498Szrj inline void
1778*38fd1498Szrj vec<T, va_heap, vl_ptr>::safe_insert (unsigned ix, const T &obj MEM_STAT_DECL)
1779*38fd1498Szrj {
1780*38fd1498Szrj   reserve (1, false PASS_MEM_STAT);
1781*38fd1498Szrj   quick_insert (ix, obj);
1782*38fd1498Szrj }
1783*38fd1498Szrj 
1784*38fd1498Szrj 
1785*38fd1498Szrj /* Remove an element from the IXth position of this vector.  Ordering of
1786*38fd1498Szrj    remaining elements is preserved.  This is an O(N) operation due to
1787*38fd1498Szrj    a memmove.  */
1788*38fd1498Szrj 
1789*38fd1498Szrj template<typename T>
1790*38fd1498Szrj inline void
1791*38fd1498Szrj vec<T, va_heap, vl_ptr>::ordered_remove (unsigned ix)
1792*38fd1498Szrj {
1793*38fd1498Szrj   m_vec->ordered_remove (ix);
1794*38fd1498Szrj }
1795*38fd1498Szrj 
1796*38fd1498Szrj 
1797*38fd1498Szrj /* Remove an element from the IXth position of this vector.  Ordering
1798*38fd1498Szrj    of remaining elements is destroyed.  This is an O(1) operation.  */
1799*38fd1498Szrj 
1800*38fd1498Szrj template<typename T>
1801*38fd1498Szrj inline void
1802*38fd1498Szrj vec<T, va_heap, vl_ptr>::unordered_remove (unsigned ix)
1803*38fd1498Szrj {
1804*38fd1498Szrj   m_vec->unordered_remove (ix);
1805*38fd1498Szrj }
1806*38fd1498Szrj 
1807*38fd1498Szrj 
1808*38fd1498Szrj /* Remove LEN elements starting at the IXth.  Ordering is retained.
1809*38fd1498Szrj    This is an O(N) operation due to memmove.  */
1810*38fd1498Szrj 
1811*38fd1498Szrj template<typename T>
1812*38fd1498Szrj inline void
1813*38fd1498Szrj vec<T, va_heap, vl_ptr>::block_remove (unsigned ix, unsigned len)
1814*38fd1498Szrj {
1815*38fd1498Szrj   m_vec->block_remove (ix, len);
1816*38fd1498Szrj }
1817*38fd1498Szrj 
1818*38fd1498Szrj 
1819*38fd1498Szrj /* Sort the contents of this vector with qsort.  CMP is the comparison
1820*38fd1498Szrj    function to pass to qsort.  */
1821*38fd1498Szrj 
1822*38fd1498Szrj template<typename T>
1823*38fd1498Szrj inline void
1824*38fd1498Szrj vec<T, va_heap, vl_ptr>::qsort (int (*cmp) (const void *, const void *))
1825*38fd1498Szrj {
1826*38fd1498Szrj   if (m_vec)
1827*38fd1498Szrj     m_vec->qsort (cmp);
1828*38fd1498Szrj }
1829*38fd1498Szrj 
1830*38fd1498Szrj 
1831*38fd1498Szrj /* Search the contents of the sorted vector with a binary search.
1832*38fd1498Szrj    CMP is the comparison function to pass to bsearch.  */
1833*38fd1498Szrj 
1834*38fd1498Szrj template<typename T>
1835*38fd1498Szrj inline T *
1836*38fd1498Szrj vec<T, va_heap, vl_ptr>::bsearch (const void *key,
1837*38fd1498Szrj 				  int (*cmp) (const void *, const void *))
1838*38fd1498Szrj {
1839*38fd1498Szrj   if (m_vec)
1840*38fd1498Szrj     return m_vec->bsearch (key, cmp);
1841*38fd1498Szrj   return NULL;
1842*38fd1498Szrj }
1843*38fd1498Szrj 
1844*38fd1498Szrj 
1845*38fd1498Szrj /* Find and return the first position in which OBJ could be inserted
1846*38fd1498Szrj    without changing the ordering of this vector.  LESSTHAN is a
1847*38fd1498Szrj    function that returns true if the first argument is strictly less
1848*38fd1498Szrj    than the second.  */
1849*38fd1498Szrj 
1850*38fd1498Szrj template<typename T>
1851*38fd1498Szrj inline unsigned
1852*38fd1498Szrj vec<T, va_heap, vl_ptr>::lower_bound (T obj,
1853*38fd1498Szrj 				      bool (*lessthan)(const T &, const T &))
1854*38fd1498Szrj     const
1855*38fd1498Szrj {
1856*38fd1498Szrj   return m_vec ? m_vec->lower_bound (obj, lessthan) : 0;
1857*38fd1498Szrj }
1858*38fd1498Szrj 
1859*38fd1498Szrj /* Return true if SEARCH is an element of V.  Note that this is O(N) in the
1860*38fd1498Szrj    size of the vector and so should be used with care.  */
1861*38fd1498Szrj 
1862*38fd1498Szrj template<typename T>
1863*38fd1498Szrj inline bool
1864*38fd1498Szrj vec<T, va_heap, vl_ptr>::contains (const T &search) const
1865*38fd1498Szrj {
1866*38fd1498Szrj   return m_vec ? m_vec->contains (search) : false;
1867*38fd1498Szrj }
1868*38fd1498Szrj 
1869*38fd1498Szrj template<typename T>
1870*38fd1498Szrj inline bool
1871*38fd1498Szrj vec<T, va_heap, vl_ptr>::using_auto_storage () const
1872*38fd1498Szrj {
1873*38fd1498Szrj   return m_vec->m_vecpfx.m_using_auto_storage;
1874*38fd1498Szrj }
1875*38fd1498Szrj 
1876*38fd1498Szrj /* Release VEC and call release of all element vectors.  */
1877*38fd1498Szrj 
1878*38fd1498Szrj template<typename T>
1879*38fd1498Szrj inline void
1880*38fd1498Szrj release_vec_vec (vec<vec<T> > &vec)
1881*38fd1498Szrj {
1882*38fd1498Szrj   for (unsigned i = 0; i < vec.length (); i++)
1883*38fd1498Szrj     vec[i].release ();
1884*38fd1498Szrj 
1885*38fd1498Szrj   vec.release ();
1886*38fd1498Szrj }
1887*38fd1498Szrj 
1888*38fd1498Szrj #if (GCC_VERSION >= 3000)
1889*38fd1498Szrj # pragma GCC poison m_vec m_vecpfx m_vecdata
1890*38fd1498Szrj #endif
1891*38fd1498Szrj 
1892*38fd1498Szrj #endif // GCC_VEC_H
1893