Skip to content

Commit c63adb5

Browse files
guohao15xiaoxiang781216
authored andcommitted
hcreate: add alloc/free func hook for user to deallocate memory
Signed-off-by: guohao15 <[email protected]>
1 parent 43bcac9 commit c63adb5

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

include/search.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct hsearch_data
4343
{
4444
FAR struct internal_head *htable;
4545
size_t htablesize;
46+
CODE void (*free_entry)(FAR ENTRY *entry);
4647
};
4748

4849
/****************************************************************************

libs/libc/search/hcreate_r.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ struct internal_entry
8282
SLIST_HEAD(internal_head, internal_entry);
8383
extern uint32_t (*g_default_hash)(FAR const void *, size_t);
8484

85+
/****************************************************************************
86+
* Private Functions
87+
****************************************************************************/
88+
89+
static void hfree_r(FAR ENTRY *entry)
90+
{
91+
lib_free(entry->key);
92+
lib_free(entry->data);
93+
}
94+
8595
/****************************************************************************
8696
* Public Functions
8797
****************************************************************************/
@@ -157,6 +167,11 @@ int hcreate_r(size_t nel, FAR struct hsearch_data *htab)
157167
SLIST_INIT(&(htab->htable[idx]));
158168
}
159169

170+
if (htab->free_entry == NULL)
171+
{
172+
htab->free_entry = hfree_r;
173+
}
174+
160175
return 1;
161176
}
162177

@@ -190,8 +205,7 @@ void hdestroy_r(FAR struct hsearch_data *htab)
190205
{
191206
ie = SLIST_FIRST(&(htab->htable[idx]));
192207
SLIST_REMOVE_HEAD(&(htab->htable[idx]), link);
193-
lib_free(ie->ent.key);
194-
lib_free(ie->ent.data);
208+
htab->free_entry(&ie->ent);
195209
lib_free(ie);
196210
}
197211
}
@@ -245,8 +259,7 @@ int hsearch_r(ENTRY item, ACTION action, FAR ENTRY **retval,
245259
if (ie != NULL)
246260
{
247261
SLIST_REMOVE(head, ie, internal_entry, link);
248-
lib_free(ie->ent.key);
249-
lib_free(ie->ent.data);
262+
htab->free_entry(&ie->ent);
250263
lib_free(ie);
251264
return 1;
252265
}

0 commit comments

Comments
 (0)