From 97338c9f3d4d3e9c56d3b27734e36dbd149b6deb Mon Sep 17 00:00:00 2001 From: chenrun1 Date: Sat, 8 Jun 2024 13:03:48 +0800 Subject: [PATCH] idr:New idr_find interface Different from idr_get_next, if idr_find directly when could not find the node id return an error Signed-off-by: chenrun1 --- include/nuttx/idr.h | 1 + libs/libc/misc/lib_idr.c | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/nuttx/idr.h b/include/nuttx/idr.h index 9d15588236..edb461556e 100644 --- a/include/nuttx/idr.h +++ b/include/nuttx/idr.h @@ -40,6 +40,7 @@ struct idr_s; * Public Function Prototypes ****************************************************************************/ +FAR void *idr_find(FAR struct idr_s *idr, unsigned int id); FAR void *idr_get_next(FAR struct idr_s *idr, FAR int *id); FAR struct idr_s *idr_init_base(int base); void idr_destroy(FAR struct idr_s *idr); diff --git a/libs/libc/misc/lib_idr.c b/libs/libc/misc/lib_idr.c index 88f8138984..1c98bf1fec 100644 --- a/libs/libc/misc/lib_idr.c +++ b/libs/libc/misc/lib_idr.c @@ -93,6 +93,32 @@ RB_GENERATE_STATIC(idr_tree_s, idr_node_s, link, idr_compare) * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: idr_find + ****************************************************************************/ + +FAR void *idr_find(FAR struct idr_s *idr, unsigned int id) +{ + FAR struct idr_node_s *node; + struct idr_node_s search; + + search.id = id; + nxmutex_lock(&idr->lock); + node = RB_FIND(idr_tree_s, &idr->alloced, &search); + if (node == NULL) + { + nxmutex_unlock(&idr->lock); + return NULL; + } + + nxmutex_unlock(&idr->lock); + return node->data; +} + +/**************************************************************************** + * Name: idr_get_next + ****************************************************************************/ + FAR void *idr_get_next(FAR struct idr_s *idr, FAR int *id) { FAR struct idr_node_s *node;