Asterisk - The Open Source Telephony Project GIT-master-27fb039
Loading...
Searching...
No Matches
Data Structures | Macros | Enumerations | Functions
vector.h File Reference

Vector container support. More...

#include "asterisk/lock.h"
Include dependency graph for vector.h:

Go to the source code of this file.

Data Structures

struct  ast_vector_const_string
 
struct  ast_vector_int
 Integer vector definition. More...
 
struct  ast_vector_string
 String vector definitions. More...
 

Macros

#define __make_room(idx, vec)
 
#define AST_VECTOR(name, type)
 Define a vector structure.
 
#define AST_VECTOR_ADD_SORTED(vec, elem, cmp)
 Add an element into a sorted vector.
 
#define AST_VECTOR_APPEND(vec, elem)
 Append an element to a vector, growing the vector if needed.
 
#define AST_VECTOR_CALLBACK(vec, callback, default_value, ...)
 Execute a callback on every element in a vector returning the first matched.
 
#define AST_VECTOR_CALLBACK_MULTIPLE(vec, callback, ...)
 Execute a callback on every element in a vector returning the matching elements in a new vector.
 
#define AST_VECTOR_CALLBACK_VOID(vec, callback, ...)
 Execute a callback on every element in a vector disregarding callback return.
 
#define AST_VECTOR_COMPACT(vec)
 Resize a vector so that its capacity is the same as its size.
 
#define AST_VECTOR_DEFAULT(vec, size, value)
 Default a vector up to size with the given value.
 
#define AST_VECTOR_ELEM_CLEANUP_NOOP(elem)
 Vector element cleanup that does nothing.
 
#define AST_VECTOR_ELEM_DEFAULT_CMP(elem, value)   ((elem) == (value))
 Default comparator for AST_VECTOR_REMOVE_ELEM_UNORDERED()
 
#define AST_VECTOR_FREE(vec)
 Deallocates this vector.
 
#define AST_VECTOR_GET(vec, idx)
 Get an element from a vector.
 
#define AST_VECTOR_GET_ADDR(vec, idx)
 Get an address of element in a vector.
 
#define AST_VECTOR_GET_CMP(vec, value, cmp)
 Get an element from a vector that matches the given comparison.
 
#define AST_VECTOR_GET_INDEX(vec, value, cmp)    AST_VECTOR_GET_INDEX_NTH(vec, 1, value, cmp)
 Get the 1st index from a vector that matches the given comparison.
 
#define AST_VECTOR_GET_INDEX_NTH(vec, nth, value, cmp)
 Get the nth index from a vector that matches the given comparison.
 
#define AST_VECTOR_INIT(vec, size)
 Initialize a vector.
 
#define AST_VECTOR_INSERT_AT(vec, idx, elem)
 Insert an element at a specific position in a vector, growing the vector if needed.
 
#define AST_VECTOR_MATCH_ALL(element)   (CMP_MATCH)
 Default callback for AST_VECTOR_CALLBACK()
 
#define AST_VECTOR_MAX_SIZE(vec)   (vec)->max
 Get the maximum number of elements the vector can currently hold.
 
#define AST_VECTOR_PTR_FREE(vec)
 Deallocates this vector pointer.
 
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
 Remove an element from a vector by index.
 
#define AST_VECTOR_REMOVE_ALL_CMP_ORDERED(vec, value, cmp, cleanup)
 Remove all elements from a vector that matches the given comparison while maintaining order.
 
#define AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(vec, value, cmp, cleanup)
 Remove all elements from a vector that matches the given comparison.
 
#define AST_VECTOR_REMOVE_CMP_ORDERED(vec, value, cmp, cleanup)
 Remove an element from a vector that matches the given comparison while maintaining order.
 
#define AST_VECTOR_REMOVE_CMP_UNORDERED(vec, value, cmp, cleanup)
 Remove an element from a vector that matches the given comparison.
 
#define AST_VECTOR_REMOVE_ELEM_ORDERED(vec, elem, cleanup)
 Remove an element from a vector while maintaining order.
 
#define AST_VECTOR_REMOVE_ELEM_UNORDERED(vec, elem, cleanup)
 Remove an element from a vector.
 
#define AST_VECTOR_REMOVE_ORDERED(vec, idx)    AST_VECTOR_REMOVE(vec, idx, 1)
 Remove an element from a vector by index while maintaining order.
 
#define AST_VECTOR_REMOVE_UNORDERED(vec, idx)    AST_VECTOR_REMOVE(vec, idx, 0)
 Remove an element from an unordered vector by index.
 
#define AST_VECTOR_REPLACE(vec, idx, elem)
 Replace an element at a specific position in a vector, growing the vector if needed.
 
#define AST_VECTOR_RESET(vec, cleanup)
 Reset vector.
 
#define AST_VECTOR_RW(name, type)
 Define a vector structure with a read/write lock.
 
#define AST_VECTOR_RW_FREE(vec)
 Deallocates this locked vector.
 
#define AST_VECTOR_RW_INIT(vec, size)
 Initialize a vector with a read/write lock.
 
#define AST_VECTOR_RW_PTR_FREE(vec)
 Deallocates this locked vector pointer.
 
#define AST_VECTOR_RW_RDLOCK(vec)   ast_rwlock_rdlock(&(vec)->lock)
 Obtain read lock on vector.
 
#define AST_VECTOR_RW_RDLOCK_TIMED(vec, timespec)   ast_rwlock_timedrdlock(&(vec)->lock, timespec)
 Try to obtain read lock on vector failing after timeout if unable.
 
#define AST_VECTOR_RW_RDLOCK_TRY(vec)   ast_rwlock_tryrdlock(&(vec)->lock)
 Try to obtain read lock on vector failing immediately if unable.
 
#define AST_VECTOR_RW_UNLOCK(vec)   ast_rwlock_unlock(&(vec)->lock)
 Unlock vector.
 
#define AST_VECTOR_RW_WRLOCK(vec)   ast_rwlock_wrlock(&(vec)->lock)
 Obtain write lock on vector.
 
#define AST_VECTOR_RW_WRLOCK_TIMED(vec, timespec)   ast_rwlock_timedwrlock(&(vec)->lock, timespec)
 Try to obtain write lock on vector failing after timeout if unable.
 
#define AST_VECTOR_RW_WRLOCK_TRY(vec)   ast_rwlock_trywrlock(&(vec)->lock)
 Try to obtain write lock on vector failing immediately if unable.
 
#define AST_VECTOR_SIZE(vec)   (vec)->current
 Get the number of elements in a vector.
 
#define AST_VECTOR_SORT(vec, cmp)
 Sort a vector in-place.
 
#define AST_VECTOR_STEAL_ELEMENTS(vec)
 Steal the elements from a vector and reinitialize.
 

Enumerations

enum  ast_vector_string_split_flags { AST_VECTOR_STRING_SPLIT_NO_TRIM = 0x01 , AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY = 0x02 }
 

Functions

char * ast_vector_string_join (struct ast_vector_string *vec, const char *delim)
 Join the elements of a string vector into a single string.
 
int ast_vector_string_split (struct ast_vector_string *dest, const char *input, const char *delim, int flags, int(*excludes_cmp)(const char *s1, const char *s2))
 Append a string vector by splitting a string.
 

Detailed Description

Vector container support.

A vector is a variable length array, with properties that can be useful when order doesn't matter.

Author
David M. Lee, II dlee@.nosp@m.digi.nosp@m.um.co.nosp@m.m
Since
12

Definition in file vector.h.

Macro Definition Documentation

◆ __make_room

#define __make_room (   idx,
  vec 
)

Definition at line 234 of file vector.h.

234 { \
235 int res = 0; \
236 do { \
237 if ((idx) >= (vec)->max) { \
238 size_t new_max = ((idx) + 1) * 2; \
239 typeof((vec)->elems) new_elems = ast_calloc(1, \
240 new_max * sizeof(*new_elems)); \
241 if (new_elems) { \
242 if ((vec)->elems) { \
243 memcpy(new_elems, (vec)->elems, \
244 (vec)->current * sizeof(*new_elems)); \
245 ast_free((vec)->elems); \
246 } \
247 (vec)->elems = new_elems; \
248 (vec)->max = new_max; \
249 } else { \
250 res = -1; \
251 break; \
252 } \
253 } \
254 } while(0); \
255 res; \
256})
#define ast_calloc(num, len)
A wrapper for calloc()
Definition astmm.h:202
size_t current
const struct ast_channelstorage_driver ** elems
#define max(a, b)
Definition f2c.h:198

◆ AST_VECTOR

#define AST_VECTOR (   name,
  type 
)
Value:
struct name { \
type *elems; \
size_t max; \
size_t current; \
}
static const char type[]
static const char name[]
Definition format_mp3.c:68

Define a vector structure.

Parameters
nameOptional vector struct name.
typeVector element type.

Definition at line 44 of file vector.h.

45 { \
46 type *elems; \
47 size_t max; \
48 size_t current; \
49 }

◆ AST_VECTOR_ADD_SORTED

#define AST_VECTOR_ADD_SORTED (   vec,
  elem,
  cmp 
)

Add an element into a sorted vector.

Parameters
vecSorted vector to add to.
elemElement to insert. Must not be an array type.
cmpA strcmp compatible compare function.
Return values
0on success.
Non-zeroon failure.
Warning
Use of this macro on an unsorted vector will produce unpredictable results
'elem' must not be an array type so passing 'x' where 'x' is defined as 'char x[4]' will fail to compile. However casting 'x' as 'char *' does result in a value that CAN be used.

Definition at line 382 of file vector.h.

382 { \
383 int res = 0; \
384 size_t __idx = (vec)->current; \
385 typeof(elem) __elem = (elem); \
386 do { \
387 if (__make_room((vec)->current, vec) != 0) { \
388 res = -1; \
389 break; \
390 } \
391 while (__idx > 0 && (cmp((vec)->elems[__idx - 1], __elem) > 0)) { \
392 (vec)->elems[__idx] = (vec)->elems[__idx - 1]; \
393 __idx--; \
394 } \
395 (vec)->elems[__idx] = __elem; \
396 (vec)->current++; \
397 } while (0); \
398 res; \
399})
#define __make_room(idx, vec)
Definition vector.h:234

◆ AST_VECTOR_APPEND

#define AST_VECTOR_APPEND (   vec,
  elem 
)

Append an element to a vector, growing the vector if needed.

Parameters
vecVector to append to.
elemElement to append.
Return values
0on success.
Non-zeroon failure.

Definition at line 267 of file vector.h.

267 { \
268 int res = 0; \
269 do { \
270 if (__make_room((vec)->current, vec) != 0) { \
271 res = -1; \
272 break; \
273 } \
274 (vec)->elems[(vec)->current++] = (elem); \
275 } while (0); \
276 res; \
277})

◆ AST_VECTOR_CALLBACK

#define AST_VECTOR_CALLBACK (   vec,
  callback,
  default_value,
  ... 
)

Execute a callback on every element in a vector returning the first matched.

Parameters
vecVector to operate on.
callbackA callback that takes at least 1 argument (the element) plus number of optional arguments
default_valueA default value to return if no elements matched
Returns
the first element matched before CMP_STOP was returned or the end of the vector was reached. Otherwise, default_value

Definition at line 776 of file vector.h.

776 { \
777 size_t idx; \
778 typeof((vec)->elems[0]) res = default_value; \
779 for (idx = 0; idx < (vec)->current; idx++) { \
780 int rc = callback((vec)->elems[idx], ##__VA_ARGS__); \
781 if (rc & CMP_MATCH) { \
782 res = (vec)->elems[idx]; \
783 break; \
784 }\
785 if (rc & CMP_STOP) { \
786 break; \
787 }\
788 } \
789 res; \
790})
@ CMP_MATCH
Definition astobj2.h:1027
@ CMP_STOP
Definition astobj2.h:1028
static struct ast_channel * callback(struct ast_channelstorage_instance *driver, ao2_callback_data_fn *cb_fn, void *arg, void *data, int ao2_flags)

◆ AST_VECTOR_CALLBACK_MULTIPLE

#define AST_VECTOR_CALLBACK_MULTIPLE (   vec,
  callback,
  ... 
)

Execute a callback on every element in a vector returning the matching elements in a new vector.

This macro basically provides a filtered clone.

Parameters
vecVector to operate on.
callbackA callback that takes at least 1 argument (the element) plus number of optional arguments
Returns
a vector containing the elements matched before CMP_STOP was returned or the end of the vector was reached. The vector may be empty and could be NULL if there was not enough memory to allocate it's control structure.
Warning
The returned vector must have AST_VECTOR_PTR_FREE() called on it after you've finished with it.
Note
The type of the returned vector must be traceable to the original vector.

The following will result in "error: assignment from incompatible pointer type" because these declare 2 different structures.

AST_VECTOR(, char *) vector_1;
AST_VECTOR(, char *) *vector_2;
vector_2 = AST_VECTOR_CALLBACK_MULTIPLE(&vector_1, callback);
#define AST_VECTOR_CALLBACK_MULTIPLE(vec, callback,...)
Execute a callback on every element in a vector returning the matching elements in a new vector.
Definition vector.h:840
#define AST_VECTOR(name, type)
Define a vector structure.
Definition vector.h:44

This will work because you're using the type of the first to declare the second:

AST_VECTOR(mytype, char *) vector_1;
struct mytype *vector_2 = NULL;
vector_2 = AST_VECTOR_CALLBACK_MULTIPLE(&vector_1, callback);
#define NULL
Definition resample.c:96

This will also work because you're declaring both vector_1 and vector_2 from the same definition.

AST_VECTOR(, char *) vector_1, *vector_2 = NULL;
vector_2 = AST_VECTOR_CALLBACK_MULTIPLE(&vector_1, callback);

Definition at line 840 of file vector.h.

840 { \
841 size_t idx; \
842 typeof((vec)) new_vec; \
843 do { \
844 new_vec = ast_malloc(sizeof(*new_vec)); \
845 if (!new_vec) { \
846 break; \
847 } \
848 if (AST_VECTOR_INIT(new_vec, AST_VECTOR_SIZE((vec))) != 0) { \
849 ast_free(new_vec); \
850 new_vec = NULL; \
851 break; \
852 } \
853 for (idx = 0; idx < (vec)->current; idx++) { \
854 int rc = callback((vec)->elems[idx], ##__VA_ARGS__); \
855 if (rc & CMP_MATCH) { \
856 AST_VECTOR_APPEND(new_vec, (vec)->elems[idx]); \
857 } \
858 if (rc & CMP_STOP) { \
859 break; \
860 }\
861 } \
862 } while(0); \
863 new_vec; \
864})
#define ast_malloc(len)
A wrapper for malloc()
Definition astmm.h:191
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition vector.h:620
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition vector.h:124

◆ AST_VECTOR_CALLBACK_VOID

#define AST_VECTOR_CALLBACK_VOID (   vec,
  callback,
  ... 
)

Execute a callback on every element in a vector disregarding callback return.

Parameters
vecVector to operate on.
callbackA callback that takes at least 1 argument (the element) plus number of optional arguments

Definition at line 873 of file vector.h.

873 { \
874 size_t idx; \
875 for (idx = 0; idx < (vec)->current; idx++) { \
876 callback((vec)->elems[idx], ##__VA_ARGS__); \
877 } \
878})

◆ AST_VECTOR_COMPACT

#define AST_VECTOR_COMPACT (   vec)

Resize a vector so that its capacity is the same as its size.

Parameters
vecVector to compact.
Return values
0on success.
Non-zeroon failure.

Definition at line 649 of file vector.h.

649 { \
650 int res = 0; \
651 do { \
652 size_t new_max = (vec)->current; \
653 if (new_max == 0) { \
654 ast_free((vec)->elems); \
655 (vec)->elems = NULL; \
656 (vec)->max = 0; \
657 } else if ((vec)->max > new_max) { \
658 typeof((vec)->elems) new_elems = ast_realloc( \
659 (vec)->elems, \
660 new_max * sizeof(*new_elems)); \
661 if (new_elems) { \
662 (vec)->elems = new_elems; \
663 (vec)->max = new_max; \
664 } else { \
665 res = -1; \
666 break; \
667 } \
668 } \
669 } while(0); \
670 res; \
671})
#define ast_realloc(p, len)
A wrapper for realloc()
Definition astmm.h:226

◆ AST_VECTOR_DEFAULT

#define AST_VECTOR_DEFAULT (   vec,
  size,
  value 
)

Default a vector up to size with the given value.

Note
If a size of 0 is given then all elements in the given vector are set.
The vector will grow to the given size if needed.
Parameters
vecVector to default.
sizeThe number of elements to default
valueThe default value to set each element to

Definition at line 320 of file vector.h.

320 { \
321 int res = 0; \
322 typeof((size)) __size = (size) ? (size) : AST_VECTOR_SIZE(vec); \
323 size_t idx; \
324 for (idx = 0; idx < __size; ++idx) { \
325 res = AST_VECTOR_REPLACE(vec, idx, value); \
326 if (res == -1) { \
327 break; \
328 } \
329 } \
330 res; \
331})
int value
Definition syslog.c:37
#define AST_VECTOR_REPLACE(vec, idx, elem)
Replace an element at a specific position in a vector, growing the vector if needed.
Definition vector.h:295

◆ AST_VECTOR_ELEM_CLEANUP_NOOP

#define AST_VECTOR_ELEM_CLEANUP_NOOP (   elem)

Vector element cleanup that does nothing.

Parameters
elemElement to cleanup

Definition at line 582 of file vector.h.

◆ AST_VECTOR_ELEM_DEFAULT_CMP

#define AST_VECTOR_ELEM_DEFAULT_CMP (   elem,
  value 
)    ((elem) == (value))

Default comparator for AST_VECTOR_REMOVE_ELEM_UNORDERED()

Parameters
elemElement to compare against
valueValue to compare with the vector element.
Return values
0if element does not match.
Non-zeroif element matches.

Definition at line 575 of file vector.h.

◆ AST_VECTOR_FREE

#define AST_VECTOR_FREE (   vec)

Deallocates this vector.

If any code to free the elements of this vector needs to be run, that should be done prior to this call.

Parameters
vecVector to deallocate.

Definition at line 185 of file vector.h.

185 { \
186 ast_free((vec)->elems); \
187 (vec)->elems = NULL; \
188 (vec)->max = 0; \
189 (vec)->current = 0; \
190} while (0)

◆ AST_VECTOR_GET

#define AST_VECTOR_GET (   vec,
  idx 
)
Value:
({ \
size_t __idx = (idx); \
ast_assert(__idx < (vec)->current); \
(vec)->elems[__idx]; \
})

Get an element from a vector.

Parameters
vecVector to query.
idxIndex of the element to get.

Definition at line 691 of file vector.h.

691 { \
692 size_t __idx = (idx); \
693 ast_assert(__idx < (vec)->current); \
694 (vec)->elems[__idx]; \
695})

◆ AST_VECTOR_GET_ADDR

#define AST_VECTOR_GET_ADDR (   vec,
  idx 
)
Value:
({ \
size_t __idx = (idx); \
ast_assert(__idx < (vec)->current); \
&(vec)->elems[__idx]; \
})

Get an address of element in a vector.

Parameters
vecVector to query.
idxIndex of the element to get address of.

Definition at line 679 of file vector.h.

679 { \
680 size_t __idx = (idx); \
681 ast_assert(__idx < (vec)->current); \
682 &(vec)->elems[__idx]; \
683})

◆ AST_VECTOR_GET_CMP

#define AST_VECTOR_GET_CMP (   vec,
  value,
  cmp 
)

Get an element from a vector that matches the given comparison.

Parameters
vecVector to get from.
valueValue to pass into comparator.
cmpComparator function/macros (called as cmp(elem, value))
Returns
a pointer to the element that was found or NULL

Definition at line 742 of file vector.h.

742 { \
743 void *res = NULL; \
744 size_t idx; \
745 typeof(value) __value = (value); \
746 for (idx = 0; idx < (vec)->current; ++idx) { \
747 if (cmp((vec)->elems[idx], __value)) { \
748 res = &(vec)->elems[idx]; \
749 break; \
750 } \
751 } \
752 res; \
753})

◆ AST_VECTOR_GET_INDEX

#define AST_VECTOR_GET_INDEX (   vec,
  value,
  cmp 
)     AST_VECTOR_GET_INDEX_NTH(vec, 1, value, cmp)

Get the 1st index from a vector that matches the given comparison.

Parameters
vecVector to get from.
valueValue to pass into comparator.
cmpComparator function/macros (called as cmp(elem, value))
Returns
a pointer to the element that was found or NULL

Definition at line 730 of file vector.h.

◆ AST_VECTOR_GET_INDEX_NTH

#define AST_VECTOR_GET_INDEX_NTH (   vec,
  nth,
  value,
  cmp 
)

Get the nth index from a vector that matches the given comparison.

Parameters
vecVector to get from.
nthThe nth index to find
valueValue to pass into comparator.
cmpComparator function/macros (called as cmp(elem, value))
Returns
a pointer to the element that was found or NULL

Definition at line 707 of file vector.h.

707 { \
708 int res = -1; \
709 size_t idx; \
710 typeof(nth) __nth = (nth); \
711 typeof(value) __value = (value); \
712 for (idx = 0; idx < (vec)->current; ++idx) { \
713 if (cmp((vec)->elems[idx], __value) && !(--__nth)) { \
714 res = (int)idx; \
715 break; \
716 } \
717 } \
718 res; \
719})

◆ AST_VECTOR_INIT

#define AST_VECTOR_INIT (   vec,
  size 
)

Initialize a vector.

If size is 0, then no space will be allocated until the vector is appended to.

Parameters
vecVector to initialize.
sizeInitial size of the vector.
Return values
0on success.
Non-zeroon failure.

Definition at line 124 of file vector.h.

124 { \
125 size_t __size = (size); \
126 size_t alloc_size = __size * sizeof(*((vec)->elems)); \
127 (vec)->elems = alloc_size ? ast_calloc(1, alloc_size) : NULL; \
128 (vec)->current = 0; \
129 if ((vec)->elems) { \
130 (vec)->max = __size; \
131 } else { \
132 (vec)->max = 0; \
133 } \
134 (alloc_size == 0 || (vec)->elems != NULL) ? 0 : -1; \
135})

◆ AST_VECTOR_INSERT_AT

#define AST_VECTOR_INSERT_AT (   vec,
  idx,
  elem 
)

Insert an element at a specific position in a vector, growing the vector if needed.

Parameters
vecVector to insert into.
idxPosition to insert at.
elemElement to insert.
Return values
0on success.
Non-zeroon failure.
Warning
This macro will shift existing elements right to make room for the new element.
Use of this macro with the expectation that the element will remain at the provided index means you can not use the UNORDERED assortment of macros. These macros alter the ordering of the vector itself.

Definition at line 349 of file vector.h.

349 { \
350 int res = 0; \
351 size_t __move; \
352 do { \
353 if (__make_room(((idx) > (vec)->current ? (idx) : (vec)->current), vec) != 0) { \
354 res = -1; \
355 break; \
356 } \
357 if ((vec)->current > 0 && (idx) < (vec)->current) { \
358 __move = ((vec)->current - (idx)) * sizeof(typeof((vec)->elems[0])); \
359 memmove(&(vec)->elems[(idx) + 1], &(vec)->elems[(idx)], __move); \
360 } \
361 (vec)->elems[(idx)] = (elem); \
362 (vec)->current = ((idx) > (vec)->current ? (idx) : (vec)->current) + 1; \
363 } while (0); \
364 res; \
365})
typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t

◆ AST_VECTOR_MATCH_ALL

#define AST_VECTOR_MATCH_ALL (   element)    (CMP_MATCH)

Default callback for AST_VECTOR_CALLBACK()

Parameters
elementElement to compare against
Return values
CMP_MATCHalways.

Definition at line 762 of file vector.h.

◆ AST_VECTOR_MAX_SIZE

#define AST_VECTOR_MAX_SIZE (   vec)    (vec)->max

Get the maximum number of elements the vector can currently hold.

Parameters
vecVector to query.
Returns
Maximum number of elements the vector can currently hold.

Definition at line 628 of file vector.h.

◆ AST_VECTOR_PTR_FREE

#define AST_VECTOR_PTR_FREE (   vec)
Value:
do { \
AST_VECTOR_FREE(vec); \
ast_free(vec); \
} while (0)

Deallocates this vector pointer.

If any code to free the elements of this vector need to be run, that should be done prior to this call.

Parameters
vecPointer to a malloc'd vector structure.

Definition at line 200 of file vector.h.

200 { \
201 AST_VECTOR_FREE(vec); \
202 ast_free(vec); \
203} while (0)

◆ AST_VECTOR_REMOVE

#define AST_VECTOR_REMOVE (   vec,
  idx,
  preserve_ordered 
)

Remove an element from a vector by index.

Note that elements in the vector may be reordered, so that the remove can happen in constant time.

Parameters
vecVector to remove from.
idxIndex of the element to remove.
preserve_orderedPreserve the vector order.
Returns
The element that was removed.

Definition at line 423 of file vector.h.

423 { \
424 typeof((vec)->elems[0]) res; \
425 size_t __idx = (idx); \
426 ast_assert(__idx < (vec)->current); \
427 res = (vec)->elems[__idx]; \
428 if ((preserve_ordered)) { \
429 size_t __move; \
430 __move = ((vec)->current - (__idx) - 1) * sizeof(typeof((vec)->elems[0])); \
431 memmove(&(vec)->elems[__idx], &(vec)->elems[__idx + 1], __move); \
432 (vec)->current--; \
433 } else { \
434 (vec)->elems[__idx] = (vec)->elems[--(vec)->current]; \
435 }; \
436 res; \
437})

◆ AST_VECTOR_REMOVE_ALL_CMP_ORDERED

#define AST_VECTOR_REMOVE_ALL_CMP_ORDERED (   vec,
  value,
  cmp,
  cleanup 
)

Remove all elements from a vector that matches the given comparison while maintaining order.

Parameters
vecVector to remove from.
valueValue to pass into comparator.
cmpComparator function/macros (called as cmp(elem, value))
cleanupHow to cleanup a removed element macro/function.
Returns
the number of deleted elements.

Definition at line 524 of file vector.h.

524 { \
525 int count = 0; \
526 size_t idx; \
527 typeof(value) __value = (value); \
528 for (idx = 0; idx < (vec)->current; ) { \
529 if (cmp((vec)->elems[idx], __value)) { \
530 cleanup((vec)->elems[idx]); \
531 AST_VECTOR_REMOVE_ORDERED((vec), idx); \
532 ++count; \
533 } else { \
534 ++idx; \
535 } \
536 } \
537 count; \
538})

◆ AST_VECTOR_REMOVE_ALL_CMP_UNORDERED

#define AST_VECTOR_REMOVE_ALL_CMP_UNORDERED (   vec,
  value,
  cmp,
  cleanup 
)

Remove all elements from a vector that matches the given comparison.

Parameters
vecVector to remove from.
valueValue to pass into comparator.
cmpComparator function/macros (called as cmp(elem, value))
cleanupHow to cleanup a removed element macro/function.
Returns
the number of deleted elements.

Definition at line 472 of file vector.h.

472 { \
473 int count = 0; \
474 size_t idx; \
475 typeof(value) __value = (value); \
476 for (idx = 0; idx < (vec)->current; ) { \
477 if (cmp((vec)->elems[idx], __value)) { \
478 cleanup((vec)->elems[idx]); \
479 AST_VECTOR_REMOVE_UNORDERED((vec), idx); \
480 ++count; \
481 } else { \
482 ++idx; \
483 } \
484 } \
485 count; \
486})

◆ AST_VECTOR_REMOVE_CMP_ORDERED

#define AST_VECTOR_REMOVE_CMP_ORDERED (   vec,
  value,
  cmp,
  cleanup 
)

Remove an element from a vector that matches the given comparison while maintaining order.

Parameters
vecVector to remove from.
valueValue to pass into comparator.
cmpComparator function/macros (called as cmp(elem, value))
cleanupHow to cleanup a removed element macro/function.
Return values
0if element was removed.
Non-zeroif element was not in the vector.

Definition at line 551 of file vector.h.

551 { \
552 int res = -1; \
553 size_t idx; \
554 typeof(value) __value = (value); \
555 for (idx = 0; idx < (vec)->current; ++idx) { \
556 if (cmp((vec)->elems[idx], __value)) { \
557 cleanup((vec)->elems[idx]); \
558 AST_VECTOR_REMOVE_ORDERED((vec), idx); \
559 res = 0; \
560 break; \
561 } \
562 } \
563 res; \
564})

◆ AST_VECTOR_REMOVE_CMP_UNORDERED

#define AST_VECTOR_REMOVE_CMP_UNORDERED (   vec,
  value,
  cmp,
  cleanup 
)

Remove an element from a vector that matches the given comparison.

Parameters
vecVector to remove from.
valueValue to pass into comparator.
cmpComparator function/macros (called as cmp(elem, value))
cleanupHow to cleanup a removed element macro/function.
Return values
0if element was removed.
Non-zeroif element was not in the vector.

Definition at line 499 of file vector.h.

499 { \
500 int res = -1; \
501 size_t idx; \
502 typeof(value) __value = (value); \
503 for (idx = 0; idx < (vec)->current; ++idx) { \
504 if (cmp((vec)->elems[idx], __value)) { \
505 cleanup((vec)->elems[idx]); \
506 AST_VECTOR_REMOVE_UNORDERED((vec), idx); \
507 res = 0; \
508 break; \
509 } \
510 } \
511 res; \
512})

◆ AST_VECTOR_REMOVE_ELEM_ORDERED

#define AST_VECTOR_REMOVE_ELEM_ORDERED (   vec,
  elem,
  cleanup 
)
Value:
({ \
AST_VECTOR_REMOVE_CMP_ORDERED((vec), (elem), \
})
static void cleanup(void)
Clean up any old apps that we don't need any more.
Definition res_stasis.c:327
#define AST_VECTOR_ELEM_DEFAULT_CMP(elem, value)
Default comparator for AST_VECTOR_REMOVE_ELEM_UNORDERED()
Definition vector.h:575

Remove an element from a vector while maintaining order.

Parameters
vecVector to remove from.
elemElement to remove
cleanupHow to cleanup a removed element macro/function.
Return values
0if element was removed.
Non-zeroif element was not in the vector.

Definition at line 609 of file vector.h.

609 { \
610 AST_VECTOR_REMOVE_CMP_ORDERED((vec), (elem), \
612})

◆ AST_VECTOR_REMOVE_ELEM_UNORDERED

#define AST_VECTOR_REMOVE_ELEM_UNORDERED (   vec,
  elem,
  cleanup 
)
Value:
({ \
AST_VECTOR_REMOVE_CMP_UNORDERED((vec), (elem), \
})

Remove an element from a vector.

Parameters
vecVector to remove from.
elemElement to remove
cleanupHow to cleanup a removed element macro/function.
Return values
0if element was removed.
Non-zeroif element was not in the vector.

Definition at line 594 of file vector.h.

594 { \
595 AST_VECTOR_REMOVE_CMP_UNORDERED((vec), (elem), \
597})

◆ AST_VECTOR_REMOVE_ORDERED

#define AST_VECTOR_REMOVE_ORDERED (   vec,
  idx 
)     AST_VECTOR_REMOVE(vec, idx, 1)

Remove an element from a vector by index while maintaining order.

Parameters
vecVector to remove from.
idxIndex of the element to remove.
Returns
The element that was removed.

Definition at line 459 of file vector.h.

◆ AST_VECTOR_REMOVE_UNORDERED

#define AST_VECTOR_REMOVE_UNORDERED (   vec,
  idx 
)     AST_VECTOR_REMOVE(vec, idx, 0)

Remove an element from an unordered vector by index.

Note that elements in the vector may be reordered, so that the remove can happen in constant time.

Parameters
vecVector to remove from.
idxIndex of the element to remove.
Returns
The element that was removed.

Definition at line 449 of file vector.h.

◆ AST_VECTOR_REPLACE

#define AST_VECTOR_REPLACE (   vec,
  idx,
  elem 
)

Replace an element at a specific position in a vector, growing the vector if needed.

Parameters
vecVector to replace into.
idxPosition to replace.
elemElement to replace.
Return values
0on success.
Non-zeroon failure.
Warning
This macro will overwrite anything already present at the position provided.
Use of this macro with the expectation that the element will remain at the provided index means you can not use the UNORDERED assortment of macros. These macros alter the ordering of the vector itself.

Definition at line 295 of file vector.h.

295 { \
296 int res = 0; \
297 do { \
298 if (__make_room((idx), vec) != 0) { \
299 res = -1; \
300 break; \
301 } \
302 (vec)->elems[(idx)] = (elem); \
303 if (((idx) + 1) > (vec)->current) { \
304 (vec)->current = (idx) + 1; \
305 } \
306 } while(0); \
307 res; \
308})

◆ AST_VECTOR_RESET

#define AST_VECTOR_RESET (   vec,
  cleanup 
)
Value:
({ \
AST_VECTOR_CALLBACK_VOID(vec, cleanup); \
(vec)->current = 0; \
})

Reset vector.

Parameters
vecVector to reset.
cleanupA cleanup callback or AST_VECTOR_ELEM_CLEANUP_NOOP.

Definition at line 636 of file vector.h.

636 { \
637 AST_VECTOR_CALLBACK_VOID(vec, cleanup); \
638 (vec)->current = 0; \
639})

◆ AST_VECTOR_RW

#define AST_VECTOR_RW (   name,
  type 
)

Define a vector structure with a read/write lock.

Parameters
nameOptional vector struct name.
typeVector element type.

Definition at line 104 of file vector.h.

105 { \
106 type *elems; \
107 size_t max; \
108 size_t current; \
110 }
ast_mutex_t lock
Definition app_sla.c:337
Structure for rwlock and tracking information.
Definition lock.h:164

◆ AST_VECTOR_RW_FREE

#define AST_VECTOR_RW_FREE (   vec)
Value:
do { \
AST_VECTOR_FREE(vec); \
ast_rwlock_destroy(&(vec)->lock); \
} while(0)

Deallocates this locked vector.

If any code to free the elements of this vector need to be run, that should be done prior to this call.

Parameters
vecVector to deallocate.

Definition at line 213 of file vector.h.

213 { \
214 AST_VECTOR_FREE(vec); \
215 ast_rwlock_destroy(&(vec)->lock); \
216} while(0)

◆ AST_VECTOR_RW_INIT

#define AST_VECTOR_RW_INIT (   vec,
  size 
)

Initialize a vector with a read/write lock.

If size is 0, then no space will be allocated until the vector is appended to.

Parameters
vecVector to initialize.
sizeInitial size of the vector.
Return values
0on success.
Non-zeroon failure.

Definition at line 169 of file vector.h.

169 { \
170 int res = -1; \
171 if (AST_VECTOR_INIT(vec, size) == 0) { \
172 res = ast_rwlock_init(&(vec)->lock); \
173 } \
174 res; \
175})
#define ast_rwlock_init(rwlock)
wrapper for rwlock with tracking enabled
Definition lock.h:231

◆ AST_VECTOR_RW_PTR_FREE

#define AST_VECTOR_RW_PTR_FREE (   vec)
Value:
do { \
AST_VECTOR_RW_FREE(vec); \
ast_free(vec); \
} while(0)

Deallocates this locked vector pointer.

If any code to free the elements of this vector need to be run, that should be done prior to this call.

Parameters
vecPointer to a malloc'd vector structure.

Definition at line 226 of file vector.h.

226 { \
227 AST_VECTOR_RW_FREE(vec); \
228 ast_free(vec); \
229} while(0)

◆ AST_VECTOR_RW_RDLOCK

#define AST_VECTOR_RW_RDLOCK (   vec)    ast_rwlock_rdlock(&(vec)->lock)

Obtain read lock on vector.

Parameters
vecVector to operate on.
Return values
0if success
Non-zeroif error

Definition at line 888 of file vector.h.

◆ AST_VECTOR_RW_RDLOCK_TIMED

#define AST_VECTOR_RW_RDLOCK_TIMED (   vec,
  timespec 
)    ast_rwlock_timedrdlock(&(vec)->lock, timespec)

Try to obtain read lock on vector failing after timeout if unable.

Parameters
vecVector to operate on.
timespec
Return values
0if success
Non-zeroif error

Definition at line 939 of file vector.h.

◆ AST_VECTOR_RW_RDLOCK_TRY

#define AST_VECTOR_RW_RDLOCK_TRY (   vec)    ast_rwlock_tryrdlock(&(vec)->lock)

Try to obtain read lock on vector failing immediately if unable.

Parameters
vecVector to operate on.
Return values
0if success
Non-zeroif error

Definition at line 918 of file vector.h.

◆ AST_VECTOR_RW_UNLOCK

#define AST_VECTOR_RW_UNLOCK (   vec)    ast_rwlock_unlock(&(vec)->lock)

Unlock vector.

Parameters
vecVector to operate on.
Return values
0if success
Non-zeroif error

Definition at line 908 of file vector.h.

◆ AST_VECTOR_RW_WRLOCK

#define AST_VECTOR_RW_WRLOCK (   vec)    ast_rwlock_wrlock(&(vec)->lock)

Obtain write lock on vector.

Parameters
vecVector to operate on.
Return values
0if success
Non-zeroif error

Definition at line 898 of file vector.h.

◆ AST_VECTOR_RW_WRLOCK_TIMED

#define AST_VECTOR_RW_WRLOCK_TIMED (   vec,
  timespec 
)    ast_rwlock_timedwrlock(&(vec)->lock, timespec)

Try to obtain write lock on vector failing after timeout if unable.

Parameters
vecVector to operate on.
timespec
Return values
0if success
Non-zeroif error

Definition at line 950 of file vector.h.

◆ AST_VECTOR_RW_WRLOCK_TRY

#define AST_VECTOR_RW_WRLOCK_TRY (   vec)    ast_rwlock_trywrlock(&(vec)->lock)

Try to obtain write lock on vector failing immediately if unable.

Parameters
vecVector to operate on.
Return values
0if success
Non-zeroif error

Definition at line 928 of file vector.h.

◆ AST_VECTOR_SIZE

#define AST_VECTOR_SIZE (   vec)    (vec)->current

Get the number of elements in a vector.

Parameters
vecVector to query.
Returns
Number of elements in the vector.

Definition at line 620 of file vector.h.

◆ AST_VECTOR_SORT

#define AST_VECTOR_SORT (   vec,
  cmp 
)
Value:
({ \
qsort((vec)->elems, (vec)->current, sizeof(typeof((vec)->elems[0])), cmp); \
})

Sort a vector in-place.

Parameters
vecVector to sort
cmpA memcmp compatible compare function

Definition at line 407 of file vector.h.

407 { \
408 qsort((vec)->elems, (vec)->current, sizeof(typeof((vec)->elems[0])), cmp); \
409})

◆ AST_VECTOR_STEAL_ELEMENTS

#define AST_VECTOR_STEAL_ELEMENTS (   vec)
Value:
({ \
typeof((vec)->elems) __elems = (vec)->elems; \
AST_VECTOR_INIT((vec), 0); \
(__elems); \
})

Steal the elements from a vector and reinitialize.

Parameters
vecVector to operate on.

This allows you to use vector.h to construct a list and use the data as a bare array.

Note
The stolen array must eventually be released using ast_free.
Warning
AST_VECTOR_SIZE and AST_VECTOR_MAX_SIZE are both reset to 0. If either are needed they must be saved to a local variable before stealing the elements.

Definition at line 151 of file vector.h.

151 { \
152 typeof((vec)->elems) __elems = (vec)->elems; \
153 AST_VECTOR_INIT((vec), 0); \
154 (__elems); \
155})

Enumeration Type Documentation

◆ ast_vector_string_split_flags

Options to override default processing of ast_vector_string_split.

Enumerator
AST_VECTOR_STRING_SPLIT_NO_TRIM 

Do not trim whitespace from values.

AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY 

Append empty strings to the vector.

Definition at line 59 of file vector.h.

59 {
60 /*! Do not trim whitespace from values. */
62 /*! Append empty strings to the vector. */
64};
@ AST_VECTOR_STRING_SPLIT_NO_TRIM
Definition vector.h:61
@ AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY
Definition vector.h:63

Function Documentation

◆ ast_vector_string_join()

char * ast_vector_string_join ( struct ast_vector_string vec,
const char *  delim 
)

Join the elements of a string vector into a single string.

Parameters
vecPointer to the vector.
delimString to separate elements with.
Return values
Resultingstring. Must be freed with ast_free.

Definition at line 406 of file strings.c.

407{
408 struct ast_str *buf = ast_str_create(256);
409 char *rtn;
410 int i;
411
412 if (!buf) {
413 return NULL;
414 }
415
416 for (i = 0; i < AST_VECTOR_SIZE(vec); i++) {
417 ast_str_append(&buf, 0, "%s%s", AST_VECTOR_GET(vec, i), delim);
418 }
419 ast_str_truncate(buf, -strlen(delim));
421 ast_free(buf);
422 return rtn;
423}
#define ast_free(a)
Definition astmm.h:180
#define ast_strdup(str)
A wrapper for strdup()
Definition astmm.h:241
char buf[BUFSIZE]
Definition eagi_proxy.c:66
char * ast_str_truncate(struct ast_str *buf, ssize_t len)
Truncates the enclosed string to the given length.
Definition strings.h:786
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition strings.h:1139
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition strings.h:659
char *attribute_pure ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition strings.h:761
Support for dynamic strings.
Definition strings.h:623
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition vector.h:691

References ast_free, ast_str_append(), ast_str_buffer(), ast_str_create, ast_str_truncate(), ast_strdup, AST_VECTOR_GET, AST_VECTOR_SIZE, buf, and NULL.

Referenced by show_sessions_cb().

◆ ast_vector_string_split()

int ast_vector_string_split ( struct ast_vector_string dest,
const char *  input,
const char *  delim,
int  flags,
int(*)(const char *s1, const char *s2)  excludes_cmp 
)

Append a string vector by splitting a string.

Parameters
destPointer to an initialized vector.
inputString buffer to split.
delimString delimeter passed to strsep.
flagsProcessing options defined by ast_vector_string_split_flags.
excludes_cmpNULL or a function like strcmp to exclude duplicate strings.
Return values
0Success
-1Failure
Note
All elements added to the vector are allocated. The caller is always responsible for calling ast_free on each element in the vector even after failure. It's possible for this function to successfully add some elements before failing.

Definition at line 425 of file strings.c.

428{
429 char *buf;
430 char *cur;
431 int no_trim = flags & AST_VECTOR_STRING_SPLIT_NO_TRIM;
432 int allow_empty = flags & AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY;
433
434 ast_assert(dest != NULL);
436
437 if (ast_strlen_zero(input)) {
438 return 0;
439 }
440
441 buf = ast_strdupa(input);
442 while ((cur = strsep(&buf, delim))) {
443 if (!no_trim) {
444 cur = ast_strip(cur);
445 }
446
447 if (!allow_empty && ast_strlen_zero(cur)) {
448 continue;
449 }
450
451 if (excludes_cmp && AST_VECTOR_GET_CMP(dest, cur, !excludes_cmp)) {
452 continue;
453 }
454
455 cur = ast_strdup(cur);
456 if (!cur || AST_VECTOR_APPEND(dest, cur)) {
457 ast_free(cur);
458
459 return -1;
460 }
461 }
462
463 return 0;
464}
char * strsep(char **str, const char *delims)
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition astmm.h:298
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition strings.h:65
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition strings.h:223
#define ast_assert(a)
Definition utils.h:779
#define AST_VECTOR_GET_CMP(vec, value, cmp)
Get an element from a vector that matches the given comparison.
Definition vector.h:742
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition vector.h:267

References ast_assert, ast_free, ast_strdup, ast_strdupa, ast_strip(), ast_strlen_zero(), AST_VECTOR_APPEND, AST_VECTOR_GET_CMP, AST_VECTOR_STRING_SPLIT_ALLOW_EMPTY, AST_VECTOR_STRING_SPLIT_NO_TRIM, buf, NULL, and strsep().

Referenced by module_post_register(), and outbound_websocket_validate_cb().