2012-09-04 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / gcc / vec.h
blob1922616fc137b7965a0120e4fab0a48057e841b3
1 /* Vector API for GNU compiler.
2 Copyright (C) 2004, 2005, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Nathan Sidwell <nathan@codesourcery.com>
5 Re-implemented in C++ by Diego Novillo <dnovillo@google.com>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 3, or (at your option) any later
12 version.
14 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #ifndef GCC_VEC_H
24 #define GCC_VEC_H
26 #include "statistics.h" /* For MEM_STAT_DECL. */
28 /* The macros here implement a set of templated vector types and
29 associated interfaces. These templates are implemented with
30 macros, as we're not in C++ land. The interface functions are
31 typesafe and use static inline functions, sometimes backed by
32 out-of-line generic functions. The vectors are designed to
33 interoperate with the GTY machinery.
35 Because of the different behavior of structure objects, scalar
36 objects and of pointers, there are three flavors, one for each of
37 these variants. Both the structure object and pointer variants
38 pass pointers to objects around -- in the former case the pointers
39 are stored into the vector and in the latter case the pointers are
40 dereferenced and the objects copied into the vector. The scalar
41 object variant is suitable for int-like objects, and the vector
42 elements are returned by value.
44 There are both 'index' and 'iterate' accessors. The iterator
45 returns a boolean iteration condition and updates the iteration
46 variable passed by reference. Because the iterator will be
47 inlined, the address-of can be optimized away.
49 The vectors are implemented using the trailing array idiom, thus
50 they are not resizeable without changing the address of the vector
51 object itself. This means you cannot have variables or fields of
52 vector type -- always use a pointer to a vector. The one exception
53 is the final field of a structure, which could be a vector type.
54 You will have to use the embedded_size & embedded_init calls to
55 create such objects, and they will probably not be resizeable (so
56 don't use the 'safe' allocation variants). The trailing array
57 idiom is used (rather than a pointer to an array of data), because,
58 if we allow NULL to also represent an empty vector, empty vectors
59 occupy minimal space in the structure containing them.
61 Each operation that increases the number of active elements is
62 available in 'quick' and 'safe' variants. The former presumes that
63 there is sufficient allocated space for the operation to succeed
64 (it dies if there is not). The latter will reallocate the
65 vector, if needed. Reallocation causes an exponential increase in
66 vector size. If you know you will be adding N elements, it would
67 be more efficient to use the reserve operation before adding the
68 elements with the 'quick' operation. This will ensure there are at
69 least as many elements as you ask for, it will exponentially
70 increase if there are too few spare slots. If you want reserve a
71 specific number of slots, but do not want the exponential increase
72 (for instance, you know this is the last allocation), use the
73 reserve_exact operation. You can also create a vector of a
74 specific size from the get go.
76 You should prefer the push and pop operations, as they append and
77 remove from the end of the vector. If you need to remove several
78 items in one go, use the truncate operation. The insert and remove
79 operations allow you to change elements in the middle of the
80 vector. There are two remove operations, one which preserves the
81 element ordering 'ordered_remove', and one which does not
82 'unordered_remove'. The latter function copies the end element
83 into the removed slot, rather than invoke a memmove operation. The
84 'lower_bound' function will determine where to place an item in the
85 array using insert that will maintain sorted order.
87 When a vector type is defined, first a non-memory managed version
88 is created. You can then define either or both garbage collected
89 and heap allocated versions. The allocation mechanism is specified
90 when the type is defined, and is therefore part of the type. If
91 you need both gc'd and heap allocated versions, you still must have
92 *exactly* one definition of the common non-memory managed base vector.
94 If you need to directly manipulate a vector, then the 'address'
95 accessor will return the address of the start of the vector. Also
96 the 'space' predicate will tell you whether there is spare capacity
97 in the vector. You will not normally need to use these two functions.
99 Vector types are defined using a DEF_VEC_{O,A,P,I}(TYPEDEF) macro, to
100 get the non-memory allocation version, and then a
101 DEF_VEC_ALLOC_{O,A,P,I}(TYPEDEF,ALLOC) macro to get memory managed
102 vectors. Variables of vector type are declared using a
103 VEC(TYPEDEF,ALLOC) macro. The ALLOC argument specifies the
104 allocation strategy, and can be either 'gc' or 'heap' for garbage
105 collected and heap allocated respectively. It can be 'none' to get
106 a vector that must be explicitly allocated (for instance as a
107 trailing array of another structure). The characters O, A, P and I
108 indicate whether TYPEDEF is a pointer (P), object (O), atomic object
109 (A) or integral (I) type. Be careful to pick the correct one, as
110 you'll get an awkward and inefficient API if you use the wrong one or
111 a even a crash if you pick the atomic object version when the object
112 version should have been chosen instead. There is a check, which
113 results in a compile-time warning, for the P and I versions, but there
114 is no check for the O versions, as that is not possible in plain C.
115 Due to the way GTY works, you must annotate any structures you wish to
116 insert or reference from a vector with a GTY(()) tag. You need to do
117 this even if you never declare the GC allocated variants.
119 An example of their use would be,
121 DEF_VEC_P(tree); // non-managed tree vector.
122 DEF_VEC_ALLOC_P(tree,gc); // gc'd vector of tree pointers. This must
123 // appear at file scope.
125 struct my_struct {
126 VEC(tree,gc) *v; // A (pointer to) a vector of tree pointers.
129 struct my_struct *s;
131 if (VEC_length(tree,s->v)) { we have some contents }
132 VEC_safe_push(tree,gc,s->v,decl); // append some decl onto the end
133 for (ix = 0; VEC_iterate(tree,s->v,ix,elt); ix++)
134 { do something with elt }
138 #if ENABLE_CHECKING
139 #define VEC_CHECK_INFO ,__FILE__,__LINE__,__FUNCTION__
140 #define VEC_CHECK_DECL ,const char *file_,unsigned line_,const char *function_
141 #define VEC_CHECK_PASS ,file_,line_,function_
143 #define VEC_ASSERT(EXPR,OP,T,A) \
144 (void)((EXPR) ? 0 : (VEC_ASSERT_FAIL(OP,VEC(T,A)), 0))
146 extern void vec_assert_fail (const char *, const char * VEC_CHECK_DECL)
147 ATTRIBUTE_NORETURN;
148 #define VEC_ASSERT_FAIL(OP,VEC) vec_assert_fail (OP,#VEC VEC_CHECK_PASS)
149 #else
150 #define VEC_CHECK_INFO
151 #define VEC_CHECK_DECL
152 #define VEC_CHECK_PASS
153 #define VEC_ASSERT(EXPR,OP,T,A) (void)(EXPR)
154 #endif
156 #define VEC(T,A) vec_t<T>
158 enum vec_allocation_t { heap, gc, stack };
160 struct vec_prefix
162 unsigned num;
163 unsigned alloc;
166 /* Vector type, user visible. */
167 template<typename T>
168 struct GTY(()) vec_t
170 vec_prefix prefix;
171 T vec[1];
174 /* Garbage collection support for vec_t. */
176 template<typename T>
177 void
178 gt_ggc_mx (vec_t<T> *v)
180 extern void gt_ggc_mx (T&);
181 for (unsigned i = 0; i < v->prefix.num; i++)
182 gt_ggc_mx (v->vec[i]);
186 /* PCH support for vec_t. */
188 template<typename T>
189 void
190 gt_pch_nx (vec_t<T> *v)
192 extern void gt_pch_nx (T&);
193 for (unsigned i = 0; i < v->prefix.num; i++)
194 gt_pch_nx (v->vec[i]);
197 template<typename T>
198 void
199 gt_pch_nx (vec_t<T *> *v, gt_pointer_operator op, void *cookie)
201 for (unsigned i = 0; i < v->prefix.num; i++)
202 op (&(v->vec[i]), cookie);
205 template<typename T>
206 void
207 gt_pch_nx (vec_t<T> *v, gt_pointer_operator op, void *cookie)
209 extern void gt_pch_nx (T *, gt_pointer_operator, void *);
210 for (unsigned i = 0; i < v->prefix.num; i++)
211 gt_pch_nx (&(v->vec[i]), op, cookie);
215 /* FIXME cxx-conversion. Remove these definitions and update all
216 calling sites. */
217 /* Vector of integer-like object. */
218 #define DEF_VEC_I(T) struct vec_swallow_trailing_semi
219 #define DEF_VEC_ALLOC_I(T,A) struct vec_swallow_trailing_semi
221 /* Vector of pointer to object. */
222 #define DEF_VEC_P(T) struct vec_swallow_trailing_semi
223 #define DEF_VEC_ALLOC_P(T,A) struct vec_swallow_trailing_semi
225 /* Vector of object. */
226 #define DEF_VEC_O(T) struct vec_swallow_trailing_semi
227 #define DEF_VEC_ALLOC_O(T,A) struct vec_swallow_trailing_semi
229 /* Vectors on the stack. */
230 #define DEF_VEC_ALLOC_P_STACK(T) struct vec_swallow_trailing_semi
231 #define DEF_VEC_ALLOC_O_STACK(T) struct vec_swallow_trailing_semi
232 #define DEF_VEC_ALLOC_I_STACK(T) struct vec_swallow_trailing_semi
234 /* Vectors of atomic types. Atomic types do not need to have its
235 elements marked for GC and PCH. To avoid unnecessary traversals,
236 we provide template instantiations for the GC/PCH functions that
237 do not traverse the vector.
239 FIXME cxx-conversion - Once vec_t users are converted this can
240 be provided in some other way (e.g., adding an additional template
241 parameter to the vec_t class). */
242 #define DEF_VEC_A(TYPE) \
243 template<typename T> \
244 void \
245 gt_ggc_mx (vec_t<TYPE> *v ATTRIBUTE_UNUSED) \
249 template<typename T> \
250 void \
251 gt_pch_nx (vec_t<TYPE> *v ATTRIBUTE_UNUSED) \
255 template<typename T> \
256 void \
257 gt_pch_nx (vec_t<TYPE> *v ATTRIBUTE_UNUSED, \
258 gt_pointer_operator op ATTRIBUTE_UNUSED, \
259 void *cookie ATTRIBUTE_UNUSED) \
262 struct vec_swallow_trailing_semi
264 #define DEF_VEC_ALLOC_A(T,A) struct vec_swallow_trailing_semi
266 /* Support functions for stack vectors. */
267 extern void *vec_stack_p_reserve_exact_1 (int, void *);
268 extern void *vec_stack_o_reserve (void *, int, size_t, size_t MEM_STAT_DECL);
269 extern void *vec_stack_o_reserve_exact (void *, int, size_t, size_t
270 MEM_STAT_DECL);
271 extern void vec_stack_free (void *);
273 /* Reallocate an array of elements with prefix. */
274 template<typename T, enum vec_allocation_t A>
275 extern vec_t<T> *vec_reserve (vec_t<T> *, int MEM_STAT_DECL);
277 template<typename T, enum vec_allocation_t A>
278 extern vec_t<T> *vec_reserve_exact (vec_t<T> *, int MEM_STAT_DECL);
280 extern void dump_vec_loc_statistics (void);
281 extern void ggc_free (void *);
282 extern void vec_heap_free (void *);
285 /* Macros to invoke API calls. A single macro works for both pointer
286 and object vectors, but the argument and return types might well be
287 different. In each macro, T is the typedef of the vector elements,
288 and A is the allocation strategy. The allocation strategy is only
289 present when it is required. Some of these macros pass the vector,
290 V, by reference (by taking its address), this is noted in the
291 descriptions. */
293 /* Length of vector
294 unsigned VEC_T_length(const VEC(T) *v);
296 Return the number of active elements in V. V can be NULL, in which
297 case zero is returned. */
299 #define VEC_length(T,V) (VEC_length_1<T> (V))
301 template<typename T>
302 static inline unsigned
303 VEC_length_1 (const vec_t<T> *vec_)
305 return vec_ ? vec_->prefix.num : 0;
309 /* Check if vector is empty
310 int VEC_T_empty(const VEC(T) *v);
312 Return nonzero if V is an empty vector (or V is NULL), zero otherwise. */
314 #define VEC_empty(T,V) (VEC_empty_1<T> (V))
316 template<typename T>
317 static inline bool
318 VEC_empty_1 (const vec_t<T> *vec_)
320 return VEC_length (T, vec_) == 0;
324 /* Get the address of the array of elements
325 T *VEC_T_address (VEC(T) v)
327 If you need to directly manipulate the array (for instance, you
328 want to feed it to qsort), use this accessor. */
330 #define VEC_address(T,V) (VEC_address_1<T> (V))
332 template<typename T>
333 static inline T *
334 VEC_address_1 (vec_t<T> *vec_)
336 return vec_ ? vec_->vec : 0;
340 /* Get the final element of the vector.
341 T VEC_T_last(VEC(T) *v); // Integer
342 T VEC_T_last(VEC(T) *v); // Pointer
343 T *VEC_T_last(VEC(T) *v); // Object
345 Return the final element. V must not be empty. */
347 #define VEC_last(T,V) (VEC_last_1<T> (V VEC_CHECK_INFO))
349 template<typename T>
350 static inline T&
351 VEC_last_1 (vec_t<T> *vec_ VEC_CHECK_DECL)
353 VEC_ASSERT (vec_ && vec_->prefix.num, "last", T, base);
354 return vec_->vec[vec_->prefix.num - 1];
358 /* Index into vector
359 T VEC_T_index(VEC(T) *v, unsigned ix); // Integer
360 T VEC_T_index(VEC(T) *v, unsigned ix); // Pointer
361 T *VEC_T_index(VEC(T) *v, unsigned ix); // Object
363 Return the IX'th element. IX must be in the domain of V. */
365 #define VEC_index(T,V,I) (VEC_index_1<T> (V, I VEC_CHECK_INFO))
367 template<typename T>
368 static inline T&
369 VEC_index_1 (vec_t<T> *vec_, unsigned ix_ VEC_CHECK_DECL)
371 VEC_ASSERT (vec_ && ix_ < vec_->prefix.num, "index", T, base);
372 return vec_->vec[ix_];
375 template<typename T>
376 static inline const T&
377 VEC_index_1 (const vec_t<T> *vec_, unsigned ix_ VEC_CHECK_DECL)
379 VEC_ASSERT (vec_ && ix_ < vec_->prefix.num, "index", T, base);
380 return vec_->vec[ix_];
384 /* Iterate over vector
385 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Integer
386 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Pointer
387 int VEC_T_iterate(VEC(T) *v, unsigned ix, T *&ptr); // Object
389 Return iteration condition and update PTR to point to the IX'th
390 element. At the end of iteration, sets PTR to NULL. Use this to
391 iterate over the elements of a vector as follows,
393 for (ix = 0; VEC_iterate(T,v,ix,ptr); ix++)
394 continue; */
396 #define VEC_iterate(T,V,I,P) (VEC_iterate_1<T> (V, I, &(P)))
398 template<typename T>
399 static inline bool
400 VEC_iterate_1 (const vec_t<T> *vec_, unsigned ix_, T *ptr)
402 if (vec_ && ix_ < vec_->prefix.num)
404 *ptr = vec_->vec[ix_];
405 return true;
407 else
409 *ptr = 0;
410 return false;
414 template<typename T>
415 static inline bool
416 VEC_iterate_1 (vec_t<T> *vec_, unsigned ix_, T **ptr)
418 if (vec_ && ix_ < vec_->prefix.num)
420 *ptr = &vec_->vec[ix_];
421 return true;
423 else
425 *ptr = 0;
426 return false;
430 /* Convenience macro for forward iteration. */
432 #define FOR_EACH_VEC_ELT(T, V, I, P) \
433 for (I = 0; VEC_iterate (T, (V), (I), (P)); ++(I))
435 /* Likewise, but start from FROM rather than 0. */
437 #define FOR_EACH_VEC_ELT_FROM(T, V, I, P, FROM) \
438 for (I = (FROM); VEC_iterate (T, (V), (I), (P)); ++(I))
440 /* Convenience macro for reverse iteration. */
442 #define FOR_EACH_VEC_ELT_REVERSE(T,V,I,P) \
443 for (I = VEC_length (T, (V)) - 1; \
444 VEC_iterate (T, (V), (I), (P)); \
445 (I)--)
448 /* Use these to determine the required size and initialization of a
449 vector embedded within another structure (as the final member).
451 size_t VEC_T_embedded_size(int reserve);
452 void VEC_T_embedded_init(VEC(T) *v, int reserve);
454 These allow the caller to perform the memory allocation. */
456 #define VEC_embedded_size(T,N) (VEC_embedded_size_1<T> (N))
458 template<typename T>
459 static inline size_t
460 VEC_embedded_size_1 (int alloc_)
462 return offsetof (vec_t<T>, vec) + alloc_ * sizeof (T);
465 #define VEC_embedded_init(T,O,N) (VEC_embedded_init_1<T> (O, N))
467 template<typename T>
468 static inline void
469 VEC_embedded_init_1 (vec_t<T> *vec_, int alloc_)
471 vec_->prefix.num = 0;
472 vec_->prefix.alloc = alloc_;
476 /* Allocate new vector.
477 VEC(T,A) *VEC_T_A_alloc(int reserve);
479 Allocate a new vector with space for RESERVE objects. If RESERVE
480 is zero, NO vector is created.
482 We support a vector which starts out with space on the stack and
483 switches to heap space when forced to reallocate. This works a
484 little differently. In the case of stack vectors, VEC_alloc will
485 expand to a call to VEC_alloc_1 that calls XALLOCAVAR to request the
486 initial allocation. This uses alloca to get the initial space.
487 Since alloca can not be usefully called in an inline function,
488 VEC_alloc must always be a macro.
490 Only the initial allocation will be made using alloca, so pass a
491 reasonable estimate that doesn't use too much stack space; don't
492 pass zero. Don't return a VEC(TYPE,stack) vector from the function
493 which allocated it. */
495 #define VEC_alloc(T,A,N) \
496 ((A == stack) \
497 ? VEC_alloc_1 (N, \
498 XALLOCAVAR (vec_t<T>, \
499 VEC_embedded_size_1<T> (N))) \
500 : VEC_alloc_1<T, A> (N MEM_STAT_INFO))
502 template<typename T, enum vec_allocation_t A>
503 static inline vec_t<T> *
504 VEC_alloc_1 (int alloc_ MEM_STAT_DECL)
506 return vec_reserve_exact<T, A> (NULL, alloc_ PASS_MEM_STAT);
509 template<typename T>
510 static inline vec_t<T> *
511 VEC_alloc_1 (int alloc_, vec_t<T> *space)
513 return (vec_t<T> *) vec_stack_p_reserve_exact_1 (alloc_, space);
517 /* Free a vector.
518 void VEC_T_A_free(VEC(T,A) *&);
520 Free a vector and set it to NULL. */
522 #define VEC_free(T,A,V) (VEC_free_1<T, A> (&V))
524 template<typename T, enum vec_allocation_t A>
525 static inline void
526 VEC_free_1 (vec_t<T> **vec_)
528 if (*vec_)
530 if (A == heap)
531 vec_heap_free (*vec_);
532 else if (A == gc)
533 ggc_free (*vec_);
534 else if (A == stack)
535 vec_stack_free (*vec_);
537 *vec_ = NULL;
541 /* Copy a vector.
542 VEC(T,A) *VEC_T_A_copy(VEC(T) *);
544 Copy the live elements of a vector into a new vector. The new and
545 old vectors need not be allocated by the same mechanism. */
547 #define VEC_copy(T,A,V) (VEC_copy_1<T, A> (V MEM_STAT_INFO))
549 template<typename T, enum vec_allocation_t A>
550 static inline vec_t<T> *
551 VEC_copy_1 (vec_t<T> *vec_ MEM_STAT_DECL)
553 size_t len_ = vec_ ? vec_->prefix.num : 0;
554 vec_t<T> *new_vec_ = NULL;
556 if (len_)
558 new_vec_ = vec_reserve_exact<T, A> (NULL, len_ PASS_MEM_STAT);
559 new_vec_->prefix.num = len_;
560 memcpy (new_vec_->vec, vec_->vec, sizeof (T) * len_);
562 return new_vec_;
566 /* Determine if a vector has additional capacity.
568 int VEC_T_space (VEC(T) *v,int reserve)
570 If V has space for RESERVE additional entries, return nonzero. You
571 usually only need to use this if you are doing your own vector
572 reallocation, for instance on an embedded vector. This returns
573 nonzero in exactly the same circumstances that VEC_T_reserve
574 will. */
576 #define VEC_space(T,V,R) (VEC_space_1<T> (V, R VEC_CHECK_INFO))
578 template<typename T>
579 static inline int
580 VEC_space_1 (vec_t<T> *vec_, int alloc_ VEC_CHECK_DECL)
582 VEC_ASSERT (alloc_ >= 0, "space", T, base);
583 return vec_
584 ? vec_->prefix.alloc - vec_->prefix.num >= (unsigned)alloc_
585 : !alloc_;
589 /* Reserve space.
590 int VEC_T_A_reserve(VEC(T,A) *&v, int reserve);
592 Ensure that V has at least RESERVE slots available. This will
593 create additional headroom. Note this can cause V to be
594 reallocated. Returns nonzero iff reallocation actually
595 occurred. */
597 #define VEC_reserve(T,A,V,R) \
598 (VEC_reserve_1<T, A> (&(V), (int)(R) VEC_CHECK_INFO MEM_STAT_INFO))
600 template<typename T, enum vec_allocation_t A>
601 static inline int
602 VEC_reserve_1 (vec_t<T> **vec_, int alloc_ VEC_CHECK_DECL MEM_STAT_DECL)
604 int extend = !VEC_space_1 (*vec_, alloc_ VEC_CHECK_PASS);
606 if (extend)
607 *vec_ = vec_reserve<T, A> (*vec_, alloc_ PASS_MEM_STAT);
609 return extend;
613 /* Reserve space exactly.
614 int VEC_T_A_reserve_exact(VEC(T,A) *&v, int reserve);
616 Ensure that V has at least RESERVE slots available. This will not
617 create additional headroom. Note this can cause V to be
618 reallocated. Returns nonzero iff reallocation actually
619 occurred. */
621 #define VEC_reserve_exact(T,A,V,R) \
622 (VEC_reserve_exact_1<T, A> (&(V), R VEC_CHECK_INFO MEM_STAT_INFO))
624 template<typename T, enum vec_allocation_t A>
625 static inline int
626 VEC_reserve_exact_1 (vec_t<T> **vec_, int alloc_ VEC_CHECK_DECL MEM_STAT_DECL)
628 int extend = !VEC_space_1 (*vec_, alloc_ VEC_CHECK_PASS);
630 if (extend)
631 *vec_ = vec_reserve_exact<T, A> (*vec_, alloc_ PASS_MEM_STAT);
633 return extend;
637 /* Copy elements with no reallocation
638 void VEC_T_splice (VEC(T) *dst, VEC(T) *src); // Integer
639 void VEC_T_splice (VEC(T) *dst, VEC(T) *src); // Pointer
640 void VEC_T_splice (VEC(T) *dst, VEC(T) *src); // Object
642 Copy the elements in SRC to the end of DST as if by memcpy. DST and
643 SRC need not be allocated with the same mechanism, although they most
644 often will be. DST is assumed to have sufficient headroom
645 available. */
647 #define VEC_splice(T,DST,SRC) (VEC_splice_1<T> (DST, SRC VEC_CHECK_INFO))
649 template<typename T>
650 static inline void
651 VEC_splice_1 (vec_t<T> *dst_, vec_t<T> *src_ VEC_CHECK_DECL)
653 if (src_)
655 unsigned len_ = src_->prefix.num;
656 VEC_ASSERT (dst_->prefix.num + len_ <= dst_->prefix.alloc, "splice",
657 T, base);
659 memcpy (&dst_->vec[dst_->prefix.num], &src_->vec[0], len_ * sizeof (T));
660 dst_->prefix.num += len_;
665 /* Copy elements with reallocation
666 void VEC_T_safe_splice (VEC(T,A) *&dst, VEC(T) *src); // Integer
667 void VEC_T_safe_splice (VEC(T,A) *&dst, VEC(T) *src); // Pointer
668 void VEC_T_safe_splice (VEC(T,A) *&dst, VEC(T) *src); // Object
670 Copy the elements in SRC to the end of DST as if by memcpy. DST and
671 SRC need not be allocated with the same mechanism, although they most
672 often will be. DST need not have sufficient headroom and will be
673 reallocated if needed. */
675 #define VEC_safe_splice(T,A,DST,SRC) \
676 (VEC_safe_splice_1<T, A> (&(DST), SRC VEC_CHECK_INFO MEM_STAT_INFO))
678 template<typename T, enum vec_allocation_t A>
679 static inline void
680 VEC_safe_splice_1 (vec_t<T> **dst_, vec_t<T> *src_ VEC_CHECK_DECL MEM_STAT_DECL)
682 if (src_)
684 VEC_reserve_exact_1<T, A> (dst_, src_->prefix.num
685 VEC_CHECK_PASS MEM_STAT_INFO);
687 VEC_splice_1 (*dst_, src_ VEC_CHECK_PASS);
692 /* Push object with no reallocation
693 T *VEC_T_quick_push (VEC(T) *v, T obj); // Integer
694 T *VEC_T_quick_push (VEC(T) *v, T obj); // Pointer
695 T *VEC_T_quick_push (VEC(T) *v, T *obj); // Object
697 Push a new element onto the end, returns a pointer to the slot
698 filled in. For object vectors, the new value can be NULL, in which
699 case NO initialization is performed. There must
700 be sufficient space in the vector. */
702 #define VEC_quick_push(T,V,O) (VEC_quick_push_1<T> (V, O VEC_CHECK_INFO))
704 template<typename T>
705 static inline T &
706 VEC_quick_push_1 (vec_t<T> *vec_, T obj_ VEC_CHECK_DECL)
708 VEC_ASSERT (vec_->prefix.num < vec_->prefix.alloc, "push", T, base);
709 vec_->vec[vec_->prefix.num] = obj_;
710 T &val_ = vec_->vec[vec_->prefix.num];
711 vec_->prefix.num++;
712 return val_;
715 template<typename T>
716 static inline T *
717 VEC_quick_push_1 (vec_t<T> *vec_, const T *ptr_ VEC_CHECK_DECL)
719 T *slot_;
720 VEC_ASSERT (vec_->prefix.num < vec_->prefix.alloc, "push", T, base);
721 slot_ = &vec_->vec[vec_->prefix.num++];
722 if (ptr_)
723 *slot_ = *ptr_;
724 return slot_;
728 /* Push object with reallocation
729 T *VEC_T_A_safe_push (VEC(T,A) *&v, T obj); // Integer
730 T *VEC_T_A_safe_push (VEC(T,A) *&v, T obj); // Pointer
731 T *VEC_T_A_safe_push (VEC(T,A) *&v, T *obj); // Object
733 Push a new element onto the end, returns a pointer to the slot
734 filled in. For object vectors, the new value can be NULL, in which
735 case NO initialization is performed. Reallocates V, if needed. */
737 #define VEC_safe_push(T,A,V,O) \
738 (VEC_safe_push_1<T, A> (&(V), O VEC_CHECK_INFO MEM_STAT_INFO))
740 template<typename T, enum vec_allocation_t A>
741 static inline T &
742 VEC_safe_push_1 (vec_t<T> **vec_, T obj_ VEC_CHECK_DECL MEM_STAT_DECL)
744 VEC_reserve_1<T, A> (vec_, 1 VEC_CHECK_PASS PASS_MEM_STAT);
745 return VEC_quick_push_1 (*vec_, obj_ VEC_CHECK_PASS);
748 template<typename T, enum vec_allocation_t A>
749 static inline T *
750 VEC_safe_push_1 (vec_t<T> **vec_, const T *ptr_ VEC_CHECK_DECL MEM_STAT_DECL)
752 VEC_reserve_1<T, A> (vec_, 1 VEC_CHECK_PASS PASS_MEM_STAT);
753 return VEC_quick_push_1 (*vec_, ptr_ VEC_CHECK_PASS);
757 /* Pop element off end
758 T VEC_T_pop (VEC(T) *v); // Integer
759 T VEC_T_pop (VEC(T) *v); // Pointer
760 void VEC_T_pop (VEC(T) *v); // Object
762 Pop the last element off the end. Returns the element popped, for
763 pointer vectors. */
765 #define VEC_pop(T,V) (VEC_pop_1<T> (V VEC_CHECK_INFO))
767 template<typename T>
768 static inline T&
769 VEC_pop_1 (vec_t<T> *vec_ VEC_CHECK_DECL)
771 VEC_ASSERT (vec_->prefix.num, "pop", T, base);
772 return vec_->vec[--vec_->prefix.num];
776 /* Truncate to specific length
777 void VEC_T_truncate (VEC(T) *v, unsigned len);
779 Set the length as specified. The new length must be less than or
780 equal to the current length. This is an O(1) operation. */
782 #define VEC_truncate(T,V,I) \
783 (VEC_truncate_1<T> (V, (unsigned)(I) VEC_CHECK_INFO))
785 template<typename T>
786 static inline void
787 VEC_truncate_1 (vec_t<T> *vec_, unsigned size_ VEC_CHECK_DECL)
789 VEC_ASSERT (vec_ ? vec_->prefix.num >= size_ : !size_, "truncate", T, base);
790 if (vec_)
791 vec_->prefix.num = size_;
795 /* Grow to a specific length.
796 void VEC_T_A_safe_grow (VEC(T,A) *&v, int len);
798 Grow the vector to a specific length. The LEN must be as
799 long or longer than the current length. The new elements are
800 uninitialized. */
802 #define VEC_safe_grow(T,A,V,I) \
803 (VEC_safe_grow_1<T, A> (&(V), (int)(I) VEC_CHECK_INFO MEM_STAT_INFO))
805 template<typename T, enum vec_allocation_t A>
806 static inline void
807 VEC_safe_grow_1 (vec_t<T> **vec_, int size_ VEC_CHECK_DECL MEM_STAT_DECL)
809 VEC_ASSERT (size_ >= 0 && VEC_length (T, *vec_) <= (unsigned)size_,
810 "grow", T, A);
811 VEC_reserve_exact_1<T, A> (vec_,
812 size_ - (int)(*vec_ ? (*vec_)->prefix.num : 0)
813 VEC_CHECK_PASS PASS_MEM_STAT);
814 (*vec_)->prefix.num = size_;
818 /* Grow to a specific length.
819 void VEC_T_A_safe_grow_cleared (VEC(T,A) *&v, int len);
821 Grow the vector to a specific length. The LEN must be as
822 long or longer than the current length. The new elements are
823 initialized to zero. */
825 #define VEC_safe_grow_cleared(T,A,V,I) \
826 (VEC_safe_grow_cleared_1<T,A> (&(V), (int)(I) \
827 VEC_CHECK_INFO MEM_STAT_INFO))
829 template<typename T, enum vec_allocation_t A>
830 static inline void
831 VEC_safe_grow_cleared_1 (vec_t<T> **vec_, int size_ VEC_CHECK_DECL
832 MEM_STAT_DECL)
834 int oldsize = VEC_length (T, *vec_);
835 VEC_safe_grow_1<T, A> (vec_, size_ VEC_CHECK_PASS PASS_MEM_STAT);
836 memset (&(VEC_address (T, *vec_)[oldsize]), 0,
837 sizeof (T) * (size_ - oldsize));
841 /* Replace element
842 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Integer
843 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Pointer
844 T *VEC_T_replace (VEC(T) *v, unsigned ix, T *val); // Object
846 Replace the IXth element of V with a new value, VAL. For pointer
847 vectors returns the original value. For object vectors returns a
848 pointer to the new value. For object vectors the new value can be
849 NULL, in which case no overwriting of the slot is actually
850 performed. */
852 #define VEC_replace(T,V,I,O) \
853 (VEC_replace_1<T> (V, (unsigned)(I), O VEC_CHECK_INFO))
855 template<typename T>
856 static inline T&
857 VEC_replace_1 (vec_t<T> *vec_, unsigned ix_, T obj_ VEC_CHECK_DECL)
859 VEC_ASSERT (ix_ < vec_->prefix.num, "replace", T, base);
860 vec_->vec[ix_] = obj_;
861 return vec_->vec[ix_];
865 /* Insert object with no reallocation
866 void VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Integer
867 void VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Pointer
868 void VEC_T_quick_insert (VEC(T) *v, unsigned ix, T *val); // Object
870 Insert an element, VAL, at the IXth position of V. For vectors of
871 object, the new value can be NULL, in which case no initialization
872 of the inserted slot takes place. There must be sufficient space. */
874 #define VEC_quick_insert(T,V,I,O) \
875 (VEC_quick_insert_1<T> (V,I,O VEC_CHECK_INFO))
877 template<typename T>
878 static inline void
879 VEC_quick_insert_1 (vec_t<T> *vec_, unsigned ix_, T obj_ VEC_CHECK_DECL)
881 T *slot_;
883 VEC_ASSERT (vec_->prefix.num < vec_->prefix.alloc, "insert", T, base);
884 VEC_ASSERT (ix_ <= vec_->prefix.num, "insert", T, base);
885 slot_ = &vec_->vec[ix_];
886 memmove (slot_ + 1, slot_, (vec_->prefix.num++ - ix_) * sizeof (T));
887 *slot_ = obj_;
890 template<typename T>
891 static inline void
892 VEC_quick_insert_1 (vec_t<T> *vec_, unsigned ix_, const T *ptr_ VEC_CHECK_DECL)
894 T *slot_;
896 VEC_ASSERT (vec_->prefix.num < vec_->prefix.alloc, "insert", T, base);
897 VEC_ASSERT (ix_ <= vec_->prefix.num, "insert", T, base);
898 slot_ = &vec_->vec[ix_];
899 memmove (slot_ + 1, slot_, (vec_->prefix.num++ - ix_) * sizeof (T));
900 if (ptr_)
901 *slot_ = *ptr_;
905 /* Insert object with reallocation
906 T *VEC_T_A_safe_insert (VEC(T,A) *&v, unsigned ix, T val); // Integer
907 T *VEC_T_A_safe_insert (VEC(T,A) *&v, unsigned ix, T val); // Pointer
908 T *VEC_T_A_safe_insert (VEC(T,A) *&v, unsigned ix, T *val); // Object
910 Insert an element, VAL, at the IXth position of V. Return a pointer
911 to the slot created. For vectors of object, the new value can be
912 NULL, in which case no initialization of the inserted slot takes
913 place. Reallocate V, if necessary. */
915 #define VEC_safe_insert(T,A,V,I,O) \
916 (VEC_safe_insert_1<T, A> (&(V),I,O VEC_CHECK_INFO MEM_STAT_INFO))
918 template<typename T, enum vec_allocation_t A>
919 static inline void
920 VEC_safe_insert_1 (vec_t<T> **vec_, unsigned ix_, T obj_
921 VEC_CHECK_DECL MEM_STAT_DECL)
923 VEC_reserve_1<T, A> (vec_, 1 VEC_CHECK_PASS PASS_MEM_STAT);
924 VEC_quick_insert_1 (*vec_, ix_, obj_ VEC_CHECK_PASS);
927 template<typename T, enum vec_allocation_t A>
928 static inline void
929 VEC_safe_insert_1 (vec_t<T> **vec_, unsigned ix_, T *ptr_
930 VEC_CHECK_DECL MEM_STAT_DECL)
932 VEC_reserve_1<T, A> (vec_, 1 VEC_CHECK_PASS PASS_MEM_STAT);
933 VEC_quick_insert_1 (*vec_, ix_, ptr_ VEC_CHECK_PASS);
938 /* Remove element retaining order
939 void VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Integer
940 void VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Pointer
941 void VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Object
943 Remove an element from the IXth position of V. Ordering of
944 remaining elements is preserved. This is an O(N) operation due to
945 a memmove. */
947 #define VEC_ordered_remove(T,V,I) \
948 (VEC_ordered_remove_1<T> (V,I VEC_CHECK_INFO))
950 template<typename T>
951 static inline void
952 VEC_ordered_remove_1 (vec_t<T> *vec_, unsigned ix_ VEC_CHECK_DECL)
954 T *slot_;
955 VEC_ASSERT (ix_ < vec_->prefix.num, "remove", T, base);
956 slot_ = &vec_->vec[ix_];
957 memmove (slot_, slot_ + 1, (--vec_->prefix.num - ix_) * sizeof (T));
961 /* Remove element destroying order
962 void VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Integer
963 void VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Pointer
964 void VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Object
966 Remove an element from the IXth position of V. Ordering of
967 remaining elements is destroyed. This is an O(1) operation. */
969 #define VEC_unordered_remove(T,V,I) \
970 (VEC_unordered_remove_1<T> (V,I VEC_CHECK_INFO))
972 template<typename T>
973 static inline void
974 VEC_unordered_remove_1 (vec_t<T> *vec_, unsigned ix_ VEC_CHECK_DECL)
976 VEC_ASSERT (ix_ < vec_->prefix.num, "remove", T, base);
977 vec_->vec[ix_] = vec_->vec[--vec_->prefix.num];
981 /* Remove a block of elements
982 void VEC_T_block_remove (VEC(T) *v, unsigned ix, unsigned len);
984 Remove LEN elements starting at the IXth. Ordering is retained.
985 This is an O(N) operation due to memmove. */
987 #define VEC_block_remove(T,V,I,L) \
988 (VEC_block_remove_1<T> (V, I, L VEC_CHECK_INFO))
990 template<typename T>
991 static inline void
992 VEC_block_remove_1 (vec_t<T> *vec_, unsigned ix_, unsigned len_ VEC_CHECK_DECL)
994 T *slot_;
995 VEC_ASSERT (ix_ + len_ <= vec_->prefix.num, "block_remove", T, base);
996 slot_ = &vec_->vec[ix_];
997 vec_->prefix.num -= len_;
998 memmove (slot_, slot_ + len_, (vec_->prefix.num - ix_) * sizeof (T));
1002 /* Conveniently sort the contents of the vector with qsort.
1003 void VEC_qsort (VEC(T) *v, int (*cmp_func)(const void *, const void *)) */
1005 #define VEC_qsort(T,V,CMP) qsort(VEC_address (T, V), VEC_length (T, V), \
1006 sizeof (T), CMP)
1009 /* Find the first index in the vector not less than the object.
1010 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
1011 bool (*lessthan) (const T, const T)); // Integer
1012 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
1013 bool (*lessthan) (const T, const T)); // Pointer
1014 unsigned VEC_T_lower_bound (VEC(T) *v, const T *val,
1015 bool (*lessthan) (const T*, const T*)); // Object
1017 Find the first position in which VAL could be inserted without
1018 changing the ordering of V. LESSTHAN is a function that returns
1019 true if the first argument is strictly less than the second. */
1021 #define VEC_lower_bound(T,V,O,LT) \
1022 (VEC_lower_bound_1<T> (V, O, LT VEC_CHECK_INFO))
1024 template<typename T>
1025 static inline unsigned
1026 VEC_lower_bound_1 (vec_t<T> *vec_, T obj_,
1027 bool (*lessthan_)(T, T) VEC_CHECK_DECL)
1029 unsigned int len_ = VEC_length (T, vec_);
1030 unsigned int half_, middle_;
1031 unsigned int first_ = 0;
1032 while (len_ > 0)
1034 T middle_elem_;
1035 half_ = len_ >> 1;
1036 middle_ = first_;
1037 middle_ += half_;
1038 middle_elem_ = VEC_index_1 (vec_, middle_ VEC_CHECK_PASS);
1039 if (lessthan_ (middle_elem_, obj_))
1041 first_ = middle_;
1042 ++first_;
1043 len_ = len_ - half_ - 1;
1045 else
1046 len_ = half_;
1048 return first_;
1051 template<typename T>
1052 static inline unsigned
1053 VEC_lower_bound_1 (vec_t<T> *vec_, const T *ptr_,
1054 bool (*lessthan_)(const T*, const T*) VEC_CHECK_DECL)
1056 unsigned int len_ = VEC_length (T, vec_);
1057 unsigned int half_, middle_;
1058 unsigned int first_ = 0;
1059 while (len_ > 0)
1061 T *middle_elem_;
1062 half_ = len_ >> 1;
1063 middle_ = first_;
1064 middle_ += half_;
1065 middle_elem_ = &VEC_index_1 (vec_, middle_ VEC_CHECK_PASS);
1066 if (lessthan_ (middle_elem_, ptr_))
1068 first_ = middle_;
1069 ++first_;
1070 len_ = len_ - half_ - 1;
1072 else
1073 len_ = half_;
1075 return first_;
1079 void *vec_heap_o_reserve_1 (void *, int, size_t, size_t, bool MEM_STAT_DECL);
1080 void *vec_gc_o_reserve_1 (void *, int, size_t, size_t, bool MEM_STAT_DECL);
1082 /* Ensure there are at least RESERVE free slots in VEC_, growing
1083 exponentially. If RESERVE < 0 grow exactly, else grow
1084 exponentially. As a special case, if VEC_ is NULL, and RESERVE is
1085 0, no vector will be created. */
1087 template<typename T, enum vec_allocation_t A>
1088 vec_t<T> *
1089 vec_reserve (vec_t<T> *vec_, int reserve MEM_STAT_DECL)
1091 if (A == gc)
1092 return (vec_t<T> *) vec_gc_o_reserve_1 (vec_, reserve,
1093 offsetof (vec_t<T>, vec),
1094 sizeof (T), false
1095 PASS_MEM_STAT);
1096 else if (A == heap)
1097 return (vec_t<T> *) vec_heap_o_reserve_1 (vec_, reserve,
1098 offsetof (vec_t<T>, vec),
1099 sizeof (T), false
1100 PASS_MEM_STAT);
1101 else
1102 return (vec_t<T> *) vec_stack_o_reserve (vec_, reserve,
1103 offsetof (vec_t<T>, vec),
1104 sizeof (T) PASS_MEM_STAT);
1108 /* Ensure there are at least RESERVE free slots in VEC_, growing
1109 exactly. If RESERVE < 0 grow exactly, else grow exponentially. As
1110 a special case, if VEC_ is NULL, and RESERVE is 0, no vector will be
1111 created. */
1113 template<typename T, enum vec_allocation_t A>
1114 vec_t<T> *
1115 vec_reserve_exact (vec_t<T> *vec_, int reserve MEM_STAT_DECL)
1117 if (A == gc)
1118 return (vec_t<T> *) vec_gc_o_reserve_1 (vec_, reserve,
1119 sizeof (struct vec_prefix),
1120 sizeof (T), true
1121 PASS_MEM_STAT);
1122 else if (A == heap)
1123 return (vec_t<T> *) vec_heap_o_reserve_1 (vec_, reserve,
1124 sizeof (struct vec_prefix),
1125 sizeof (T), true
1126 PASS_MEM_STAT);
1127 else if (A == stack)
1129 /* Only allow stack vectors when re-growing them. The initial
1130 allocation of stack vectors must be done with VEC_alloc,
1131 because it uses alloca() for the allocation. */
1132 if (vec_ == NULL)
1134 fprintf (stderr, "Stack vectors must be initially allocated "
1135 "with VEC_stack_alloc.\n");
1136 gcc_unreachable ();
1138 return (vec_t<T> *) vec_stack_o_reserve_exact (vec_, reserve,
1139 sizeof (struct vec_prefix),
1140 sizeof (T)
1141 PASS_MEM_STAT);
1145 #endif /* GCC_VEC_H */