Asterisk - The Open Source Telephony Project GIT-master-5467495
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_BSEARCH(vec, key, cmp)
 Binary search a sorted vector.
 
#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_BSEARCH

#define AST_VECTOR_BSEARCH (   vec,
  key,
  cmp 
)
Value:
({ \
typeof(key) __key = (key); \
bsearch(&__key, (vec)->elems, (vec)->current, \
sizeof(typeof((vec)->elems[0])), cmp); \
})
typedef typeof(dummy_tv_var_for_types.tv_sec) ast_time_t

Binary search a sorted vector.

Parameters
vecSorted vector to search.
keyThe key to search for.
cmpA bsearch compatible compare function.
Returns
a pointer to the found element, or NULL if not found.
Warning
This macro requires the vector to have been sorted.

Definition at line 422 of file vector.h.

422 { \
423 typeof(key) __key = (key); \
424 bsearch(&__key, (vec)->elems, (vec)->current, \
425 sizeof(typeof((vec)->elems[0])), cmp); \
426})

◆ 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 793 of file vector.h.

793 { \
794 size_t idx; \
795 typeof((vec)->elems[0]) res = default_value; \
796 for (idx = 0; idx < (vec)->current; idx++) { \
797 int rc = callback((vec)->elems[idx], ##__VA_ARGS__); \
798 if (rc & CMP_MATCH) { \
799 res = (vec)->elems[idx]; \
800 break; \
801 }\
802 if (rc & CMP_STOP) { \
803 break; \
804 }\
805 } \
806 res; \
807})
@ 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, int rdlock)

◆ 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:857
#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 857 of file vector.h.

857 { \
858 size_t idx; \
859 typeof((vec)) new_vec; \
860 do { \
861 new_vec = ast_malloc(sizeof(*new_vec)); \
862 if (!new_vec) { \
863 break; \
864 } \
865 if (AST_VECTOR_INIT(new_vec, AST_VECTOR_SIZE((vec))) != 0) { \
866 ast_free(new_vec); \
867 new_vec = NULL; \
868 break; \
869 } \
870 for (idx = 0; idx < (vec)->current; idx++) { \
871 int rc = callback((vec)->elems[idx], ##__VA_ARGS__); \
872 if (rc & CMP_MATCH) { \
873 AST_VECTOR_APPEND(new_vec, (vec)->elems[idx]); \
874 } \
875 if (rc & CMP_STOP) { \
876 break; \
877 }\
878 } \
879 } while(0); \
880 new_vec; \
881})
#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:637
#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 890 of file vector.h.

890 { \
891 size_t idx; \
892 for (idx = 0; idx < (vec)->current; idx++) { \
893 callback((vec)->elems[idx], ##__VA_ARGS__); \
894 } \
895})

◆ 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 666 of file vector.h.

666 { \
667 int res = 0; \
668 do { \
669 size_t new_max = (vec)->current; \
670 if (new_max == 0) { \
671 ast_free((vec)->elems); \
672 (vec)->elems = NULL; \
673 (vec)->max = 0; \
674 } else if ((vec)->max > new_max) { \
675 typeof((vec)->elems) new_elems = ast_realloc( \
676 (vec)->elems, \
677 new_max * sizeof(*new_elems)); \
678 if (new_elems) { \
679 (vec)->elems = new_elems; \
680 (vec)->max = new_max; \
681 } else { \
682 res = -1; \
683 break; \
684 } \
685 } \
686 } while(0); \
687 res; \
688})
#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 599 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 592 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 708 of file vector.h.

708 { \
709 size_t __idx = (idx); \
710 ast_assert(__idx < (vec)->current); \
711 (vec)->elems[__idx]; \
712})

◆ 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 696 of file vector.h.

696 { \
697 size_t __idx = (idx); \
698 ast_assert(__idx < (vec)->current); \
699 &(vec)->elems[__idx]; \
700})

◆ 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 759 of file vector.h.

759 { \
760 void *res = NULL; \
761 size_t idx; \
762 typeof(value) __value = (value); \
763 for (idx = 0; idx < (vec)->current; ++idx) { \
764 if (cmp((vec)->elems[idx], __value)) { \
765 res = &(vec)->elems[idx]; \
766 break; \
767 } \
768 } \
769 res; \
770})

◆ 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 747 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 724 of file vector.h.

724 { \
725 int res = -1; \
726 size_t idx; \
727 typeof(nth) __nth = (nth); \
728 typeof(value) __value = (value); \
729 for (idx = 0; idx < (vec)->current; ++idx) { \
730 if (cmp((vec)->elems[idx], __value) && !(--__nth)) { \
731 res = (int)idx; \
732 break; \
733 } \
734 } \
735 res; \
736})

◆ 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})

◆ 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 779 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 645 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 440 of file vector.h.

440 { \
441 typeof((vec)->elems[0]) res; \
442 size_t __idx = (idx); \
443 ast_assert(__idx < (vec)->current); \
444 res = (vec)->elems[__idx]; \
445 if ((preserve_ordered)) { \
446 size_t __move; \
447 __move = ((vec)->current - (__idx) - 1) * sizeof(typeof((vec)->elems[0])); \
448 memmove(&(vec)->elems[__idx], &(vec)->elems[__idx + 1], __move); \
449 (vec)->current--; \
450 } else { \
451 (vec)->elems[__idx] = (vec)->elems[--(vec)->current]; \
452 }; \
453 res; \
454})

◆ 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 541 of file vector.h.

541 { \
542 int count = 0; \
543 size_t idx; \
544 typeof(value) __value = (value); \
545 for (idx = 0; idx < (vec)->current; ) { \
546 if (cmp((vec)->elems[idx], __value)) { \
547 cleanup((vec)->elems[idx]); \
548 AST_VECTOR_REMOVE_ORDERED((vec), idx); \
549 ++count; \
550 } else { \
551 ++idx; \
552 } \
553 } \
554 count; \
555})

◆ 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 489 of file vector.h.

489 { \
490 int count = 0; \
491 size_t idx; \
492 typeof(value) __value = (value); \
493 for (idx = 0; idx < (vec)->current; ) { \
494 if (cmp((vec)->elems[idx], __value)) { \
495 cleanup((vec)->elems[idx]); \
496 AST_VECTOR_REMOVE_UNORDERED((vec), idx); \
497 ++count; \
498 } else { \
499 ++idx; \
500 } \
501 } \
502 count; \
503})

◆ 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 568 of file vector.h.

568 { \
569 int res = -1; \
570 size_t idx; \
571 typeof(value) __value = (value); \
572 for (idx = 0; idx < (vec)->current; ++idx) { \
573 if (cmp((vec)->elems[idx], __value)) { \
574 cleanup((vec)->elems[idx]); \
575 AST_VECTOR_REMOVE_ORDERED((vec), idx); \
576 res = 0; \
577 break; \
578 } \
579 } \
580 res; \
581})

◆ 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 516 of file vector.h.

516 { \
517 int res = -1; \
518 size_t idx; \
519 typeof(value) __value = (value); \
520 for (idx = 0; idx < (vec)->current; ++idx) { \
521 if (cmp((vec)->elems[idx], __value)) { \
522 cleanup((vec)->elems[idx]); \
523 AST_VECTOR_REMOVE_UNORDERED((vec), idx); \
524 res = 0; \
525 break; \
526 } \
527 } \
528 res; \
529})

◆ 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:592

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 626 of file vector.h.

626 { \
627 AST_VECTOR_REMOVE_CMP_ORDERED((vec), (elem), \
629})

◆ 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 611 of file vector.h.

611 { \
612 AST_VECTOR_REMOVE_CMP_UNORDERED((vec), (elem), \
614})

◆ 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 476 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 466 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 653 of file vector.h.

653 { \
654 AST_VECTOR_CALLBACK_VOID(vec, cleanup); \
655 (vec)->current = 0; \
656})

◆ 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 905 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 956 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 935 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 925 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 915 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 967 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 945 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 637 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:708

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:759
#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().