This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit c63adb5c14093061c0b6c74b4755373535a32c10
Author: guohao15 <[email protected]>
AuthorDate: Fri Aug 2 10:19:43 2024 +0800

    hcreate: add alloc/free func hook for user to deallocate memory
    
    Signed-off-by: guohao15 <[email protected]>
---
 include/search.h             |  1 +
 libs/libc/search/hcreate_r.c | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/include/search.h b/include/search.h
index 20fa93fbd3..36eaa7ed74 100644
--- a/include/search.h
+++ b/include/search.h
@@ -43,6 +43,7 @@ struct hsearch_data
 {
   FAR struct internal_head *htable;
   size_t htablesize;
+  CODE void (*free_entry)(FAR ENTRY *entry);
 };
 
 /****************************************************************************
diff --git a/libs/libc/search/hcreate_r.c b/libs/libc/search/hcreate_r.c
index 6c35e78032..13f835f1ec 100644
--- a/libs/libc/search/hcreate_r.c
+++ b/libs/libc/search/hcreate_r.c
@@ -82,6 +82,16 @@ struct internal_entry
 SLIST_HEAD(internal_head, internal_entry);
 extern uint32_t (*g_default_hash)(FAR const void *, size_t);
 
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+static void hfree_r(FAR ENTRY *entry)
+{
+  lib_free(entry->key);
+  lib_free(entry->data);
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -157,6 +167,11 @@ int hcreate_r(size_t nel, FAR struct hsearch_data *htab)
       SLIST_INIT(&(htab->htable[idx]));
     }
 
+  if (htab->free_entry == NULL)
+    {
+      htab->free_entry = hfree_r;
+    }
+
   return 1;
 }
 
@@ -190,8 +205,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);
+          htab->free_entry(&ie->ent);
           lib_free(ie);
         }
     }
@@ -245,8 +259,7 @@ int hsearch_r(ENTRY item, ACTION action, FAR ENTRY **retval,
       if (ie != NULL)
         {
           SLIST_REMOVE(head, ie, internal_entry, link);
-          lib_free(ie->ent.key);
-          lib_free(ie->ent.data);
+          htab->free_entry(&ie->ent);
           lib_free(ie);
           return 1;
         }

Reply via email to