Commit dbb786da authored by J M's avatar J M Committed by Carles Cufi
Browse files

sys: lists: Add list functions to compute list length



This commit adds functions to slist, sflist, and dlist to return the
length of provided lists, which will allow future commits to refactor
code that implements this manually.

The new functions all take a reference to any node in the list and compute
the length of the entire list. If the list is empty, they return 0.

Signed-off-by: default avatarJ M <montytyper@msn.com>
parent 5119af04
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -534,6 +534,24 @@ static inline sys_dnode_t *sys_dlist_get(sys_dlist_t *list)
	return node;
}

/**
 * @brief Compute the size of the given list in O(n) time
 *
 * @param list A pointer on the list
 *
 * @return an integer equal to the size of the list, or 0 if empty
 */
static inline size_t sys_dlist_len(sys_dlist_t *list)
{
	size_t len = 0;
	sys_dnode_t *node = NULL;

	SYS_DLIST_FOR_EACH_NODE(list, node) {
		len++;
	}
	return len;
}

/** @} */

#ifdef __cplusplus
+11 −0
Original line number Diff line number Diff line
@@ -234,4 +234,15 @@
		return false;						 \
	}

#define Z_GENLIST_LEN(__lname, __nname)                                                            \
	static inline size_t sys_##__lname##_len(sys_##__lname##_t * list)                         \
	{                                                                                          \
		size_t len = 0;                                                                    \
		static sys_##__nname##_t * node;                                                   \
		Z_GENLIST_FOR_EACH_NODE(__lname, list, node) {                                     \
			len++;                                                                     \
		}                                                                                  \
		return len;                                                                        \
	}

#endif /* ZEPHYR_INCLUDE_SYS_LIST_GEN_H_ */
+11 −0
Original line number Diff line number Diff line
@@ -489,6 +489,17 @@ static inline bool sys_sflist_find_and_remove(sys_sflist_t *list,

Z_GENLIST_FIND_AND_REMOVE(sflist, sfnode)

/**
 * @brief Compute the size of the given list in O(n) time
 *
 * @param list A pointer on the list
 *
 * @return an integer equal to the size of the list, or 0 if empty
 */
static inline size_t sys_sflist_len(sys_sflist_t *list);

Z_GENLIST_LEN(sflist, sfnode)

/** @} */

#ifdef __cplusplus
+11 −0
Original line number Diff line number Diff line
@@ -420,6 +420,17 @@ Z_GENLIST_REMOVE(slist, snode)
static inline bool sys_slist_find_and_remove(sys_slist_t *list,
					     sys_snode_t *node);

/**
 * @brief Compute the size of the given list in O(n) time
 *
 * @param list A pointer on the list
 *
 * @return an integer equal to the size of the list, or 0 if empty
 */
static inline size_t sys_slist_len(sys_slist_t *list);

Z_GENLIST_LEN(slist, snode)

/** @} */
Z_GENLIST_FIND_AND_REMOVE(slist, snode)