hsearch_r: add DELETE action

Signed-off-by: guohao15 <guohao15@xiaomi.com>
This commit is contained in:
guohao15 2023-10-27 18:01:13 +08:00 committed by Alan Carvalho de Assis
parent 4fd175fd5c
commit b36521d4a4
2 changed files with 17 additions and 2 deletions

View File

@ -35,7 +35,8 @@ typedef struct entry
typedef enum
{
FIND,
ENTER
ENTER,
DELETE
} ACTION;
struct hsearch_data

View File

@ -191,6 +191,7 @@ void hdestroy_r(FAR struct hsearch_data *htab)
ie = SLIST_FIRST(&(htab->htable[idx]));
SLIST_REMOVE_HEAD(&(htab->htable[idx]), link);
lib_free(ie->ent.key);
lib_free(ie->ent.data);
lib_free(ie);
}
}
@ -239,7 +240,20 @@ int hsearch_r(ENTRY item, ACTION action, FAR ENTRY **retval,
ie = SLIST_NEXT(ie, link);
}
if (ie != NULL)
if (action == DELETE)
{
if (ie != NULL)
{
SLIST_REMOVE(head, ie, internal_entry, link);
lib_free(ie->ent.key);
lib_free(ie->ent.data);
lib_free(ie);
return 1;
}
return 0;
}
else if (ie != NULL)
{
*retval = &ie->ent;
return 1;