Skip to content

Commit

Permalink
slist: add sys_slist_get() to fetch and remove the head
Browse files Browse the repository at this point in the history
More straightforward than doing sys_slist_peek_head() followed by
sys_slist_remove().

Also add a version that does not check if the list is non-empty to be
used when the list is known bo be non-empty.

Change-Id: I8fd10e20e2c84c7d8972c9207f3d4917884808cb
Signed-off-by: Benjamin Walsh <[email protected]>
  • Loading branch information
benwrs committed Sep 13, 2016
1 parent 8cff799 commit e69982b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions include/misc/slist.h
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,39 @@ static inline void sys_slist_insert(sys_slist_t *list,
}
}

/**
* @brief Fetch and remove the first node of the given list
*
* List must be known to be non-empty.
*
* @param list A pointer on the list to affect
*
* @return A pointer to the first node of the list
*/
static inline sys_snode_t *sys_slist_get_not_empty(sys_slist_t *list)
{
sys_snode_t *node = list->head;

list->head = node->next;
if (list->tail == node) {
list->tail = list->head;
}

return node;
}

/**
* @brief Fetch and remove the first node of the given list
*
* @param list A pointer on the list to affect
*
* @return A pointer to the first node of the list (or NULL if empty)
*/
static inline sys_snode_t *sys_slist_get(sys_slist_t *list)
{
return sys_slist_is_empty(list) ? NULL : sys_slist_get_not_empty(list);
}

/**
* @brief Remove a node
*
Expand Down

0 comments on commit e69982b

Please sign in to comment.