2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3 Contributed by Nathan Sidwell <nathan@codesourcery.com>
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #if !defined (GDB_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.
31 Because of the different behavior of structure objects, scalar
32 objects and of pointers, there are three flavors, one for each of
33 these variants. Both the structure object and pointer variants
34 pass pointers to objects around -- in the former case the pointers
35 are stored into the vector and in the latter case the pointers are
36 dereferenced and the objects copied into the vector. The scalar
37 object variant is suitable for int-like objects, and the vector
38 elements are returned by value.
40 There are both 'index' and 'iterate' accessors. The iterator
41 returns a boolean iteration condition and updates the iteration
42 variable passed by reference. Because the iterator will be
43 inlined, the address-of can be optimized away.
45 The vectors are implemented using the trailing array idiom, thus
46 they are not resizeable without changing the address of the vector
47 object itself. This means you cannot have variables or fields of
48 vector type -- always use a pointer to a vector. The one exception
49 is the final field of a structure, which could be a vector type.
50 You will have to use the embedded_size & embedded_init calls to
51 create such objects, and they will probably not be resizeable (so
52 don't use the 'safe' allocation variants). The trailing array
53 idiom is used (rather than a pointer to an array of data), because,
54 if we allow NULL to also represent an empty vector, empty vectors
55 occupy minimal space in the structure containing them.
57 Each operation that increases the number of active elements is
58 available in 'quick' and 'safe' variants. The former presumes that
59 there is sufficient allocated space for the operation to succeed
60 (it dies if there is not). The latter will reallocate the
61 vector, if needed. Reallocation causes an exponential increase in
62 vector size. If you know you will be adding N elements, it would
63 be more efficient to use the reserve operation before adding the
64 elements with the 'quick' operation. This will ensure there are at
65 least as many elements as you ask for, it will exponentially
66 increase if there are too few spare slots. If you want reserve a
67 specific number of slots, but do not want the exponential increase
68 (for instance, you know this is the last allocation), use a
69 negative number for reservation. You can also create a vector of a
70 specific size from the get go.
72 You should prefer the push and pop operations, as they append and
73 remove from the end of the vector. If you need to remove several
74 items in one go, use the truncate operation. The insert and remove
75 operations allow you to change elements in the middle of the
76 vector. There are two remove operations, one which preserves the
77 element ordering 'ordered_remove', and one which does not
78 'unordered_remove'. The latter function copies the end element
79 into the removed slot, rather than invoke a memmove operation. The
80 'lower_bound' function will determine where to place an item in the
81 array using insert that will maintain sorted order.
83 If you need to directly manipulate a vector, then the 'address'
84 accessor will return the address of the start of the vector. Also
85 the 'space' predicate will tell you whether there is spare capacity
86 in the vector. You will not normally need to use these two functions.
88 Vector types are defined using a DEF_VEC_{O,P,I}(TYPEDEF) macro.
89 Variables of vector type are declared using a VEC(TYPEDEF) macro.
90 The characters O, P and I indicate whether TYPEDEF is a pointer
91 (P), object (O) or integral (I) type. Be careful to pick the
92 correct one, as you'll get an awkward and inefficient API if you
93 use the wrong one. There is a check, which results in a
94 compile-time warning, for the P and I versions, but there is no
95 check for the O versions, as that is not possible in plain C.
97 An example of their use would be,
99 DEF_VEC_P(tree); // non-managed tree vector.
102 VEC(tree) *v; // A (pointer to) a vector of tree pointers.
107 if (VEC_length(tree, s->v)) { we have some contents }
108 VEC_safe_push(tree, s->v, decl); // append some decl onto the end
109 for (ix = 0; VEC_iterate(tree, s->v, ix, elt); ix++)
110 { do something with elt }
114 /* Macros to invoke API calls. A single macro works for both pointer
115 and object vectors, but the argument and return types might well be
116 different. In each macro, T is the typedef of the vector elements.
117 Some of these macros pass the vector, V, by reference (by taking
118 its address), this is noted in the descriptions. */
121 unsigned VEC_T_length(const VEC(T) *v);
123 Return the number of active elements in V. V can be NULL, in which
124 case zero is returned. */
126 #define VEC_length(T,V) (VEC_OP(T,length)(V))
129 /* Check if vector is empty
130 int VEC_T_empty(const VEC(T) *v);
132 Return nonzero if V is an empty vector (or V is NULL), zero otherwise. */
134 #define VEC_empty(T,V) (VEC_length (T,V) == 0)
137 /* Get the final element of the vector.
138 T VEC_T_last(VEC(T) *v); // Integer
139 T VEC_T_last(VEC(T) *v); // Pointer
140 T *VEC_T_last(VEC(T) *v); // Object
142 Return the final element. V must not be empty. */
144 #define VEC_last(T,V) (VEC_OP(T,last)(V VEC_ASSERT_INFO))
147 T VEC_T_index(VEC(T) *v, unsigned ix); // Integer
148 T VEC_T_index(VEC(T) *v, unsigned ix); // Pointer
149 T *VEC_T_index(VEC(T) *v, unsigned ix); // Object
151 Return the IX'th element. If IX must be in the domain of V. */
153 #define VEC_index(T,V,I) (VEC_OP(T,index)(V,I VEC_ASSERT_INFO))
155 /* Iterate over vector
156 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Integer
157 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Pointer
158 int VEC_T_iterate(VEC(T) *v, unsigned ix, T *&ptr); // Object
160 Return iteration condition and update PTR to point to the IX'th
161 element. At the end of iteration, sets PTR to NULL. Use this to
162 iterate over the elements of a vector as follows,
164 for (ix = 0; VEC_iterate(T,v,ix,ptr); ix++)
167 #define VEC_iterate(T,V,I,P) (VEC_OP(T,iterate)(V,I,&(P)))
169 /* Allocate new vector.
170 VEC(T,A) *VEC_T_alloc(int reserve);
172 Allocate a new vector with space for RESERVE objects. If RESERVE
173 is zero, NO vector is created. */
175 #define VEC_alloc(T,N) (VEC_OP(T,alloc)(N))
178 void VEC_T_free(VEC(T,A) *&);
180 Free a vector and set it to NULL. */
182 #define VEC_free(T,V) (VEC_OP(T,free)(&V))
184 /* A cleanup function for a vector.
185 void VEC_T_cleanup(void *);
187 Clean up a vector. */
189 #define VEC_cleanup(T) (VEC_OP(T,cleanup))
191 /* Use these to determine the required size and initialization of a
192 vector embedded within another structure (as the final member).
194 size_t VEC_T_embedded_size(int reserve);
195 void VEC_T_embedded_init(VEC(T) *v, int reserve);
197 These allow the caller to perform the memory allocation. */
199 #define VEC_embedded_size(T,N) (VEC_OP(T,embedded_size)(N))
200 #define VEC_embedded_init(T,O,N) (VEC_OP(T,embedded_init)(VEC_BASE(O),N))
203 VEC(T,A) *VEC_T_copy(VEC(T) *);
205 Copy the live elements of a vector into a new vector. The new and
206 old vectors need not be allocated by the same mechanism. */
208 #define VEC_copy(T,V) (VEC_OP(T,copy)(V))
210 /* Merge two vectors.
211 VEC(T,A) *VEC_T_merge(VEC(T) *, VEC(T) *);
213 Copy the live elements of both vectors into a new vector. The new
214 and old vectors need not be allocated by the same mechanism. */
215 #define VEC_merge(T,V1,V2) (VEC_OP(T,merge)(V1, V2))
217 /* Determine if a vector has additional capacity.
219 int VEC_T_space (VEC(T) *v,int reserve)
221 If V has space for RESERVE additional entries, return nonzero. You
222 usually only need to use this if you are doing your own vector
223 reallocation, for instance on an embedded vector. This returns
224 nonzero in exactly the same circumstances that VEC_T_reserve
227 #define VEC_space(T,V,R) (VEC_OP(T,space)(V,R VEC_ASSERT_INFO))
230 int VEC_T_reserve(VEC(T,A) *&v, int reserve);
232 Ensure that V has at least abs(RESERVE) slots available. The
233 signedness of RESERVE determines the reallocation behavior. A
234 negative value will not create additional headroom beyond that
235 requested. A positive value will create additional headroom. Note
236 this can cause V to be reallocated. Returns nonzero iff
237 reallocation actually occurred. */
239 #define VEC_reserve(T,V,R) (VEC_OP(T,reserve)(&(V),R VEC_ASSERT_INFO))
241 /* Push object with no reallocation
242 T *VEC_T_quick_push (VEC(T) *v, T obj); // Integer
243 T *VEC_T_quick_push (VEC(T) *v, T obj); // Pointer
244 T *VEC_T_quick_push (VEC(T) *v, T *obj); // Object
246 Push a new element onto the end, returns a pointer to the slot
247 filled in. For object vectors, the new value can be NULL, in which
248 case NO initialization is performed. There must
249 be sufficient space in the vector. */
251 #define VEC_quick_push(T,V,O) (VEC_OP(T,quick_push)(V,O VEC_ASSERT_INFO))
253 /* Push object with reallocation
254 T *VEC_T_safe_push (VEC(T,A) *&v, T obj); // Integer
255 T *VEC_T_safe_push (VEC(T,A) *&v, T obj); // Pointer
256 T *VEC_T_safe_push (VEC(T,A) *&v, T *obj); // Object
258 Push a new element onto the end, returns a pointer to the slot
259 filled in. For object vectors, the new value can be NULL, in which
260 case NO initialization is performed. Reallocates V, if needed. */
262 #define VEC_safe_push(T,V,O) (VEC_OP(T,safe_push)(&(V),O VEC_ASSERT_INFO))
264 /* Pop element off end
265 T VEC_T_pop (VEC(T) *v); // Integer
266 T VEC_T_pop (VEC(T) *v); // Pointer
267 void VEC_T_pop (VEC(T) *v); // Object
269 Pop the last element off the end. Returns the element popped, for
272 #define VEC_pop(T,V) (VEC_OP(T,pop)(V VEC_ASSERT_INFO))
274 /* Truncate to specific length
275 void VEC_T_truncate (VEC(T) *v, unsigned len);
277 Set the length as specified. The new length must be less than or
278 equal to the current length. This is an O(1) operation. */
280 #define VEC_truncate(T,V,I) \
281 (VEC_OP(T,truncate)(V,I VEC_ASSERT_INFO))
283 /* Grow to a specific length.
284 void VEC_T_safe_grow (VEC(T,A) *&v, int len);
286 Grow the vector to a specific length. The LEN must be as
287 long or longer than the current length. The new elements are
290 #define VEC_safe_grow(T,V,I) \
291 (VEC_OP(T,safe_grow)(&(V),I VEC_ASSERT_INFO))
294 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Integer
295 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Pointer
296 T *VEC_T_replace (VEC(T) *v, unsigned ix, T *val); // Object
298 Replace the IXth element of V with a new value, VAL. For pointer
299 vectors returns the original value. For object vectors returns a
300 pointer to the new value. For object vectors the new value can be
301 NULL, in which case no overwriting of the slot is actually
304 #define VEC_replace(T,V,I,O) (VEC_OP(T,replace)(V,I,O VEC_ASSERT_INFO))
306 /* Insert object with no reallocation
307 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Integer
308 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Pointer
309 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T *val); // Object
311 Insert an element, VAL, at the IXth position of V. Return a pointer
312 to the slot created. For vectors of object, the new value can be
313 NULL, in which case no initialization of the inserted slot takes
314 place. There must be sufficient space. */
316 #define VEC_quick_insert(T,V,I,O) \
317 (VEC_OP(T,quick_insert)(V,I,O VEC_ASSERT_INFO))
319 /* Insert object with reallocation
320 T *VEC_T_safe_insert (VEC(T,A) *&v, unsigned ix, T val); // Integer
321 T *VEC_T_safe_insert (VEC(T,A) *&v, unsigned ix, T val); // Pointer
322 T *VEC_T_safe_insert (VEC(T,A) *&v, unsigned ix, T *val); // Object
324 Insert an element, VAL, at the IXth position of V. Return a pointer
325 to the slot created. For vectors of object, the new value can be
326 NULL, in which case no initialization of the inserted slot takes
327 place. Reallocate V, if necessary. */
329 #define VEC_safe_insert(T,V,I,O) \
330 (VEC_OP(T,safe_insert)(&(V),I,O VEC_ASSERT_INFO))
332 /* Remove element retaining order
333 T VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Integer
334 T VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Pointer
335 void VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Object
337 Remove an element from the IXth position of V. Ordering of
338 remaining elements is preserved. For pointer vectors returns the
339 removed object. This is an O(N) operation due to a memmove. */
341 #define VEC_ordered_remove(T,V,I) \
342 (VEC_OP(T,ordered_remove)(V,I VEC_ASSERT_INFO))
344 /* Remove element destroying order
345 T VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Integer
346 T VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Pointer
347 void VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Object
349 Remove an element from the IXth position of V. Ordering of
350 remaining elements is destroyed. For pointer vectors returns the
351 removed object. This is an O(1) operation. */
353 #define VEC_unordered_remove(T,V,I) \
354 (VEC_OP(T,unordered_remove)(V,I VEC_ASSERT_INFO))
356 /* Remove a block of elements
357 void VEC_T_block_remove (VEC(T) *v, unsigned ix, unsigned len);
359 Remove LEN elements starting at the IXth. Ordering is retained.
360 This is an O(N) operation due to memmove. */
362 #define VEC_block_remove(T,V,I,L) \
363 (VEC_OP(T,block_remove)(V,I,L VEC_ASSERT_INFO))
365 /* Get the address of the array of elements
366 T *VEC_T_address (VEC(T) v)
368 If you need to directly manipulate the array (for instance, you
369 want to feed it to qsort), use this accessor. */
371 #define VEC_address(T,V) (VEC_OP(T,address)(V))
373 /* Find the first index in the vector not less than the object.
374 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
375 int (*lessthan) (const T, const T)); // Integer
376 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
377 int (*lessthan) (const T, const T)); // Pointer
378 unsigned VEC_T_lower_bound (VEC(T) *v, const T *val,
379 int (*lessthan) (const T*, const T*)); // Object
381 Find the first position in which VAL could be inserted without
382 changing the ordering of V. LESSTHAN is a function that returns
383 true if the first argument is strictly less than the second. */
385 #define VEC_lower_bound(T,V,O,LT) \
386 (VEC_OP(T,lower_bound)(V,O,LT VEC_ASSERT_INFO))
388 /* Reallocate an array of elements with prefix. */
389 extern void *vec_p_reserve (void *, int);
390 extern void *vec_o_reserve (void *, int, size_t, size_t);
391 #define vec_free_(V) xfree (V)
393 #define VEC_ASSERT_INFO ,__FILE__,__LINE__
394 #define VEC_ASSERT_DECL ,const char *file_,unsigned line_
395 #define VEC_ASSERT_PASS ,file_,line_
396 #define vec_assert(expr, op) \
397 ((void)((expr) ? 0 : (gdb_assert_fail (op, file_, line_, \
400 #define VEC(T) VEC_##T
401 #define VEC_OP(T,OP) VEC_##T##_##OP
404 typedef struct VEC(T) \
411 /* Vector of integer-like object. */
412 #define DEF_VEC_I(T) \
413 static inline void VEC_OP (T,must_be_integral_type) (void) \
420 DEF_VEC_ALLOC_FUNC_I(T) \
421 struct vec_swallow_trailing_semi
423 /* Vector of pointer to object. */
424 #define DEF_VEC_P(T) \
425 static inline void VEC_OP (T,must_be_pointer_type) (void) \
427 (void)((T)1 == (void *)1); \
432 DEF_VEC_ALLOC_FUNC_P(T) \
433 struct vec_swallow_trailing_semi
435 /* Vector of object. */
436 #define DEF_VEC_O(T) \
439 DEF_VEC_ALLOC_FUNC_O(T) \
440 struct vec_swallow_trailing_semi
442 #define DEF_VEC_ALLOC_FUNC_I(T) \
443 static inline VEC(T) *VEC_OP (T,alloc) \
446 /* We must request exact size allocation, hence the negation. */ \
447 return (VEC(T) *) vec_o_reserve (NULL, -alloc_, \
448 offsetof (VEC(T),vec), sizeof (T)); \
451 static inline VEC(T) *VEC_OP (T,copy) (VEC(T) *vec_) \
453 size_t len_ = vec_ ? vec_->num : 0; \
454 VEC (T) *new_vec_ = NULL; \
458 /* We must request exact size allocation, hence the negation. */ \
459 new_vec_ = (VEC (T) *) \
460 vec_o_reserve (NULL, -len_, offsetof (VEC(T),vec), sizeof (T)); \
462 new_vec_->num = len_; \
463 memcpy (new_vec_->vec, vec_->vec, sizeof (T) * len_); \
468 static inline VEC(T) *VEC_OP (T,merge) (VEC(T) *vec1_, VEC(T) *vec2_) \
470 if (vec1_ && vec2_) \
472 size_t len_ = vec1_->num + vec2_->num; \
473 VEC (T) *new_vec_ = NULL; \
475 /* We must request exact size allocation, hence the negation. */ \
476 new_vec_ = (VEC (T) *) \
477 vec_o_reserve (NULL, -len_, offsetof (VEC(T),vec), sizeof (T)); \
479 new_vec_->num = len_; \
480 memcpy (new_vec_->vec, vec1_->vec, sizeof (T) * vec1_->num); \
481 memcpy (new_vec_->vec + vec1_->num, vec2_->vec, \
482 sizeof (T) * vec2_->num); \
487 return VEC_copy (T, vec1_ ? vec1_ : vec2_); \
490 static inline void VEC_OP (T,free) \
498 static inline void VEC_OP (T,cleanup) \
501 VEC(T) **vec_ = arg_; \
507 static inline int VEC_OP (T,reserve) \
508 (VEC(T) **vec_, int alloc_ VEC_ASSERT_DECL) \
510 int extend = !VEC_OP (T,space) \
511 (*vec_, alloc_ < 0 ? -alloc_ : alloc_ VEC_ASSERT_PASS); \
514 *vec_ = (VEC(T) *) vec_o_reserve (*vec_, alloc_, \
515 offsetof (VEC(T),vec), sizeof (T)); \
520 static inline void VEC_OP (T,safe_grow) \
521 (VEC(T) **vec_, int size_ VEC_ASSERT_DECL) \
523 vec_assert (size_ >= 0 && VEC_OP(T,length) (*vec_) <= (unsigned)size_, \
525 VEC_OP (T,reserve) (vec_, (int)(*vec_ ? (*vec_)->num : 0) - size_ \
527 (*vec_)->num = size_; \
530 static inline T *VEC_OP (T,safe_push) \
531 (VEC(T) **vec_, const T obj_ VEC_ASSERT_DECL) \
533 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
535 return VEC_OP (T,quick_push) (*vec_, obj_ VEC_ASSERT_PASS); \
538 static inline T *VEC_OP (T,safe_insert) \
539 (VEC(T) **vec_, unsigned ix_, const T obj_ VEC_ASSERT_DECL) \
541 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
543 return VEC_OP (T,quick_insert) (*vec_, ix_, obj_ VEC_ASSERT_PASS); \
546 #define DEF_VEC_FUNC_P(T) \
547 static inline unsigned VEC_OP (T,length) (const VEC(T) *vec_) \
549 return vec_ ? vec_->num : 0; \
552 static inline T VEC_OP (T,last) \
553 (const VEC(T) *vec_ VEC_ASSERT_DECL) \
555 vec_assert (vec_ && vec_->num, "last"); \
557 return vec_->vec[vec_->num - 1]; \
560 static inline T VEC_OP (T,index) \
561 (const VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
563 vec_assert (vec_ && ix_ < vec_->num, "index"); \
565 return vec_->vec[ix_]; \
568 static inline int VEC_OP (T,iterate) \
569 (const VEC(T) *vec_, unsigned ix_, T *ptr) \
571 if (vec_ && ix_ < vec_->num) \
573 *ptr = vec_->vec[ix_]; \
583 static inline size_t VEC_OP (T,embedded_size) \
586 return offsetof (VEC(T),vec) + alloc_ * sizeof(T); \
589 static inline void VEC_OP (T,embedded_init) \
590 (VEC(T) *vec_, int alloc_) \
593 vec_->alloc = alloc_; \
596 static inline int VEC_OP (T,space) \
597 (VEC(T) *vec_, int alloc_ VEC_ASSERT_DECL) \
599 vec_assert (alloc_ >= 0, "space"); \
600 return vec_ ? vec_->alloc - vec_->num >= (unsigned)alloc_ : !alloc_; \
603 static inline T *VEC_OP (T,quick_push) \
604 (VEC(T) *vec_, T obj_ VEC_ASSERT_DECL) \
608 vec_assert (vec_->num < vec_->alloc, "quick_push"); \
609 slot_ = &vec_->vec[vec_->num++]; \
615 static inline T VEC_OP (T,pop) (VEC(T) *vec_ VEC_ASSERT_DECL) \
619 vec_assert (vec_->num, "pop"); \
620 obj_ = vec_->vec[--vec_->num]; \
625 static inline void VEC_OP (T,truncate) \
626 (VEC(T) *vec_, unsigned size_ VEC_ASSERT_DECL) \
628 vec_assert (vec_ ? vec_->num >= size_ : !size_, "truncate"); \
633 static inline T VEC_OP (T,replace) \
634 (VEC(T) *vec_, unsigned ix_, T obj_ VEC_ASSERT_DECL) \
638 vec_assert (ix_ < vec_->num, "replace"); \
639 old_obj_ = vec_->vec[ix_]; \
640 vec_->vec[ix_] = obj_; \
645 static inline T *VEC_OP (T,quick_insert) \
646 (VEC(T) *vec_, unsigned ix_, T obj_ VEC_ASSERT_DECL) \
650 vec_assert (vec_->num < vec_->alloc && ix_ <= vec_->num, "quick_insert"); \
651 slot_ = &vec_->vec[ix_]; \
652 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (T)); \
658 static inline T VEC_OP (T,ordered_remove) \
659 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
664 vec_assert (ix_ < vec_->num, "ordered_remove"); \
665 slot_ = &vec_->vec[ix_]; \
667 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (T)); \
672 static inline T VEC_OP (T,unordered_remove) \
673 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
678 vec_assert (ix_ < vec_->num, "unordered_remove"); \
679 slot_ = &vec_->vec[ix_]; \
681 *slot_ = vec_->vec[--vec_->num]; \
686 static inline void VEC_OP (T,block_remove) \
687 (VEC(T) *vec_, unsigned ix_, unsigned len_ VEC_ASSERT_DECL) \
691 vec_assert (ix_ + len_ <= vec_->num, "block_remove"); \
692 slot_ = &vec_->vec[ix_]; \
694 memmove (slot_, slot_ + len_, (vec_->num - ix_) * sizeof (T)); \
697 static inline T *VEC_OP (T,address) \
700 return vec_ ? vec_->vec : 0; \
703 static inline unsigned VEC_OP (T,lower_bound) \
704 (VEC(T) *vec_, const T obj_, \
705 int (*lessthan_)(const T, const T) VEC_ASSERT_DECL) \
707 unsigned int len_ = VEC_OP (T, length) (vec_); \
708 unsigned int half_, middle_; \
709 unsigned int first_ = 0; \
716 middle_elem_ = VEC_OP (T,index) (vec_, middle_ VEC_ASSERT_PASS); \
717 if (lessthan_ (middle_elem_, obj_)) \
721 len_ = len_ - half_ - 1; \
729 #define DEF_VEC_ALLOC_FUNC_P(T) \
730 static inline VEC(T) *VEC_OP (T,alloc) \
733 /* We must request exact size allocation, hence the negation. */ \
734 return (VEC(T) *) vec_p_reserve (NULL, -alloc_); \
737 static inline void VEC_OP (T,free) \
745 static inline void VEC_OP (T,cleanup) \
748 VEC(T) **vec_ = arg_; \
754 static inline VEC(T) *VEC_OP (T,copy) (VEC(T) *vec_) \
756 size_t len_ = vec_ ? vec_->num : 0; \
757 VEC (T) *new_vec_ = NULL; \
761 /* We must request exact size allocation, hence the negation. */ \
762 new_vec_ = (VEC (T) *)(vec_p_reserve (NULL, -len_)); \
764 new_vec_->num = len_; \
765 memcpy (new_vec_->vec, vec_->vec, sizeof (T) * len_); \
770 static inline VEC(T) *VEC_OP (T,merge) (VEC(T) *vec1_, VEC(T) *vec2_) \
772 if (vec1_ && vec2_) \
774 size_t len_ = vec1_->num + vec2_->num; \
775 VEC (T) *new_vec_ = NULL; \
777 /* We must request exact size allocation, hence the negation. */ \
778 new_vec_ = (VEC (T) *)(vec_p_reserve (NULL, -len_)); \
780 new_vec_->num = len_; \
781 memcpy (new_vec_->vec, vec1_->vec, sizeof (T) * vec1_->num); \
782 memcpy (new_vec_->vec + vec1_->num, vec2_->vec, \
783 sizeof (T) * vec2_->num); \
788 return VEC_copy (T, vec1_ ? vec1_ : vec2_); \
791 static inline int VEC_OP (T,reserve) \
792 (VEC(T) **vec_, int alloc_ VEC_ASSERT_DECL) \
794 int extend = !VEC_OP (T,space) \
795 (*vec_, alloc_ < 0 ? -alloc_ : alloc_ VEC_ASSERT_PASS); \
798 *vec_ = (VEC(T) *) vec_p_reserve (*vec_, alloc_); \
803 static inline void VEC_OP (T,safe_grow) \
804 (VEC(T) **vec_, int size_ VEC_ASSERT_DECL) \
806 vec_assert (size_ >= 0 && VEC_OP(T,length) (*vec_) <= (unsigned)size_, \
809 (vec_, (int)(*vec_ ? (*vec_)->num : 0) - size_ VEC_ASSERT_PASS); \
810 (*vec_)->num = size_; \
813 static inline T *VEC_OP (T,safe_push) \
814 (VEC(T) **vec_, T obj_ VEC_ASSERT_DECL) \
816 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
818 return VEC_OP (T,quick_push) (*vec_, obj_ VEC_ASSERT_PASS); \
821 static inline T *VEC_OP (T,safe_insert) \
822 (VEC(T) **vec_, unsigned ix_, T obj_ VEC_ASSERT_DECL) \
824 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
826 return VEC_OP (T,quick_insert) (*vec_, ix_, obj_ VEC_ASSERT_PASS); \
829 #define DEF_VEC_FUNC_O(T) \
830 static inline unsigned VEC_OP (T,length) (const VEC(T) *vec_) \
832 return vec_ ? vec_->num : 0; \
835 static inline T *VEC_OP (T,last) (VEC(T) *vec_ VEC_ASSERT_DECL) \
837 vec_assert (vec_ && vec_->num, "last"); \
839 return &vec_->vec[vec_->num - 1]; \
842 static inline T *VEC_OP (T,index) \
843 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
845 vec_assert (vec_ && ix_ < vec_->num, "index"); \
847 return &vec_->vec[ix_]; \
850 static inline int VEC_OP (T,iterate) \
851 (VEC(T) *vec_, unsigned ix_, T **ptr) \
853 if (vec_ && ix_ < vec_->num) \
855 *ptr = &vec_->vec[ix_]; \
865 static inline size_t VEC_OP (T,embedded_size) \
868 return offsetof (VEC(T),vec) + alloc_ * sizeof(T); \
871 static inline void VEC_OP (T,embedded_init) \
872 (VEC(T) *vec_, int alloc_) \
875 vec_->alloc = alloc_; \
878 static inline int VEC_OP (T,space) \
879 (VEC(T) *vec_, int alloc_ VEC_ASSERT_DECL) \
881 vec_assert (alloc_ >= 0, "space"); \
882 return vec_ ? vec_->alloc - vec_->num >= (unsigned)alloc_ : !alloc_; \
885 static inline T *VEC_OP (T,quick_push) \
886 (VEC(T) *vec_, const T *obj_ VEC_ASSERT_DECL) \
890 vec_assert (vec_->num < vec_->alloc, "quick_push"); \
891 slot_ = &vec_->vec[vec_->num++]; \
898 static inline void VEC_OP (T,pop) (VEC(T) *vec_ VEC_ASSERT_DECL) \
900 vec_assert (vec_->num, "pop"); \
904 static inline void VEC_OP (T,truncate) \
905 (VEC(T) *vec_, unsigned size_ VEC_ASSERT_DECL) \
907 vec_assert (vec_ ? vec_->num >= size_ : !size_, "truncate"); \
912 static inline T *VEC_OP (T,replace) \
913 (VEC(T) *vec_, unsigned ix_, const T *obj_ VEC_ASSERT_DECL) \
917 vec_assert (ix_ < vec_->num, "replace"); \
918 slot_ = &vec_->vec[ix_]; \
925 static inline T *VEC_OP (T,quick_insert) \
926 (VEC(T) *vec_, unsigned ix_, const T *obj_ VEC_ASSERT_DECL) \
930 vec_assert (vec_->num < vec_->alloc && ix_ <= vec_->num, "quick_insert"); \
931 slot_ = &vec_->vec[ix_]; \
932 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (T)); \
939 static inline void VEC_OP (T,ordered_remove) \
940 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
944 vec_assert (ix_ < vec_->num, "ordered_remove"); \
945 slot_ = &vec_->vec[ix_]; \
946 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (T)); \
949 static inline void VEC_OP (T,unordered_remove) \
950 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
952 vec_assert (ix_ < vec_->num, "unordered_remove"); \
953 vec_->vec[ix_] = vec_->vec[--vec_->num]; \
956 static inline void VEC_OP (T,block_remove) \
957 (VEC(T) *vec_, unsigned ix_, unsigned len_ VEC_ASSERT_DECL) \
961 vec_assert (ix_ + len_ <= vec_->num, "block_remove"); \
962 slot_ = &vec_->vec[ix_]; \
964 memmove (slot_, slot_ + len_, (vec_->num - ix_) * sizeof (T)); \
967 static inline T *VEC_OP (T,address) \
970 return vec_ ? vec_->vec : 0; \
973 static inline unsigned VEC_OP (T,lower_bound) \
974 (VEC(T) *vec_, const T *obj_, \
975 int (*lessthan_)(const T *, const T *) VEC_ASSERT_DECL) \
977 unsigned int len_ = VEC_OP (T, length) (vec_); \
978 unsigned int half_, middle_; \
979 unsigned int first_ = 0; \
986 middle_elem_ = VEC_OP (T,index) (vec_, middle_ VEC_ASSERT_PASS); \
987 if (lessthan_ (middle_elem_, obj_)) \
991 len_ = len_ - half_ - 1; \
999 #define DEF_VEC_ALLOC_FUNC_O(T) \
1000 static inline VEC(T) *VEC_OP (T,alloc) \
1003 /* We must request exact size allocation, hence the negation. */ \
1004 return (VEC(T) *) vec_o_reserve (NULL, -alloc_, \
1005 offsetof (VEC(T),vec), sizeof (T)); \
1008 static inline VEC(T) *VEC_OP (T,copy) (VEC(T) *vec_) \
1010 size_t len_ = vec_ ? vec_->num : 0; \
1011 VEC (T) *new_vec_ = NULL; \
1015 /* We must request exact size allocation, hence the negation. */ \
1016 new_vec_ = (VEC (T) *) \
1017 vec_o_reserve (NULL, -len_, offsetof (VEC(T),vec), sizeof (T)); \
1019 new_vec_->num = len_; \
1020 memcpy (new_vec_->vec, vec_->vec, sizeof (T) * len_); \
1025 static inline VEC(T) *VEC_OP (T,merge) (VEC(T) *vec1_, VEC(T) *vec2_) \
1027 if (vec1_ && vec2_) \
1029 size_t len_ = vec1_->num + vec2_->num; \
1030 VEC (T) *new_vec_ = NULL; \
1032 /* We must request exact size allocation, hence the negation. */ \
1033 new_vec_ = (VEC (T) *) \
1034 vec_o_reserve (NULL, -len_, offsetof (VEC(T),vec), sizeof (T)); \
1036 new_vec_->num = len_; \
1037 memcpy (new_vec_->vec, vec1_->vec, sizeof (T) * vec1_->num); \
1038 memcpy (new_vec_->vec + vec1_->num, vec2_->vec, \
1039 sizeof (T) * vec2_->num); \
1044 return VEC_copy (T, vec1_ ? vec1_ : vec2_); \
1047 static inline void VEC_OP (T,free) \
1051 vec_free_ (*vec_); \
1055 static inline void VEC_OP (T,cleanup) \
1058 VEC(T) **vec_ = arg_; \
1060 vec_free_ (*vec_); \
1064 static inline int VEC_OP (T,reserve) \
1065 (VEC(T) **vec_, int alloc_ VEC_ASSERT_DECL) \
1067 int extend = !VEC_OP (T,space) (*vec_, alloc_ < 0 ? -alloc_ : alloc_ \
1071 *vec_ = (VEC(T) *) \
1072 vec_o_reserve (*vec_, alloc_, offsetof (VEC(T),vec), sizeof (T)); \
1077 static inline void VEC_OP (T,safe_grow) \
1078 (VEC(T) **vec_, int size_ VEC_ASSERT_DECL) \
1080 vec_assert (size_ >= 0 && VEC_OP(T,length) (*vec_) <= (unsigned)size_, \
1082 VEC_OP (T,reserve) \
1083 (vec_, (int)(*vec_ ? (*vec_)->num : 0) - size_ VEC_ASSERT_PASS); \
1084 (*vec_)->num = size_; \
1087 static inline T *VEC_OP (T,safe_push) \
1088 (VEC(T) **vec_, const T *obj_ VEC_ASSERT_DECL) \
1090 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
1092 return VEC_OP (T,quick_push) (*vec_, obj_ VEC_ASSERT_PASS); \
1095 static inline T *VEC_OP (T,safe_insert) \
1096 (VEC(T) **vec_, unsigned ix_, const T *obj_ VEC_ASSERT_DECL) \
1098 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
1100 return VEC_OP (T,quick_insert) (*vec_, ix_, obj_ VEC_ASSERT_PASS); \
1103 #endif /* GDB_VEC_H */
This page took 0.05276 seconds and 5 git commands to generate.