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