* varasm.c (assemble_start_function): Remove reset of in_section.
[official-gcc.git] / gcc / vec.h
blob945a4137f6b172e4e872709e1b3735fb2b9364cc
1 /* Vector API for GNU compiler.
2 Copyright (C) 2004 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 #ifndef GCC_VEC_H
23 #define GCC_VEC_H
25 /* The macros here implement a set of templated vector types and
26 associated interfaces. These templates are implemented with
27 macros, as we're not in C++ land. The interface functions are
28 typesafe and use static inline functions, sometimes backed by
29 out-of-line generic functions. The vectors are designed to
30 interoperate with the GTY machinery.
32 Because of the different behavior of objects and of pointers to
33 objects, there are two flavors. One to deal with a vector of
34 pointers to objects, and one to deal with a vector of objects
35 themselves. Both of these pass pointers to objects around -- in
36 the former case the pointers are stored into the vector and in the
37 latter case the pointers are dereferenced and the objects copied
38 into the vector. Therefore, when using a vector of pointers, the
39 objects pointed to must be long lived, but when dealing with a
40 vector of objects, the source objects need not be.
42 There are both 'index' and 'iterate' accessors. The iterator
43 returns a boolean iteration condition and updates the iteration
44 variable passed by reference. Because the iterator will be
45 inlined, the address-of can be optimized away.
47 The vectors are implemented using the trailing array idiom, thus
48 they are not resizeable without changing the address of the vector
49 object itself. This means you cannot have variables or fields of
50 vector type -- always use a pointer to a vector. The one exception
51 is the final field of a structure, which could be a vector type.
52 You will have to use the embedded_size & embedded_init calls to
53 create such objects, and they will probably not be resizeable (so
54 don't use the 'safe' allocation variants). The trailing array
55 idiom is used (rather than a pointer to an array of data), because,
56 if we allow NULL to also represent an empty vector, empty vectors
57 occupy minimal space in the structure containing them.
59 Each operation that increases the number of active elements is
60 available in 'quick' and 'safe' variants. The former presumes that
61 there is sufficient allocated space for the operation to succeed
62 (it aborts if there is not). The latter will reallocate the
63 vector, if needed. Reallocation causes an exponential increase in
64 vector size. If you know you will be adding N elements, it would
65 be more efficient to use the reserve operation before adding the
66 elements with the 'quick' operation. You may also use the reserve
67 operation with a -1 operand, to gain control over exactly when
68 reallocation occurs.
70 You should prefer the push and pop operations, as they append and
71 remove from the end of the vector. If you need to remove several
72 items in one go, use the truncate operation. The insert and remove
73 operations allow you to change elements in the middle of the
74 vector. There are two remove operations, one which preserves the
75 element ordering 'ordered_remove', and one which does not
76 'unordered_remove'. The latter function copies the end element
77 into the removed slot, rather than invoke a memmove operation.
78 The 'lower_bound' function will determine where to place an item in the
79 array using insert that will maintain sorted order.
81 Both garbage collected and explicitly managed vector types are
82 creatable. The allocation mechanism is specified when the type is
83 defined, and is therefore part of the type.
85 If you need to directly manipulate a vector, then the 'address'
86 accessor will return the address of the start of the vector. Also
87 the 'space' predicate will tell you whether there is spare capacity
88 in the vector. You will not normally need to use these two functions.
90 Vector types are defined using a DEF_VEC_{GC,MALLOC}_{O,P}(TYPEDEF)
91 macro, and variables of vector type are declared using a
92 VEC(TYPEDEF) macro. The tags GC and MALLOC specify the allocation
93 method -- garbage collected or explicit malloc/free calls. The
94 characters O and P indicate whether TYPEDEF is a pointer (P) or
95 object (O) type.
97 An example of their use would be,
99 DEF_VEC_GC_P(tree); // define a gc'd vector of tree pointers. This must
100 // appear at file scope.
102 struct my_struct {
103 VEC(tree) *v; // A (pointer to) a vector of tree pointers.
106 struct my_struct *s;
108 if (VEC_length(tree,s->v)) { we have some contents }
109 VEC_safe_push(tree,s->v,decl); // append some decl onto the end
110 for (ix = 0; VEC_iterate(tree,s->v,ix,elt); ix++)
111 { do something with elt }
115 /* Macros to invoke API calls. A single macro works for both pointer
116 and object vectors, but the argument and return types might well be
117 different. In each macro, TDEF is the typedef of the vector
118 elements. Some of these macros pass the vector, V, by reference
119 (by taking its address), this is noted in the descriptions. */
121 /* Length of vector
122 unsigned VEC_T_length(const VEC(T) *v);
124 Return the number of active elements in V. V can be NULL, in which
125 case zero is returned. */
127 #define VEC_length(TDEF,V) (VEC_OP(TDEF,length)(V))
129 /* Get the final element of the vector.
130 T VEC_T_last(VEC(T) *v); // Pointer
131 T *VEC_T_last(VEC(T) *v); // Object
133 Return the final element. If V is empty, abort. */
135 #define VEC_last(TDEF,V) (VEC_OP(TDEF,last)(V VEC_CHECK_INFO))
137 /* Index into vector
138 T VEC_T_index(VEC(T) *v, unsigned ix); // Pointer
139 T *VEC_T_index(VEC(T) *v, unsigned ix); // Object
141 Return the IX'th element. If IX is outside the domain of V,
142 abort. */
144 #define VEC_index(TDEF,V,I) (VEC_OP(TDEF,index)(V,I VEC_CHECK_INFO))
146 /* Iterate over vector
147 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Pointer
148 int VEC_T_iterate(VEC(T) *v, unsigned ix, T *&ptr); // Object
150 Return iteration condition and update PTR to point to the IX'th
151 element. At the end of iteration, sets PTR to NULL. Use this to
152 iterate over the elements of a vector as follows,
154 for (ix = 0; VEC_iterate(T,v,ix,ptr); ix++)
155 continue; */
157 #define VEC_iterate(TDEF,V,I,P) (VEC_OP(TDEF,iterate)(V,I,&(P)))
159 /* Allocate new vector.
160 VEC(T) *VEC_T_alloc(int reserve);
162 Allocate a new vector with space for RESERVE objects. If RESERVE
163 is <= 0, a default number of slots are created. */
165 #define VEC_alloc(TDEF,A) (VEC_OP(TDEF,alloc)(A MEM_STAT_INFO))
167 /* Free a vector.
168 void VEC_T_alloc(VEC(T) *&);
170 Free a vector and set it to NULL. */
172 #define VEC_free(TDEF,V) (VEC_OP(TDEF,free)(&V))
174 /* Use these to determine the required size and initialization of a
175 vector embedded within another structure (as the final member).
177 size_t VEC_T_embedded_size(int reserve);
178 void VEC_T_embedded_init(VEC(T) *v, int reserve);
180 These allow the caller to perform the memory allocation. */
182 #define VEC_embedded_size(TDEF,A) (VEC_OP(TDEF,embedded_size)(A))
183 #define VEC_embedded_init(TDEF,O,A) (VEC_OP(TDEF,embedded_init)(O,A))
185 /* Determine if a vector has additional capacity.
187 int VEC_T_space (VEC(T) *v,int reserve)
189 If V has space for RESERVE additional entries, return nonzero. If
190 RESERVE is < 0, ensure there is at least one space slot. You
191 usually only need to use this if you are doing your own vector
192 reallocation, for instance on an embedded vector. This returns
193 nonzero in exactly the same circumstances that VEC_T_reserve
194 will. */
196 #define VEC_space(TDEF,V,R) (VEC_OP(TDEF,space)(V,R))
198 /* Reserve space.
199 int VEC_T_reserve(VEC(T) *&v, int reserve);
201 Ensure that V has at least RESERVE slots available, if RESERVE is
202 >= 0. If RESERVE < 0, ensure that there is at least one spare
203 slot. These differ in their reallocation behavior, the first will
204 not create additional headroom, but the second mechanism will
205 perform the usual exponential headroom increase. Note this can
206 cause V to be reallocated. Returns nonzero iff reallocation
207 actually occurred. */
209 #define VEC_reserve(TDEF,V,R) (VEC_OP(TDEF,reserve)(&(V),R MEM_STAT_INFO))
211 /* Push object with no reallocation
212 T *VEC_T_quick_push (VEC(T) *v, T obj); // Pointer
213 T *VEC_T_quick_push (VEC(T) *v, T *obj); // Object
215 Push a new element onto the end, returns a pointer to the slot
216 filled in. For object vectors, the new value can be NULL, in which
217 case NO initialization is performed. Aborts if there is
218 insufficient space in the vector. */
220 #define VEC_quick_push(TDEF,V,O) \
221 (VEC_OP(TDEF,quick_push)(V,O VEC_CHECK_INFO))
223 /* Push object with reallocation
224 T *VEC_T_safe_push (VEC(T) *&v, T obj); // Pointer
225 T *VEC_T_safe_push (VEC(T) *&v, T *obj); // Object
227 Push a new element onto the end, returns a pointer to the slot
228 filled in. For object vectors, the new value can be NULL, in which
229 case NO initialization is performed. Reallocates V, if needed. */
231 #define VEC_safe_push(TDEF,V,O) \
232 (VEC_OP(TDEF,safe_push)(&(V),O VEC_CHECK_INFO MEM_STAT_INFO))
234 /* Pop element off end
235 T VEC_T_pop (VEC(T) *v); // Pointer
236 void VEC_T_pop (VEC(T) *v); // Object
238 Pop the last element off the end. Returns the element popped, for
239 pointer vectors. */
241 #define VEC_pop(TDEF,V) (VEC_OP(TDEF,pop)(V VEC_CHECK_INFO))
243 /* Truncate to specific length
244 void VEC_T_truncate (VEC(T) *v, unsigned len);
246 Set the length as specified. This is an O(1) operation. */
248 #define VEC_truncate(TDEF,V,I) \
249 (VEC_OP(TDEF,truncate)(V,I VEC_CHECK_INFO))
251 /* Replace element
252 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Pointer
253 T *VEC_T_replace (VEC(T) *v, unsigned ix, T *val); // Object
255 Replace the IXth element of V with a new value, VAL. For pointer
256 vectors returns the original value. For object vectors returns a
257 pointer to the new value. For object vectors the new value can be
258 NULL, in which case no overwriting of the slot is actually
259 performed. */
261 #define VEC_replace(TDEF,V,I,O) \
262 (VEC_OP(TDEF,replace)(V,I,O VEC_CHECK_INFO))
264 /* Insert object with no reallocation
265 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Pointer
266 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T *val); // Object
268 Insert an element, VAL, at the IXth position of V. Return a pointer
269 to the slot created. For vectors of object, the new value can be
270 NULL, in which case no initialization of the inserted slot takes
271 place. Aborts if there is insufficient space. */
273 #define VEC_quick_insert(TDEF,V,I,O) \
274 (VEC_OP(TDEF,quick_insert)(V,I,O VEC_CHECK_INFO))
276 /* Insert object with reallocation
277 T *VEC_T_safe_insert (VEC(T) *&v, unsigned ix, T val); // Pointer
278 T *VEC_T_safe_insert (VEC(T) *&v, unsigned ix, T *val); // Object
280 Insert an element, VAL, at the IXth position of V. Return a pointer
281 to the slot created. For vectors of object, the new value can be
282 NULL, in which case no initialization of the inserted slot takes
283 place. Reallocate V, if necessary. */
285 #define VEC_safe_insert(TDEF,V,I,O) \
286 (VEC_OP(TDEF,safe_insert)(&(V),I,O VEC_CHECK_INFO MEM_STAT_INFO))
288 /* Remove element retaining order
289 T VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Pointer
290 void VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Object
292 Remove an element from the IXth position of V. Ordering of
293 remaining elements is preserved. For pointer vectors returns the
294 removed object. This is an O(N) operation due to a memmove. */
296 #define VEC_ordered_remove(TDEF,V,I) \
297 (VEC_OP(TDEF,ordered_remove)(V,I VEC_CHECK_INFO))
299 /* Remove element destroying order
300 T VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Pointer
301 void VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Object
303 Remove an element from the IXth position of V. Ordering of
304 remaining elements is destroyed. For pointer vectors returns the
305 removed object. This is an O(1) operation. */
307 #define VEC_unordered_remove(TDEF,V,I) \
308 (VEC_OP(TDEF,unordered_remove)(V,I VEC_CHECK_INFO))
310 /* Get the address of the array of elements
311 T *VEC_T_address (VEC(T) v)
313 If you need to directly manipulate the array (for instance, you
314 want to feed it to qsort), use this accessor. */
316 #define VEC_address(TDEF,V) (VEC_OP(TDEF,address)(V))
318 /* Find the first index in the vector not less than the object.
319 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
320 bool (*lessthan) (const T, const T)); // Pointer
321 unsigned VEC_T_lower_bound (VEC(T) *v, const T *val,
322 bool (*lessthan) (const T*, const T*)); // Object
324 Find the first position in which VAL could be inserted without
325 changing the ordering of V. LESSTHAN is a function that returns
326 true if the first argument is strictly less than the second. */
328 #define VEC_lower_bound(TDEF,V,O,LT) \
329 (VEC_OP(TDEF,lower_bound)(V,O,LT VEC_CHECK_INFO))
331 #if !IN_GENGTYPE
332 /* Reallocate an array of elements with prefix. */
333 extern void *vec_gc_p_reserve (void *, int MEM_STAT_DECL);
334 extern void *vec_gc_o_reserve (void *, int, size_t, size_t MEM_STAT_DECL);
335 extern void vec_gc_free (void *);
336 extern void *vec_heap_p_reserve (void *, int MEM_STAT_DECL);
337 extern void *vec_heap_o_reserve (void *, int, size_t, size_t MEM_STAT_DECL);
338 extern void vec_heap_free (void *);
340 #if ENABLE_CHECKING
341 #define VEC_CHECK_INFO ,__FILE__,__LINE__,__FUNCTION__
342 #define VEC_CHECK_DECL ,const char *file_,unsigned line_,const char *function_
343 #define VEC_CHECK_PASS ,file_,line_,function_
345 #define VEC_ASSERT(EXPR,OP,TDEF) \
346 (void)((EXPR) ? 0 : (VEC_ASSERT_FAIL(OP,VEC(TDEF)), 0))
348 extern void vec_assert_fail (const char *, const char * VEC_CHECK_DECL)
349 ATTRIBUTE_NORETURN;
350 #define VEC_ASSERT_FAIL(OP,VEC) vec_assert_fail (OP,#VEC VEC_CHECK_PASS)
351 #else
352 #define VEC_CHECK_INFO
353 #define VEC_CHECK_DECL
354 #define VEC_CHECK_PASS
355 #define VEC_ASSERT(EXPR,OP,TYPE) (void)(EXPR)
356 #endif
358 #define VEC(TDEF) VEC_##TDEF
359 #define VEC_OP(TDEF,OP) VEC_OP_(VEC(TDEF),OP)
360 #define VEC_OP_(VEC,OP) VEC_OP__(VEC,OP)
361 #define VEC_OP__(VEC,OP) VEC ## _ ## OP
362 #else /* IN_GENGTYPE */
363 #define VEC(TDEF) VEC_ TDEF
364 #define VEC_STRINGIFY(X) VEC_STRINGIFY_(X)
365 #define VEC_STRINGIFY_(X) #X
366 #undef GTY
367 #endif /* IN_GENGTYPE */
369 #define VEC_TDEF(TDEF) \
370 typedef struct VEC (TDEF) GTY(()) \
372 unsigned num; \
373 unsigned alloc; \
374 TDEF GTY ((length ("%h.num"))) vec[1]; \
375 } VEC (TDEF)
377 /* Vector of pointer to object. */
378 #if IN_GENGTYPE
379 {"DEF_VEC_GC_P", VEC_STRINGIFY (VEC_TDEF (#)) ";", NULL},
380 {"DEF_VEC_MALLOC_P", "", NULL},
381 #else
382 #define DEF_VEC_GC_P(TDEF) DEF_VEC_P(TDEF,gc)
383 #define DEF_VEC_MALLOC_P(TDEF) DEF_VEC_P(TDEF,heap)
385 #define DEF_VEC_P(TDEF,a) \
386 VEC_TDEF (TDEF); \
388 static inline unsigned VEC_OP (TDEF,length) \
389 (const VEC (TDEF) *vec_) \
391 return vec_ ? vec_->num : 0; \
394 static inline TDEF VEC_OP (TDEF,last) \
395 (const VEC (TDEF) *vec_ VEC_CHECK_DECL) \
397 VEC_ASSERT (vec_ && vec_->num, "last", TDEF); \
399 return vec_->vec[vec_->num - 1]; \
402 static inline TDEF VEC_OP (TDEF,index) \
403 (const VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
405 VEC_ASSERT (vec_ && ix_ < vec_->num, "index", TDEF); \
407 return vec_->vec[ix_]; \
410 static inline int VEC_OP (TDEF,iterate) \
411 (const VEC (TDEF) *vec_, unsigned ix_, TDEF *ptr) \
413 if (vec_ && ix_ < vec_->num) \
415 *ptr = vec_->vec[ix_]; \
416 return 1; \
418 else \
420 *ptr = 0; \
421 return 0; \
425 static inline VEC (TDEF) *VEC_OP (TDEF,alloc) \
426 (int alloc_ MEM_STAT_DECL) \
428 return (VEC (TDEF) *) vec_##a##_p_reserve (NULL, alloc_ - !alloc_ PASS_MEM_STAT);\
431 static inline void VEC_OP (TDEF,free) \
432 (VEC (TDEF) **vec_) \
434 vec_##a##_free (*vec_); \
435 *vec_ = NULL; \
438 static inline size_t VEC_OP (TDEF,embedded_size) \
439 (int alloc_) \
441 return offsetof (VEC(TDEF),vec) + alloc_ * sizeof(TDEF); \
444 static inline void VEC_OP (TDEF,embedded_init) \
445 (VEC (TDEF) *vec_, int alloc_) \
447 vec_->num = 0; \
448 vec_->alloc = alloc_; \
451 static inline int VEC_OP (TDEF,space) \
452 (VEC (TDEF) *vec_, int alloc_) \
454 return vec_ ? ((vec_)->alloc - (vec_)->num \
455 >= (unsigned)(alloc_ < 0 ? 1 : alloc_)) : !alloc_; \
458 static inline int VEC_OP (TDEF,reserve) \
459 (VEC (TDEF) **vec_, int alloc_ MEM_STAT_DECL) \
461 int extend = !VEC_OP (TDEF,space) (*vec_, alloc_); \
463 if (extend) \
464 *vec_ = (VEC (TDEF) *) vec_##a##_p_reserve (*vec_, alloc_ PASS_MEM_STAT); \
466 return extend; \
469 static inline TDEF *VEC_OP (TDEF,quick_push) \
470 (VEC (TDEF) *vec_, TDEF obj_ VEC_CHECK_DECL) \
472 TDEF *slot_; \
474 VEC_ASSERT (vec_->num < vec_->alloc, "push", TDEF); \
475 slot_ = &vec_->vec[vec_->num++]; \
476 *slot_ = obj_; \
478 return slot_; \
481 static inline TDEF *VEC_OP (TDEF,safe_push) \
482 (VEC (TDEF) **vec_, TDEF obj_ VEC_CHECK_DECL MEM_STAT_DECL) \
484 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
486 return VEC_OP (TDEF,quick_push) (*vec_, obj_ VEC_CHECK_PASS); \
489 static inline TDEF VEC_OP (TDEF,pop) \
490 (VEC (TDEF) *vec_ VEC_CHECK_DECL) \
492 TDEF obj_; \
494 VEC_ASSERT (vec_->num, "pop", TDEF); \
495 obj_ = vec_->vec[--vec_->num]; \
497 return obj_; \
500 static inline void VEC_OP (TDEF,truncate) \
501 (VEC (TDEF) *vec_, unsigned size_ VEC_CHECK_DECL) \
503 VEC_ASSERT (vec_ ? vec_->num >= size_ : !size_, "truncate", TDEF); \
504 if (vec_) \
505 vec_->num = size_; \
508 static inline TDEF VEC_OP (TDEF,replace) \
509 (VEC (TDEF) *vec_, unsigned ix_, TDEF obj_ VEC_CHECK_DECL) \
511 TDEF old_obj_; \
513 VEC_ASSERT (ix_ < vec_->num, "replace", TDEF); \
514 old_obj_ = vec_->vec[ix_]; \
515 vec_->vec[ix_] = obj_; \
517 return old_obj_; \
520 static inline unsigned VEC_OP (TDEF,lower_bound) \
521 (VEC (TDEF) *vec_, const TDEF obj_, bool (*lessthan_)(const TDEF, const TDEF) VEC_CHECK_DECL) \
523 unsigned int len_ = VEC_OP (TDEF, length) (vec_); \
524 unsigned int half_, middle_; \
525 unsigned int first_ = 0; \
526 while (len_ > 0) \
528 TDEF middle_elem_; \
529 half_ = len_ >> 1; \
530 middle_ = first_; \
531 middle_ += half_; \
532 middle_elem_ = VEC_OP (TDEF, index) (vec_, middle_ VEC_CHECK_PASS); \
533 if (lessthan_ (middle_elem_, obj_)) \
535 first_ = middle_; \
536 ++first_; \
537 len_ = len_ - half_ - 1; \
539 else \
540 len_ = half_; \
542 return first_; \
545 static inline TDEF *VEC_OP (TDEF,quick_insert) \
546 (VEC (TDEF) *vec_, unsigned ix_, TDEF obj_ VEC_CHECK_DECL) \
548 TDEF *slot_; \
550 VEC_ASSERT (vec_->num < vec_->alloc, "insert", TDEF); \
551 VEC_ASSERT (ix_ <= vec_->num, "insert", TDEF); \
552 slot_ = &vec_->vec[ix_]; \
553 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (TDEF)); \
554 *slot_ = obj_; \
556 return slot_; \
559 static inline TDEF *VEC_OP (TDEF,safe_insert) \
560 (VEC (TDEF) **vec_, unsigned ix_, TDEF obj_ \
561 VEC_CHECK_DECL MEM_STAT_DECL) \
563 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
565 return VEC_OP (TDEF,quick_insert) (*vec_, ix_, obj_ VEC_CHECK_PASS); \
568 static inline TDEF VEC_OP (TDEF,ordered_remove) \
569 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
571 TDEF *slot_; \
572 TDEF obj_; \
574 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
575 slot_ = &vec_->vec[ix_]; \
576 obj_ = *slot_; \
577 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (TDEF)); \
579 return obj_; \
582 static inline TDEF VEC_OP (TDEF,unordered_remove) \
583 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
585 TDEF *slot_; \
586 TDEF obj_; \
588 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
589 slot_ = &vec_->vec[ix_]; \
590 obj_ = *slot_; \
591 *slot_ = vec_->vec[--vec_->num]; \
593 return obj_; \
596 static inline TDEF *VEC_OP (TDEF,address) \
597 (VEC (TDEF) *vec_) \
599 return vec_ ? vec_->vec : 0; \
602 struct vec_swallow_trailing_semi
603 #endif
605 /* Vector of object. */
606 #if IN_GENGTYPE
607 {"DEF_VEC_GC_O", VEC_STRINGIFY (VEC_TDEF (#)) ";", NULL},
608 {"DEF_VEC_MALLOC_O", "", NULL},
609 #else
611 #define DEF_VEC_GC_O(TDEF) DEF_VEC_O(TDEF,gc)
612 #define DEF_VEC_MALLOC_O(TDEF) DEF_VEC_O(TDEF,heap)
614 #define DEF_VEC_O(TDEF,a) \
615 VEC_TDEF (TDEF); \
617 static inline unsigned VEC_OP (TDEF,length) \
618 (const VEC (TDEF) *vec_) \
620 return vec_ ? vec_->num : 0; \
623 static inline TDEF *VEC_OP (TDEF,last) \
624 (VEC (TDEF) *vec_ VEC_CHECK_DECL) \
626 VEC_ASSERT (vec_ && vec_->num, "last", TDEF); \
628 return &vec_->vec[vec_->num - 1]; \
631 static inline TDEF *VEC_OP (TDEF,index) \
632 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
634 VEC_ASSERT (vec_ && ix_ < vec_->num, "index", TDEF); \
636 return &vec_->vec[ix_]; \
639 static inline int VEC_OP (TDEF,iterate) \
640 (VEC (TDEF) *vec_, unsigned ix_, TDEF **ptr) \
642 if (vec_ && ix_ < vec_->num) \
644 *ptr = &vec_->vec[ix_]; \
645 return 1; \
647 else \
649 *ptr = 0; \
650 return 0; \
654 static inline VEC (TDEF) *VEC_OP (TDEF,alloc) \
655 (int alloc_ MEM_STAT_DECL) \
657 return (VEC (TDEF) *) vec_##a##_o_reserve (NULL, alloc_ - !alloc_, \
658 offsetof (VEC(TDEF),vec), sizeof (TDEF)\
659 PASS_MEM_STAT); \
662 static inline void VEC_OP (TDEF,free) \
663 (VEC (TDEF) **vec_) \
665 vec_##a##_free (*vec_); \
666 *vec_ = NULL; \
669 static inline size_t VEC_OP (TDEF,embedded_size) \
670 (int alloc_) \
672 return offsetof (VEC(TDEF),vec) + alloc_ * sizeof(TDEF); \
675 static inline void VEC_OP (TDEF,embedded_init) \
676 (VEC (TDEF) *vec_, int alloc_) \
678 vec_->num = 0; \
679 vec_->alloc = alloc_; \
682 static inline int VEC_OP (TDEF,space) \
683 (VEC (TDEF) *vec_, int alloc_) \
685 return vec_ ? ((vec_)->alloc - (vec_)->num \
686 >= (unsigned)(alloc_ < 0 ? 1 : alloc_)) : !alloc_; \
689 static inline int VEC_OP (TDEF,reserve) \
690 (VEC (TDEF) **vec_, int alloc_ MEM_STAT_DECL) \
692 int extend = !VEC_OP (TDEF,space) (*vec_, alloc_); \
694 if (extend) \
695 *vec_ = (VEC (TDEF) *) vec_##a##_o_reserve (*vec_, alloc_, \
696 offsetof (VEC(TDEF),vec), sizeof (TDEF) \
697 PASS_MEM_STAT); \
699 return extend; \
702 static inline TDEF *VEC_OP (TDEF,quick_push) \
703 (VEC (TDEF) *vec_, const TDEF *obj_ VEC_CHECK_DECL) \
705 TDEF *slot_; \
707 VEC_ASSERT (vec_->num < vec_->alloc, "push", TDEF); \
708 slot_ = &vec_->vec[vec_->num++]; \
709 if (obj_) \
710 *slot_ = *obj_; \
712 return slot_; \
715 static inline TDEF *VEC_OP (TDEF,safe_push) \
716 (VEC (TDEF) **vec_, const TDEF *obj_ VEC_CHECK_DECL MEM_STAT_DECL) \
718 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
720 return VEC_OP (TDEF,quick_push) (*vec_, obj_ VEC_CHECK_PASS); \
723 static inline void VEC_OP (TDEF,pop) \
724 (VEC (TDEF) *vec_ VEC_CHECK_DECL) \
726 VEC_ASSERT (vec_->num, "pop", TDEF); \
727 --vec_->num; \
730 static inline void VEC_OP (TDEF,truncate) \
731 (VEC (TDEF) *vec_, unsigned size_ VEC_CHECK_DECL) \
733 VEC_ASSERT (vec_ ? vec_->num >= size_ : !size_, "truncate", TDEF); \
734 if (vec_) \
735 vec_->num = size_; \
738 static inline TDEF *VEC_OP (TDEF,replace) \
739 (VEC (TDEF) *vec_, unsigned ix_, const TDEF *obj_ VEC_CHECK_DECL) \
741 TDEF *slot_; \
743 VEC_ASSERT (ix_ < vec_->num, "replace", TDEF); \
744 slot_ = &vec_->vec[ix_]; \
745 if (obj_) \
746 *slot_ = *obj_; \
748 return slot_; \
751 static inline unsigned VEC_OP (TDEF,lower_bound) \
752 (VEC (TDEF) *vec_, const TDEF *obj_, bool (*lessthan_)(const TDEF *, const TDEF *) VEC_CHECK_DECL) \
754 unsigned int len_ = VEC_OP (TDEF, length) (vec_); \
755 unsigned int half_, middle_; \
756 unsigned int first_ = 0; \
757 while (len_ > 0) \
759 TDEF *middle_elem_; \
760 half_ = len_ >> 1; \
761 middle_ = first_; \
762 middle_ += half_; \
763 middle_elem_ = VEC_OP (TDEF, index) (vec_, middle_ VEC_CHECK_PASS); \
764 if (lessthan_ (middle_elem_, obj_)) \
766 first_ = middle_; \
767 ++first_; \
768 len_ = len_ - half_ - 1; \
770 else \
771 len_ = half_; \
773 return first_; \
776 static inline TDEF *VEC_OP (TDEF,quick_insert) \
777 (VEC (TDEF) *vec_, unsigned ix_, const TDEF *obj_ VEC_CHECK_DECL) \
779 TDEF *slot_; \
781 VEC_ASSERT (vec_->num < vec_->alloc, "insert", TDEF); \
782 VEC_ASSERT (ix_ <= vec_->num, "insert", TDEF); \
783 slot_ = &vec_->vec[ix_]; \
784 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (TDEF)); \
785 if (obj_) \
786 *slot_ = *obj_; \
788 return slot_; \
791 static inline TDEF *VEC_OP (TDEF,safe_insert) \
792 (VEC (TDEF) **vec_, unsigned ix_, const TDEF *obj_ \
793 VEC_CHECK_DECL MEM_STAT_DECL) \
795 VEC_OP (TDEF,reserve) (vec_, -1 PASS_MEM_STAT); \
797 return VEC_OP (TDEF,quick_insert) (*vec_, ix_, obj_ VEC_CHECK_PASS); \
800 static inline void VEC_OP (TDEF,ordered_remove) \
801 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
803 TDEF *slot_; \
805 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
806 slot_ = &vec_->vec[ix_]; \
807 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (TDEF)); \
810 static inline void VEC_OP (TDEF,unordered_remove) \
811 (VEC (TDEF) *vec_, unsigned ix_ VEC_CHECK_DECL) \
813 VEC_ASSERT (ix_ < vec_->num, "remove", TDEF); \
814 vec_->vec[ix_] = vec_->vec[--vec_->num]; \
817 static inline TDEF *VEC_OP (TDEF,address) \
818 (VEC (TDEF) *vec_) \
820 return vec_ ? vec_->vec : 0; \
823 struct vec_swallow_trailing_semi
824 #endif
826 #endif /* GCC_VEC_H */