2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Nathan Sidwell <nathan@codesourcery.com>
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #if !defined (GDB_VEC_H)
25 #include "gdb_string.h"
26 #include "gdb_assert.h"
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.
34 Because of the different behavior of structure objects, scalar
35 objects and of pointers, there are three flavors, one for each of
36 these variants. Both the structure object and pointer variants
37 pass pointers to objects around -- in the former case the pointers
38 are stored into the vector and in the latter case the pointers are
39 dereferenced and the objects copied into the vector. The scalar
40 object variant is suitable for int-like objects, and the vector
41 elements are returned by value.
43 There are both 'index' and 'iterate' accessors. The iterator
44 returns a boolean iteration condition and updates the iteration
45 variable passed by reference. Because the iterator will be
46 inlined, the address-of can be optimized away.
48 The vectors are implemented using the trailing array idiom, thus
49 they are not resizeable without changing the address of the vector
50 object itself. This means you cannot have variables or fields of
51 vector type -- always use a pointer to a vector. The one exception
52 is the final field of a structure, which could be a vector type.
53 You will have to use the embedded_size & embedded_init calls to
54 create such objects, and they will probably not be resizeable (so
55 don't use the 'safe' allocation variants). The trailing array
56 idiom is used (rather than a pointer to an array of data), because,
57 if we allow NULL to also represent an empty vector, empty vectors
58 occupy minimal space in the structure containing them.
60 Each operation that increases the number of active elements is
61 available in 'quick' and 'safe' variants. The former presumes that
62 there is sufficient allocated space for the operation to succeed
63 (it dies if there is not). The latter will reallocate the
64 vector, if needed. Reallocation causes an exponential increase in
65 vector size. If you know you will be adding N elements, it would
66 be more efficient to use the reserve operation before adding the
67 elements with the 'quick' operation. This will ensure there are at
68 least as many elements as you ask for, it will exponentially
69 increase if there are too few spare slots. If you want reserve a
70 specific number of slots, but do not want the exponential increase
71 (for instance, you know this is the last allocation), use a
72 negative number for reservation. You can also create a vector of a
73 specific size from the get go.
75 You should prefer the push and pop operations, as they append and
76 remove from the end of the vector. If you need to remove several
77 items in one go, use the truncate operation. The insert and remove
78 operations allow you to change elements in the middle of the
79 vector. There are two remove operations, one which preserves the
80 element ordering 'ordered_remove', and one which does not
81 'unordered_remove'. The latter function copies the end element
82 into the removed slot, rather than invoke a memmove operation. The
83 'lower_bound' function will determine where to place an item in the
84 array using insert that will maintain sorted order.
86 If you need to directly manipulate a vector, then the 'address'
87 accessor will return the address of the start of the vector. Also
88 the 'space' predicate will tell you whether there is spare capacity
89 in the vector. You will not normally need to use these two functions.
91 Vector types are defined using a DEF_VEC_{O,P,I}(TYPEDEF) macro.
92 Variables of vector type are declared using a VEC(TYPEDEF) macro.
93 The characters O, P and I indicate whether TYPEDEF is a pointer
94 (P), object (O) or integral (I) type. Be careful to pick the
95 correct one, as you'll get an awkward and inefficient API if you
96 use the wrong one. There is a check, which results in a
97 compile-time warning, for the P and I versions, but there is no
98 check for the O versions, as that is not possible in plain C.
100 An example of their use would be,
102 DEF_VEC_P(tree); // non-managed tree vector.
105 VEC(tree) *v; // A (pointer to) a vector of tree pointers.
110 if (VEC_length(tree, s->v)) { we have some contents }
111 VEC_safe_push(tree, s->v, decl); // append some decl onto the end
112 for (ix = 0; VEC_iterate(tree, s->v, ix, elt); ix++)
113 { do something with elt }
117 /* Macros to invoke API calls. A single macro works for both pointer
118 and object vectors, but the argument and return types might well be
119 different. In each macro, T is the typedef of the vector elements.
120 Some of these macros pass the vector, V, by reference (by taking
121 its address), this is noted in the descriptions. */
124 unsigned VEC_T_length(const VEC(T) *v);
126 Return the number of active elements in V. V can be NULL, in which
127 case zero is returned. */
129 #define VEC_length(T,V) (VEC_OP(T,length)(V))
132 /* Check if vector is empty
133 int VEC_T_empty(const VEC(T) *v);
135 Return nonzero if V is an empty vector (or V is NULL), zero otherwise. */
137 #define VEC_empty(T,V) (VEC_length (T,V) == 0)
140 /* Get the final element of the vector.
141 T VEC_T_last(VEC(T) *v); // Integer
142 T VEC_T_last(VEC(T) *v); // Pointer
143 T *VEC_T_last(VEC(T) *v); // Object
145 Return the final element. V must not be empty. */
147 #define VEC_last(T,V) (VEC_OP(T,last)(V VEC_ASSERT_INFO))
150 T VEC_T_index(VEC(T) *v, unsigned ix); // Integer
151 T VEC_T_index(VEC(T) *v, unsigned ix); // Pointer
152 T *VEC_T_index(VEC(T) *v, unsigned ix); // Object
154 Return the IX'th element. If IX must be in the domain of V. */
156 #define VEC_index(T,V,I) (VEC_OP(T,index)(V,I VEC_ASSERT_INFO))
158 /* Iterate over vector
159 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Integer
160 int VEC_T_iterate(VEC(T) *v, unsigned ix, T &ptr); // Pointer
161 int VEC_T_iterate(VEC(T) *v, unsigned ix, T *&ptr); // Object
163 Return iteration condition and update PTR to point to the IX'th
164 element. At the end of iteration, sets PTR to NULL. Use this to
165 iterate over the elements of a vector as follows,
167 for (ix = 0; VEC_iterate(T,v,ix,ptr); ix++)
170 #define VEC_iterate(T,V,I,P) (VEC_OP(T,iterate)(V,I,&(P)))
172 /* Allocate new vector.
173 VEC(T,A) *VEC_T_alloc(int reserve);
175 Allocate a new vector with space for RESERVE objects. If RESERVE
176 is zero, NO vector is created. */
178 #define VEC_alloc(T,N) (VEC_OP(T,alloc)(N))
181 void VEC_T_free(VEC(T,A) *&);
183 Free a vector and set it to NULL. */
185 #define VEC_free(T,V) (VEC_OP(T,free)(&V))
187 /* Use these to determine the required size and initialization of a
188 vector embedded within another structure (as the final member).
190 size_t VEC_T_embedded_size(int reserve);
191 void VEC_T_embedded_init(VEC(T) *v, int reserve);
193 These allow the caller to perform the memory allocation. */
195 #define VEC_embedded_size(T,N) (VEC_OP(T,embedded_size)(N))
196 #define VEC_embedded_init(T,O,N) (VEC_OP(T,embedded_init)(VEC_BASE(O),N))
199 VEC(T,A) *VEC_T_copy(VEC(T) *);
201 Copy the live elements of a vector into a new vector. The new and
202 old vectors need not be allocated by the same mechanism. */
204 #define VEC_copy(T,V) (VEC_OP(T,copy)(V))
206 /* Determine if a vector has additional capacity.
208 int VEC_T_space (VEC(T) *v,int reserve)
210 If V has space for RESERVE additional entries, return nonzero. You
211 usually only need to use this if you are doing your own vector
212 reallocation, for instance on an embedded vector. This returns
213 nonzero in exactly the same circumstances that VEC_T_reserve
216 #define VEC_space(T,V,R) (VEC_OP(T,space)(V,R VEC_ASSERT_INFO))
219 int VEC_T_reserve(VEC(T,A) *&v, int reserve);
221 Ensure that V has at least abs(RESERVE) slots available. The
222 signedness of RESERVE determines the reallocation behavior. A
223 negative value will not create additional headroom beyond that
224 requested. A positive value will create additional headroom. Note
225 this can cause V to be reallocated. Returns nonzero iff
226 reallocation actually occurred. */
228 #define VEC_reserve(T,V,R) (VEC_OP(T,reserve)(&(V),R VEC_ASSERT_INFO))
230 /* Push object with no reallocation
231 T *VEC_T_quick_push (VEC(T) *v, T obj); // Integer
232 T *VEC_T_quick_push (VEC(T) *v, T obj); // Pointer
233 T *VEC_T_quick_push (VEC(T) *v, T *obj); // Object
235 Push a new element onto the end, returns a pointer to the slot
236 filled in. For object vectors, the new value can be NULL, in which
237 case NO initialization is performed. There must
238 be sufficient space in the vector. */
240 #define VEC_quick_push(T,V,O) (VEC_OP(T,quick_push)(V,O VEC_ASSERT_INFO))
242 /* Push object with reallocation
243 T *VEC_T_safe_push (VEC(T,A) *&v, T obj); // Integer
244 T *VEC_T_safe_push (VEC(T,A) *&v, T obj); // Pointer
245 T *VEC_T_safe_push (VEC(T,A) *&v, T *obj); // Object
247 Push a new element onto the end, returns a pointer to the slot
248 filled in. For object vectors, the new value can be NULL, in which
249 case NO initialization is performed. Reallocates V, if needed. */
251 #define VEC_safe_push(T,V,O) (VEC_OP(T,safe_push)(&(V),O VEC_ASSERT_INFO))
253 /* Pop element off end
254 T VEC_T_pop (VEC(T) *v); // Integer
255 T VEC_T_pop (VEC(T) *v); // Pointer
256 void VEC_T_pop (VEC(T) *v); // Object
258 Pop the last element off the end. Returns the element popped, for
261 #define VEC_pop(T,V) (VEC_OP(T,pop)(V VEC_ASSERT_INFO))
263 /* Truncate to specific length
264 void VEC_T_truncate (VEC(T) *v, unsigned len);
266 Set the length as specified. The new length must be less than or
267 equal to the current length. This is an O(1) operation. */
269 #define VEC_truncate(T,V,I) \
270 (VEC_OP(T,truncate)(V,I VEC_ASSERT_INFO))
272 /* Grow to a specific length.
273 void VEC_T_safe_grow (VEC(T,A) *&v, int len);
275 Grow the vector to a specific length. The LEN must be as
276 long or longer than the current length. The new elements are
279 #define VEC_safe_grow(T,V,I) \
280 (VEC_OP(T,safe_grow)(&(V),I VEC_ASSERT_INFO))
283 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Integer
284 T VEC_T_replace (VEC(T) *v, unsigned ix, T val); // Pointer
285 T *VEC_T_replace (VEC(T) *v, unsigned ix, T *val); // Object
287 Replace the IXth element of V with a new value, VAL. For pointer
288 vectors returns the original value. For object vectors returns a
289 pointer to the new value. For object vectors the new value can be
290 NULL, in which case no overwriting of the slot is actually
293 #define VEC_replace(T,V,I,O) (VEC_OP(T,replace)(V,I,O VEC_ASSERT_INFO))
295 /* Insert object with no reallocation
296 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Integer
297 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T val); // Pointer
298 T *VEC_T_quick_insert (VEC(T) *v, unsigned ix, T *val); // Object
300 Insert an element, VAL, at the IXth position of V. Return a pointer
301 to the slot created. For vectors of object, the new value can be
302 NULL, in which case no initialization of the inserted slot takes
303 place. There must be sufficient space. */
305 #define VEC_quick_insert(T,V,I,O) \
306 (VEC_OP(T,quick_insert)(V,I,O VEC_ASSERT_INFO))
308 /* Insert object with reallocation
309 T *VEC_T_safe_insert (VEC(T,A) *&v, unsigned ix, T val); // Integer
310 T *VEC_T_safe_insert (VEC(T,A) *&v, unsigned ix, T val); // Pointer
311 T *VEC_T_safe_insert (VEC(T,A) *&v, unsigned ix, T *val); // Object
313 Insert an element, VAL, at the IXth position of V. Return a pointer
314 to the slot created. For vectors of object, the new value can be
315 NULL, in which case no initialization of the inserted slot takes
316 place. Reallocate V, if necessary. */
318 #define VEC_safe_insert(T,V,I,O) \
319 (VEC_OP(T,safe_insert)(&(V),I,O VEC_ASSERT_INFO))
321 /* Remove element retaining order
322 T VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Integer
323 T VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Pointer
324 void VEC_T_ordered_remove (VEC(T) *v, unsigned ix); // Object
326 Remove an element from the IXth position of V. Ordering of
327 remaining elements is preserved. For pointer vectors returns the
328 removed object. This is an O(N) operation due to a memmove. */
330 #define VEC_ordered_remove(T,V,I) \
331 (VEC_OP(T,ordered_remove)(V,I VEC_ASSERT_INFO))
333 /* Remove element destroying order
334 T VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Integer
335 T VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Pointer
336 void VEC_T_unordered_remove (VEC(T) *v, unsigned ix); // Object
338 Remove an element from the IXth position of V. Ordering of
339 remaining elements is destroyed. For pointer vectors returns the
340 removed object. This is an O(1) operation. */
342 #define VEC_unordered_remove(T,V,I) \
343 (VEC_OP(T,unordered_remove)(V,I VEC_ASSERT_INFO))
345 /* Remove a block of elements
346 void VEC_T_block_remove (VEC(T) *v, unsigned ix, unsigned len);
348 Remove LEN elements starting at the IXth. Ordering is retained.
349 This is an O(1) operation. */
351 #define VEC_block_remove(T,V,I,L) \
352 (VEC_OP(T,block_remove)(V,I,L) VEC_ASSERT_INFO)
354 /* Get the address of the array of elements
355 T *VEC_T_address (VEC(T) v)
357 If you need to directly manipulate the array (for instance, you
358 want to feed it to qsort), use this accessor. */
360 #define VEC_address(T,V) (VEC_OP(T,address)(V))
362 /* Find the first index in the vector not less than the object.
363 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
364 int (*lessthan) (const T, const T)); // Integer
365 unsigned VEC_T_lower_bound (VEC(T) *v, const T val,
366 int (*lessthan) (const T, const T)); // Pointer
367 unsigned VEC_T_lower_bound (VEC(T) *v, const T *val,
368 int (*lessthan) (const T*, const T*)); // Object
370 Find the first position in which VAL could be inserted without
371 changing the ordering of V. LESSTHAN is a function that returns
372 true if the first argument is strictly less than the second. */
374 #define VEC_lower_bound(T,V,O,LT) \
375 (VEC_OP(T,lower_bound)(V,O,LT VEC_ASSERT_INFO))
377 /* Reallocate an array of elements with prefix. */
378 extern void *vec_p_reserve (void *, int);
379 extern void *vec_o_reserve (void *, int, size_t, size_t);
380 #define vec_free_(V) xfree (V)
382 #define VEC_ASSERT_INFO ,__FILE__,__LINE__
383 #define VEC_ASSERT_DECL ,const char *file_,unsigned line_
384 #define VEC_ASSERT_PASS ,file_,line_
385 #define vec_assert(expr, op) \
386 ((void)((expr) ? 0 : (gdb_assert_fail (op, file_, line_, ASSERT_FUNCTION), 0)))
388 #define VEC(T) VEC_##T
389 #define VEC_OP(T,OP) VEC_##T##_##OP
392 typedef struct VEC(T) \
399 /* Vector of integer-like object. */
400 #define DEF_VEC_I(T) \
401 static inline void VEC_OP (T,must_be_integral_type) (void) \
408 DEF_VEC_ALLOC_FUNC_I(T) \
409 struct vec_swallow_trailing_semi
411 /* Vector of pointer to object. */
412 #define DEF_VEC_P(T) \
413 static inline void VEC_OP (T,must_be_pointer_type) (void) \
415 (void)((T)1 == (void *)1); \
420 DEF_VEC_ALLOC_FUNC_P(T) \
421 struct vec_swallow_trailing_semi
423 /* Vector of object. */
424 #define DEF_VEC_O(T) \
427 DEF_VEC_ALLOC_FUNC_O(T) \
428 struct vec_swallow_trailing_semi
430 #define DEF_VEC_ALLOC_FUNC_I(T) \
431 static inline VEC(T) *VEC_OP (T,alloc) \
434 /* We must request exact size allocation, hence the negation. */ \
435 return (VEC(T) *) vec_o_reserve (NULL, -alloc_, \
436 offsetof (VEC(T),vec), sizeof (T)); \
439 static inline VEC(T) *VEC_OP (T,copy) (VEC(T) *vec_) \
441 size_t len_ = vec_ ? vec_->num : 0; \
442 VEC (T) *new_vec_ = NULL; \
446 /* We must request exact size allocation, hence the negation. */ \
447 new_vec_ = (VEC (T) *) \
448 vec_o_reserve (NULL, -len_, offsetof (VEC(T),vec), sizeof (T)); \
450 new_vec_->num = len_; \
451 memcpy (new_vec_->vec, vec_->vec, sizeof (T) * len_); \
456 static inline void VEC_OP (T,free) \
464 static inline int VEC_OP (T,reserve) \
465 (VEC(T) **vec_, int alloc_ VEC_ASSERT_DECL) \
467 int extend = !VEC_OP (T,space) \
468 (*vec_, alloc_ < 0 ? -alloc_ : alloc_ VEC_ASSERT_PASS); \
471 *vec_ = (VEC(T) *) vec_o_reserve (*vec_, alloc_, \
472 offsetof (VEC(T),vec), sizeof (T)); \
477 static inline void VEC_OP (T,safe_grow) \
478 (VEC(T) **vec_, int size_ VEC_ASSERT_DECL) \
480 vec_assert (size_ >= 0 && VEC_OP(T,length) (*vec_) <= (unsigned)size_, \
482 VEC_OP (T,reserve) (vec_, (int)(*vec_ ? (*vec_)->num : 0) - size_ \
484 (*vec_)->num = size_; \
487 static inline T *VEC_OP (T,safe_push) \
488 (VEC(T) **vec_, const T obj_ VEC_ASSERT_DECL) \
490 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
492 return VEC_OP (T,quick_push) (*vec_, obj_ VEC_ASSERT_PASS); \
495 static inline T *VEC_OP (T,safe_insert) \
496 (VEC(T) **vec_, unsigned ix_, const T obj_ VEC_ASSERT_DECL) \
498 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
500 return VEC_OP (T,quick_insert) (*vec_, ix_, obj_ VEC_ASSERT_PASS); \
503 #define DEF_VEC_FUNC_P(T) \
504 static inline unsigned VEC_OP (T,length) (const VEC(T) *vec_) \
506 return vec_ ? vec_->num : 0; \
509 static inline T VEC_OP (T,last) \
510 (const VEC(T) *vec_ VEC_ASSERT_DECL) \
512 vec_assert (vec_ && vec_->num, "last"); \
514 return vec_->vec[vec_->num - 1]; \
517 static inline T VEC_OP (T,index) \
518 (const VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
520 vec_assert (vec_ && ix_ < vec_->num, "index"); \
522 return vec_->vec[ix_]; \
525 static inline int VEC_OP (T,iterate) \
526 (const VEC(T) *vec_, unsigned ix_, T *ptr) \
528 if (vec_ && ix_ < vec_->num) \
530 *ptr = vec_->vec[ix_]; \
540 static inline size_t VEC_OP (T,embedded_size) \
543 return offsetof (VEC(T),vec) + alloc_ * sizeof(T); \
546 static inline void VEC_OP (T,embedded_init) \
547 (VEC(T) *vec_, int alloc_) \
550 vec_->alloc = alloc_; \
553 static inline int VEC_OP (T,space) \
554 (VEC(T) *vec_, int alloc_ VEC_ASSERT_DECL) \
556 vec_assert (alloc_ >= 0, "space"); \
557 return vec_ ? vec_->alloc - vec_->num >= (unsigned)alloc_ : !alloc_; \
560 static inline T *VEC_OP (T,quick_push) \
561 (VEC(T) *vec_, T obj_ VEC_ASSERT_DECL) \
565 vec_assert (vec_->num < vec_->alloc, "quick_push"); \
566 slot_ = &vec_->vec[vec_->num++]; \
572 static inline T VEC_OP (T,pop) (VEC(T) *vec_ VEC_ASSERT_DECL) \
576 vec_assert (vec_->num, "pop"); \
577 obj_ = vec_->vec[--vec_->num]; \
582 static inline void VEC_OP (T,truncate) \
583 (VEC(T) *vec_, unsigned size_ VEC_ASSERT_DECL) \
585 vec_assert (vec_ ? vec_->num >= size_ : !size_, "truncate"); \
590 static inline T VEC_OP (T,replace) \
591 (VEC(T) *vec_, unsigned ix_, T obj_ VEC_ASSERT_DECL) \
595 vec_assert (ix_ < vec_->num, "replace"); \
596 old_obj_ = vec_->vec[ix_]; \
597 vec_->vec[ix_] = obj_; \
602 static inline T *VEC_OP (T,quick_insert) \
603 (VEC(T) *vec_, unsigned ix_, T obj_ VEC_ASSERT_DECL) \
607 vec_assert (vec_->num < vec_->alloc && ix_ <= vec_->num, "quick_insert"); \
608 slot_ = &vec_->vec[ix_]; \
609 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (T)); \
615 static inline T VEC_OP (T,ordered_remove) \
616 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
621 vec_assert (ix_ < vec_->num, "ordered_remove"); \
622 slot_ = &vec_->vec[ix_]; \
624 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (T)); \
629 static inline T VEC_OP (T,unordered_remove) \
630 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
635 vec_assert (ix_ < vec_->num, "unordered_remove"); \
636 slot_ = &vec_->vec[ix_]; \
638 *slot_ = vec_->vec[--vec_->num]; \
643 static inline void VEC_OP (T,block_remove) \
644 (VEC(T) *vec_, unsigned ix_, unsigned len_ VEC_ASSERT_DECL) \
648 vec_assert (ix_ + len_ <= vec_->num, "block_remove"); \
649 slot_ = &vec_->vec[ix_]; \
651 memmove (slot_, slot_ + len_, (vec_->num - ix_) * sizeof (T)); \
654 static inline T *VEC_OP (T,address) \
657 return vec_ ? vec_->vec : 0; \
660 static inline unsigned VEC_OP (T,lower_bound) \
661 (VEC(T) *vec_, const T obj_, \
662 int (*lessthan_)(const T, const T) VEC_ASSERT_DECL) \
664 unsigned int len_ = VEC_OP (T, length) (vec_); \
665 unsigned int half_, middle_; \
666 unsigned int first_ = 0; \
673 middle_elem_ = VEC_OP (T,index) (vec_, middle_ VEC_ASSERT_PASS); \
674 if (lessthan_ (middle_elem_, obj_)) \
678 len_ = len_ - half_ - 1; \
686 #define DEF_VEC_ALLOC_FUNC_P(T) \
687 static inline VEC(T) *VEC_OP (T,alloc) \
690 /* We must request exact size allocation, hence the negation. */ \
691 return (VEC(T) *) vec_p_reserve (NULL, -alloc_); \
694 static inline void VEC_OP (T,free) \
702 static inline VEC(T) *VEC_OP (T,copy) (VEC(T) *vec_) \
704 size_t len_ = vec_ ? vec_->num : 0; \
705 VEC (T) *new_vec_ = NULL; \
709 /* We must request exact size allocation, hence the negation. */ \
710 new_vec_ = (VEC (T) *)(vec_p_reserve (NULL, -len_)); \
712 new_vec_->num = len_; \
713 memcpy (new_vec_->vec, vec_->vec, sizeof (T) * len_); \
718 static inline int VEC_OP (T,reserve) \
719 (VEC(T) **vec_, int alloc_ VEC_ASSERT_DECL) \
721 int extend = !VEC_OP (T,space) \
722 (*vec_, alloc_ < 0 ? -alloc_ : alloc_ VEC_ASSERT_PASS); \
725 *vec_ = (VEC(T) *) vec_p_reserve (*vec_, alloc_); \
730 static inline void VEC_OP (T,safe_grow) \
731 (VEC(T) **vec_, int size_ VEC_ASSERT_DECL) \
733 vec_assert (size_ >= 0 && VEC_OP(T,length) (*vec_) <= (unsigned)size_, \
736 (vec_, (int)(*vec_ ? (*vec_)->num : 0) - size_ VEC_ASSERT_PASS); \
737 (*vec_)->num = size_; \
740 static inline T *VEC_OP (T,safe_push) \
741 (VEC(T) **vec_, T obj_ VEC_ASSERT_DECL) \
743 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
745 return VEC_OP (T,quick_push) (*vec_, obj_ VEC_ASSERT_PASS); \
748 static inline T *VEC_OP (T,safe_insert) \
749 (VEC(T) **vec_, unsigned ix_, T obj_ VEC_ASSERT_DECL) \
751 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
753 return VEC_OP (T,quick_insert) (*vec_, ix_, obj_ VEC_ASSERT_PASS); \
756 #define DEF_VEC_FUNC_O(T) \
757 static inline unsigned VEC_OP (T,length) (const VEC(T) *vec_) \
759 return vec_ ? vec_->num : 0; \
762 static inline T *VEC_OP (T,last) (VEC(T) *vec_ VEC_ASSERT_DECL) \
764 vec_assert (vec_ && vec_->num, "last"); \
766 return &vec_->vec[vec_->num - 1]; \
769 static inline T *VEC_OP (T,index) \
770 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
772 vec_assert (vec_ && ix_ < vec_->num, "index"); \
774 return &vec_->vec[ix_]; \
777 static inline int VEC_OP (T,iterate) \
778 (VEC(T) *vec_, unsigned ix_, T **ptr) \
780 if (vec_ && ix_ < vec_->num) \
782 *ptr = &vec_->vec[ix_]; \
792 static inline size_t VEC_OP (T,embedded_size) \
795 return offsetof (VEC(T),vec) + alloc_ * sizeof(T); \
798 static inline void VEC_OP (T,embedded_init) \
799 (VEC(T) *vec_, int alloc_) \
802 vec_->alloc = alloc_; \
805 static inline int VEC_OP (T,space) \
806 (VEC(T) *vec_, int alloc_ VEC_ASSERT_DECL) \
808 vec_assert (alloc_ >= 0, "space"); \
809 return vec_ ? vec_->alloc - vec_->num >= (unsigned)alloc_ : !alloc_; \
812 static inline T *VEC_OP (T,quick_push) \
813 (VEC(T) *vec_, const T *obj_ VEC_ASSERT_DECL) \
817 vec_assert (vec_->num < vec_->alloc, "quick_push"); \
818 slot_ = &vec_->vec[vec_->num++]; \
825 static inline void VEC_OP (T,pop) (VEC(T) *vec_ VEC_ASSERT_DECL) \
827 vec_assert (vec_->num, "pop"); \
831 static inline void VEC_OP (T,truncate) \
832 (VEC(T) *vec_, unsigned size_ VEC_ASSERT_DECL) \
834 vec_assert (vec_ ? vec_->num >= size_ : !size_, "truncate"); \
839 static inline T *VEC_OP (T,replace) \
840 (VEC(T) *vec_, unsigned ix_, const T *obj_ VEC_ASSERT_DECL) \
844 vec_assert (ix_ < vec_->num, "replace"); \
845 slot_ = &vec_->vec[ix_]; \
852 static inline T *VEC_OP (T,quick_insert) \
853 (VEC(T) *vec_, unsigned ix_, const T *obj_ VEC_ASSERT_DECL) \
857 vec_assert (vec_->num < vec_->alloc && ix_ <= vec_->num, "quick_insert"); \
858 slot_ = &vec_->vec[ix_]; \
859 memmove (slot_ + 1, slot_, (vec_->num++ - ix_) * sizeof (T)); \
866 static inline void VEC_OP (T,ordered_remove) \
867 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
871 vec_assert (ix_ < vec_->num, "ordered_remove"); \
872 slot_ = &vec_->vec[ix_]; \
873 memmove (slot_, slot_ + 1, (--vec_->num - ix_) * sizeof (T)); \
876 static inline void VEC_OP (T,unordered_remove) \
877 (VEC(T) *vec_, unsigned ix_ VEC_ASSERT_DECL) \
879 vec_assert (ix_ < vec_->num, "unordered_remove"); \
880 vec_->vec[ix_] = vec_->vec[--vec_->num]; \
883 static inline void VEC_OP (T,block_remove) \
884 (VEC(T) *vec_, unsigned ix_, unsigned len_ VEC_ASSERT_DECL) \
888 vec_assert (ix_ + len_ <= vec_->num, "block_remove"); \
889 slot_ = &vec_->vec[ix_]; \
891 memmove (slot_, slot_ + len_, (vec_->num - ix_) * sizeof (T)); \
894 static inline T *VEC_OP (T,address) \
897 return vec_ ? vec_->vec : 0; \
900 static inline unsigned VEC_OP (T,lower_bound) \
901 (VEC(T) *vec_, const T *obj_, \
902 int (*lessthan_)(const T *, const T *) VEC_ASSERT_DECL) \
904 unsigned int len_ = VEC_OP (T, length) (vec_); \
905 unsigned int half_, middle_; \
906 unsigned int first_ = 0; \
913 middle_elem_ = VEC_OP (T,index) (vec_, middle_ VEC_ASSERT_PASS); \
914 if (lessthan_ (middle_elem_, obj_)) \
918 len_ = len_ - half_ - 1; \
926 #define DEF_VEC_ALLOC_FUNC_O(T) \
927 static inline VEC(T) *VEC_OP (T,alloc) \
930 /* We must request exact size allocation, hence the negation. */ \
931 return (VEC(T) *) vec_o_reserve (NULL, -alloc_, \
932 offsetof (VEC(T),vec), sizeof (T)); \
935 static inline VEC(T) *VEC_OP (T,copy) (VEC(T) *vec_) \
937 size_t len_ = vec_ ? vec_->num : 0; \
938 VEC (T) *new_vec_ = NULL; \
942 /* We must request exact size allocation, hence the negation. */ \
943 new_vec_ = (VEC (T) *) \
944 vec_o_reserve (NULL, -len_, offsetof (VEC(T),vec), sizeof (T)); \
946 new_vec_->num = len_; \
947 memcpy (new_vec_->vec, vec_->vec, sizeof (T) * len_); \
952 static inline void VEC_OP (T,free) \
960 static inline int VEC_OP (T,reserve) \
961 (VEC(T) **vec_, int alloc_ VEC_ASSERT_DECL) \
963 int extend = !VEC_OP (T,space) (*vec_, alloc_ < 0 ? -alloc_ : alloc_ \
968 vec_o_reserve (*vec_, alloc_, offsetof (VEC(T),vec), sizeof (T)); \
973 static inline void VEC_OP (T,safe_grow) \
974 (VEC(T) **vec_, int size_ VEC_ASSERT_DECL) \
976 vec_assert (size_ >= 0 && VEC_OP(T,length) (*vec_) <= (unsigned)size_, \
979 (vec_, (int)(*vec_ ? (*vec_)->num : 0) - size_ VEC_ASSERT_PASS); \
980 (*vec_)->num = size_; \
983 static inline T *VEC_OP (T,safe_push) \
984 (VEC(T) **vec_, const T *obj_ VEC_ASSERT_DECL) \
986 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
988 return VEC_OP (T,quick_push) (*vec_, obj_ VEC_ASSERT_PASS); \
991 static inline T *VEC_OP (T,safe_insert) \
992 (VEC(T) **vec_, unsigned ix_, const T *obj_ VEC_ASSERT_DECL) \
994 VEC_OP (T,reserve) (vec_, 1 VEC_ASSERT_PASS); \
996 return VEC_OP (T,quick_insert) (*vec_, ix_, obj_ VEC_ASSERT_PASS); \
999 #endif /* GDB_VEC_H */
This page took 0.048469 seconds and 4 git commands to generate.