Skip to content
Snippets Groups Projects
Commit 615b1e60 authored by Kaspar Schleiser's avatar Kaspar Schleiser
Browse files

core/clist: add argument to clist_foreach

parent f142908f
No related branches found
No related tags found
No related merge requests found
......@@ -322,13 +322,16 @@ static inline clist_node_t *clist_remove(clist_node_t *list, clist_node_t *node)
/**
* @brief Traverse clist, call function for each member
*
* The pointer supplied by @p arg will be passed to every call to @p func.
*
* If @p func returns non-zero, traversal will be aborted like when calling
* break within a for loop.
*
* @param[in] list List to traverse.
* @param[in] func Function to call for each member.
* @param[in] arg Pointer to pass to every call to @p func
*/
static inline void clist_foreach(clist_node_t *list, int(*func)(clist_node_t *))
static inline void clist_foreach(clist_node_t *list, int(*func)(clist_node_t *, void *), void *arg)
{
clist_node_t *node = list->next;
if (! node) {
......@@ -336,7 +339,7 @@ static inline void clist_foreach(clist_node_t *list, int(*func)(clist_node_t *))
}
do {
node = node->next;
if (func(node)) {
if (func(node, arg)) {
return;
}
} while (node != list->next);
......
......@@ -223,8 +223,9 @@ static void _foreach_test(clist_node_t *node)
/* embunit test macros only work within void returning functions, so this
* trampoline function is needed */
static int _foreach_test_trampoline(clist_node_t *node)
static int _foreach_test_trampoline(clist_node_t *node, void *arg)
{
(void)arg;
_foreach_test(node);
if (_foreach_called == _foreach_abort_after) {
return 1;
......@@ -242,7 +243,7 @@ static void test_clist_foreach(void)
clist_rpush(list, &tests_clist_buf[i]);
}
clist_foreach(list, _foreach_test_trampoline);
clist_foreach(list, _foreach_test_trampoline, NULL);
TEST_ASSERT(_foreach_called == _foreach_abort_after);
......@@ -252,7 +253,7 @@ static void test_clist_foreach(void)
}
_foreach_abort_after = (TEST_CLIST_LEN + 1);
clist_foreach(list, _foreach_test_trampoline);
clist_foreach(list, _foreach_test_trampoline, NULL);
TEST_ASSERT(_foreach_called == TEST_CLIST_LEN);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment