Skip to content

Commit 0fc9a27

Browse files
committed
sys: dlist: Add sys_dlist_peek_prev_no_check and sys_dlist_peek_prev
Added function for peeking into previous item in the list. Signed-off-by: Krzysztof Chruscinski <[email protected]>
1 parent 685be0c commit 0fc9a27

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

include/misc/dlist.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,41 @@ static inline sys_dnode_t *sys_dlist_peek_next(sys_dlist_t *list,
310310
return node ? sys_dlist_peek_next_no_check(list, node) : NULL;
311311
}
312312

313+
/**
314+
* @brief get a reference to the previous item in the list, node is not NULL
315+
*
316+
* Faster than sys_dlist_peek_prev() if node is known not to be NULL.
317+
*
318+
* @param list the doubly-linked list to operate on
319+
* @param node the node from which to get the previous element in the list
320+
*
321+
* @return a pointer to the previous element from a node, NULL if node is the
322+
* tail
323+
*/
324+
325+
static inline sys_dnode_t *sys_dlist_peek_prev_no_check(sys_dlist_t *list,
326+
sys_dnode_t *node)
327+
{
328+
return (node == list->head) ? NULL : node->prev;
329+
}
330+
331+
/**
332+
* @brief get a reference to the previous item in the list
333+
*
334+
* @param list the doubly-linked list to operate on
335+
* @param node the node from which to get the previous element in the list
336+
*
337+
* @return a pointer to the previous element from a node, NULL if node is the
338+
* tail or NULL (when node comes from reading the head of an empty
339+
* list).
340+
*/
341+
342+
static inline sys_dnode_t *sys_dlist_peek_prev(sys_dlist_t *list,
343+
sys_dnode_t *node)
344+
{
345+
return node ? sys_dlist_peek_prev_no_check(list, node) : NULL;
346+
}
347+
313348
/**
314349
* @brief get a reference to the tail item in the list
315350
*

0 commit comments

Comments
 (0)