From 9da3489d247775fb06bee40fe4cf26640681fb93 Mon Sep 17 00:00:00 2001 From: Badalyan Vyacheslav Date: Mon, 10 Oct 2016 11:59:38 -0400 Subject: res_pjsip_config_wizard: Memory leak in module_unload Fixed a memory leak. It removes only the first element. Added a useful feature in vector.h to remove all items under the CMP through a callback function / macro. ASTERISK-26453 #close Change-Id: I84508353463456d2495678f125738e20052da950 --- include/asterisk/vector.h | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'include') diff --git a/include/asterisk/vector.h b/include/asterisk/vector.h index 0a13c560b..ef6104d57 100644 --- a/include/asterisk/vector.h +++ b/include/asterisk/vector.h @@ -353,6 +353,30 @@ #define AST_VECTOR_REMOVE_ORDERED(vec, idx) \ AST_VECTOR_REMOVE(vec, idx, 1) +/*! + * \brief Remove all elements from a vector that matches the given comparison + * + * \param vec Vector to remove from. + * \param value Value to pass into comparator. + * \param cmp Comparator function/macros (called as \c cmp(elem, value)) + * \param cleanup How to cleanup a removed element macro/function. + * + * \return the number of deleted elements. + */ +#define AST_VECTOR_REMOVE_ALL_CMP_UNORDERED(vec, value, cmp, cleanup) ({ \ + int count = 0; \ + size_t idx; \ + typeof(value) __value = (value); \ + for (idx = 0; idx < (vec)->current; ++idx) { \ + if (cmp((vec)->elems[idx], __value)) { \ + cleanup((vec)->elems[idx]); \ + AST_VECTOR_REMOVE_UNORDERED((vec), idx); \ + ++count; \ + } \ + } \ + count; \ +}) + /*! * \brief Remove an element from a vector that matches the given comparison * @@ -379,6 +403,30 @@ res; \ }) +/*! + * \brief Remove all elements from a vector that matches the given comparison while maintaining order + * + * \param vec Vector to remove from. + * \param value Value to pass into comparator. + * \param cmp Comparator function/macros (called as \c cmp(elem, value)) + * \param cleanup How to cleanup a removed element macro/function. + * + * \return the number of deleted elements. + */ +#define AST_VECTOR_REMOVE_ALL_CMP_ORDERED(vec, value, cmp, cleanup) ({ \ + int count = 0; \ + size_t idx; \ + typeof(value) __value = (value); \ + for (idx = 0; idx < (vec)->current; ++idx) { \ + if (cmp((vec)->elems[idx], __value)) { \ + cleanup((vec)->elems[idx]); \ + AST_VECTOR_REMOVE_ORDERED((vec), idx); \ + ++count; \ + } \ + } \ + oount; \ +}) + /*! * \brief Remove an element from a vector that matches the given comparison while maintaining order * -- cgit v1.2.3